Skip to content

Commit

Permalink
Fix older migrations on Ruby 3 (mastodon#16174)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored May 7, 2021
1 parent 0ad240c commit a5f91a1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative '../lib/enumerable'
require_relative '../lib/sanitize_ext/sanitize_config'
require_relative '../lib/redis/namespace_extensions'
require_relative '../lib/paperclip/schema_extensions'
require_relative '../lib/paperclip/validation_extensions'
require_relative '../lib/paperclip/url_generator_extensions'
require_relative '../lib/paperclip/attachment_extensions'
Expand Down
16 changes: 8 additions & 8 deletions lib/mastodon/migration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def add_timestamps_with_timezone(table_name, **options)
allow_null: options[:null]
)
else
add_column(table_name, column_name, :datetime_with_timezone, options)
add_column(table_name, column_name, :datetime_with_timezone, **options)
end
end
end
Expand All @@ -120,7 +120,7 @@ def add_concurrent_index(table_name, column_name, **options)
options = options.merge({ algorithm: :concurrently })
disable_statement_timeout

add_index(table_name, column_name, options)
add_index(table_name, column_name, **options)
end

# Removes an existed index, concurrently when supported
Expand All @@ -144,7 +144,7 @@ def remove_concurrent_index(table_name, column_name, **options)
disable_statement_timeout
end

remove_index(table_name, options.merge({ column: column_name }))
remove_index(table_name, **options.merge({ column: column_name }))
end

# Removes an existing index, concurrently when supported
Expand All @@ -168,7 +168,7 @@ def remove_concurrent_index_by_name(table_name, index_name, **options)
disable_statement_timeout
end

remove_index(table_name, options.merge({ name: index_name }))
remove_index(table_name, **options.merge({ name: index_name }))
end

# Only available on Postgresql >= 9.2
Expand Down Expand Up @@ -472,7 +472,7 @@ def rename_column_concurrently(table, old, new, type: nil)
col_opts[:limit] = old_col.limit
end

add_column(table, new, new_type, col_opts)
add_column(table, new, new_type, **col_opts)

# We set the default value _after_ adding the column so we don't end up
# updating any existing data with the default value. This isn't
Expand Down Expand Up @@ -510,10 +510,10 @@ def change_column_type_concurrently(table, column, new_type)
new_pk_index_name = "index_#{table}_on_#{column}_cm"

unless indexes_for(table, column).find{|i| i.name == old_pk_index_name}
add_concurrent_index(table, [temp_column], {
add_concurrent_index(table, [temp_column],
unique: true,
name: new_pk_index_name
})
)
end
end
end
Expand Down Expand Up @@ -763,7 +763,7 @@ def copy_indexes(table, old, new)
options[:using] = index.using if index.using
options[:where] = index.where if index.where

add_concurrent_index(table, new_columns, options)
add_concurrent_index(table, new_columns, **options)
end
end

Expand Down
37 changes: 37 additions & 0 deletions lib/paperclip/schema_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# Monkey-patch various Paperclip methods for Ruby 3.0 compatibility

module Paperclip
module Schema
module StatementsExtensions
def add_attachment(table_name, *attachment_names)
raise ArgumentError, 'Please specify attachment name in your add_attachment call in your migration.' if attachment_names.empty?

options = attachment_names.extract_options!

attachment_names.each do |attachment_name|
COLUMNS.each_pair do |column_name, column_type|
column_options = options.merge(options[column_name.to_sym] || {})
add_column(table_name, "#{attachment_name}_#{column_name}", column_type, **column_options)
end
end
end
end

module TableDefinitionExtensions
def attachment(*attachment_names)
options = attachment_names.extract_options!
attachment_names.each do |attachment_name|
COLUMNS.each_pair do |column_name, column_type|
column_options = options.merge(options[column_name.to_sym] || {})
column("#{attachment_name}_#{column_name}", column_type, **column_options)
end
end
end
end
end
end

Paperclip::Schema::Statements.prepend(Paperclip::Schema::StatementsExtensions)
Paperclip::Schema::TableDefinition.prepend(Paperclip::Schema::TableDefinitionExtensions)

0 comments on commit a5f91a1

Please sign in to comment.