class CreateUnifiedJsonAttributeIndexes < ActiveRecord::Migration[7.2]
  def change
    # Domain::Post::FaPost.fa_id
    add_index :domain_posts,
              "(cast(json_attributes->>'fa_id' as integer))",
              where: "type = 'Domain::Post::FaPost'",
              name: "idx_domain_fa_posts_on_fa_id",
              unique: true

    # Domain::Post::E621Post.e621_id
    add_index :domain_posts,
              "((json_attributes->>'e621_id')::integer)",
              where: "type = 'Domain::Post::E621Post'",
              name: "idx_domain_e621_posts_on_e621_id",
              unique: true

    # Domain::Post::InkbunnyPost.ib_id
    add_index :domain_posts,
              "((json_attributes->>'ib_id')::integer)",
              where: "type = 'Domain::Post::InkbunnyPost'",
              name: "idx_domain_inkbunny_posts_on_ib_id",
              unique: true

    # Domain::Post::E621Post.uploader_user_id
    add_index :domain_posts,
              "((json_attributes->>'uploader_user_id')::integer)",
              where: "type = 'Domain::Post::E621Post'",
              name: "idx_domain_e621_posts_on_uploader_user_id",
              unique: true

    # Domain::User::FaUser.url_name
    add_index :domain_users,
              "((json_attributes->>'url_name')::text)",
              where: "type = 'Domain::User::FaUser'",
              name: "idx_domain_fa_users_on_url_name",
              unique: true

    # Domain::User::E621User.e621_id
    add_index :domain_users,
              "((json_attributes->>'e621_id')::integer)",
              where: "type = 'Domain::User::E621User'",
              name: "idx_domain_e621_users_on_e621_id",
              unique: true

    # Domain::User::InkbunnyUser.ib_id
    add_index :domain_users,
              "((json_attributes->>'ib_id')::integer)",
              where: "type = 'Domain::User::InkbunnyUser'",
              name: "idx_domain_inkbunny_users_on_ib_id",
              unique: true

    # Domain::User::InkbunnyUser.name
    add_index :domain_users,
              "((json_attributes->>'name')::text)",
              where: "type = 'Domain::User::InkbunnyUser'",
              name: "idx_domain_inkbunny_users_on_name",
              unique: true

    # Domain::User.migrated_user_favs_at
    add_index :domain_users,
              "(json_attributes->>'migrated_user_favs_at')",
              name: "idx_domain_users_on_migrated_user_favs_at"

    # Domain::PostFile::InkbunnyPostFile.ib_id
    add_index :domain_post_files,
              "((json_attributes->>'ib_id')::integer)",
              where: "type = 'Domain::PostFile::InkbunnyPostFile'",
              name: "idx_domain_inkbunny_post_files_on_ib_id",
              unique: true
  end
end
