From 9beaf240dd60e37165ab304f66b6eee2234e1566 Mon Sep 17 00:00:00 2001 From: Nat Date: Wed, 17 Jul 2024 16:44:26 +0800 Subject: [PATCH] Be a bit smarter about migrations. CREATE TABLE LIKE INCLUDING ALL actually creates an index as well, so there is no need to create an index again. It is probably also better to set the nextval of a seq than to change ownership from one table to another. --- .../20240707170311_rename_voting_tables.rb | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/db/migrate/20240707170311_rename_voting_tables.rb b/db/migrate/20240707170311_rename_voting_tables.rb index 83440e7..ff2bdcf 100644 --- a/db/migrate/20240707170311_rename_voting_tables.rb +++ b/db/migrate/20240707170311_rename_voting_tables.rb @@ -18,13 +18,9 @@ class RenameVotingTables < ActiveRecord::Migration[7.0] SQL execute <<~SQL - ALTER SEQUENCE discourse_voting_topic_vote_count_id_seq - RENAME TO topic_voting_topic_vote_count_id_seq - SQL - - execute <<~SQL - ALTER SEQUENCE topic_voting_topic_vote_count_id_seq - OWNED BY topic_voting_topic_vote_count.id + ALTER TABLE topic_voting_topic_vote_count + ALTER COLUMN id + SET DEFAULT nextval('discourse_voting_topic_vote_count_id_seq') SQL add_index :topic_voting_topic_vote_count, :topic_id, unique: true @@ -44,16 +40,10 @@ class RenameVotingTables < ActiveRecord::Migration[7.0] SQL execute <<~SQL - ALTER SEQUENCE discourse_voting_votes_id_seq - RENAME TO topic_voting_votes_id_seq + ALTER TABLE topic_voting_votes + ALTER COLUMN id + SET DEFAULT nextval('discourse_voting_votes_id_seq') SQL - - execute <<~SQL - ALTER SEQUENCE topic_voting_votes_id_seq - OWNED BY topic_voting_votes.id - SQL - - add_index :topic_voting_votes, %i[user_id topic_id], unique: true end unless table_exists?(:topic_voting_category_settings) @@ -70,16 +60,10 @@ class RenameVotingTables < ActiveRecord::Migration[7.0] SQL execute <<~SQL - ALTER SEQUENCE discourse_voting_category_settings_id_seq - RENAME TO topic_voting_category_settings_id_seq + ALTER TABLE topic_voting_category_settings + ALTER COLUMN id + SET DEFAULT nextval('discourse_voting_category_settings_id_seq') SQL - - execute <<~SQL - ALTER SEQUENCE topic_voting_category_settings_id_seq - OWNED BY topic_voting_category_settings.id - SQL - - add_index :topic_voting_category_settings, :category_id, unique: true end end