Skip to content

Commit ce46560

Browse files
authored
Merge pull request rails#55313 from joshuay03/better-index-name-length-error-message
Include current character length in index and table name length error messages
2 parents b0405f8 + c2e2527 commit ce46560

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Include current character length in error messages for index and table name length validations.
2+
3+
*Joshua Young*
4+
15
* Add `rename_schema` method for PostgreSQL.
26

37
*T S Vallender*

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "active_support/core_ext/string/access"
4+
require "active_support/core_ext/string/filters"
45
require "openssl"
56

67
module ActiveRecord
@@ -1871,13 +1872,19 @@ def check_constraint_for!(table_name, expression: nil, **options)
18711872

18721873
def validate_index_length!(table_name, new_name, internal = false)
18731874
if new_name.length > index_name_length
1874-
raise ArgumentError, "Index name '#{new_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
1875+
raise ArgumentError, <<~MSG.squish
1876+
Index name '#{new_name}' on table '#{table_name}' is too long (#{new_name.length} characters); the limit
1877+
is #{index_name_length} characters
1878+
MSG
18751879
end
18761880
end
18771881

18781882
def validate_table_length!(table_name)
18791883
if table_name.length > table_name_length
1880-
raise ArgumentError, "Table name '#{table_name}' is too long; the limit is #{table_name_length} characters"
1884+
raise ArgumentError, <<~MSG.squish
1885+
Table name '#{table_name}' is too long (#{table_name.length} characters); the limit is
1886+
#{table_name_length} characters
1887+
MSG
18811888
end
18821889
end
18831890

activerecord/test/cases/migration/index_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_rename_index_too_long
5353
e = assert_raises(ArgumentError) {
5454
connection.rename_index(table_name, "old_idx", too_long_index_name)
5555
}
56-
assert_match(/too long; the limit is #{connection.index_name_length} characters/, e.message)
56+
assert_match(/too long \(#{too_long_index_name.length} characters\); the limit is #{connection.index_name_length} characters/, e.message)
5757

5858
assert connection.index_name_exists?(table_name, "old_idx")
5959
end
@@ -75,7 +75,7 @@ def test_add_index_does_not_accept_too_long_index_names
7575
e = assert_raises(ArgumentError) {
7676
connection.add_index(table_name, "foo", name: too_long_index_name)
7777
}
78-
assert_match(/too long; the limit is #{connection.index_name_length} characters/, e.message)
78+
assert_match(/too long \(#{too_long_index_name.length} characters\); the limit is #{connection.index_name_length} characters/, e.message)
7979

8080
assert_not connection.index_name_exists?(table_name, too_long_index_name)
8181
connection.add_index(table_name, "foo", name: good_index_name)

activerecord/test/cases/migration/rename_table_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_rename_table_raises_for_long_table_names
5656
error = assert_raises(ArgumentError) do
5757
connection.rename_table :test_models, long_name
5858
end
59-
assert_equal "Table name '#{long_name}' is too long; the limit is #{name_limit} characters", error.message
59+
assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message
6060

6161
connection.rename_table :test_models, short_name
6262
assert connection.table_exists?(short_name)

activerecord/test/cases/migration_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_create_table_raises_for_long_table_names
194194
error = assert_raises(ArgumentError) do
195195
connection.create_table(long_name)
196196
end
197-
assert_equal "Table name '#{long_name}' is too long; the limit is #{name_limit} characters", error.message
197+
assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message
198198

199199
connection.create_table(short_name)
200200
assert connection.table_exists?(short_name)

0 commit comments

Comments
 (0)