summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Limberg <grant.limberg@zerotier.com>2019-07-16 12:15:38 -0700
committerGrant Limberg <grant.limberg@zerotier.com>2019-07-16 12:15:38 -0700
commit2ab2f687253f27e6c8736f8fbb2767cac0d52890 (patch)
treef4a17ce75ec74edd3bf2624a0c8f7bd9bc721eb4
parentbdc2ec1f5e2074c067ce225352de3e0dd92b1356 (diff)
downloadinfinitytier-2ab2f687253f27e6c8736f8fbb2767cac0d52890.tar.gz
infinitytier-2ab2f687253f27e6c8736f8fbb2767cac0d52890.zip
Add a central database schema version check to central controller startup
-rw-r--r--controller/PostgreSQL.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/controller/PostgreSQL.cpp b/controller/PostgreSQL.cpp
index de6b4f46..594591bd 100644
--- a/controller/PostgreSQL.cpp
+++ b/controller/PostgreSQL.cpp
@@ -39,6 +39,8 @@
using json = nlohmann::json;
namespace {
+static const int DB_MINIMUM_VERSION = 5;
+
static const char *_timestr()
{
time_t t = time(0);
@@ -86,6 +88,36 @@ PostgreSQL::PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId
{
_connString = std::string(path) + " application_name=controller_" +_myAddressStr;
+ // Database Schema Version Check
+ PGconn *conn = getPgConn();
+ if (PQstatus(conn) != CONNECTION_OK) {
+ fprintf(stderr, "Bad Database Connection: %s", PQerrorMessage(conn));
+ exit(1);
+ }
+
+ PGresult *res = PQexec(conn, "SELECT version FROM ztc_database");
+ if (PQresultStatus(res) != PGRES_TUPLES_OK) {
+ fprintf(stderr, "Error determining database version");
+ exit(1);
+ }
+
+ if (PQntuples(res) != 1) {
+ fprintf(stderr, "Invalid number of db version tuples returned.");
+ exit(1);
+ }
+
+ int dbVersion = std::stoi(PQgetvalue(res, 0, 0));
+
+ if (dbVersion < DB_MINIMUM_VERSION) {
+ fprintf(stderr, "Central database schema version too low. This controller version requires a minimum schema version of %d. Please upgrade your Central instance", DB_MINIMUM_VERSION);
+ exit(1);
+ }
+
+ PQclear(res);
+ res = NULL;
+ PQfinish(conn);
+ conn = NULL;
+
_readyLock.lock();
_heartbeatThread = std::thread(&PostgreSQL::heartbeat, this);
_membersDbWatcher = std::thread(&PostgreSQL::membersDbWatcher, this);