summary refs log tree commit diff
path: root/litterbox.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-02 01:37:00 -0500
committerJune McEnroe <june@causal.agency>2019-12-02 01:37:00 -0500
commitb99220696b128962711a96d577af7a1a9d5115bb (patch)
treed17d8063d8e98518ddd228246694d97644be2bb1 /litterbox.c
parentRename header file to database.h (diff)
downloadlitterbox-b99220696b128962711a96d577af7a1a9d5115bb.tar.gz
litterbox-b99220696b128962711a96d577af7a1a9d5115bb.zip
Implement DB initialization and migration
Diffstat (limited to 'litterbox.c')
-rw-r--r--litterbox.c42
1 files changed, 39 insertions, 3 deletions
diff --git a/litterbox.c b/litterbox.c
index 8264d95..ca59df0 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -16,13 +16,49 @@
 
 #include <err.h>
 #include <sqlite3.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sysexits.h>
+#include <unistd.h>
 
 #include "database.h"
 
-int main(void) {
-	sqlite3 *db = dbOpen(SQLITE_OPEN_READWRITE);
-	printf("%p\n", (void *)db);
+int main(int argc, char *argv[]) {
+	bool init = false;
+	bool migrate = false;
+
+	int opt;
+	while (0 < (opt = getopt(argc, argv, "im"))) {
+		switch (opt) {
+			break; case 'i': init = true;
+			break; case 'm': migrate = true;
+			break; default:  return EX_USAGE;
+		}
+	}
+
+	int flags = SQLITE_OPEN_READWRITE;
+	if (init) flags |= SQLITE_OPEN_CREATE;
+
+	sqlite3 *db;
+	if (optind < argc) {
+		db = dbOpen(argv[optind], flags);
+	} else {
+		db = dbFind(flags);
+	}
+	if (!db) errx(EX_NOINPUT, "database not found");
+
+	if (init) {
+		dbInit(db);
+		return EX_OK;
+	}
+	if (migrate) {
+		dbMigrate(db);
+		return EX_OK;
+	}
+
+	int version = dbVersion(db);
+	if (version != DatabaseVersion) {
+		errx(EX_CONFIG, "database needs migration");
+	}
 }