From 70e0f7eba69b3a353effe0a619904366bd935969 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 4 Jan 2026 17:02:55 +0000 Subject: [PATCH] Use an application id and user version number for schema. Rather than having to craft a new CHECK_DB_LATEST that fails with all old schema versions and works with the new schema version every time we make a schema change, we can just change the user_version number. --- pkgin.sql | 3 +++ pkgindb.c | 34 +++++++++++++++++++++++++++++++++- pkgindb_queries.c | 11 ----------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/pkgin.sql b/pkgin.sql index 748cb85..817f163 100644 --- a/pkgin.sql +++ b/pkgin.sql @@ -195,3 +195,6 @@ CREATE INDEX [idx_local_pkg_comment] ON [LOCAL_PKG] ( CREATE INDEX [idx_local_pkg_name] ON [LOCAL_PKG] ( [PKGNAME] ASC ); + +PRAGMA application_id = 1886087022; +PRAGMA user_version = 1; diff --git a/pkgindb.c b/pkgindb.c index da7ffb5..af1f9d2 100644 --- a/pkgindb.c +++ b/pkgindb.c @@ -233,6 +233,31 @@ pkgindb_savepoint_release(uint64_t savepoint) } } +/* + * Check the application_id and user_version parameters. + */ +static int +check_appid(void *param, int argc, char **argv, char **colname) +{ + + if (argc != 1 || argv == NULL) + return PDB_ERR; + if (strcmp(argv[0], "1886087022") != 0) + return PDB_ERR; + return PDB_OK; +} + +static int +check_version(void *param, int argc, char **argv, char **colname) +{ + + if (argc != 1 || argv == NULL) + return PDB_ERR; + if (strcmp(argv[0], "1") != 0) + return PDB_ERR; + return PDB_OK; +} + /* * Configure the pkgin database. Returns 0 if opening an existing compatible * database, or 1 if the database needs to be created or recreated (in the case @@ -277,7 +302,14 @@ pkgindb_open(void) errx(EXIT_FAILURE, "cannot create database: %s", sqlite3_errmsg(pdb)); } else { - if (pkgindb_doquery(CHECK_DB_LATEST, NULL, NULL) != PDB_OK) { + if (pkgindb_doquery("PRAGMA application_id", check_appid, NULL) + != PDB_OK) { + if (unlink(pkgin_sqldb) < 0) + err(EXIT_FAILURE, "cannot recreate database"); + goto recreate; + } + if (pkgindb_doquery("PRAGMA user_version", check_version, NULL) + != PDB_OK) { if (unlink(pkgin_sqldb) < 0) err(EXIT_FAILURE, "cannot recreate database"); goto recreate; diff --git a/pkgindb_queries.c b/pkgindb_queries.c index 96688f1..05898d5 100644 --- a/pkgindb_queries.c +++ b/pkgindb_queries.c @@ -27,17 +27,6 @@ * SUCH DAMAGE. */ -/* - * This query checks the compatibility of the current database, and should be - * one that either completes or fails due to an SQL error based on the most - * recent schema change. Returned rows are ignored, so choose a query that - * runs quickly. - */ -const char CHECK_DB_LATEST[] = - "SELECT pkgbase " - " FROM local_conflicts " - " LIMIT 1;"; - const char DELETE_LOCAL[] = "DELETE FROM LOCAL_PKG;" "DELETE FROM LOCAL_CONFLICTS;"