diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-03-17 15:20:45 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-03-17 15:20:45 -0700 |
commit | ba69240bcbabdb667b8fcc9208eeb3ebb5845832 (patch) | |
tree | 16a1ff2f4403f3e9c99269d531719a1018054c41 /netconf | |
parent | 49a2450e76488780987fbd247d2f3984899957b0 (diff) | |
download | infinitytier-ba69240bcbabdb667b8fcc9208eeb3ebb5845832.tar.gz infinitytier-ba69240bcbabdb667b8fcc9208eeb3ebb5845832.zip |
Sqlite auto-init and version check.
Diffstat (limited to 'netconf')
-rw-r--r-- | netconf/SqliteNetworkConfigMaster.cpp | 28 | ||||
-rw-r--r-- | netconf/netconf-schema.sql.c | 2 | ||||
-rwxr-xr-x | netconf/schema2c.sh | 2 |
3 files changed, 30 insertions, 2 deletions
diff --git a/netconf/SqliteNetworkConfigMaster.cpp b/netconf/SqliteNetworkConfigMaster.cpp index 1dfb1a44..48711c86 100644 --- a/netconf/SqliteNetworkConfigMaster.cpp +++ b/netconf/SqliteNetworkConfigMaster.cpp @@ -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() diff --git a/netconf/netconf-schema.sql.c b/netconf/netconf-schema.sql.c index 0e360563..42e71073 100644 --- a/netconf/netconf-schema.sql.c +++ b/netconf/netconf-schema.sql.c @@ -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"\ diff --git a/netconf/schema2c.sh b/netconf/schema2c.sh index 7775cfef..2ef1393a 100755 --- a/netconf/schema2c.sh +++ b/netconf/schema2c.sh @@ -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 |