Skip to content

Commit

Permalink
support multi-line migrations and try to fix beta databases
Browse files Browse the repository at this point in the history
  • Loading branch information
crschardt committed Dec 21, 2023
1 parent 1499c6d commit 3689069
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

0 comments on commit 3689069

Please sign in to comment.