capistrano-db-tasks 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ CapistranoDbTasks
2
2
  =================
3
3
 
4
4
  Add database AND assets tasks to capistrano to a Rails project.
5
+ It only works with capistrano 2. Any pull requests for capistrano 3 support are welcome :)
5
6
 
6
7
  Currently
7
8
 
@@ -40,6 +41,11 @@ Add to config/deploy.rb:
40
41
  set :locals_rails_env, "production"
41
42
  ```
42
43
 
44
+ Add to .gitignore
45
+ ```yml
46
+ /db/*.sql
47
+ ```
48
+
43
49
  Available tasks
44
50
  ===============
45
51
 
@@ -15,9 +15,10 @@ module Database
15
15
 
16
16
  def credentials
17
17
  if mysql?
18
- " -u #{@config['username']} " + (@config['password'] ? " -p\"#{@config['password']}\" " : '') + (@config['host'] ? " -h #{@config['host']}" : '') + (@config['socket'] ? " -S#{@config['socket']}" : '')
18
+ username = @config['username'] || @config['user']
19
+ (username ? " -u #{username} " : '') + (@config['password'] ? " -p'#{@config['password']}' " : '') + (@config['host'] ? " -h #{@config['host']}" : '') + (@config['socket'] ? " -S#{@config['socket']}" : '')
19
20
  elsif postgresql?
20
- " -U #{@config['username']} " + (@config['host'] ? " -h #{@config['host']}" : '')
21
+ (@config['username'] ? " -U #{@config['username']} " : '') + (@config['host'] ? " -h #{@config['host']}" : '')
21
22
  end
22
23
  end
23
24
 
@@ -33,6 +34,9 @@ module Database
33
34
  @output_file ||= "db/#{database}_#{current_time}.sql.bz2"
34
35
  end
35
36
 
37
+ def pgpass
38
+ "PGPASSWORD='#{@config['password']}'" if @config['password']
39
+ end
36
40
 
37
41
  private
38
42
 
@@ -40,7 +44,7 @@ module Database
40
44
  if mysql?
41
45
  "mysqldump #{credentials} #{database} --lock-tables=false"
42
46
  elsif postgresql?
43
- "pg_dump #{credentials} -c -O #{database}"
47
+ "#{pgpass} pg_dump #{credentials} -c -O #{database}"
44
48
  end
45
49
  end
46
50
 
@@ -48,7 +52,7 @@ module Database
48
52
  if mysql?
49
53
  "mysql #{credentials} -D #{database} < #{file}"
50
54
  elsif postgresql?
51
- "psql #{credentials} #{database} < #{file}"
55
+ "#{pgpass} psql #{credentials} #{database} < #{file}"
52
56
  end
53
57
  end
54
58
 
@@ -61,7 +65,7 @@ module Database
61
65
  @cap.run("cat #{@cap.current_path}/config/database.yml") do |c, s, d|
62
66
  @config += d
63
67
  end
64
- @config = YAML.load(@config)[@cap.rails_env]
68
+ @config = YAML.load(ERB.new(@config).result)[@cap.rails_env.to_s]
65
69
  end
66
70
 
67
71
  def dump
@@ -86,7 +90,7 @@ module Database
86
90
  class Local < Base
87
91
  def initialize(cap_instance)
88
92
  super(cap_instance)
89
- @config = YAML.load_file(File.join('config', 'database.yml'))[@cap.local_rails_env]
93
+ @config = YAML.load(ERB.new(File.read(File.join('config', 'database.yml'))).result)[@cap.local_rails_env.to_s]
90
94
  puts "local #{@config}"
91
95
  end
92
96
 
@@ -94,8 +98,15 @@ module Database
94
98
  def load(file, cleanup)
95
99
  unzip_file = File.join(File.dirname(file), File.basename(file, '.bz2'))
96
100
  # system("bunzip2 -f #{file} && bundle exec rake db:drop db:create && #{import_cmd(unzip_file)} && bundle exec rake db:migrate")
101
+ @cap.logger.info("executing local: bunzip2 -f #{file} && #{import_cmd(unzip_file)}")
97
102
  system("bunzip2 -f #{file} && #{import_cmd(unzip_file)}")
98
- File.unlink(unzip_file) if cleanup
103
+ if cleanup
104
+ @cap.logger.info("removing #{unzip_file}")
105
+ File.unlink(unzip_file)
106
+ else
107
+ @cap.logger.info("leaving #{unzip_file} (specify :db_local_clean in deploy.rb to remove)")
108
+ end
109
+ @cap.logger.info("Completed database import")
99
110
  end
100
111
 
101
112
  def dump
@@ -8,7 +8,6 @@ if Capistrano::Configuration.instance(false)
8
8
 
9
9
  instance.set :local_rails_env, ENV['RAILS_ENV'] || 'development' unless exists?(:local_rails_env)
10
10
  instance.set :rails_env, 'production' unless exists?(:rails_env)
11
- instance.set :stage, 'production' unless exists?(:stage)
12
11
  instance.set :db_local_clean, false unless exists?(:db_local_clean)
13
12
  instance.set :assets_dir, 'system' unless exists?(:assets_dir)
14
13
  instance.set :local_assets_dir, 'public' unless exists?(:local_assets_dir)
@@ -112,4 +111,4 @@ if Capistrano::Configuration.instance(false)
112
111
  end
113
112
  end
114
113
 
115
- end
114
+ end
@@ -1,3 +1,3 @@
1
1
  module CapistranoDbTasks
2
- VERSION = "0.2"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-db-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-09 00:00:00.000000000 Z
12
+ date: 2014-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano
@@ -59,12 +59,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ segments:
63
+ - 0
64
+ hash: 22134957769426335
62
65
  required_rubygems_version: !ruby/object:Gem::Requirement
63
66
  none: false
64
67
  requirements:
65
68
  - - ! '>='
66
69
  - !ruby/object:Gem::Version
67
70
  version: '0'
71
+ segments:
72
+ - 0
73
+ hash: 22134957769426335
68
74
  requirements: []
69
75
  rubyforge_project: capistrano-db-tasks
70
76
  rubygems_version: 1.8.23