guard-migrate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,37 @@
1
- = guard-migrate
1
+ = Guard::Migrate
2
2
 
3
- Description goes here.
3
+ Migrate guard allows you to keep you migrations up to date while developing without needing to rerun them every time. I developed it because when I am first working out a project, the schema can change several times and this saves me several downs/ups/resets.
4
+
5
+ - This should be compatible with 2.x rails, but has only currently been tested on 3.x rails.
6
+
7
+ = Install
8
+
9
+ Please be sure to have {Guard}[https://github.com/guard/guard] installed before continue.
10
+
11
+ Install the gem:
12
+
13
+ gem install guard-migrate
14
+
15
+ Add it to your Gemfile (inside test group):
16
+
17
+ gem 'guard-migrate'
18
+
19
+ Add guard definition to your Guardfile by running this command:
20
+
21
+ guard init migrate
22
+
23
+ == Options
24
+
25
+ Available options:
26
+
27
+ * :run_on_start - this will run the migration task when you start, reload or run all. Defaults to false. If reset is set to true with this, then it will run a reset on the start, reload, run all instead of just a regular migrate
28
+ * :test_clone - this will run the with the additional `db:test:clone` to update the test database. Defaults to true.
29
+ * :reset - this will run `rake db:migrate:reset` every time migrate is run. Defaults to false.
30
+ * :rails_env - passing this will add "RAILS_ENV=" together with the environment.
31
+
32
+ == Todos
33
+
34
+ * move the customized code to a separate class/module.
4
35
 
5
36
  == Contributing to guard-migrate
6
37
 
@@ -12,6 +43,9 @@ Description goes here.
12
43
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
44
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
45
 
46
+ == Authors
47
+
48
+ {Geoff Lanotte}[https://github.com/glanotte]
15
49
  == Copyright
16
50
 
17
51
  Copyright (c) 2011 Geoff Lanotte. See LICENSE.txt for
data/lib/guard/migrate.rb CHANGED
@@ -6,12 +6,16 @@ module Guard
6
6
  def initialize(watchers=[], options={})
7
7
  super
8
8
 
9
- @reset = true unless options[:reset] == false
9
+ @reset = true if options[:reset] == true
10
10
  @test_clone = true unless options[:test_clone] == false
11
11
  @run_on_start = true if options[:run_on_start] == true
12
12
  @rails_env = options[:rails_env]
13
13
  end
14
14
 
15
+ def bundler?
16
+ @bundler ||= File.exist?("#{Dir.pwd}/Gemfile")
17
+ end
18
+
15
19
  def run_on_start?
16
20
  !!@run_on_start
17
21
  end
@@ -60,17 +64,25 @@ module Guard
60
64
 
61
65
  # Called on file(s) modifications
62
66
  def run_on_change(paths)
63
- self.migrate
67
+ self.migrate(paths.map{|path| path.scan(%r{^db/migrate/(\d+).+\.rb}).flatten.first})
64
68
  end
65
69
 
66
- def migrate
67
- system self.rake_string
70
+ def migrate(paths = [])
71
+ return if !self.reset? && paths.empty?
72
+ system self.rake_string if self.reset?
73
+ paths.each do |path|
74
+ UI.info "Running #{self.rake_string(path)}"
75
+ system self.rake_string(path)
76
+ end
68
77
  end
69
78
 
70
- def rake_string
71
- @rake_string = 'rake'
79
+ def rake_string(path = nil)
80
+ @rake_string = ''
81
+ @rake_string += 'bundle exec ' if self.bundler?
82
+ @rake_string += 'rake'
72
83
  @rake_string += ' db:migrate'
73
84
  @rake_string += ':reset' if self.reset?
85
+ @rake_string += ":redo VERSION=#{path}" if !self.reset? && path && !path.empty?
74
86
  @rake_string += ' db:test:clone' if self.test_clone?
75
87
  @rake_string += " RAILS_ENV=#{self.rails_env}" if self.rails_env
76
88
  @rake_string
@@ -1,4 +1,4 @@
1
1
  guard 'migrate' do
2
- watch('db/migrate/*.rb')
2
+ watch(%r{^db/migrate/(\d+).+\.rb})
3
3
  end
4
4
 
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module MigrateVersion
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -2,11 +2,27 @@ require 'spec_helper'
2
2
 
3
3
  describe Guard::Migrate do
4
4
  let(:options){ {}}
5
- subject{ Guard::Migrate.new([], options) }
5
+ let(:paths){{}}
6
+
7
+ subject{ Guard::Migrate.new(paths, options) }
6
8
 
7
9
  describe "options" do
10
+ context "bundler" do
11
+ context "with a gemfile found" do
12
+ before{File.stub!(:exist?).and_return(true) }
13
+ its(:bundler?){should be_true}
14
+ its(:rake_string){should match(/^bundle exec rake/)}
15
+
16
+ end
17
+ context "with no gemfile found" do
18
+ before{File.stub!(:exist?).and_return(false)}
19
+ its(:bundler?){should_not be_true}
20
+ its(:rake_string){should match(/^rake/)}
21
+ end
22
+
23
+ end
8
24
  context "test clone" do
9
- context "with no optios passed" do
25
+ context "with no options passed" do
10
26
  its(:test_clone?){should be_true}
11
27
  its(:rake_string){should match(/db:test:clone/)}
12
28
  end
@@ -21,15 +37,20 @@ describe Guard::Migrate do
21
37
 
22
38
  context "reset" do
23
39
  context "with no options passed" do
24
- its(:reset?){should be_true}
25
- its(:rake_string){should match(/rake db:migrate:reset/)}
40
+ its(:reset?){should_not be_true}
41
+
42
+ context "with paths" do
43
+ let(:paths){ ['1234'] }
44
+ it "rake string should attempt redo of changed migration" do
45
+ subject.rake_string(paths.first).should match(/rake db:migrate:redo VERSION\=1234/)
46
+ end
47
+ end
26
48
  end
27
49
 
28
- context "when passed false" do
29
- let(:options){ {:reset => false} }
30
- its(:reset?){should_not be_true}
31
- its(:rake_string){should match(/rake db:migrate/)}
32
- its(:rake_string){should_not match(/rake db:migrate:reset/)}
50
+ context "when passed true" do
51
+ let(:options){ {:reset => true} }
52
+ its(:reset?){should be_true}
53
+ its(:rake_string){should match(/rake db:migrate:reset/)}
33
54
  end
34
55
  end
35
56
 
@@ -71,6 +92,22 @@ describe Guard::Migrate do
71
92
  subject.should_receive(:migrate)
72
93
  subject.run_all
73
94
  end
95
+
96
+ context "with reset set to true" do
97
+ let(:options){ {:run_on_start => true, :reset => true} }
98
+ it "should run a migrate reset on start" do
99
+ subject.rake_string.should match(/db:migrate:reset/)
100
+ end
101
+ end
102
+
103
+ context "with reset set to false" do
104
+ let(:options){ {:run_on_start => true, :reset => false} }
105
+ it "should run a regular migrate on start" do
106
+ subject.rake_string.should match(/db:migrate/)
107
+ subject.rake_string.should_not match(/db:migrate:reset/)
108
+ subject.rake_string.should_not match(/db:migrate:redo/)
109
+ end
110
+ end
74
111
  end
75
112
  end
76
113
 
@@ -88,10 +125,12 @@ describe Guard::Migrate do
88
125
  end
89
126
  end
90
127
 
91
- context "migrate" do
128
+ context "run on change should fixup the path to only the version" do
129
+ ##I don't like this test much - consider refactoring
130
+ let(:paths){ ['db/migrate/1234_i_like_cheese.rb'] }
92
131
  it "should run the rake command" do
93
- subject.should_receive(:system).with(subject.rake_string)
94
- subject.run_on_change []
132
+ subject.should_receive(:system).with(subject.rake_string('1234'))
133
+ subject.run_on_change paths
95
134
  end
96
135
  end
97
136
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: guard-migrate
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Geoff Lanotte
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-15 00:00:00 Z
13
+ date: 2011-05-22 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: guard