diff --git a/src/db.c b/src/db.c index f93747f9..60e13ad7 100644 --- a/src/db.c +++ b/src/db.c @@ -3947,7 +3947,7 @@ db_spotify_files_delete(void) /* Admin */ int -db_admin_add(const char *key, const char *value) +db_admin_set(const char *key, const char *value) { #define Q_TMPL "INSERT OR REPLACE INTO admin (key, value) VALUES ('%q', '%q');" char *query; @@ -4016,18 +4016,6 @@ db_admin_get(const char *key) #undef Q_TMPL } -int -db_admin_update(const char *key, const char *value) -{ -#define Q_TMPL "UPDATE admin SET value='%q' WHERE key='%q';" - char *query; - - query = sqlite3_mprintf(Q_TMPL, value, key); - - return db_query_run(query, 1, 0); -#undef Q_TMPL -} - int db_admin_delete(const char *key) { @@ -4204,7 +4192,7 @@ queue_inc_version_and_notify() return; } - ret = db_admin_update("queue_version", version); + ret = db_admin_set("queue_version", version); if (ret < 0) { DPRINTF(E_LOG, L_DB, "Error incrementing queue version. Could not update version in admin table: %d\n", queue_version); diff --git a/src/db.h b/src/db.h index 5136d937..92837deb 100644 --- a/src/db.h +++ b/src/db.h @@ -670,14 +670,11 @@ db_spotify_files_delete(void); /* Admin */ int -db_admin_add(const char *key, const char *value); +db_admin_set(const char *key, const char *value); char * db_admin_get(const char *key); -int -db_admin_update(const char *key, const char *value); - int db_admin_delete(const char *key); diff --git a/src/db_init.c b/src/db_init.c index b92dd822..b7f473a9 100644 --- a/src/db_init.c +++ b/src/db_init.c @@ -25,10 +25,10 @@ #include "logger.h" -#define T_ADMIN \ - "CREATE TABLE IF NOT EXISTS admin(" \ - " key VARCHAR(32) NOT NULL," \ - " value VARCHAR(32) NOT NULL" \ +#define T_ADMIN \ + "CREATE TABLE IF NOT EXISTS admin(" \ + " key VARCHAR(32) PRIMARY KEY NOT NULL," \ + " value VARCHAR(32) NOT NULL" \ ");" #define T_FILES \ diff --git a/src/db_init.h b/src/db_init.h index 88287f18..4ed6b0df 100644 --- a/src/db_init.h +++ b/src/db_init.h @@ -26,7 +26,7 @@ * is a major upgrade. In other words minor version upgrades permit downgrading * forked-daapd after the database was upgraded. */ #define SCHEMA_VERSION_MAJOR 19 -#define SCHEMA_VERSION_MINOR 01 +#define SCHEMA_VERSION_MINOR 02 int db_init_indices(sqlite3 *hdl); diff --git a/src/db_upgrade.c b/src/db_upgrade.c index 0f619d72..f25c0e00 100644 --- a/src/db_upgrade.c +++ b/src/db_upgrade.c @@ -1441,7 +1441,7 @@ db_upgrade_v19(sqlite3 *hdl) return 0; } -/* Upgrade from schema v19.00 to v20.00 */ +/* Upgrade from schema v19.00 to v19.01 */ /* Create new table queue for persistent playqueue */ @@ -1488,6 +1488,46 @@ static const struct db_upgrade_query db_upgrade_v1901_queries[] = { U_V1901_SCVER_MINOR, "set schema_version_minor to 01" }, }; +/* Upgrade from schema v19.01 to v19.02 */ +/* Set key column as primary key in the admin table + */ + +#define U_V1902_CREATE_TABLE_ADMINTMP \ + "CREATE TEMPORARY TABLE IF NOT EXISTS admin_tmp(" \ + " key VARCHAR(32) NOT NULL," \ + " value VARCHAR(32) NOT NULL" \ + ");" +#define U_V1902_INSERT_ADMINTMP \ + "INSERT INTO admin_tmp SELECT * FROM admin;" +#define U_V1902_DROP_TABLE_ADMIN \ + "DROP TABLE admin;" +#define U_V1902_CREATE_TABLE_ADMIN \ + "CREATE TABLE IF NOT EXISTS admin(" \ + " key VARCHAR(32) PRIMARY KEY NOT NULL," \ + " value VARCHAR(32) NOT NULL" \ + ");" +#define U_V1902_INSERT_ADMIN \ + "INSERT OR IGNORE INTO admin SELECT * FROM admin_tmp;" +#define U_V1902_DROP_TABLE_ADMINTMP \ + "DROP TABLE admin_tmp;" + +#define U_V1902_SCVER_MAJOR \ + "UPDATE admin SET value = '19' WHERE key = 'schema_version_major';" +#define U_V1902_SCVER_MINOR \ + "UPDATE admin SET value = '02' WHERE key = 'schema_version_minor';" + +static const struct db_upgrade_query db_upgrade_v1902_queries[] = + { + { U_V1902_CREATE_TABLE_ADMINTMP, "create temporary table admin_tmp" }, + { U_V1902_INSERT_ADMINTMP, "insert admin_tmp" }, + { U_V1902_DROP_TABLE_ADMIN, "drop table admin" }, + { U_V1902_CREATE_TABLE_ADMIN, "create table admin" }, + { U_V1902_INSERT_ADMIN, "insert admin" }, + { U_V1902_DROP_TABLE_ADMINTMP, "drop table admin_tmp" }, + + { U_V1902_SCVER_MAJOR, "set schema_version_major to 19" }, + { U_V1902_SCVER_MINOR, "set schema_version_minor to 02" }, + }; int db_upgrade(sqlite3 *hdl, int db_ver) @@ -1606,6 +1646,11 @@ db_upgrade(sqlite3 *hdl, int db_ver) if (ret < 0) return -1; + case 1901: + ret = db_generic_upgrade(hdl, db_upgrade_v1902_queries, sizeof(db_upgrade_v1902_queries) / sizeof(db_upgrade_v1902_queries[0])); + if (ret < 0) + return -1; + break; default: diff --git a/src/lastfm.c b/src/lastfm.c index d1a5a15f..434263b6 100644 --- a/src/lastfm.c +++ b/src/lastfm.c @@ -297,7 +297,7 @@ response_proces(struct http_client_ctx *ctx) if (sk) { DPRINTF(E_LOG, L_LASTFM, "Got session key from LastFM: %s\n", sk); - db_admin_add("lastfm_sk", sk); + db_admin_set("lastfm_sk", sk); if (lastfm_session_key) free(lastfm_session_key);