# typed: true
class UniqueE621TaggingsIndex < ActiveRecord::Migration[7.2]
  def up
    add_index :domain_e621_taggings, %i[post_id tag_id], unique: true
    remove_index :domain_e621_taggings, :post_id
    remove_index :domain_e621_taggings, :tag_id
    remove_column :domain_e621_taggings, :id, :bigint

    # columns cannot be null
    change_column_null :domain_e621_taggings, :post_id, false
    change_column_null :domain_e621_taggings, :tag_id, false

    # foreign key constraints
    add_foreign_key :domain_e621_taggings, :domain_e621_posts, column: :post_id
    add_foreign_key :domain_e621_taggings, :domain_e621_tags, column: :tag_id
  end

  def down
    add_column :domain_e621_taggings, :id, :bigint, primary_key: true
    add_index :domain_e621_taggings, :post_id
    add_index :domain_e621_taggings, :tag_id
    remove_index :domain_e621_taggings, %i[post_id tag_id]

    change_column_null :domain_e621_taggings, :post_id, true
    change_column_null :domain_e621_taggings, :tag_id, true

    # foreign key constraints
    remove_foreign_key :domain_e621_taggings, :domain_e621_posts
    remove_foreign_key :domain_e621_taggings, :domain_e621_tags
  end
end
