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

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

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..395ecfb
--- /dev/null
+++ b/db/migrate/20200409102643_rename_tables_to_discourse_post_event.rb
@@ -0,0 +1,43 @@
+# 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
+      AS TABLE discourse_calendar_post_events
+      WITH NO DATA;
+      SQL
+
+      execute <<~SQL
+      INSERT INTO discourse_post_event_events
+      SELECT *
+      FROM discourse_calendar_post_events
+      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
+      AS TABLE discourse_calendar_invitees
+      WITH NO DATA;
+      SQL
+
+      execute <<~SQL
+      INSERT INTO discourse_post_event_invitees
+      SELECT *
+      FROM discourse_calendar_invitees
+      SQL
+    end
+  end
+
+  def down
+    raise ActiveRecord::IrrelversibleMigration
+  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: 17b0147c

This commit appears in #55 which was approved by davidtaylorhq. It was merged by davidtaylorhq.