Skip to content

Commit

Permalink
don't choke on migrations without an up, fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
jenseng committed Mar 31, 2014
1 parent c3e6202 commit e70fe7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/hair_trigger/migration_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -32,6 +33,7 @@ def get_triggers(source, options)
triggers
rescue
$stderr.puts "Error reading triggers in #{source.filename rescue "schema.rb"}: #{$!}"
[]
end

private
Expand Down
22 changes: 21 additions & 1 deletion spec/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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*")
Expand Down Expand Up @@ -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

0 comments on commit e70fe7f

Please sign in to comment.