From fe84f7e323a286d4c6578a2be500533dfe55dd7b Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 1 Jun 2023 09:49:06 +0200 Subject: [PATCH] Add missing primary keys to `accounts_tags` and `statuses_tags` (#25210) --- ...primary_key_to_accounts_tags_join_table.rb | 24 +++++++++++++++++++ ...primary_key_to_statuses_tags_join_table.rb | 24 +++++++++++++++++++ db/schema.rb | 8 +++---- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20230531153942_add_primary_key_to_accounts_tags_join_table.rb create mode 100644 db/migrate/20230531154811_add_primary_key_to_statuses_tags_join_table.rb diff --git a/db/migrate/20230531153942_add_primary_key_to_accounts_tags_join_table.rb b/db/migrate/20230531153942_add_primary_key_to_accounts_tags_join_table.rb new file mode 100644 index 00000000000..0c3c17a3668 --- /dev/null +++ b/db/migrate/20230531153942_add_primary_key_to_accounts_tags_join_table.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class AddPrimaryKeyToAccountsTagsJoinTable < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def up + ActiveRecord::Base.transaction do + safety_assured do + execute 'ALTER TABLE accounts_tags ADD PRIMARY KEY USING INDEX index_accounts_tags_on_tag_id_and_account_id' + + # Rename for consistency as the primary key's name is not represented in db/schema.rb + execute 'ALTER INDEX index_accounts_tags_on_tag_id_and_account_id RENAME TO accounts_tags_pkey' + end + end + end + + def down + safety_assured do + # I have found no way to demote the primary key to an index, instead, re-create the index + execute 'CREATE UNIQUE INDEX CONCURRENTLY index_accounts_tags_on_tag_id_and_account_id ON accounts_tags (tag_id, account_id)' + execute 'ALTER TABLE accounts_tags DROP CONSTRAINT accounts_tags_pkey' + end + end +end diff --git a/db/migrate/20230531154811_add_primary_key_to_statuses_tags_join_table.rb b/db/migrate/20230531154811_add_primary_key_to_statuses_tags_join_table.rb new file mode 100644 index 00000000000..6581943da29 --- /dev/null +++ b/db/migrate/20230531154811_add_primary_key_to_statuses_tags_join_table.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class AddPrimaryKeyToStatusesTagsJoinTable < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def up + ActiveRecord::Base.transaction do + safety_assured do + execute 'ALTER TABLE statuses_tags ADD PRIMARY KEY USING INDEX index_statuses_tags_on_tag_id_and_status_id' + + # Rename for consistency as the primary key's name is not represented in db/schema.rb + execute 'ALTER INDEX index_statuses_tags_on_tag_id_and_status_id RENAME TO statuses_tags_pkey' + end + end + end + + def down + safety_assured do + # I have found no way to demote the primary key to an index, instead, re-create the index + execute 'CREATE UNIQUE INDEX CONCURRENTLY index_statuses_tags_on_tag_id_and_status_id ON statuses_tags (tag_id, status_id)' + execute 'ALTER TABLE statuses_tags DROP CONSTRAINT statuses_tags_pkey' + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f452fef7bab..98fa5d6004f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_05_24_194155) do +ActiveRecord::Schema.define(version: 2023_05_31_154811) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -194,11 +194,10 @@ ActiveRecord::Schema.define(version: 2023_05_24_194155) do t.index ["url"], name: "index_accounts_on_url", opclass: :text_pattern_ops, where: "(url IS NOT NULL)" end - create_table "accounts_tags", id: false, force: :cascade do |t| + create_table "accounts_tags", primary_key: ["tag_id", "account_id"], force: :cascade do |t| t.bigint "account_id", null: false t.bigint "tag_id", null: false t.index ["account_id", "tag_id"], name: "index_accounts_tags_on_account_id_and_tag_id" - t.index ["tag_id", "account_id"], name: "index_accounts_tags_on_tag_id_and_account_id", unique: true end create_table "admin_action_logs", force: :cascade do |t| @@ -979,11 +978,10 @@ ActiveRecord::Schema.define(version: 2023_05_24_194155) do t.index ["uri"], name: "index_statuses_on_uri", unique: true, opclass: :text_pattern_ops, where: "(uri IS NOT NULL)" end - create_table "statuses_tags", id: false, force: :cascade do |t| + create_table "statuses_tags", primary_key: ["tag_id", "status_id"], force: :cascade do |t| t.bigint "status_id", null: false t.bigint "tag_id", null: false t.index ["status_id"], name: "index_statuses_tags_on_status_id" - t.index ["tag_id", "status_id"], name: "index_statuses_tags_on_tag_id_and_status_id", unique: true end create_table "system_keys", force: :cascade do |t|