discourse-topic-voting/db/migrate/20240707170311_rename_voting_tables.rb
Natalie Tay 3d3037729c
DEV: Scope topic voting tables to avoid confusion with post voting (#196)
Renaming discourse_voting to topic_voting since there are two forms of voting in Discourse - posts and topics.

This PR also moves a OnceOff into a post migration. The post migration will be executed, but should ideally be a no-op. This allows us to not have to maintain the OnceOff as it had to be modified before with a previous migration. I considered removing this file altogether, but I don't think there is anything negative from just converting it into a migration, and it might be useful in the unlikely scenario that a forum from the past has never ran the OnceOff before.
2024-07-17 20:26:40 +08:00

73 lines
2 KiB
Ruby

# frozen_string_literal: true
require "migration/table_dropper"
class RenameVotingTables < ActiveRecord::Migration[7.0]
def up
unless table_exists?(:topic_voting_topic_vote_count)
Migration::TableDropper.read_only_table(:discourse_voting_topic_vote_count)
execute <<~SQL
CREATE TABLE topic_voting_topic_vote_count
(LIKE discourse_voting_topic_vote_count INCLUDING ALL);
SQL
execute <<~SQL
INSERT INTO topic_voting_topic_vote_count
SELECT *
FROM discourse_voting_topic_vote_count
SQL
execute <<~SQL
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
end
unless table_exists?(:topic_voting_votes)
Migration::TableDropper.read_only_table(:discourse_voting_votes)
execute <<~SQL
CREATE TABLE topic_voting_votes
(LIKE discourse_voting_votes INCLUDING ALL);
SQL
execute <<~SQL
INSERT INTO topic_voting_votes
SELECT *
FROM discourse_voting_votes
SQL
execute <<~SQL
ALTER TABLE topic_voting_votes
ALTER COLUMN id
SET DEFAULT nextval('discourse_voting_votes_id_seq')
SQL
end
unless table_exists?(:topic_voting_category_settings)
Migration::TableDropper.read_only_table(:discourse_voting_category_settings)
execute <<~SQL
CREATE TABLE topic_voting_category_settings
(LIKE discourse_voting_category_settings INCLUDING ALL);
SQL
execute <<~SQL
INSERT INTO topic_voting_category_settings
SELECT *
FROM discourse_voting_category_settings
SQL
execute <<~SQL
ALTER TABLE topic_voting_category_settings
ALTER COLUMN id
SET DEFAULT nextval('discourse_voting_category_settings_id_seq')
SQL
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end