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