diff --git a/lib/hair_trigger/migration_reader.rb b/lib/hair_trigger/migration_reader.rb index 2f079a3..29e3b68 100644 --- a/lib/hair_trigger/migration_reader.rb +++ b/lib/hair_trigger/migration_reader.rb @@ -23,6 +23,7 @@ def get_triggers(source, options) # find the block of the up method sexps = sexps.last if sexps.last.is_a?(Sexp) && sexps.last[0] == :block sexps = sexps.detect{ |s| s.is_a?(Sexp) && (s[0] == :defs && s[1] && s[1][0] == :self && s[2] == :up || s[0] == :defn && s[1] == :up) } + return [] unless sexps # no `up` method... unsupported `change` perhaps? sexps.each do |sexp| next unless (method = extract_method_call(sexp)) && [:create_trigger, :drop_trigger].include?(method) trigger = instance_eval("generate_" + generator.process(sexp)) @@ -32,6 +33,7 @@ def get_triggers(source, options) triggers rescue $stderr.puts "Error reading triggers in #{source.filename rescue "schema.rb"}: #{$!}" + [] end private diff --git a/spec/migrations_spec.rb b/spec/migrations_spec.rb index f9acc1c..a56fd95 100644 --- a/spec/migrations_spec.rb +++ b/spec/migrations_spec.rb @@ -6,9 +6,9 @@ describe "migrations" do include_context "hairtrigger utils" + let(:adapter) { :sqlite3 } describe "migrations_current?" do - let(:adapter) { :sqlite3 } it "should return false if there are pending model triggers" do reset_tmp(:migration_glob => "*initial_tables*") @@ -37,4 +37,24 @@ HairTrigger.should be_migrations_current end end + + describe "current_triggers" do + it "should be inferred from self.up methods" do + reset_tmp(:migration_glob => "20110331212*") + initialize_db + + migrations = HairTrigger.current_migrations + migrations.size.should == 1 + migrations[0][1].prepared_name.should == "users_after_insert_row_when_new_name_bob__tr" + end + + it "should not be inferred from change methods" do + reset_tmp(:migration_glob => "*manual*") + replace_file_contents("tmp/migrations/20110417185102_manual_user_trigger.rb", "def up", "def change") + initialize_db + + migrations = HairTrigger.current_migrations + migrations.size.should == 0 + end + end end