mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #33585 from yahonda/diag33520
SQLite3 adapter `alter_table` method restores foreign keys
This commit is contained in:
commit
b40fbb5f95
3 changed files with 26 additions and 1 deletions
|
@ -1,3 +1,7 @@
|
|||
* SQLite3 adapter `alter_table` method restores foreign keys.
|
||||
|
||||
*Yasuo Honda*
|
||||
|
||||
* Allow `:to_table` option to `invert_remove_foreign_key`.
|
||||
|
||||
Example:
|
||||
|
|
|
@ -409,13 +409,14 @@ module ActiveRecord
|
|||
|
||||
def alter_table(table_name, options = {})
|
||||
altered_table_name = "a#{table_name}"
|
||||
options_to_restore = { foreign_keys: foreign_keys(table_name) }
|
||||
caller = lambda { |definition| yield definition if block_given? }
|
||||
|
||||
transaction do
|
||||
disable_referential_integrity do
|
||||
move_table(table_name, altered_table_name,
|
||||
options.merge(temporary: true))
|
||||
move_table(altered_table_name, table_name, &caller)
|
||||
move_table(altered_table_name, table_name, options_to_restore, &caller)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -446,6 +447,13 @@ module ActiveRecord
|
|||
primary_key: column_name == from_primary_key
|
||||
)
|
||||
end
|
||||
|
||||
if options[:foreign_keys]
|
||||
options[:foreign_keys].each do |fk|
|
||||
@definition.foreign_key(fk.to_table, fk.options)
|
||||
end
|
||||
end
|
||||
|
||||
yield @definition if block_given?
|
||||
end
|
||||
copy_table_indexes(from, to, options[:rename] || {})
|
||||
|
|
|
@ -64,6 +64,19 @@ if ActiveRecord::Base.connection.supports_foreign_keys_in_create?
|
|||
assert_equal "astronauts", fk.from_table
|
||||
assert_equal "rockets", fk.to_table
|
||||
end
|
||||
|
||||
def test_rename_column_of_child_table
|
||||
rocket = Rocket.create!(name: "myrocket")
|
||||
rocket.astronauts << Astronaut.create!
|
||||
|
||||
@connection.rename_column :astronauts, :name, :astronaut_name
|
||||
|
||||
foreign_keys = @connection.foreign_keys("astronauts")
|
||||
fk = foreign_keys.first
|
||||
assert_equal "myrocket", Rocket.first.name
|
||||
assert_equal "astronauts", fk.from_table
|
||||
assert_equal "rockets", fk.to_table
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue