add database schema versioning to the HLR database

Make use of pragma user_version to store our database schema version.
The present schema is now identitifed as 'version 0', which is also
the default value for databases on which we never ran the statement
'pragma user_version' before.

Only bootstrap the database if it hasn't been bootstrapped yet.
Previously, bootstrap SQL statements ran every time osmo-hlr
opened the database, and any errors were being ignored in SQL.
Instead, we now first run a query which checks whether tables
already exist, and only create them if necessary.
This change will allow future schema updates to work properly.

Prepare for future schema upgrades by adding a new command-line
option which enables upgrades. This option defaults to 'false'
in order to avoid accidental upgrades.

Change-Id: I8aeaa9a404b622657cbc7138106f38aa6ad8d01b
Related: OS#2838
diff --git a/src/hlr_db_tool.c b/src/hlr_db_tool.c
index e83b098..1a9c60c 100644
--- a/src/hlr_db_tool.c
+++ b/src/hlr_db_tool.c
@@ -44,8 +44,10 @@
 	const char *db_file;
 	bool bootstrap;
 	const char *import_nitb_db;
+	bool db_upgrade;
 } cmdline_opts = {
 	.db_file = "hlr.db",
+	.db_upgrade = false,
 };
 
 static void print_help()
@@ -59,6 +61,7 @@
 	printf("  -s --disable-color         Do not print ANSI colors in the log\n");
 	printf("  -T --timestamp             Prefix every log line with a timestamp.\n");
 	printf("  -e --log-level number      Set a global loglevel.\n");
+	printf("  -U --db-upgrade            Allow HLR database schema upgrades.\n");
 	printf("  -V --version               Print the version of OsmoHLR-db-tool.\n");
 	printf("\n");
 	printf("Commands:\n");
@@ -96,11 +99,12 @@
 			{"disable-color", 0, 0, 's'},
 			{"timestamp", 0, 0, 'T'},
 			{"log-level", 1, 0, 'e'},
+			{"db-upgrade", 0, 0, 'U' },
 			{"version", 0, 0, 'V' },
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "hl:d:sTe:V",
+		c = getopt_long(argc, argv, "hl:d:sTe:UV",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -124,6 +128,9 @@
 		case 'e':
 			log_set_log_level(osmo_stderr_target, atoi(optarg));
 			break;
+		case 'U':
+			cmdline_opts.db_upgrade = true;
+			break;
 		case 'V':
 			print_version(1);
 			exit(EXIT_SUCCESS);
@@ -409,7 +416,7 @@
 		exit(EXIT_FAILURE);
 	}
 
-	g_hlr_db_tool_ctx->dbc = db_open(g_hlr_db_tool_ctx, cmdline_opts.db_file, true);
+	g_hlr_db_tool_ctx->dbc = db_open(g_hlr_db_tool_ctx, cmdline_opts.db_file, true, cmdline_opts.db_upgrade);
 	if (!g_hlr_db_tool_ctx->dbc) {
 		LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
 		exit(EXIT_FAILURE);