FIX: Redo migrations such that tables are dropped in a safe manner (#56)

FIX: Redo migrations such that tables are dropped in a safe manner (#56)

This is a reimplementation of 17b0147c which ensures the new table has the correct indexes, defaults and sequences

Co-authored-by: Alan Guo Xiang Tan gxtan1990@gmail.com Co-authored-by: Daniel Waterworth me@danielwaterworth.com

diff --git a/db/migrate/20200409102643_rename_tables_to_discourse_post_event.rb b/db/migrate/20200409102643_rename_tables_to_discourse_post_event.rb
new file mode 100644
index 0000000..2960d18
--- /dev/null
+++ b/db/migrate/20200409102643_rename_tables_to_discourse_post_event.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+require 'migration/table_dropper'
+
+class RenameTablesToDiscoursePostEvent < ActiveRecord::Migration[6.0]
+  def up
+    unless table_exists?(:discourse_post_event_events)
+      Migration::TableDropper.read_only_table(:discourse_calendar_post_events)
+
+      execute <<~SQL
+        CREATE TABLE discourse_post_event_events
+        (LIKE discourse_calendar_post_events INCLUDING ALL);
+      SQL
+
+      execute <<~SQL
+        INSERT INTO discourse_post_event_events
+        SELECT *
+        FROM discourse_calendar_post_events
+      SQL
+
+      execute <<~SQL
+        ALTER SEQUENCE discourse_calendar_post_events_id_seq
+        RENAME TO discourse_post_event_events_id_seq
+      SQL
+
+      execute <<~SQL
+        ALTER SEQUENCE discourse_post_event_events_id_seq
+        OWNED BY discourse_post_event_events.id
+      SQL
+    end
+
+    unless table_exists?(:discourse_post_event_invitees)
+      Migration::TableDropper.read_only_table(:discourse_calendar_invitees)
+
+      execute <<~SQL
+        CREATE TABLE discourse_post_event_invitees
+        (LIKE discourse_calendar_invitees INCLUDING ALL)
+      SQL
+
+      execute <<~SQL
+        INSERT INTO discourse_post_event_invitees
+        SELECT *
+        FROM discourse_calendar_invitees
+      SQL
+
+      execute <<~SQL
+        ALTER SEQUENCE discourse_calendar_invitees_id_seq
+        RENAME TO discourse_post_event_invitees_id_seq
+      SQL
+
+      execute <<~SQL
+        ALTER SEQUENCE discourse_post_event_invitees_id_seq
+        OWNED BY discourse_post_event_invitees.id
+      SQL
+    end
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end
diff --git a/db/post_migrate/20200409102643_rename_tables_to_discourse_post_event.rb b/db/post_migrate/20200409102643_rename_tables_to_discourse_post_event.rb
deleted file mode 100644
index ee7f779..0000000
--- a/db/post_migrate/20200409102643_rename_tables_to_discourse_post_event.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-# frozen_string_literal: true
-
-class RenameTablesToDiscoursePostEvent < ActiveRecord::Migration[6.0]
-  def up
-    rename_table :discourse_calendar_post_events, :discourse_post_event_events
-    rename_table :discourse_calendar_invitees, :discourse_post_event_invitees
-  end
-
-  def down
-    rename_table :discourse_post_event_events, :discourse_calendar_post_events
-    rename_table :discourse_post_event_invitees, :discourse_calendar_invitees
-  end
-end
diff --git a/db/post_migrate/20200805073343_drop_old_discourse_calendar_tables.rb b/db/post_migrate/20200805073343_drop_old_discourse_calendar_tables.rb
new file mode 100644
index 0000000..9b69281
--- /dev/null
+++ b/db/post_migrate/20200805073343_drop_old_discourse_calendar_tables.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+require 'migration/table_dropper'
+
+class DropOldDiscourseCalendarTables < ActiveRecord::Migration[6.0]
+  def up
+    if table_exists?(:discourse_calendar_post_events)
+      Migration::TableDropper.execute_drop(:discourse_calendar_post_events)
+    end
+
+    if table_exists?(:discourse_calendar_invitees)
+      Migration::TableDropper.execute_drop(:discourse_calendar_invitees)
+    end
+  end
+
+  def down
+    raise ActiveRecord::IrrelversibleMigration
+  end
+end

GitHub sha: 4b540f14

This commit appears in #56 which was merged by tgxworld.