From f40b28d4c7e0128fbdbef585a7964626982e04a7 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 9 Apr 2019 14:12:57 +0800 Subject: [PATCH] DEV: Safeguard for migration that disables DDL transactions. Follow up to c82a9290250f39e67acfed429e3888781447f720. --- ...54736_add_index_for_rebake_old_on_posts.rb | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/db/migrate/20190409054736_add_index_for_rebake_old_on_posts.rb b/db/migrate/20190409054736_add_index_for_rebake_old_on_posts.rb index 74c79805f3..05746115e5 100644 --- a/db/migrate/20190409054736_add_index_for_rebake_old_on_posts.rb +++ b/db/migrate/20190409054736_add_index_for_rebake_old_on_posts.rb @@ -2,21 +2,20 @@ class AddIndexForRebakeOldOnPosts < ActiveRecord::Migration[5.2] disable_ddl_transaction! def up - remove_index :posts, name: :index_posts_on_id_and_baked_version + if index_exists?(:posts, :index_posts_on_id_and_baked_version) + remove_index :posts, name: :index_posts_on_id_and_baked_version + end - add_index :posts, :id, - order: { id: :desc }, - where: "(baked_version IS NULL OR baked_version < 2) AND deleted_at IS NULL", - name: :index_for_rebake_old, - algorithm: :concurrently + if !index_exists?(:posts, :index_for_rebake_old) + add_index :posts, :id, + order: { id: :desc }, + where: "(baked_version IS NULL OR baked_version < 2) AND deleted_at IS NULL", + name: :index_for_rebake_old, + algorithm: :concurrently + end end def down - remove_index :posts, name: :index_for_rebake_old - - add_index :posts, [:id, :baked_version], - order: { id: :desc }, - where: "(deleted_at IS NULL)", - algorithm: :concurrently + raise ActiveRecord::IrreversibleMigration end end