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.c b/src/hlr.c
index 78d6c91..14945b6 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -483,6 +483,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.\n");
 }
 
@@ -490,10 +491,12 @@
 	const char *config_file;
 	const char *db_file;
 	bool daemonize;
+	bool db_upgrade;
 } cmdline_opts = {
 	.config_file = "osmo-hlr.cfg",
 	.db_file = "hlr.db",
 	.daemonize = false,
+	.db_upgrade = false,
 };
 
 static void handle_options(int argc, char **argv)
@@ -509,11 +512,12 @@
 			{"disable-color", 0, 0, 's'},
 			{"log-level", 1, 0, 'e'},
 			{"timestamp", 0, 0, 'T'},
+			{"db-upgrade", 0, 0, 'U' },
 			{"version", 0, 0, 'V' },
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "hc:l:d:Dse:TV",
+		c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -544,6 +548,9 @@
 		case 'T':
 			log_set_print_timestamp(osmo_stderr_target, 1);
 			break;
+		case 'U':
+			cmdline_opts.db_upgrade = true;
+			break;
 		case 'V':
 			print_version(1);
 			exit(0);
@@ -637,7 +644,7 @@
 		exit(1);
 	}
 
-	g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true);
+	g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true, cmdline_opts.db_upgrade);
 	if (!g_hlr->dbc) {
 		LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
 		exit(1);