mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #8588 from marcandre/fix_reversible
Fix Migration#reversible by not using `transaction`.
This commit is contained in:
commit
7ab469c839
5 changed files with 24 additions and 9 deletions
|
@ -669,10 +669,13 @@ module ActiveRecord
|
||||||
rename_column_sql
|
rename_column_sql
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_column_sql(table_name, *column_names)
|
def remove_column_sql(table_name, column_name, type = nil, options = {})
|
||||||
columns_for_remove(table_name, *column_names).map {|column_name| "DROP #{column_name}" }
|
"DROP #{quote_column_name(column_name)}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_columns_sql(table_name, *column_names)
|
||||||
|
column_names.map {|column_name| remove_column_sql(table_name, column_name) }
|
||||||
end
|
end
|
||||||
alias :remove_columns_sql :remove_column
|
|
||||||
|
|
||||||
def add_index_sql(table_name, column_name, options = {})
|
def add_index_sql(table_name, column_name, options = {})
|
||||||
index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
|
index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
|
||||||
|
|
|
@ -477,7 +477,7 @@ module ActiveRecord
|
||||||
# end
|
# end
|
||||||
def reversible
|
def reversible
|
||||||
helper = ReversibleBlockHelper.new(reverting?)
|
helper = ReversibleBlockHelper.new(reverting?)
|
||||||
transaction{ yield helper }
|
execute_block{ yield helper }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Runs the given migration classes.
|
# Runs the given migration classes.
|
||||||
|
@ -639,6 +639,15 @@ module ActiveRecord
|
||||||
"%.3d" % number
|
"%.3d" % number
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def execute_block
|
||||||
|
if connection.respond_to? :execute_block
|
||||||
|
super # use normal delegation to record the block
|
||||||
|
else
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# MigrationProxy is used to defer loading of the actual migration classes
|
# MigrationProxy is used to defer loading of the actual migration classes
|
||||||
|
|
|
@ -73,7 +73,7 @@ module ActiveRecord
|
||||||
[:create_table, :create_join_table, :rename_table, :add_column, :remove_column,
|
[:create_table, :create_join_table, :rename_table, :add_column, :remove_column,
|
||||||
:rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
|
:rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
|
||||||
:change_column_default, :add_reference, :remove_reference, :transaction,
|
:change_column_default, :add_reference, :remove_reference, :transaction,
|
||||||
:drop_join_table, :drop_table,
|
:drop_join_table, :drop_table, :execute_block,
|
||||||
:change_column, :execute, :remove_columns, # irreversible methods need to be here too
|
:change_column, :execute, :remove_columns, # irreversible methods need to be here too
|
||||||
].each do |method|
|
].each do |method|
|
||||||
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
||||||
|
@ -94,6 +94,7 @@ module ActiveRecord
|
||||||
module StraightReversions
|
module StraightReversions
|
||||||
private
|
private
|
||||||
{ transaction: :transaction,
|
{ transaction: :transaction,
|
||||||
|
execute_block: :execute_block,
|
||||||
create_table: :drop_table,
|
create_table: :drop_table,
|
||||||
create_join_table: :drop_join_table,
|
create_join_table: :drop_join_table,
|
||||||
add_column: :remove_column,
|
add_column: :remove_column,
|
||||||
|
|
|
@ -89,8 +89,10 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
if ActiveRecord::Base.connection.table_exists?("horses")
|
%w[horses new_horses].each do |table|
|
||||||
ActiveRecord::Base.connection.drop_table("horses")
|
if ActiveRecord::Base.connection.table_exists?(table)
|
||||||
|
ActiveRecord::Base.connection.drop_table(table)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -50,9 +50,9 @@ module ApplicationTests
|
||||||
assert_match(/AddEmailToUsers: migrated/, output)
|
assert_match(/AddEmailToUsers: migrated/, output)
|
||||||
|
|
||||||
output = `rake db:rollback STEP=2`
|
output = `rake db:rollback STEP=2`
|
||||||
assert_match(/drop_table\("users"\)/, output)
|
assert_match(/drop_table\(:users\)/, output)
|
||||||
assert_match(/CreateUsers: reverted/, output)
|
assert_match(/CreateUsers: reverted/, output)
|
||||||
assert_match(/remove_column\("users", :email\)/, output)
|
assert_match(/remove_column\(:users, :email, :string\)/, output)
|
||||||
assert_match(/AddEmailToUsers: reverted/, output)
|
assert_match(/AddEmailToUsers: reverted/, output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue