Skip to content

Commit

Permalink
Moved max constraint name lengths to adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 29, 2025
1 parent 1cff936 commit 6110962
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/strong_migrations/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def auto_incrementing_types
["primary_key"]
end

def max_constraint_name_length
end

private

def connection
Expand Down
4 changes: 4 additions & 0 deletions lib/strong_migrations/adapters/mysql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def rewrite_blocks
"writes"
end

def max_constraint_name_length
64
end

private

# do not memoize
Expand Down
4 changes: 4 additions & 0 deletions lib/strong_migrations/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def auto_incrementing_types
["primary_key", "serial", "bigserial"]
end

def max_constraint_name_length
63
end

private

def set_timeout(setting, timeout)
Expand Down
3 changes: 1 addition & 2 deletions lib/strong_migrations/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ def check_change_column_null(*args)

# match https://github.com/nullobject/rein
constraint_name = "#{table}_#{column}_null"
# 63 characters is max length for Postgres, 64 characters for MySQL and MariaDB
if constraint_name.bytesize > 63
if adapter.max_constraint_name_length && constraint_name.bytesize > adapter.max_constraint_name_length
constraint_name = connection.check_constraint_options(table, expression, {})[:name]

# avoid collision with Active Record naming for safe_by_default
Expand Down

0 comments on commit 6110962

Please sign in to comment.