diff options
Diffstat (limited to 'netconf')
-rw-r--r-- | netconf/SqliteNetworkConfigMaster.cpp | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/netconf/SqliteNetworkConfigMaster.cpp b/netconf/SqliteNetworkConfigMaster.cpp index 48711c86..88d96c2a 100644 --- a/netconf/SqliteNetworkConfigMaster.cpp +++ b/netconf/SqliteNetworkConfigMaster.cpp @@ -66,27 +66,36 @@ SqliteNetworkConfigMaster::SqliteNetworkConfigMaster(const Identity &signingId,c throw std::runtime_error("SqliteNetworkConfigMaster cannot open database file"); sqlite3_busy_timeout(_db,10000); - sqlite3_stmt *s = (sqlite3_stmt *)0; - if (sqlite3_prepare_v2(_db,"SELECT v FROM Config WHERE k = 'schemaVersion';",-1,&s,(const char **)0) != SQLITE_OK) { - sqlite3_close(_db); - throw std::runtime_error("SqliteNetworkConfigMaster cannot create prepared statement (library problem?)"); + sqlite3_stmt *s; + for(int k=0;k<2;++k) { + s = (sqlite3_stmt *)0; + if ((sqlite3_prepare_v2(_db,"SELECT 'v' FROM Config WHERE 'k' = 'schemaVersion';",-1,&s,(const char **)0) != SQLITE_OK)||(!s)) { + if (sqlite3_exec(_db,ZT_NETCONF_SCHEMA_SQL"INSERT INTO Config (k,v) VALUES ('schemaVersion',"ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR");",0,0,0) != SQLITE_OK) { + sqlite3_close(_db); + throw std::runtime_error("SqliteNetworkConfigMaster cannot initialize database and/or insert schemaVersion into Config table"); + } else { + // Initialized database and set schema version, so we are done. + return; + } + } else break; } if (!s) { sqlite3_close(_db); - throw std::runtime_error("SqliteNetworkConfigMaster cannot create prepared statement (library problem?)"); + throw std::runtime_error("SqliteNetworkConfigMaster unable to create prepared statement or initialize database"); } - int schemaVersion = -1; + // If we made it here, database was opened and prepared statement was created + // to check schema version. Check and upgrade if needed. + + int schemaVersion = -1234; if (sqlite3_step(s) == SQLITE_ROW) schemaVersion = sqlite3_column_int(s,0); sqlite3_finalize(s); - if (schemaVersion == -1) { - if (sqlite3_exec(_db,ZT_NETCONF_SCHEMA_SQL"INSERT INTO Config (k,v) VALUES ('schemaVersion',"ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR");",0,0,0) != SQLITE_OK) { - sqlite3_close(_db); - throw std::runtime_error("SqliteNetworkConfigMaster cannot initialize database and/or insert schemaVersion into Config table"); - } + if (schemaVersion == -1234) { + sqlite3_close(_db); + throw std::runtime_error("SqliteNetworkConfigMaster schemaVersion not found in Config table (init failure?)"); } else if (schemaVersion != ZT_NETCONF_SQLITE_SCHEMA_VERSION) { // Note -- this will eventually run auto-upgrades so this isn't how it'll work going forward sqlite3_close(_db); |