Skip to content

Commit

Permalink
add missing column to the cameras table
Browse files Browse the repository at this point in the history
  • Loading branch information
crschardt committed Dec 17, 2023
1 parent 73256f0 commit 35b637d
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ private void initDatabase() {
}

Connection conn = null;
Statement createGlobalTableStatement = null, createCameraTableStatement = null;
Statement createGlobalTableStatement = null;
Statement createCameraTableStatement = null;
Statement oldCameraTableStatement = null;
Statement updateCameraTableStatement = null;

try {
conn = createConn();
if (conn == null) {
Expand Down Expand Up @@ -156,11 +160,42 @@ private void initDatabase() {
logger.error("Err creating cameras table", e);
}

boolean oldTable = false;

try {
oldCameraTableStatement = conn.createStatement();
var sql =
"SELECT COUNT(*) AS CNTREC FROM pragma_table_info('cameras') WHERE name='otherpaths_json';";
var result = oldCameraTableStatement.executeQuery(sql);
logger.debug("otherpaths_json: " + String.valueOf(result.getInt("CNTREC")));
if (result.getInt("CNTREC") == 0) {
// This is an older table, need to migrate to the new format
oldTable = true;
}
} catch (SQLException e) {
logger.error("Err checking for missing otherpaths_json column in cameras table", e);
}
logger.debug("otherpaths_json missing: " + String.valueOf(oldTable));

if (oldTable == true){
try {
updateCameraTableStatement = conn.createStatement();
// String[] otherPaths = {};
var sql =
"ALTER TABLE cameras ADD COLUMN otherpaths_json TEXT NOT NULL DEFAULT '[]'";
updateCameraTableStatement.execute(sql);
logger.debug("Added column otherpaths_json to cameras table");
} catch (SQLException e) {
logger.error("Err updating cameras table with otherpaths_json column", e);
}
}

this.tryCommit(conn);
} finally {
try {
if (createGlobalTableStatement != null) createGlobalTableStatement.close();
if (createCameraTableStatement != null) createCameraTableStatement.close();
if (oldCameraTableStatement != null) oldCameraTableStatement.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
Expand Down

0 comments on commit 35b637d

Please sign in to comment.