guard-migrate 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/guard/migrate.rb CHANGED
@@ -41,7 +41,7 @@ module Guard
41
41
  # Called once when Guard starts
42
42
  # Please override initialize method to init stuff
43
43
  def start
44
- self.migrate if self.run_on_start?
44
+ migrate if run_on_start?
45
45
  end
46
46
 
47
47
  # Called on Ctrl-C signal (when Guard quits)
@@ -52,41 +52,89 @@ module Guard
52
52
  # Called on Ctrl-Z signal
53
53
  # This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
54
54
  def reload
55
- self.migrate if self.run_on_start?
55
+ migrate if run_on_start?
56
56
  end
57
57
 
58
58
  # Called on Ctrl-/ signal
59
59
  # This method should be principally used for long action like running all specs/tests/...
60
60
  def run_all
61
- self.migrate if self.run_on_start?
61
+ migrate if run_on_start?
62
62
  end
63
63
 
64
64
  # Called on file(s) modifications
65
65
  def run_on_changes(paths)
66
- self.migrate(paths.map{|path| path.scan(%r{^db/migrate/(\d+).+\.rb}).flatten.first})
66
+ if paths.any?{|path| path.match(%r{^db/migrate/(\d+).+\.rb})}
67
+ migrate(paths.map{|path| path.scan(%r{^db/migrate/(\d+).+\.rb}).flatten.first})
68
+ elsif paths.any?{|path| path.match(%r{^db/seeds\.rb$})}
69
+ seed_only
70
+ end
67
71
  end
68
72
 
69
73
  def migrate(paths = [])
70
- return if !self.reset? && paths.empty?
71
- system self.rake_string if self.reset?
74
+ return if !reset? && paths.empty?
75
+ system rake_string if reset?
72
76
  paths.each do |path|
73
- UI.info "Running #{self.rake_string(path)}"
74
- system self.rake_string(path)
77
+ UI.info "Running #{rake_string(path)}"
78
+ system rake_string(path)
75
79
  end
76
80
  end
77
81
 
82
+ def seed_only
83
+ UI.info "Running #{seed_only_string}"
84
+ system seed_only_string
85
+ end
86
+
87
+ def run_redo?(path)
88
+ !reset? && path && !path.empty?
89
+ end
90
+
78
91
  def rake_string(path = nil)
79
- @rake_string = ''
80
- @rake_string += 'bundle exec ' if self.bundler?
81
- @rake_string += 'rake'
82
- @rake_string += ' db:migrate'
83
- @rake_string += ':reset' if self.reset?
84
- @rake_string += ":redo VERSION=#{path}" if !self.reset? && path && !path.empty?
85
- @rake_string += " db:seed" if @seed
86
- @rake_string += ' db:test:clone' if self.test_clone?
87
- @rake_string += " RAILS_ENV=#{self.rails_env}" if self.rails_env
88
- @rake_string
92
+ [
93
+ rake_command,
94
+ migrate_string(path),
95
+ seed_string,
96
+ clone_string,
97
+ rails_env_string
98
+ ].compact.join(" ")
99
+ end
100
+
101
+ def seed_only_string
102
+ [
103
+ rake_command,
104
+ seed_string,
105
+ clone_string,
106
+ rails_env_string
107
+ ].compact.join(" ")
108
+ end
109
+
110
+ private
111
+
112
+ def rake_command
113
+ command = ""
114
+ command += "bundle exec " if bundler?
115
+ command += "rake"
116
+ command
89
117
  end
118
+
119
+ def rails_env_string
120
+ "RAILS_ENV=#{rails_env}" if rails_env
121
+ end
122
+
123
+ def clone_string
124
+ "db:test:clone" if test_clone?
125
+ end
126
+
127
+ def seed_string
128
+ "db:seed" if @seed
129
+ end
130
+
131
+ def migrate_string(path)
132
+ string = "db:migrate"
133
+ string += ":reset" if reset?
134
+ string += ":redo VERSION=#{path}" if run_redo?(path)
135
+ string
136
+ end
137
+
90
138
  end
91
139
  end
92
140
 
@@ -1,4 +1,5 @@
1
1
  guard 'migrate' do
2
2
  watch(%r{^db/migrate/(\d+).+\.rb})
3
+ watch('db/seeds.rb')
3
4
  end
4
5
 
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module MigrateVersion
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -142,6 +142,16 @@ describe Guard::Migrate do
142
142
  end
143
143
  end
144
144
  end
145
+
146
+ context "when the seeds file is passed as the paths" do
147
+ let(:paths){ ['db/seeds.rb'] }
148
+ let(:options){ {:seed => true, :test_clone => true} }
149
+ its(:seed_only_string){ should match(/db:seed db:test:clone/) }
150
+ it "runs the rake command with seed only" do
151
+ subject.should_receive(:system).with(subject.seed_only_string)
152
+ subject.run_on_changes paths
153
+ end
154
+ end
145
155
  end
146
156
 
147
157
  context "run on change should fixup the path to only the version" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-migrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
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: 2012-07-15 00:00:00.000000000 Z
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: guard
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 2.6.0
37
+ version: 2.11.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 2.6.0
45
+ version: 2.11.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: guard-rspec
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 0.3.1
53
+ version: 1.2.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 0.3.1
61
+ version: 1.2.0
62
62
  description: Guard::Migrate automatically runs your database migrations when needed
63
63
  email:
64
64
  - geofflanotte@gmail.com
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  segments:
93
93
  - 0
94
- hash: 1535392744399967098
94
+ hash: 4308447101882295894
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements: