From 36890699affad7a202932b47b2fd94c9ea24cfe8 Mon Sep 17 00:00:00 2001 From: Craig Schardt Date: Thu, 21 Dec 2023 10:33:28 -0600 Subject: [PATCH] support multi-line migrations and try to fix beta databases --- .../configuration/SqlConfigProvider.java | 44 +++++++++++++++---- .../common/configuration/SqlMigrations.java | 20 ++++----- 2 files changed, 46 insertions(+), 18 deletions(-) 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 c784a9fa51..7b0132ef48 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 @@ -154,7 +154,8 @@ private void doMigration(int index) throws SQLException { stmt.addBatch(sql); } stmt.executeBatch(); - setUserVersion(conn, index); + // stmt.execute(SqlMigrations.SQL[index]); + setUserVersion(conn, index + 1); tryCommit(conn); } catch (SQLException e) { logger.error("Err with migration step " + index, e); @@ -163,19 +164,46 @@ private void doMigration(int index) throws SQLException { } private void initDatabase() { - int currentSchema = getUserVersion(); + int userVersion = getUserVersion(); - if (currentSchema > SqlMigrations.SQL.length) { + if (userVersion > SqlMigrations.SQL.length) { // database must be from a newer version, so warn - } else if (currentSchema < SqlMigrations.SQL.length) { + logger.warn( + "This database is from a newer version of PhotonVision. Check that you are running the right version."); + } else if (userVersion < SqlMigrations.SQL.length) { // older database, run migrations + // first, check to see if this is one of the ones from 2024 beta that need special handling + if (userVersion == 0 && getSchemaVersion() > 0) { + String sql = + "SELECT COUNT(*) AS CNTREC FROM pragma_table_info('cameras') WHERE name='otherpaths_json';"; + try (Connection conn = createConn(true); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); ) { + if (rs.getInt("CNTREC") == 0) { + // need to add otherpaths_json + userVersion = 1; + } else { + // already there, no need to add the column + userVersion = 2; + } + setUserVersion(conn, userVersion); + } catch (SQLException e) { + logger.error( + "Could not determine the version of the database. Delete " + + dbName + + "and restart photonvision.", + e); + } + } + logger.debug("Older database version. Updating schema ... "); - for (int index = currentSchema; index < SqlMigrations.SQL.length; index++) { - try { + try { + for (int index = userVersion; index < SqlMigrations.SQL.length; index++) { doMigration(index); - } catch (SQLException e) { - logger.error("Err with migration", e); } + logger.debug("Database migration succeded. Now on version: " + getUserVersion()); + } catch (SQLException e) { + logger.error("Err with migration", e); } } } 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 35362e88ca..6d0ab38cca 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 @@ -12,17 +12,17 @@ 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" - + ");\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" + + ");" + + "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 '[]'", + "ALTER TABLE cameras ADD COLUMN otherpaths_json TEXT NOT NULL DEFAULT '[]';", // add future migrations here }; }