Precompile H2 helper functions

This commit is contained in:
grimsi
2025-09-29 17:27:50 +02:00
parent 8c2cedefe4
commit 90a9e8ba20
2 changed files with 33 additions and 17 deletions
@@ -0,0 +1,30 @@
package org.gameyfin.db.h2
import java.sql.Connection
import java.sql.SQLException
/**
* H2 helper methods exposed as SQL ALIASes.
* <p>
* 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
}
}
}
}
}
@@ -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