diff options
author | Thibaut Girka <thib@sitedethib.com> | 2019-11-20 17:03:25 +0100 |
---|---|---|
committer | Thibaut Girka <thib@sitedethib.com> | 2019-11-20 17:03:25 +0100 |
commit | 6475525f3176698d6d8902b32af62ffd23ddd9e3 (patch) | |
tree | cfd769c83574024085f3af05069dd8871f43f1fb | |
parent | ff67385cfbfd6f5403b2f8b58407dc730dc7e694 (diff) |
Monkey-patch Rails' migrator to ignore duplicate migrations across Mastodon and glitch-soc
-rw-r--r-- | config/initializers/0_duplicate_migrations.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/config/initializers/0_duplicate_migrations.rb b/config/initializers/0_duplicate_migrations.rb new file mode 100644 index 000000000..4c7440fa4 --- /dev/null +++ b/config/initializers/0_duplicate_migrations.rb @@ -0,0 +1,28 @@ +ALLOWED_DUPLICATES = [20180410220657, 20180831171112].freeze + +module ActiveRecord + class Migrator + old_initialize = instance_method(:initialize) + + define_method(:initialize) do |direction, migrations, target_version| + migrated = Set.new(Base.connection.migration_context.get_all_versions) + + migrations.group_by(&:name).each do |name, duplicates| + if 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) } + + if duplicates.length == non_migrated.length + # There weren't any migrated one, so we have to pick one “canonical” migration + migrations = migrations - duplicates[1..-1] + else + # Just reject every duplicate which hasn't been migrated yet + migrations = migrations - non_migrated + end + end + end + + old_initialize.bind(self).(direction, migrations, target_version) + end + end +end |