osmith has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-hlr/+/39804?usp=email )
Change subject: src/hlr: reopen DB after changing schema version ......................................................................
src/hlr: reopen DB after changing schema version
Close and reopen the DB after changing the version to so it gets written to disk. Otherwise it may be that the DB does not get closed, and when sqlite restores the DB the version can be 0 again, leading to errors when trying to re-apply the schema upgrades
Related: SYS#7394 Change-Id: I5943a1cb3447c038c6e7972f68f9656b73a9248b --- M src/db.c M tests/db_upgrade/db_upgrade_test.ok 2 files changed, 17 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/04/39804/1
diff --git a/src/db.c b/src/db.c index 7b8a415..c9f526c 100644 --- a/src/db.c +++ b/src/db.c @@ -595,6 +595,7 @@ int rc; bool has_sqlite_config_sqllog = false; int version; + bool version_changed = false;
LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname); LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION); @@ -672,6 +673,7 @@ goto out_free; } version = CURRENT_SCHEMA_VERSION; + version_changed = true; }
LOGP(DDB, LOGL_NOTICE, "Database '%s' has HLR DB schema version %d\n", dbc->fname, version); @@ -686,6 +688,7 @@ } LOGP(DDB, LOGL_NOTICE, "Database '%s' has been upgraded to HLR DB schema version %d\n", dbc->fname, version+1); + version_changed = true; }
if (version != CURRENT_SCHEMA_VERSION) { @@ -702,6 +705,17 @@ goto out_free; }
+ /* Close and reopen the DB after changing the version to so it gets + * written to disk. Otherwise it may be that the DB does not get + * closed, and when sqlite restores the DB the version can be 0 again, + * leading to errors when trying to re-apply the schema upgrades + * (SYS#7394). */ + if (version_changed) { + LOGP(DDB, LOGL_NOTICE, "HLR DB schema version was changed, reopening...\n"); + db_close(dbc); + return db_open(ctx, fname, enable_sqlite_logging, false); + } + /* prepare all SQL statements */ for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) { rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1, diff --git a/tests/db_upgrade/db_upgrade_test.ok b/tests/db_upgrade/db_upgrade_test.ok index c719498..7248606 100644 --- a/tests/db_upgrade/db_upgrade_test.ok +++ b/tests/db_upgrade/db_upgrade_test.ok @@ -87,6 +87,9 @@ DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 5 DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 6 DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 7 +DDB HLR DB schema version was changed, reopening... +DDB using database: <PATH>test.db +DDB Database <PATH>test.db' has HLR DB schema version 7 DMAIN Cmdline option --db-check: Database was opened successfully, quitting.
Resulting db: