From bd91a238ec67dc752af5c06d98f4e63d0af3986b Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sun, 31 Jan 2021 10:27:23 +0100 Subject: [PATCH] [db] Upgrade schema to 21.06, change default of query_limit to 0 --- src/db.c | 7 ++-- src/db.h | 2 +- src/db_init.c | 2 +- src/db_init.h | 2 +- src/db_upgrade.c | 55 +++++++++++++++++++++++++++++++ src/library/filescanner_smartpl.c | 2 +- 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/db.c b/src/db.c index 79209e6c..e6a420d6 100644 --- a/src/db.c +++ b/src/db.c @@ -1849,7 +1849,10 @@ db_build_query_clause(struct query_params *qp) switch (qp->idx_type) { case I_FIRST: - qc->index = sqlite3_mprintf("LIMIT %d", qp->limit); + if (qp->limit) + qc->index = sqlite3_mprintf("LIMIT %d", qp->limit); + else + qc->index = sqlite3_mprintf(""); break; case I_LAST: @@ -2004,7 +2007,7 @@ db_build_query_plitems_smart(struct query_params *qp, struct playlist_info *pli) if (!qc) return NULL; - count = sqlite3_mprintf("SELECT COUNT(*) FROM files f %s AND %s LIMIT %d;", qc->where, pli->query, pli->query_limit); + count = sqlite3_mprintf("SELECT COUNT(*) FROM files f %s AND %s LIMIT %d;", qc->where, pli->query, pli->query_limit ? pli->query_limit : -1); query = sqlite3_mprintf("SELECT f.* FROM files f %s AND %s %s %s;", qc->where, pli->query, qc->order, qc->index); db_free_query_clause(qc); diff --git a/src/db.h b/src/db.h index 3c6df0da..f23f3097 100644 --- a/src/db.h +++ b/src/db.h @@ -255,7 +255,7 @@ struct playlist_info { uint32_t parent_id; /* Id of parent playlist if the playlist is nested */ uint32_t directory_id; /* Id of directory */ char *query_order; /* order by clause, used by e.g. a smart playlists */ - int32_t query_limit; /* limit, used by e.g. smart playlists */ + uint32_t query_limit; /* limit, used by e.g. smart playlists */ uint32_t media_kind; char *artwork_url; /* optional artwork */ uint32_t items; /* number of items (mimc) */ diff --git a/src/db_init.c b/src/db_init.c index 45626ea3..5e358c7a 100644 --- a/src/db_init.c +++ b/src/db_init.c @@ -114,7 +114,7 @@ " parent_id INTEGER DEFAULT 0," \ " directory_id INTEGER DEFAULT 0," \ " query_order VARCHAR(1024)," \ - " query_limit INTEGER DEFAULT -1," \ + " query_limit INTEGER DEFAULT 0," \ " media_kind INTEGER DEFAULT 1," \ " artwork_url VARCHAR(4096) DEFAULT NULL" \ ");" diff --git a/src/db_init.h b/src/db_init.h index a28463d2..d200860e 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 21 -#define SCHEMA_VERSION_MINOR 05 +#define SCHEMA_VERSION_MINOR 06 int db_init_indices(sqlite3 *hdl); diff --git a/src/db_upgrade.c b/src/db_upgrade.c index de84ab79..a68e2999 100644 --- a/src/db_upgrade.c +++ b/src/db_upgrade.c @@ -1074,6 +1074,49 @@ static const struct db_upgrade_query db_upgrade_v2105_queries[] = }; +/* ---------------------------- 21.05 -> 21.06 ------------------------------ */ + +// Reload table, required for changing the default of query_limit from -1 to 0 +#define U_V2106_NEW_PLAYLISTS_TABLE \ + "CREATE TABLE new_playlists (" \ + " id INTEGER PRIMARY KEY NOT NULL," \ + " title VARCHAR(255) NOT NULL COLLATE DAAP," \ + " type INTEGER NOT NULL," \ + " query VARCHAR(1024)," \ + " db_timestamp INTEGER NOT NULL," \ + " disabled INTEGER DEFAULT 0," \ + " path VARCHAR(4096)," \ + " idx INTEGER NOT NULL," \ + " special_id INTEGER DEFAULT 0," \ + " virtual_path VARCHAR(4096)," \ + " parent_id INTEGER DEFAULT 0," \ + " directory_id INTEGER DEFAULT 0," \ + " query_order VARCHAR(1024)," \ + " query_limit INTEGER DEFAULT 0," \ + " media_kind INTEGER DEFAULT 1," \ + " artwork_url VARCHAR(4096) DEFAULT NULL" \ + ");" + +static int +db_upgrade_v2106(sqlite3 *hdl) +{ + return db_table_upgrade(hdl, "playlists", U_V2106_NEW_PLAYLISTS_TABLE); +} + +// Previously, query_limit had multiple defaults: -1, 0 and UINT32_MAX +#define U_v2106_UPDATE_PLAYLISTS_QUERY_LIMIT \ + "UPDATE playlists SET query_limit = 0 WHERE query_limit = -1 OR query_limit = 4294967295;" +#define U_v2106_SCVER_MINOR \ + "UPDATE admin SET value = '06' WHERE key = 'schema_version_minor';" + +static const struct db_upgrade_query db_upgrade_v2106_queries[] = + { + { U_v2106_UPDATE_PLAYLISTS_QUERY_LIMIT, "update table playlists query_limit default" }, + + { U_v2106_SCVER_MINOR, "set schema_version_minor to 06" }, + }; + + int db_upgrade(sqlite3 *hdl, int db_ver) { @@ -1257,6 +1300,18 @@ db_upgrade(sqlite3 *hdl, int db_ver) if (ret < 0) return -1; + /* FALLTHROUGH */ + + case 2105: + ret = db_upgrade_v2106(hdl); + if (ret < 0) + return -1; + + ret = db_generic_upgrade(hdl, db_upgrade_v2106_queries, ARRAY_SIZE(db_upgrade_v2106_queries)); + if (ret < 0) + return -1; + + /* Last case statement is the only one that ends with a break statement! */ break; diff --git a/src/library/filescanner_smartpl.c b/src/library/filescanner_smartpl.c index 34d5f4b9..09501299 100644 --- a/src/library/filescanner_smartpl.c +++ b/src/library/filescanner_smartpl.c @@ -73,7 +73,7 @@ scan_smartpl(const char *file, time_t mtime, int dir_id) swap_pointers(&pli->title, &smartpl.title); swap_pointers(&pli->query, &smartpl.query_where); swap_pointers(&pli->query_order, &smartpl.order); - pli->query_limit = smartpl.limit; + pli->query_limit = (smartpl.limit > 0) ? smartpl.limit : 0; free_smartpl(&smartpl, 1);