diff --git a/supysonic/db.py b/supysonic/db.py index 931b7b1..ee10fa6 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -1,7 +1,7 @@ # This file is part of Supysonic. # Supysonic is a Python implementation of the Subsonic server API. # -# Copyright (C) 2013-2022 Alban 'spl0k' Féron +# Copyright (C) 2013-2023 Alban 'spl0k' Féron # # Distributed under terms of the GNU AGPLv3 license. @@ -31,7 +31,7 @@ from playhouse.db_url import parseresult_to_dict, schemes from urllib.parse import urlparse from uuid import UUID, uuid4 -SCHEMA_VERSION = "20230111" +SCHEMA_VERSION = "20230115" def now(): @@ -608,8 +608,9 @@ def init_database(database_uri): # Check if we should create the tables if not db.table_exists("meta"): - execute_sql_resource_script(f"schema/{provider}.sql") - Meta.create(key="schema_version", value=SCHEMA_VERSION) + with db.atomic(): + execute_sql_resource_script(f"schema/{provider}.sql") + Meta.create(key="schema_version", value=SCHEMA_VERSION) # Check for schema changes version = Meta["schema_version"] @@ -618,11 +619,16 @@ def init_database(database_uri): pkg_resources.resource_listdir(__package__, f"schema/migration/{provider}") ) for migration in migrations: + if migration[0] in ("_", "."): + continue + date, ext = os.path.splitext(migration) if date <= version.value: continue + if ext == ".sql": - execute_sql_resource_script(f"schema/migration/{provider}/{migration}") + with db.atomic(): + execute_sql_resource_script(f"schema/migration/{provider}/{migration}") elif ext == ".py": m = importlib.import_module( f".schema.migration.{provider}.{date}", __package__ diff --git a/supysonic/schema/migration/mysql/20230115.sql b/supysonic/schema/migration/mysql/20230115.sql new file mode 100644 index 0000000..475de5a --- /dev/null +++ b/supysonic/schema/migration/mysql/20230115.sql @@ -0,0 +1,2 @@ +ALTER TABLE client_prefs ADD FOREIGN KEY (user_id) REFERENCES user(id); +CREATE INDEX IF NOT EXISTS index_client_prefs_user_id_fk ON client_prefs(user_id); diff --git a/supysonic/schema/migration/postgres/20230115.sql b/supysonic/schema/migration/postgres/20230115.sql new file mode 100644 index 0000000..f10ad71 --- /dev/null +++ b/supysonic/schema/migration/postgres/20230115.sql @@ -0,0 +1,2 @@ +ALTER TABLE client_prefs ADD FOREIGN KEY (user_id) REFERENCES "user"; +CREATE INDEX IF NOT EXISTS index_client_prefs_user_id_fk ON client_prefs(user_id); diff --git a/supysonic/schema/migration/sqlite/20230115.sql b/supysonic/schema/migration/sqlite/20230115.sql new file mode 100644 index 0000000..22484e8 --- /dev/null +++ b/supysonic/schema/migration/sqlite/20230115.sql @@ -0,0 +1,16 @@ +CREATE TABLE client_prefs_new ( + user_id CHAR(36) NOT NULL REFERENCES user, + client_name VARCHAR(32) NOT NULL, + format VARCHAR(8), + bitrate INTEGER, + PRIMARY KEY (user_id, client_name) +); + +INSERT INTO client_prefs_new(user_id, client_name, format, bitrate) +SELECT user_id, client_name, format, bitrate +FROM client_prefs; + +DROP TABLE client_prefs; +ALTER TABLE client_prefs_new RENAME TO client_prefs; + +CREATE INDEX IF NOT EXISTS index_client_prefs_user_id_fk ON client_prefs(user_id); diff --git a/supysonic/schema/mysql.sql b/supysonic/schema/mysql.sql index dd14798..4c85bba 100644 --- a/supysonic/schema/mysql.sql +++ b/supysonic/schema/mysql.sql @@ -65,12 +65,13 @@ CREATE TABLE IF NOT EXISTS user ( CREATE INDEX index_user_last_play_id_fk ON user(last_play_id); CREATE TABLE IF NOT EXISTS client_prefs ( - user_id CHAR(32) NOT NULL, + user_id CHAR(32) NOT NULL REFERENCES user(id), client_name VARCHAR(32) NOT NULL, format VARCHAR(8), bitrate INTEGER, PRIMARY KEY (user_id, client_name) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +CREATE INDEX index_client_prefs_user_id_fk ON client_prefs(user_id); CREATE TABLE IF NOT EXISTS starred_folder ( user_id CHAR(32) NOT NULL REFERENCES user(id), diff --git a/supysonic/schema/postgres.sql b/supysonic/schema/postgres.sql index 47b301c..1984266 100644 --- a/supysonic/schema/postgres.sql +++ b/supysonic/schema/postgres.sql @@ -65,12 +65,13 @@ CREATE TABLE IF NOT EXISTS "user" ( CREATE INDEX IF NOT EXISTS index_user_last_play_id_fk ON "user"(last_play_id); CREATE TABLE IF NOT EXISTS client_prefs ( - user_id UUID NOT NULL, + user_id UUID NOT NULL REFERENCES "user", client_name VARCHAR(32) NOT NULL, format VARCHAR(8), bitrate INTEGER, PRIMARY KEY (user_id, client_name) ); +CREATE INDEX IF NOT EXISTS index_client_prefs_user_id_fk ON client_prefs(user_id); CREATE TABLE IF NOT EXISTS starred_folder ( user_id UUID NOT NULL REFERENCES "user", diff --git a/supysonic/schema/sqlite.sql b/supysonic/schema/sqlite.sql index d2b905c..ae39a87 100644 --- a/supysonic/schema/sqlite.sql +++ b/supysonic/schema/sqlite.sql @@ -67,12 +67,13 @@ CREATE TABLE IF NOT EXISTS user ( CREATE INDEX IF NOT EXISTS index_user_last_play_id_fk ON user(last_play_id); CREATE TABLE IF NOT EXISTS client_prefs ( - user_id CHAR(36) NOT NULL, + user_id CHAR(36) NOT NULL REFERENCES user, client_name VARCHAR(32) NOT NULL, format VARCHAR(8), bitrate INTEGER, PRIMARY KEY (user_id, client_name) ); +CREATE INDEX IF NOT EXISTS index_client_prefs_user_id_fk ON client_prefs(user_id); CREATE TABLE IF NOT EXISTS starred_folder ( user_id CHAR(36) NOT NULL REFERENCES user,