# HG changeset patch # User Søren Løvborg # Date 2016-07-05 13:40:19 # Node ID c095a2f38add63ff57049ef4ea0111e0549e42af # Parent b9fe529d5752edb39bdd253ff4972eacc6af8e11 db: stamp Alembic version into database This ensures that a freshly created database will have the correct Alembic version. diff --git a/kallithea/alembic/env.py b/kallithea/alembic/env.py --- a/kallithea/alembic/env.py +++ b/kallithea/alembic/env.py @@ -39,8 +39,11 @@ database_url = ( logging.getLogger('alembic').setLevel(logging.WARNING) # Setup Python loggers based on the config file provided to the alembic -# command. -fileConfig(config.config_file_name, disable_existing_loggers=False) +# command. If we're being invoked via the Alembic API (presumably for +# stamping during "paster setup-db"), config_file_name is not available, +# and loggers are assumed to already have been configured. +if config.config_file_name: + fileConfig(config.config_file_name, disable_existing_loggers=False) def include_in_autogeneration(object, name, type, reflected, compare_to): diff --git a/kallithea/lib/db_manage.py b/kallithea/lib/db_manage.py --- a/kallithea/lib/db_manage.py +++ b/kallithea/lib/db_manage.py @@ -33,6 +33,9 @@ import uuid import logging from os.path import dirname +import alembic.config +import alembic.command + from kallithea import __dbversion__, __py_version__, EXTERN_TYPE_INTERNAL from kallithea.model.user import UserModel from kallithea.lib.utils import ask_ok @@ -103,6 +106,18 @@ class DbManage(object): checkfirst = not override Base.metadata.create_all(checkfirst=checkfirst) + + # Create an Alembic configuration and generate the version table, + # "stamping" it with the most recent Alembic migration revision, to + # tell Alembic that all the schema upgrades are already in effect. + alembic_cfg = alembic.config.Config() + alembic_cfg.set_main_option('script_location', 'kallithea:alembic') + alembic_cfg.set_main_option('sqlalchemy.url', self.dburi) + # This command will give an error in an Alembic multi-head scenario, + # but in practice, such a scenario should not come up during database + # creation, even during development. + alembic.command.stamp(alembic_cfg, 'head') + log.info('Created tables for %s', self.dbname) def fix_repo_paths(self):