diff --git a/app/src/main/kotlin/org/gameyfin/db/h2/H2Aliases.kt b/app/src/main/kotlin/org/gameyfin/db/h2/H2Aliases.kt new file mode 100644 index 0000000..f74ade9 --- /dev/null +++ b/app/src/main/kotlin/org/gameyfin/db/h2/H2Aliases.kt @@ -0,0 +1,30 @@ +package org.gameyfin.db.h2 + +import java.sql.Connection +import java.sql.SQLException + +/** + * H2 helper methods exposed as SQL ALIASes. + *

+ * Kotlin implementation replacing the former Java version so a JDK (javac) is not + * required at runtime for defining aliases in migration scripts. + */ +object H2Aliases { + /** + * Renames a constraint if it exists, swallowing only H2 error code 90057 (constraint not found). + */ + @JvmStatic + @Throws(SQLException::class) + fun renameConstraintIfExists(conn: Connection, table: String, oldName: String, newName: String) { + conn.createStatement().use { st -> + try { + st.execute("ALTER TABLE $table RENAME CONSTRAINT $oldName TO $newName") + } catch (e: SQLException) { + if (e.errorCode != 90057) { // ignore only 'constraint not found' + throw e + } + } + } + } +} + diff --git a/app/src/main/resources/db/migration/V2.1.0.1__Drop_game_image_uniques_and_rename_constraints.sql b/app/src/main/resources/db/migration/V2.1.0.1__Drop_game_image_uniques_and_rename_constraints.sql index f1e776b..24d5d25 100644 --- a/app/src/main/resources/db/migration/V2.1.0.1__Drop_game_image_uniques_and_rename_constraints.sql +++ b/app/src/main/resources/db/migration/V2.1.0.1__Drop_game_image_uniques_and_rename_constraints.sql @@ -9,24 +9,10 @@ /****************************************************************************************** * Helper: Idempotent constraint rename for H2 - * H2 does not support RENAME CONSTRAINT IF EXISTS, so we create a Java alias that - * executes a rename and suppresses error 90057 (constraint not found). - * NOTE: Do not declare method as public static; H2 wraps it and adds modifiers itself. + * (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 AS -$$ -import java.sql.*; -@CODE -void run(Connection conn, String table, String oldName, String newName) throws SQLException { - try (Statement st = conn.createStatement()) { - st.execute("ALTER TABLE " + table + " RENAME CONSTRAINT " + oldName + " TO " + newName); - } catch (SQLException e) { - if (e.getErrorCode() != 90057) { // ignore only 'constraint not found' - throw e; - } - } -} -$$; +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