From 9ab5801a1bc5bfc505673d0c74f1a3c1e6c28461 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 May 2020 14:23:27 +0800 Subject: [PATCH] FIX: Allow post migrations using `#change` to carry out unsafe migration --- lib/migration/safe_migrate.rb | 8 ++++++-- spec/components/migration/safe_migrate_spec.rb | 18 ++++++++++++++---- .../change/20990309014015_drop_email_logs.rb | 10 ++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/db/post_migrate/change/20990309014015_drop_email_logs.rb diff --git a/lib/migration/safe_migrate.rb b/lib/migration/safe_migrate.rb index cf6c43be912..0b65ee0a261 100644 --- a/lib/migration/safe_migrate.rb +++ b/lib/migration/safe_migrate.rb @@ -34,13 +34,17 @@ class Migration::SafeMigrate private def is_post_deploy_migration? + instance_methods = self.class.instance_methods(false) + method = - if self.respond_to?(:up) + if instance_methods.include?(:up) :up - elsif self.respond_to?(:change) + elsif instance_methods.include?(:change) :change end + return false if !method + self.method(method).source_location.first.include?( Discourse::DB_POST_MIGRATE_PATH ) diff --git a/spec/components/migration/safe_migrate_spec.rb b/spec/components/migration/safe_migrate_spec.rb index f7d3a3839d6..d15416e4a27 100644 --- a/spec/components/migration/safe_migrate_spec.rb +++ b/spec/components/migration/safe_migrate_spec.rb @@ -99,18 +99,28 @@ describe Migration::SafeMigrate do end describe 'for a post deployment migration' do - it 'should not ban unsafe migrations' do - user = Fabricate(:user) + it 'should not ban unsafe migrations using up' do Migration::SafeMigrate::SafeMigration.enable_safe! - path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate" + path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate/drop_table" + + output = capture_stdout do + migrate_up(path) + end + + expect(output).to include("drop_table(:email_logs)") + end + + it 'should not ban unsafe migrations using change' do + Migration::SafeMigrate::SafeMigration.enable_safe! + + path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate/change" output = capture_stdout do migrate_up(path) end expect(output).to include("drop_table(:email_logs)") - expect(user.reload).to eq(user) end end end diff --git a/spec/fixtures/db/post_migrate/change/20990309014015_drop_email_logs.rb b/spec/fixtures/db/post_migrate/change/20990309014015_drop_email_logs.rb new file mode 100644 index 00000000000..7b4b1e2252d --- /dev/null +++ b/spec/fixtures/db/post_migrate/change/20990309014015_drop_email_logs.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class DropEmailLogs < ActiveRecord::Migration[5.2] + DROPPED_TABLES ||= %i{email_logs} + + def change + drop_table :email_logs + raise ActiveRecord::Rollback + end +end