automatically create db tables on osmo-hlr invocation
If a database file is missing, osmo-hlr creates it, as is the default sqlite3
API behavior -- before this patch, that db file is created, but lacks useful
tables. Actually also create initial tables in it, as osmo-nitb did.
In effect, the 'vty-test' target in tests/Makefile.am no longer needs to create
a database manually. (The 'ctrl-test' still does, because it also wants to add
subscriber data on top of the bare tables.)
Note: it could be desirable to bail if the desired database file does not
exist. That is however a different semantic from this patch; this is not
changing the fact that a db file is created, this just creates a usable one.
Note: I am about to add osmo-hlr-db-tool to do database migration from
osmo-nitb. For that, it is desirable to bootstrap a usable database, which is
the core reason for this patch.
Don't plainly duplicate hlr.sql to .c, but create db_bootstrap.h as a
BUILT_SOURCE from reading in sql/hlr.sql and mangling via sed to a list of SQL
statement strings. On each db_open(), run this bootstrap sequence.
In sql/hlr.sql, these tweaks are necessary:
* Add 'IF NOT EXISTS' to 'CREATE TABLE', so that the bootstrap sequence can be
run on an already bootstrapped db.
* Drop the final comment at the bottom, which ended up being an empty SQL
statement and causing sqlite3 API errors, seemed to have no purpose anyway.
Note: by composing the statement strings as multiline and including the SQL
comments, sqlite3 actually retains the comments contained in table definitions
and prints them back during 'sqlite3 hlr.db .dump'.
Change-Id: If77dbbfe1af3e66aaec91cb6295b687f37678636
diff --git a/src/Makefile.am b/src/Makefile.am
index fc7c653..3b09b7b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,8 +10,14 @@
EXTRA_DIST = \
populate_hlr_db.pl \
+ db_bootstrap.sed \
$(NULL)
+BUILT_SOURCES = \
+ db_bootstrap.h \
+ $(NULL)
+CLEANFILES = $(BUILT_SOURCES)
+
noinst_HEADERS = \
auc.h \
db.h \
@@ -24,6 +30,7 @@
ctrl.h \
hlr_vty.h \
hlr_vty_subscr.h \
+ db_bootstrap.h \
$(NULL)
bin_PROGRAMS = \
@@ -73,3 +80,14 @@
$(LIBOSMOGSM_LIBS) \
$(SQLITE3_LIBS) \
$(NULL)
+
+BOOTSTRAP_SQL = $(top_srcdir)/sql/hlr.sql
+
+db_bootstrap.h: $(BOOTSTRAP_SQL) $(srcdir)/db_bootstrap.sed
+ echo "/* DO NOT EDIT THIS FILE. It is generated from osmo-hlr.git/sql/hlr.sql */" > "$@"
+ echo "#pragma once" >> "$@"
+ echo "static const char *stmt_bootstrap_sql[] = {" >> "$@"
+ cat "$(BOOTSTRAP_SQL)" \
+ | sed -f "$(srcdir)/db_bootstrap.sed" \
+ >> "$@"
+ echo "};" >> "$@"