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>
This commit is contained in:
David Taylor 2020-08-06 02:05:17 +01:00 committed by GitHub
parent b82b80fb22
commit 4b540f143d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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