Skip to content

Fix rename_table for compatibility migrations #2418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def primary_key(*args)
rebuild_primary_key_index_to_default_tablespace(table_name, options)
end

def rename_table(table_name, new_name) # :nodoc:
def rename_table(table_name, new_name, **options) # :nodoc:
if new_name.to_s.length > DatabaseLimits::IDENTIFIER_MAX_LENGTH
raise ArgumentError, "New table name '#{new_name}' is too long; the limit is #{DatabaseLimits::IDENTIFIER_MAX_LENGTH} characters"
end
Expand All @@ -261,7 +261,7 @@ def rename_table(table_name, new_name) # :nodoc:
execute "RENAME #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
execute "RENAME #{quote_table_name("#{table_name}_seq")} TO #{default_sequence_name(new_name)}" rescue nil

rename_table_indexes(table_name, new_name)
rename_table_indexes(table_name, new_name, **options)
end

def drop_table(table_name, **options) # :nodoc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

describe "compatibility migrations" do
include SchemaSpecHelper

before(:all) do
ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
@conn = ActiveRecord::Base.connection
schema_define do
create_table :test_employees, force: true
end
end

after(:all) do
schema_define do
drop_table :test_employees, if_exists: true
drop_table :new_test_employees, if_exists: true
end
end

it "should rename table on 7_0 and below" do
migration = Class.new(ActiveRecord::Migration[7.0]) {
def change
rename_table :test_employees, :new_test_employees
end
}.new

migration.migrate(:up)
expect(@conn.table_exists?(:new_test_employees)).to be_truthy
expect(@conn.table_exists?(:test_employees)).not_to be_truthy

migration.migrate(:down)
expect(@conn.table_exists?(:new_test_employees)).not_to be_truthy
expect(@conn.table_exists?(:test_employees)).to be_truthy
end
end
Loading