From 45be9a1cb1971786c72f82f8d236edbdfd662b84 Mon Sep 17 00:00:00 2001 From: default Date: Fri, 25 Nov 2022 10:53:16 +0100 Subject: [PATCH] New command-line command 'upgrade'. --- data.c | 11 +++++++++-- main.c | 16 +++++++++++++++- snac.h | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/data.c b/data.c index 3403b06..cbdfea4 100644 --- a/data.c +++ b/data.c @@ -19,7 +19,7 @@ double db_layout = 2.3; int db_upgrade(d_char **error); -int srv_open(char *basedir) +int srv_open(char *basedir, int auto_upgrade) /* opens a server */ { int ret = 0; @@ -69,7 +69,14 @@ int srv_open(char *basedir) error = xs_fmt("DEBUG level set to %d from environment", dbglevel); } - ret = db_upgrade(&error); + if (auto_upgrade) + ret = db_upgrade(&error); + else { + if (xs_number_get(xs_dict_get(srv_config, "layout")) < db_layout) { + ret = 0; + error = xs_fmt("ERROR: db layout changed - execute 'snac upgrade' first"); + } + } } } diff --git a/main.c b/main.c index 8e3dbd2..862f30d 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,7 @@ int usage(void) printf("Commands:\n"); printf("\n"); printf("init [{basedir}] Initializes the database\n"); + printf("upgrade {basedir} Upgrade to a new version\n"); printf("adduser {basedir} [{uid}] Adds a new user\n"); printf("httpd {basedir} Starts the HTTPD daemon\n"); printf("purge {basedir} Purges old data\n"); @@ -76,6 +77,19 @@ int main(int argc, char *argv[]) return initdb(basedir); } + if (strcmp(cmd, "upgrade") == 0) { + int ret; + + /* database upgrade */ + if ((basedir = GET_ARGV()) == NULL) + return usage(); + + if ((ret = srv_open(basedir, 1)) == 1) + srv_log(xs_dup("OK")); + + return ret; + } + if (strcmp(cmd, "markdown") == 0) { /* undocumented, for testing only */ xs *c = xs_readall(stdin); @@ -88,7 +102,7 @@ int main(int argc, char *argv[]) if ((basedir = GET_ARGV()) == NULL) return usage(); - if (!srv_open(basedir)) { + if (!srv_open(basedir, 0)) { srv_log(xs_fmt("error opening database at %s", basedir)); return 1; } diff --git a/snac.h b/snac.h index 6edf50c..bdbdade 100644 --- a/snac.h +++ b/snac.h @@ -24,7 +24,7 @@ double ftime(void); void srv_debug(int level, d_char *str); #define srv_log(str) srv_debug(0, str) -int srv_open(char *basedir); +int srv_open(char *basedir, int auto_upgrade); void srv_free(void); typedef struct _snac {