2023-04-09 09:25:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-20 16:18:00 +00:00
|
|
|
# Some migrations have been present in glitch-soc for a long time and have then
|
|
|
|
# been merged in upstream Mastodon, under a different version number.
|
|
|
|
#
|
|
|
|
# This puts us in an uneasy situation in which if we remove upstream's
|
|
|
|
# migration file, people migrating from upstream will end up having a conflict
|
|
|
|
# with their already-ran migration.
|
|
|
|
#
|
|
|
|
# On the other hand, if we keep upstream's migration and remove our own,
|
|
|
|
# any current glitch-soc user will have a conflict during migration.
|
|
|
|
#
|
|
|
|
# For lack of a better solution, as those migrations are indeed identical,
|
|
|
|
# we decided monkey-patching Rails' Migrator to completely ignore the duplicate,
|
|
|
|
# keeping only the one that has run, or an arbitrary one.
|
|
|
|
|
2023-04-09 09:25:30 +00:00
|
|
|
ALLOWED_DUPLICATES = [2018_04_10_220657, 2018_08_31_171112].freeze
|
2019-11-20 16:03:25 +00:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class Migrator
|
2023-10-24 18:23:46 +00:00
|
|
|
def self.new(direction, migrations, schema_migration, internal_metadata, target_version = nil)
|
2019-11-20 16:03:25 +00:00
|
|
|
migrated = Set.new(Base.connection.migration_context.get_all_versions)
|
|
|
|
|
2023-12-20 21:25:23 +00:00
|
|
|
migrations.group_by(&:name).each_value do |duplicates|
|
2023-04-09 09:25:30 +00:00
|
|
|
next unless duplicates.length > 1 && duplicates.all? { |m| ALLOWED_DUPLICATES.include?(m.version) }
|
|
|
|
|
|
|
|
# We have a set of allowed duplicates. Keep the migrated one, if any.
|
|
|
|
non_migrated = duplicates.reject { |m| migrated.include?(m.version.to_i) }
|
2019-11-20 16:03:25 +00:00
|
|
|
|
2023-04-09 09:25:30 +00:00
|
|
|
migrations = begin
|
|
|
|
if duplicates.length == non_migrated.length || non_migrated.empty?
|
2019-11-20 16:03:25 +00:00
|
|
|
# There weren't any migrated one, so we have to pick one “canonical” migration
|
2023-04-09 09:25:30 +00:00
|
|
|
migrations - duplicates[1..]
|
2019-11-20 16:03:25 +00:00
|
|
|
else
|
|
|
|
# Just reject every duplicate which hasn't been migrated yet
|
2023-04-09 09:25:30 +00:00
|
|
|
migrations - non_migrated
|
2019-11-20 16:03:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-24 18:23:46 +00:00
|
|
|
super(direction, migrations, schema_migration, internal_metadata, target_version)
|
2019-11-20 16:03:25 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-10 18:03:14 +00:00
|
|
|
|
|
|
|
class MigrationContext
|
|
|
|
def needs_migration?
|
|
|
|
# A set of duplicated migrations is considered migrated if at least one of
|
|
|
|
# them is migrated.
|
|
|
|
migrated = get_all_versions
|
2023-12-20 21:25:23 +00:00
|
|
|
migrations.group_by(&:name).each_value do |duplicates|
|
2020-02-10 18:03:14 +00:00
|
|
|
return true unless duplicates.any? { |m| migrated.include?(m.version.to_i) }
|
|
|
|
end
|
2023-04-09 09:25:30 +00:00
|
|
|
false
|
2020-02-10 18:03:14 +00:00
|
|
|
end
|
|
|
|
end
|
2019-11-20 16:03:25 +00:00
|
|
|
end
|