aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-03-01 11:39:39 +0100
committerYves Senn <yves.senn@gmail.com>2013-03-05 16:12:08 +0100
commitb337390889cb4a9f80ed08daf072a043f0e7ddf3 (patch)
tree896987ee08707fd2c09579bc9f0732674a9ba53d /activerecord/test
parentf1241ef959a45d1b66e2e9399a3e1b09cf95cc2c (diff)
downloadrails-b337390889cb4a9f80ed08daf072a043f0e7ddf3.tar.gz
rails-b337390889cb4a9f80ed08daf072a043f0e7ddf3.tar.bz2
rails-b337390889cb4a9f80ed08daf072a043f0e7ddf3.zip
transactions can be turned off per Migration.
Closes #9483. There are SQL Queries that can't run inside a transaction. Since the Migrator used to wrap all Migrations inside a transaction there was no way to run these queries within a migration. This patch adds `self.disable_ddl_transaction!` to the migration to turn transactions off when necessary.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/migration/logger_test.rb2
-rw-r--r--activerecord/test/cases/migration_test.rb32
2 files changed, 33 insertions, 1 deletions
diff --git a/activerecord/test/cases/migration/logger_test.rb b/activerecord/test/cases/migration/logger_test.rb
index ee0c20747e..97efb94b66 100644
--- a/activerecord/test/cases/migration/logger_test.rb
+++ b/activerecord/test/cases/migration/logger_test.rb
@@ -7,6 +7,7 @@ module ActiveRecord
self.use_transactional_fixtures = false
Migration = Struct.new(:name, :version) do
+ def disable_ddl_transaction; false end
def migrate direction
# do nothing
end
@@ -34,4 +35,3 @@ module ActiveRecord
end
end
end
-
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 831b2ee2b4..960d28fcf5 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -254,7 +254,39 @@ class MigrationTest < ActiveRecord::TestCase
assert_equal "An error has occurred, this and all later migrations canceled:\n\nSomething broke", e.message
Person.reset_column_information
+ assert_not Person.column_methods_hash.include?(:last_name),
+ "On error, the Migrator should revert schema changes but it did not."
+ end
+
+ def test_migration_without_transaction
+ unless ActiveRecord::Base.connection.supports_ddl_transactions?
+ skip "not supported on #{ActiveRecord::Base.connection.class}"
+ end
+
assert_not Person.column_methods_hash.include?(:last_name)
+
+ migration = Class.new(ActiveRecord::Migration) {
+ self.disable_ddl_transaction!
+
+ def version; 101 end
+ def migrate(x)
+ add_column "people", "last_name", :string
+ raise 'Something broke'
+ end
+ }.new
+
+ migrator = ActiveRecord::Migrator.new(:up, [migration], 101)
+ e = assert_raise(StandardError) { migrator.migrate }
+ assert_equal "An error has occurred, all later migrations canceled:\n\nSomething broke", e.message
+
+ Person.reset_column_information
+ assert Person.column_methods_hash.include?(:last_name),
+ "without ddl transactions, the Migrator should not rollback on error but it did."
+ ensure
+ Person.reset_column_information
+ if Person.column_methods_hash.include?(:last_name)
+ Person.connection.remove_column('people', 'last_name')
+ end
end
def test_schema_migrations_table_name