Skip to content

Commit ab3b206

Browse files
authored
Merge pull request rails#49592 from fatkodima/pg-table-name-length-back-to-63
Return back maximum allowed PostgreSQL table name to 63 characters
2 parents 7ead85f + bd9d7c0 commit ab3b206

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,18 @@ def rename_table(table_name, new_name, **options)
385385
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
386386
pk, seq = pk_and_sequence_for(new_name)
387387
if pk
388-
idx = "#{table_name}_pkey"
389-
new_idx = "#{new_name}_pkey"
388+
# PostgreSQL automatically creates an index for PRIMARY KEY with name consisting of
389+
# truncated table name and "_pkey" suffix fitting into max_identifier_length number of characters.
390+
max_pkey_prefix = max_identifier_length - "_pkey".size
391+
idx = "#{table_name[0, max_pkey_prefix]}_pkey"
392+
new_idx = "#{new_name[0, max_pkey_prefix]}_pkey"
390393
execute "ALTER INDEX #{quote_table_name(idx)} RENAME TO #{quote_table_name(new_idx)}"
391-
if seq && seq.identifier == "#{table_name}_#{pk}_seq"
392-
new_seq = "#{new_name}_#{pk}_seq"
394+
395+
# PostgreSQL automatically creates a sequence for PRIMARY KEY with name consisting of
396+
# truncated table name and "#{primary_key}_seq" suffix fitting into max_identifier_length number of characters.
397+
max_seq_prefix = max_identifier_length - "_#{pk}_seq".size
398+
if seq && seq.identifier == "#{table_name[0, max_seq_prefix]}_#{pk}_seq"
399+
new_seq = "#{new_name[0, max_seq_prefix]}_#{pk}_seq"
393400
execute "ALTER TABLE #{seq.quoted} RENAME TO #{quote_table_name(new_seq)}"
394401
end
395402
end

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -605,14 +605,6 @@ def max_identifier_length
605605
@max_identifier_length ||= query_value("SHOW max_identifier_length", "SCHEMA").to_i
606606
end
607607

608-
# Returns the maximum length of a table name.
609-
def table_name_length
610-
# PostgreSQL automatically creates an index for PRIMARY KEY with name consisting of
611-
# truncated table name and "_pkey" suffix fitting into max_identifier_length number of characters.
612-
# We allow smaller table names to be able to correctly rename this index when renaming the table.
613-
max_identifier_length - "_pkey".length
614-
end
615-
616608
# Set the authorized user for this session
617609
def session_auth=(user)
618610
clear_cache!

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ def change
570570
assert connection.table_exists?(:more_testings)
571571
ensure
572572
connection.drop_table(:more_testings) rescue nil
573+
connection.drop_table(:new_more_testings) rescue nil
573574
end
574575

575576
def test_change_column_null_with_non_boolean_arguments_raises_in_a_migration

0 commit comments

Comments
 (0)