From 2568b9bc913f4ce13b45dc728e4602c781ea1ed0 Mon Sep 17 00:00:00 2001 From: spl0k Date: Wed, 29 Aug 2018 16:09:41 +0200 Subject: [PATCH] Added a small table to store the schema version Defined in a dedicated 'pony database', allowing to check only this table to determine if we need to create the tables, and so existing tables getting a new attribute won't trigger a table creation --- supysonic/db.py | 24 ++++++++++++++++++++---- supysonic/schema/mysql.sql | 5 +++++ supysonic/schema/postgres.sql | 5 +++++ supysonic/schema/sqlite.sql | 5 +++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/supysonic/db.py b/supysonic/db.py index 0489686..0a4533a 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -28,9 +28,18 @@ try: except ImportError: from urlparse import urlparse, parse_qsl +SCHEMA_VERSION = '20180829' + def now(): return datetime.now().replace(microsecond = 0) +metadb = Database() + +class Meta(metadb.Entity): + _table_ = 'meta' + key = PrimaryKey(str, 32) + value = Required(str, 256) + db = Database() @db.on_connect(provider = 'sqlite') @@ -500,18 +509,25 @@ def parse_uri(database_uri): def init_database(database_uri): settings = parse_uri(database_uri) - db.bind(**settings) - db.generate_mapping(check_tables = False) + + metadb.bind(**settings) + metadb.generate_mapping(check_tables = False) try: - db.check_tables() + metadb.check_tables() except DatabaseError: sql = pkg_resources.resource_string(__package__, 'schema/' + settings['provider'] + '.sql').decode('utf-8') with db_session: for statement in sql.split(';'): statement = statement.strip() if statement: - db.execute(statement) + metadb.execute(statement) + Meta(key = 'schema_version', value = SCHEMA_VERSION) + finally: + metadb.disconnect() + + db.bind(**settings) + db.generate_mapping(check_tables = False) def release_database(): db.disconnect() diff --git a/supysonic/schema/mysql.sql b/supysonic/schema/mysql.sql index 7881946..9787df2 100644 --- a/supysonic/schema/mysql.sql +++ b/supysonic/schema/mysql.sql @@ -123,3 +123,8 @@ CREATE TABLE IF NOT EXISTS playlist ( tracks TEXT ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +CREATE TABLE meta ( + key VARCHAR(32) PRIMARY KEY, + value VARCHAR(256) NOT NULL +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + diff --git a/supysonic/schema/postgres.sql b/supysonic/schema/postgres.sql index 90c3156..9339f55 100644 --- a/supysonic/schema/postgres.sql +++ b/supysonic/schema/postgres.sql @@ -123,3 +123,8 @@ CREATE TABLE IF NOT EXISTS playlist ( tracks TEXT ); +CREATE TABLE meta ( + key VARCHAR(32) PRIMARY KEY, + value VARCHAR(256) NOT NULL +); + diff --git a/supysonic/schema/sqlite.sql b/supysonic/schema/sqlite.sql index 09ed1b4..bf8e567 100644 --- a/supysonic/schema/sqlite.sql +++ b/supysonic/schema/sqlite.sql @@ -127,3 +127,8 @@ CREATE TABLE IF NOT EXISTS playlist ( tracks TEXT ); +CREATE TABLE meta ( + key CHAR(32) PRIMARY KEY, + value CHAR(256) NOT NULL +); +