20230803112520_add_primary_key_to_preview_cards_statuses_join_table.rb 770 B

1234567891011121314151617181920
  1. # frozen_string_literal: true
  2. class AddPrimaryKeyToPreviewCardsStatusesJoinTable < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. def up
  5. safety_assured do
  6. execute 'ALTER TABLE preview_cards_statuses ADD PRIMARY KEY USING INDEX preview_cards_statuses_pkey'
  7. end
  8. end
  9. def down
  10. safety_assured do
  11. # I have found no way to demote the primary key to an index, instead, re-create the index
  12. execute 'CREATE UNIQUE INDEX CONCURRENTLY preview_cards_statuses_pkey_tmp ON preview_cards_statuses (status_id, preview_card_id)'
  13. execute 'ALTER TABLE preview_cards_statuses DROP CONSTRAINT preview_cards_statuses_pkey'
  14. execute 'ALTER INDEX preview_cards_statuses_pkey_tmp RENAME TO preview_cards_statuses_pkey'
  15. end
  16. end
  17. end