From e84459d6278bfc735293edc19b535c62bc2ccd8d Mon Sep 17 00:00:00 2001 From: spl0k Date: Wed, 29 Aug 2018 17:46:59 +0200 Subject: [PATCH] First throw at automatic database migrations Only supporting SQL scripts for now --- MANIFEST.in | 2 +- supysonic/db.py | 29 ++++++++++++++----- .../schema/migration/mysql/20161030.sql | 0 .../schema/migration/mysql/20171022.sql | 0 .../schema/migration/mysql/20171230.py | 0 .../schema/migration/mysql/20180221.sql | 0 .../schema/migration/mysql/20180317.sql | 0 .../schema/migration/mysql/20180521.sql | 0 .../schema/migration/postgres/20161030.sql | 0 .../schema/migration/postgres/20171022.sql | 0 .../schema/migration/postgres/20180311.sql | 0 .../schema/migration/postgres/20180317.py | 0 .../schema/migration/postgres/20180521.sql | 0 .../schema/migration/sqlite/20161030.sql | 0 .../schema/migration/sqlite/20171022.sql | 0 .../schema/migration/sqlite/20171230.py | 0 .../schema/migration/sqlite/20180311.sql | 0 .../schema/migration/sqlite/20180317.py | 0 .../schema/migration/sqlite/20180521.sql | 0 19 files changed, 23 insertions(+), 8 deletions(-) rename schema/migration/20161030.mysql.sql => supysonic/schema/migration/mysql/20161030.sql (100%) rename schema/migration/20171022.mysql.sql => supysonic/schema/migration/mysql/20171022.sql (100%) rename schema/migration/20171230.mysql.py => supysonic/schema/migration/mysql/20171230.py (100%) rename schema/migration/20180221.mysql.sql => supysonic/schema/migration/mysql/20180221.sql (100%) rename schema/migration/20180317.mysql.sql => supysonic/schema/migration/mysql/20180317.sql (100%) rename schema/migration/20180521.mysql.sql => supysonic/schema/migration/mysql/20180521.sql (100%) rename schema/migration/20161030.postgresql.sql => supysonic/schema/migration/postgres/20161030.sql (100%) rename schema/migration/20171022.postgresql.sql => supysonic/schema/migration/postgres/20171022.sql (100%) rename schema/migration/20180311.postgresql.sql => supysonic/schema/migration/postgres/20180311.sql (100%) rename schema/migration/20180317.postgresql.py => supysonic/schema/migration/postgres/20180317.py (100%) rename schema/migration/20180521.postgresql.sql => supysonic/schema/migration/postgres/20180521.sql (100%) rename schema/migration/20161030.sqlite.sql => supysonic/schema/migration/sqlite/20161030.sql (100%) rename schema/migration/20171022.sqlite.sql => supysonic/schema/migration/sqlite/20171022.sql (100%) rename schema/migration/20171230.sqlite.py => supysonic/schema/migration/sqlite/20171230.py (100%) rename schema/migration/20180311.sqlite.sql => supysonic/schema/migration/sqlite/20180311.sql (100%) rename schema/migration/20180317.sqlite.py => supysonic/schema/migration/sqlite/20180317.py (100%) rename schema/migration/20180521.sqlite.sql => supysonic/schema/migration/sqlite/20180521.sql (100%) diff --git a/MANIFEST.in b/MANIFEST.in index 7a5f63a..6492ed7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,6 +1,6 @@ include cgi-bin/* include config.sample include README.md -include supysonic/schema/* +recursive-include supysonic/schema * recursive-include supysonic/templates * recursive-include supysonic/static * diff --git a/supysonic/db.py b/supysonic/db.py index 0a4533a..88f4574 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -507,25 +507,40 @@ def parse_uri(database_uri): return dict(provider = 'mysql', user = uri.username, passwd = uri.password, host = uri.hostname, db = uri.path[1:], **args) return dict() +def execute_sql_resource_script(respath): + sql = pkg_resources.resource_string(__package__, respath).decode('utf-8') + for statement in sql.split(';'): + statement = statement.strip() + if statement and not statement.startswith('--'): + metadb.execute(statement) + def init_database(database_uri): settings = parse_uri(database_uri) metadb.bind(**settings) metadb.generate_mapping(check_tables = False) + # Check if we should create the tables try: 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: - metadb.execute(statement) + execute_sql_resource_script('schema/' + settings['provider'] + '.sql') Meta(key = 'schema_version', value = SCHEMA_VERSION) - finally: - metadb.disconnect() + # Check for schema changes + with db_session: + version = Meta['schema_version'] + if version.value < SCHEMA_VERSION: + migrations = sorted(pkg_resources.resource_listdir(__package__, 'schema/migration/' + settings['provider'])) + for migration in migrations: + date, ext = os.path.splitext(migration) + if date <= version.value or ext != '.sql': + continue + execute_sql_resource_script('schema/migration/{}/{}'.format(settings['provider'], migration)) + version.value = SCHEMA_VERSION + + metadb.disconnect() db.bind(**settings) db.generate_mapping(check_tables = False) diff --git a/schema/migration/20161030.mysql.sql b/supysonic/schema/migration/mysql/20161030.sql similarity index 100% rename from schema/migration/20161030.mysql.sql rename to supysonic/schema/migration/mysql/20161030.sql diff --git a/schema/migration/20171022.mysql.sql b/supysonic/schema/migration/mysql/20171022.sql similarity index 100% rename from schema/migration/20171022.mysql.sql rename to supysonic/schema/migration/mysql/20171022.sql diff --git a/schema/migration/20171230.mysql.py b/supysonic/schema/migration/mysql/20171230.py similarity index 100% rename from schema/migration/20171230.mysql.py rename to supysonic/schema/migration/mysql/20171230.py diff --git a/schema/migration/20180221.mysql.sql b/supysonic/schema/migration/mysql/20180221.sql similarity index 100% rename from schema/migration/20180221.mysql.sql rename to supysonic/schema/migration/mysql/20180221.sql diff --git a/schema/migration/20180317.mysql.sql b/supysonic/schema/migration/mysql/20180317.sql similarity index 100% rename from schema/migration/20180317.mysql.sql rename to supysonic/schema/migration/mysql/20180317.sql diff --git a/schema/migration/20180521.mysql.sql b/supysonic/schema/migration/mysql/20180521.sql similarity index 100% rename from schema/migration/20180521.mysql.sql rename to supysonic/schema/migration/mysql/20180521.sql diff --git a/schema/migration/20161030.postgresql.sql b/supysonic/schema/migration/postgres/20161030.sql similarity index 100% rename from schema/migration/20161030.postgresql.sql rename to supysonic/schema/migration/postgres/20161030.sql diff --git a/schema/migration/20171022.postgresql.sql b/supysonic/schema/migration/postgres/20171022.sql similarity index 100% rename from schema/migration/20171022.postgresql.sql rename to supysonic/schema/migration/postgres/20171022.sql diff --git a/schema/migration/20180311.postgresql.sql b/supysonic/schema/migration/postgres/20180311.sql similarity index 100% rename from schema/migration/20180311.postgresql.sql rename to supysonic/schema/migration/postgres/20180311.sql diff --git a/schema/migration/20180317.postgresql.py b/supysonic/schema/migration/postgres/20180317.py similarity index 100% rename from schema/migration/20180317.postgresql.py rename to supysonic/schema/migration/postgres/20180317.py diff --git a/schema/migration/20180521.postgresql.sql b/supysonic/schema/migration/postgres/20180521.sql similarity index 100% rename from schema/migration/20180521.postgresql.sql rename to supysonic/schema/migration/postgres/20180521.sql diff --git a/schema/migration/20161030.sqlite.sql b/supysonic/schema/migration/sqlite/20161030.sql similarity index 100% rename from schema/migration/20161030.sqlite.sql rename to supysonic/schema/migration/sqlite/20161030.sql diff --git a/schema/migration/20171022.sqlite.sql b/supysonic/schema/migration/sqlite/20171022.sql similarity index 100% rename from schema/migration/20171022.sqlite.sql rename to supysonic/schema/migration/sqlite/20171022.sql diff --git a/schema/migration/20171230.sqlite.py b/supysonic/schema/migration/sqlite/20171230.py similarity index 100% rename from schema/migration/20171230.sqlite.py rename to supysonic/schema/migration/sqlite/20171230.py diff --git a/schema/migration/20180311.sqlite.sql b/supysonic/schema/migration/sqlite/20180311.sql similarity index 100% rename from schema/migration/20180311.sqlite.sql rename to supysonic/schema/migration/sqlite/20180311.sql diff --git a/schema/migration/20180317.sqlite.py b/supysonic/schema/migration/sqlite/20180317.py similarity index 100% rename from schema/migration/20180317.sqlite.py rename to supysonic/schema/migration/sqlite/20180317.py diff --git a/schema/migration/20180521.sqlite.sql b/supysonic/schema/migration/sqlite/20180521.sql similarity index 100% rename from schema/migration/20180521.sqlite.sql rename to supysonic/schema/migration/sqlite/20180521.sql