DEV: Safeguard for migration that disables DDL transactions.

DEV: Safeguard for migration that disables DDL transactions.

Follow up to c82a9290250f39e67acfed429e3888781447f720.

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 74c7980..0574611 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

GitHub sha: f40b28d4

1 Like