Skip to content

Commit

Permalink
Reuse existing validate_index_length! method
Browse files Browse the repository at this point in the history
- Followup of 1ea6cc1.
  • Loading branch information
prathamesh-sonpatki committed Jun 9, 2016
1 parent d653207 commit 8f7ab13
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def index_name_exists?(table_name, index_name, default)
# [<tt>:type</tt>]
# The reference column type. Defaults to +:integer+.
# [<tt>:index</tt>]
# Add an appropriate index. Defaults to false.
# Add an appropriate index. Defaults to false.
# See #add_index for usage of this option.
# [<tt>:foreign_key</tt>]
# Add an appropriate foreign key constraint. Defaults to false.
Expand Down Expand Up @@ -1128,7 +1128,6 @@ def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc
index_type ||= options[:unique] ? "UNIQUE" : ""
index_name = options[:name].to_s if options.key?(:name)
index_name ||= index_name(table_name, index_name_options(column_names))
max_index_length = options.fetch(:internal, false) ? index_name_length : allowed_index_name_length

if options.key?(:algorithm)
algorithm = index_algorithms.fetch(options[:algorithm]) {
Expand All @@ -1142,9 +1141,8 @@ def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc
index_options = options[:where] ? " WHERE #{options[:where]}" : ""
end

if index_name.length > max_index_length
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{max_index_length} characters"
end
validate_index_length!(table_name, index_name, options.fetch(:internal, false))

if data_source_exists?(table_name) && index_name_exists?(table_name, index_name, false)
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
end
Expand Down Expand Up @@ -1276,8 +1274,10 @@ def foreign_key_name(table_name, options) # :nodoc:
end
end

def validate_index_length!(table_name, new_name) # :nodoc:
if new_name.length > allowed_index_name_length
def validate_index_length!(table_name, new_name, internal = false) # :nodoc:
max_index_length = internal ? index_name_length : allowed_index_name_length

if new_name.length > max_index_length
raise ArgumentError, "Index name '#{new_name}' on table '#{table_name}' is too long; the limit is #{allowed_index_name_length} characters"
end
end
Expand Down

0 comments on commit 8f7ab13

Please sign in to comment.