-- Flyway Migration: V2.1.0 (Part 1)
-- Purpose:
-- 1. Drop unique constraints on GAME.COVER_IMAGE_ID and GAME.HEADER_IMAGE_ID
-- 2. Rename all remaining UK*/FK* constraints to human-readable names (idempotent)
--    This version has been updated to be resilient: if a hashed constraint name
--    does not exist (e.g. on legacy schemas where Hibernate never created it, or
--    on future fresh installs where the friendly name may already be present),
--    the rename attempt is ignored instead of failing the migration.

/******************************************************************************************
 * Helper: Idempotent constraint rename for H2
 * (Updated) Use precompiled Java class instead of inline Java to avoid needing 'javac' at runtime.
 * Class: org.gameyfin.db.h2.H2Aliases.renameConstraintIfExists(Connection,String,String,String)
 ******************************************************************************************/
CREATE ALIAS IF NOT EXISTS RENAME_CONSTRAINT_IF_EXISTS FOR "org.gameyfin.db.h2.H2Aliases.renameConstraintIfExists";

/******************************************************************************************
 * 1. Drop the two unwanted unique constraints on GAME
 ******************************************************************************************/
ALTER TABLE GAME
    DROP CONSTRAINT IF EXISTS UK52RQ62FLPBNTI77BYKM7UAHKQ; -- COVER_IMAGE_ID unique
ALTER TABLE GAME
    DROP CONSTRAINT IF EXISTS UK30B16LLQV54H40XIOGP7T9P35;
-- HEADER_IMAGE_ID unique

/******************************************************************************************
 * 2. Rename remaining UNIQUE constraints (UK*) (tolerant)
 ******************************************************************************************/
CALL RENAME_CONSTRAINT_IF_EXISTS('COMPANY', 'UK4UCNYHR8I0URHWDUDFAHKOB9E', 'UQ_COMPANY_NAME_TYPE');
CALL RENAME_CONSTRAINT_IF_EXISTS('DIRECTORY_MAPPING', 'UKJ3GSATFAHEWFOLSEAJ29O3KYT',
                                 'UQ_DIRECTORY_MAPPING_INTERNAL_PATH');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME', 'UK4WXN9FPXFQ8QXPSB7FY0O3NOA', 'UQ_GAME_PATH');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_IMAGES', 'UKBDE7M3TKHIEEYBINM2ED0B6X1', 'UQ_GAME_IMAGES_IMAGE_ID');
CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_DIRECTORIES', 'UKB5UM4CADBNC6UC8DVOMO81N5F',
                                 'UQ_LIBRARY_DIRECTORIES_DIRECTORY_ID');
CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_GAMES', 'UK3E4VB9NQXPY27VMTA27GU5FY8', 'UQ_LIBRARY_GAMES_GAME_ID');
CALL RENAME_CONSTRAINT_IF_EXISTS('USERS', 'UK6DOTKOTT2KJSP8VW4D0M25FB7', 'UQ_USERS_EMAIL');
CALL RENAME_CONSTRAINT_IF_EXISTS('USERS', 'UKR43AF9AP4EDM43MMTQ01ODDJ6', 'UQ_USERS_USERNAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('USERS', 'UKRSULCN2GYNJY3CDDPWMOSV881', 'UQ_USERS_AVATAR_ID');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELD_METADATA', 'UKHW6U2Y9FLWPTI57QB7K0P27BL',
                                 'UQ_GAME_FIELD_METADATA_SOURCE_ID');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELDS', 'UK1L5OAH0UOOUV4V5A9P0PAK77X', 'UQ_GAME_FIELDS_FIELD_METADATA_ID');

