diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java b/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java index 4443c283a2..c784a9fa51 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java @@ -147,13 +147,17 @@ private void setUserVersion(Connection conn, int value) { } private void doMigration(int index) throws SQLException { + logger.debug("Running migration step " + index); try (Connection conn = createConn(); Statement stmt = conn.createStatement()) { - stmt.execute(SqlMigrations.SQL[index]); + for (String sql : SqlMigrations.SQL[index].split(";")) { + stmt.addBatch(sql); + } + stmt.executeBatch(); setUserVersion(conn, index); tryCommit(conn); } catch (SQLException e) { - logger.error("Err with migration step" + index, e); + logger.error("Err with migration step " + index, e); throw e; } } @@ -165,6 +169,7 @@ private void initDatabase() { // database must be from a newer version, so warn } else if (currentSchema < SqlMigrations.SQL.length) { // older database, run migrations + logger.debug("Older database version. Updating schema ... "); for (int index = currentSchema; index < SqlMigrations.SQL.length; index++) { try { doMigration(index); diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/SqlMigrations.java b/photon-core/src/main/java/org/photonvision/common/configuration/SqlMigrations.java index 62d334fc89..35362e88ca 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/SqlMigrations.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/SqlMigrations.java @@ -4,20 +4,23 @@ Add migrations by adding the SQL commands for each migration sequentially to this array. DO NOT edit or delete existing SQL commands. That will lead to producing an icompatible database. + +You can use multiple SQL statements in one migration step as long as you separate them +with a semicolon (;). */ public final class SqlMigrations { public static final String[] SQL = { // #1 - initial schema "CREATE TABLE IF NOT EXISTS global (\n" - + " filename TINYTEXT PRIMARY KEY,\n" - + " contents mediumtext NOT NULL\n" - + ");" - + "CREATE TABLE IF NOT EXISTS cameras (\n" - + " unique_name TINYTEXT PRIMARY KEY,\n" - + " config_json text NOT NULL,\n" - + " drivermode_json text NOT NULL,\n" - + " pipeline_jsons mediumtext NOT NULL\n" - + ");", + + " filename TINYTEXT PRIMARY KEY,\n" + + " contents mediumtext NOT NULL\n" + + ");\n" + + "CREATE TABLE IF NOT EXISTS cameras (\n" + + " unique_name TINYTEXT PRIMARY KEY,\n" + + " config_json text NOT NULL,\n" + + " drivermode_json text NOT NULL,\n" + + " pipeline_jsons mediumtext NOT NULL\n" + + ");", // #2 - add column otherpaths_json "ALTER TABLE cameras ADD COLUMN otherpaths_json TEXT NOT NULL DEFAULT '[]'", // add future migrations here