Remove MySQL-specific code from Mastodon::MigrationHelpers (#15924)

Mastodon::MigrationHelpers has been forked from Gitlab a long time ago, but
Mastodon has never supported using a MySQL database.

Removing MySQL support from Mastodon::MigrationHelpers makes it a little easier
to maintain. In particular, it removes code that would need updating with
Rails 6.
This commit is contained in:
Claire 2021-03-19 13:14:40 +01:00 committed by GitHub
parent 82caed594c
commit c31c95ffe4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,42 +41,18 @@
module Mastodon module Mastodon
module MigrationHelpers module MigrationHelpers
# Stub for Database.postgresql? from GitLab
def self.postgresql?
ActiveRecord::Base.configurations[Rails.env]['adapter'].casecmp('postgresql').zero?
end
# Stub for Database.mysql? from GitLab
def self.mysql?
ActiveRecord::Base.configurations[Rails.env]['adapter'].casecmp('mysql2').zero?
end
# Model that can be used for querying permissions of a SQL user. # Model that can be used for querying permissions of a SQL user.
class Grant < ActiveRecord::Base class Grant < ActiveRecord::Base
self.table_name = self.table_name = 'information_schema.role_table_grants'
if Mastodon::MigrationHelpers.postgresql?
'information_schema.role_table_grants'
else
'mysql.user'
end
def self.scope_to_current_user def self.scope_to_current_user
if Mastodon::MigrationHelpers.postgresql?
where('grantee = user') where('grantee = user')
else
where("CONCAT(User, '@', Host) = current_user()")
end
end end
# Returns true if the current user can create and execute triggers on the # Returns true if the current user can create and execute triggers on the
# given table. # given table.
def self.create_and_execute_trigger?(table) def self.create_and_execute_trigger?(table)
priv = priv = where(privilege_type: 'TRIGGER', table_name: table)
if Mastodon::MigrationHelpers.postgresql?
where(privilege_type: 'TRIGGER', table_name: table)
else
where(Trigger_priv: 'Y')
end
priv.scope_to_current_user.any? priv.scope_to_current_user.any?
end end
@ -141,10 +117,8 @@ module Mastodon
'in the body of your migration class' 'in the body of your migration class'
end end
if MigrationHelpers.postgresql?
options = options.merge({ algorithm: :concurrently }) options = options.merge({ algorithm: :concurrently })
disable_statement_timeout disable_statement_timeout
end
add_index(table_name, column_name, options) add_index(table_name, column_name, options)
end end
@ -199,8 +173,6 @@ module Mastodon
# Only available on Postgresql >= 9.2 # Only available on Postgresql >= 9.2
def supports_drop_index_concurrently? def supports_drop_index_concurrently?
return false unless MigrationHelpers.postgresql?
version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
version >= 90200 version >= 90200
@ -226,13 +198,7 @@ module Mastodon
# While MySQL does allow disabling of foreign keys it has no equivalent # While MySQL does allow disabling of foreign keys it has no equivalent
# of PostgreSQL's "VALIDATE CONSTRAINT". As a result we'll just fall # of PostgreSQL's "VALIDATE CONSTRAINT". As a result we'll just fall
# back to the normal foreign key procedure. # back to the normal foreign key procedure.
if MigrationHelpers.mysql?
return add_foreign_key(source, target,
column: column,
on_delete: on_delete)
else
on_delete = 'SET NULL' if on_delete == :nullify on_delete = 'SET NULL' if on_delete == :nullify
end
disable_statement_timeout disable_statement_timeout
@ -270,7 +236,7 @@ module Mastodon
# the database. Disable the session's statement timeout to ensure # the database. Disable the session's statement timeout to ensure
# migrations don't get killed prematurely. (PostgreSQL only) # migrations don't get killed prematurely. (PostgreSQL only)
def disable_statement_timeout def disable_statement_timeout
execute('SET statement_timeout TO 0') if MigrationHelpers.postgresql? execute('SET statement_timeout TO 0')
end end
# Updates the value of a column in batches. # Updates the value of a column in batches.
@ -487,11 +453,7 @@ module Mastodon
# If we were in the middle of update_column_in_batches, we should remove # If we were in the middle of update_column_in_batches, we should remove
# the old column and start over, as we have no idea where we were. # the old column and start over, as we have no idea where we were.
if column_for(table, new) if column_for(table, new)
if MigrationHelpers.postgresql?
remove_rename_triggers_for_postgresql(table, trigger_name) remove_rename_triggers_for_postgresql(table, trigger_name)
else
remove_rename_triggers_for_mysql(trigger_name)
end
remove_column(table, new) remove_column(table, new)
end end
@ -521,13 +483,8 @@ module Mastodon
quoted_old = quote_column_name(old) quoted_old = quote_column_name(old)
quoted_new = quote_column_name(new) quoted_new = quote_column_name(new)
if MigrationHelpers.postgresql?
install_rename_triggers_for_postgresql(trigger_name, quoted_table, install_rename_triggers_for_postgresql(trigger_name, quoted_table,
quoted_old, quoted_new) quoted_old, quoted_new)
else
install_rename_triggers_for_mysql(trigger_name, quoted_table,
quoted_old, quoted_new)
end
update_column_in_batches(table, new, Arel::Table.new(table)[old]) update_column_in_batches(table, new, Arel::Table.new(table)[old])
@ -685,11 +642,7 @@ module Mastodon
check_trigger_permissions!(table) check_trigger_permissions!(table)
if MigrationHelpers.postgresql?
remove_rename_triggers_for_postgresql(table, trigger_name) remove_rename_triggers_for_postgresql(table, trigger_name)
else
remove_rename_triggers_for_mysql(trigger_name)
end
remove_column(table, old) remove_column(table, old)
end end
@ -844,19 +797,10 @@ module Mastodon
quoted_pattern = Arel::Nodes::Quoted.new(pattern.to_s) quoted_pattern = Arel::Nodes::Quoted.new(pattern.to_s)
quoted_replacement = Arel::Nodes::Quoted.new(replacement.to_s) quoted_replacement = Arel::Nodes::Quoted.new(replacement.to_s)
if MigrationHelpers.mysql?
locate = Arel::Nodes::NamedFunction
.new('locate', [quoted_pattern, column])
insert_in_place = Arel::Nodes::NamedFunction
.new('insert', [column, locate, pattern.size, quoted_replacement])
Arel::Nodes::SqlLiteral.new(insert_in_place.to_sql)
else
replace = Arel::Nodes::NamedFunction replace = Arel::Nodes::NamedFunction
.new("regexp_replace", [column, quoted_pattern, quoted_replacement]) .new("regexp_replace", [column, quoted_pattern, quoted_replacement])
Arel::Nodes::SqlLiteral.new(replace.to_sql) Arel::Nodes::SqlLiteral.new(replace.to_sql)
end end
end
def remove_foreign_key_without_error(*args) def remove_foreign_key_without_error(*args)
remove_foreign_key(*args) remove_foreign_key(*args)