changeset 6021:c095a2f38add

db: stamp Alembic version into database This ensures that a freshly created database will have the correct Alembic version.
author Søren Løvborg <sorenl@unity3d.com>
date Tue, 05 Jul 2016 13:40:19 +0200
parents b9fe529d5752
children e36bc85e3ecb
files kallithea/alembic/env.py kallithea/lib/db_manage.py
diffstat 2 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/alembic/env.py	Tue Jul 05 16:57:01 2016 +0200
+++ b/kallithea/alembic/env.py	Tue Jul 05 13:40:19 2016 +0200
@@ -39,8 +39,11 @@
 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):
--- a/kallithea/lib/db_manage.py	Tue Jul 05 16:57:01 2016 +0200
+++ b/kallithea/lib/db_manage.py	Tue Jul 05 13:40:19 2016 +0200
@@ -33,6 +33,9 @@
 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 @@
 
         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):