aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/migration.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-01-12 15:29:16 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-01-13 14:33:57 -0800
commit01f86cd8ea882bda08071a3e1f5f9e690e83eae9 (patch)
tree6c642e2418f5accc9a7202f29ddb061826140d58 /activerecord/lib/active_record/migration.rb
parent5baa66cbaded58ba59d23b7df5166746222e92b0 (diff)
downloadrails-01f86cd8ea882bda08071a3e1f5f9e690e83eae9.tar.gz
rails-01f86cd8ea882bda08071a3e1f5f9e690e83eae9.tar.bz2
rails-01f86cd8ea882bda08071a3e1f5f9e690e83eae9.zip
moving migrator tests to a migrator test class
Diffstat (limited to 'activerecord/lib/active_record/migration.rb')
-rw-r--r--activerecord/lib/active_record/migration.rb79
1 files changed, 41 insertions, 38 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 0a7a558675..c000b94bc8 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -342,9 +342,9 @@ module ActiveRecord
attr_accessor :name, :version
- def initialize
- @name = self.class.name
- @version = nil
+ def initialize(name = self.class.name, version = nil)
+ @name = name
+ @version = version
@connection = nil
@reverting = false
end
@@ -619,8 +619,6 @@ module ActiveRecord
files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]
- seen = Hash.new false
-
migrations = files.map do |file|
version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?.rb/).first
@@ -628,11 +626,6 @@ module ActiveRecord
version = version.to_i
name = name.camelize
- raise DuplicateMigrationVersionError.new(version) if seen[version]
- raise DuplicateMigrationNameError.new(name) if seen[name]
-
- seen[version] = seen[name] = true
-
MigrationProxy.new(name, version, file, scope)
end
@@ -655,9 +648,9 @@ module ActiveRecord
def initialize(direction, migrations, target_version = nil)
raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
- Base.connection.initialize_schema_migrations_table
@direction = direction
+ @target_version = target_version
if Array(migrations).grep(String).empty?
@migrations = migrations
@@ -666,7 +659,9 @@ module ActiveRecord
@migrations = self.class.migrations(migrations)
end
- @target_version = target_version
+ validate(@migrations)
+
+ Base.connection.initialize_schema_migrations_table
end
def current_version
@@ -748,36 +743,44 @@ module ActiveRecord
end
private
- def record_version_state_after_migrating(version)
- table = Arel::Table.new(self.class.schema_migrations_table_name)
-
- @migrated_versions ||= []
- if down?
- @migrated_versions.delete(version)
- stmt = table.where(table["version"].eq(version.to_s)).compile_delete
- Base.connection.delete stmt
- else
- @migrated_versions.push(version).sort!
- stmt = table.compile_insert table["version"] => version.to_s
- Base.connection.insert stmt
- end
- end
+ def validate(migrations)
+ name ,= migrations.group_by(&:name).find { |_,v| v.length > 1 }
+ raise DuplicateMigrationNameError.new(name) if name
- def up?
- @direction == :up
- end
+ version ,= migrations.group_by(&:version).find { |_,v| v.length > 1 }
+ raise DuplicateMigrationVersionError.new(version) if version
+ end
+
+ def record_version_state_after_migrating(version)
+ table = Arel::Table.new(self.class.schema_migrations_table_name)
- def down?
- @direction == :down
+ @migrated_versions ||= []
+ if down?
+ @migrated_versions.delete(version)
+ stmt = table.where(table["version"].eq(version.to_s)).compile_delete
+ Base.connection.delete stmt
+ else
+ @migrated_versions.push(version).sort!
+ stmt = table.compile_insert table["version"] => version.to_s
+ Base.connection.insert stmt
end
+ end
- # Wrap the migration in a transaction only if supported by the adapter.
- def ddl_transaction(&block)
- if Base.connection.supports_ddl_transactions?
- Base.transaction { block.call }
- else
- block.call
- end
+ def up?
+ @direction == :up
+ end
+
+ def down?
+ @direction == :down
+ end
+
+ # Wrap the migration in a transaction only if supported by the adapter.
+ def ddl_transaction(&block)
+ if Base.connection.supports_ddl_transactions?
+ Base.transaction { block.call }
+ else
+ block.call
end
+ end
end
end