Sqlite auto-init and version check.

This commit is contained in:
Adam Ierymenko 2015-03-17 15:20:45 -07:00
parent 49a2450e76
commit ba69240bcb
3 changed files with 30 additions and 2 deletions

View File

@ -49,6 +49,7 @@
// If not present, database is assumed to be empty and at the current schema version
// and this key/value is added automatically.
#define ZT_NETCONF_SQLITE_SCHEMA_VERSION 1
#define ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR "1"
namespace ZeroTier {
@ -64,6 +65,33 @@ SqliteNetworkConfigMaster::SqliteNetworkConfigMaster(const Identity &signingId,c
if (sqlite3_open_v2(dbPath,&_db,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,(const char *)0) != SQLITE_OK)
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?)");
}
if (!s) {
sqlite3_close(_db);
throw std::runtime_error("SqliteNetworkConfigMaster cannot create prepared statement (library problem?)");
}
int schemaVersion = -1;
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");
}
} 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);
throw std::runtime_error("SqliteNetworkConfigMaster database schema version mismatch");
}
}
SqliteNetworkConfigMaster::~SqliteNetworkConfigMaster()

View File

@ -86,7 +86,7 @@
" ipProtocol integer(4),\n"\
" ipSourcePort integer(8),\n"\
" ipDestPort integer(8),\n"\
" "action" varchar(4096) NOT NULL DEFAULT('accept')\n"\
" \"action\" varchar(4096) NOT NULL DEFAULT('accept')\n"\
");\n"\
"\n"\
"CREATE INDEX Rule_networkId ON Rule (networkId);\n"\

View File

@ -4,5 +4,5 @@
rm -f netconf-schema.sql.c
echo '#define ZT_NETCONF_SCHEMA_SQL \' >netconf-schema.sql.c
cat netconf-schema.sql | sed 's/^/"/' | sed 's/$/\\n"\\/' >>netconf-schema.sql.c
cat netconf-schema.sql | sed 's/"/\\"/g' | sed 's/^/"/' | sed 's/$/\\n"\\/' >>netconf-schema.sql.c
echo '""' >>netconf-schema.sql.c