Skip to content

Commit

Permalink
Fix rename_table for compatibility migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-smith-nz committed Nov 13, 2024
1 parent 67fef7d commit a1bc9d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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

0 comments on commit a1bc9d9

Please sign in to comment.