/******************************************************************************************
 * 3. Rename FOREIGN KEY constraints (FK*) (tolerant)
 ******************************************************************************************/
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME', 'FK6CVB43REAYSNYPI0XDY6HQTVF', 'FK_GAME_COVER_IMAGE');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME', 'FK8N86NDPGKMOO7YOLX6HL8N84G', 'FK_GAME_HEADER_IMAGE');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME', 'FKIUVR8XFB63T1K6T43EYYXVO2C', 'FK_GAME_LIBRARY');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_DEVELOPERS', 'FKB12PO9L2B9OJBAIHC82MM2QXB', 'FK_GAME_DEVELOPERS_COMPANY');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_DEVELOPERS', 'FKS4IJSVPIJ53DSL143XVRGBS09', 'FK_GAME_DEVELOPERS_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FEATURES', 'FK63XLTCT60SCIMPM06K8BHBE4A', 'FK_GAME_FEATURES_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_GENRES', 'FKDTSX09YOPD98E0LUEWRUSJD9E', 'FK_GAME_GENRES_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_IMAGES', 'FK5YWV1DMXCM2VSQUEB7RHQ3JK9', 'FK_GAME_IMAGES_IMAGE');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_IMAGES', 'FKOWCPUCV45OX8GT28TXGVHF1AA', 'FK_GAME_IMAGES_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_KEYWORDS', 'FKMVF6HNJ7ROMQQM2EX70A9NVAC', 'FK_GAME_KEYWORDS_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_PERSPECTIVES', 'FKHUEENG29Y1GHBRDI5QHGUXH6E', 'FK_GAME_PERSPECTIVES_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_PUBLISHERS', 'FK49R2KB61LIJ54BQB4VNTST97N', 'FK_GAME_PUBLISHERS_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_PUBLISHERS', 'FKNGLD5ESGRBRH95J5BJF0HEF85', 'FK_GAME_PUBLISHERS_COMPANY');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_THEMES', 'FKRV351JXLIOY0A17Y5BBJJ6FW4', 'FK_GAME_THEMES_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_VIDEO_URLS', 'FKJKKWO8WDS086AS7B2KSLSVKM6', 'FK_GAME_VIDEO_URLS_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_DIRECTORIES', 'FKFNCKIU58I9L89MLXV388DY13B',
                                 'FK_LIBRARY_DIRECTORIES_LIBRARY');
CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_DIRECTORIES', 'FKJDXS58Q1IRTU0IDP6DXJHWAPM',
                                 'FK_LIBRARY_DIRECTORIES_DIRECTORY_MAPPING');

CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_GAMES', 'FK6C71EEDM0I2N1JXDE9BOBWG5M', 'FK_LIBRARY_GAMES_LIBRARY');
CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_GAMES', 'FKDKKKES3DAY0WJ1QMV42KMMFDK', 'FK_LIBRARY_GAMES_GAME');

CALL RENAME_CONSTRAINT_IF_EXISTS('LIBRARY_UNMATCHED_PATHS', 'FKSJ51WC2LBNNXY0LKLWELI6VSB',
                                 'FK_LIBRARY_UNMATCHED_PATHS_LIBRARY');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_ORIGINAL_IDS', 'FK1CSD5QD7VJT7BTTA3G7HGYBUX', 'FK_GAME_ORIGINAL_IDS_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_ORIGINAL_IDS', 'FKMT0XWLPWPU9NP0Q289JBAHJRY', 'FK_GAME_ORIGINAL_IDS_PLUGIN');

CALL RENAME_CONSTRAINT_IF_EXISTS('USERS', 'FK19LFLPG5SEIS4DWRM2LVJLXFV', 'FK_USERS_AVATAR_IMAGE');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELD_SOURCE', 'FKNJC4QSS5APFHTPWP42OAEAL5G', 'FK_GAME_FIELD_SOURCE_PLUGIN');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELD_SOURCE', 'FKSR1BGTX5XJVMAL7FEFGL982TP', 'FK_GAME_FIELD_SOURCE_USER');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELD_METADATA', 'FKQ4RC409TP8FUBTTM733PMJD8F', 'FK_GAME_FIELD_METADATA_SOURCE');

CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELDS', 'FKLNEPI7YWCI86YH21KO9WD9PYF', 'FK_GAME_FIELDS_GAME');
CALL RENAME_CONSTRAINT_IF_EXISTS('GAME_FIELDS', 'FKT8FLOFDAPX5M746S5LW54C5B3', 'FK_GAME_FIELDS_METADATA');

CALL RENAME_CONSTRAINT_IF_EXISTS('TOKEN', 'FKGHOIALAPTI5JFEJ506JBB1O8Y', 'FK_TOKEN_CREATOR_USER');

CALL RENAME_CONSTRAINT_IF_EXISTS('USER_ROLES', 'FKHFH9DX7W3UBF1CO1VDEV94G3F', 'FK_USER_ROLES_USER');

-- End of migration
