1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Give a nice message if there are duplicate migrations instead of raising a strange insert error

This commit is contained in:
Jeremy Kemper 2008-06-04 12:06:55 -07:00
parent 30a0ebb3eb
commit e660055472

View file

@ -331,15 +331,26 @@ module ActiveRecord
end end
def assume_migrated_upto_version(version) def assume_migrated_upto_version(version)
version = version.to_i
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)
migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i) migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i)
versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename| versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename|
filename.split('/').last.split('_').first.to_i filename.split('/').last.split('_').first.to_i
end end
execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" unless migrated.include?(version.to_i) unless migrated.include?(version)
(versions - migrated).select { |v| v < version.to_i }.each do |v| execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
end
inserted = Set.new
(versions - migrated).each do |v|
if inserted.include?(v)
raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict."
elsif v < version
execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')"
inserted << v
end
end end
end end