changeset 6019:ccc66ed2f85b

db: enable use of main Kallithea config as Alembic config Newly generated Kallithea config .ini files will be valid Alembic config files, eliminating the need for a separate alembic.ini config redundantly specifying the database connection string. We reference the Alembic migration environment using kallithea:alembic, which should work independently of how Kallithea is installed. We also configure a default 'alembic' log level of WARNING, to reduce the amount of clutter in the config file, reduce the changes needed to upgrade existing config files for use with Alembic, and allowing us to change the default Alembic log level for all users down the road. (It makes sense to define Alembic logging in code, while all other loggers are configured in the configuration file, because Alembic is special: it runs on the command line, not as part of the web app.)
author Søren Løvborg <sorenl@unity3d.com>
date Mon, 18 Jul 2016 13:32:34 +0200
parents 520d39490a06
children b9fe529d5752
files development.ini kallithea/alembic/env.py kallithea/bin/template.ini.mako kallithea/config/deployment.ini_tmpl kallithea/tests/test.ini
diffstat 5 files changed, 48 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/development.ini	Mon Jul 04 17:18:49 2016 +0200
+++ b/development.ini	Mon Jul 18 13:32:34 2016 +0200
@@ -502,6 +502,13 @@
 sqlalchemy.db1.pool_recycle = 3600
 
 ################################
+### ALEMBIC CONFIGURATION   ####
+################################
+
+[alembic]
+script_location = kallithea:alembic
+
+################################
 ### LOGGING CONFIGURATION   ####
 ################################
 
--- a/kallithea/alembic/env.py	Mon Jul 04 17:18:49 2016 +0200
+++ b/kallithea/alembic/env.py	Mon Jul 18 13:32:34 2016 +0200
@@ -14,6 +14,7 @@
 
 # Alembic migration environment (configuration).
 
+import logging
 from logging.config import fileConfig
 
 from alembic import context
@@ -23,9 +24,21 @@
 # The alembic.config.Config object, which wraps the current .ini file.
 config = context.config
 
-# Interpret the config file for Python logging.
-# This line sets up loggers basically.
-fileConfig(config.config_file_name)
+# Default to use the main Kallithea database string in [app:main].
+# For advanced uses, this can be overridden by specifying an explicit
+# [alembic] sqlalchemy.url.
+database_url = (
+    config.get_main_option('sqlalchemy.url') or
+    config.get_section_option('app:main', 'sqlalchemy.db1.url')
+)
+
+# Configure default logging for Alembic. (This can be overriden by the
+# config file, but usually isn't.)
+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)
 
 
 def run_migrations_offline():
@@ -34,9 +47,8 @@
     This produces an SQL script instead of directly applying the changes.
     Some migrations may not run in offline mode.
     """
-    url = config.get_main_option("sqlalchemy.url")
     context.configure(
-        url=url,
+        url=database_url,
         literal_binds=True,
     )
 
@@ -50,8 +62,10 @@
     Connects to the database and directly applies the necessary
     migrations.
     """
+    cfg = config.get_section(config.config_ini_section)
+    cfg['sqlalchemy.url'] = database_url
     connectable = engine_from_config(
-        config.get_section(config.config_ini_section),
+        cfg,
         prefix='sqlalchemy.',
         poolclass=pool.NullPool)
 
--- a/kallithea/bin/template.ini.mako	Mon Jul 04 17:18:49 2016 +0200
+++ b/kallithea/bin/template.ini.mako	Mon Jul 18 13:32:34 2016 +0200
@@ -505,6 +505,13 @@
 sqlalchemy.db1.pool_recycle = 3600
 
 <%text>################################</%text>
+<%text>### ALEMBIC CONFIGURATION   ####</%text>
+<%text>################################</%text>
+
+[alembic]
+script_location = kallithea:alembic
+
+<%text>################################</%text>
 <%text>### LOGGING CONFIGURATION   ####</%text>
 <%text>################################</%text>
 
--- a/kallithea/config/deployment.ini_tmpl	Mon Jul 04 17:18:49 2016 +0200
+++ b/kallithea/config/deployment.ini_tmpl	Mon Jul 18 13:32:34 2016 +0200
@@ -494,6 +494,13 @@
 sqlalchemy.db1.pool_recycle = 3600
 
 ################################
+### ALEMBIC CONFIGURATION   ####
+################################
+
+[alembic]
+script_location = kallithea:alembic
+
+################################
 ### LOGGING CONFIGURATION   ####
 ################################
 
--- a/kallithea/tests/test.ini	Mon Jul 04 17:18:49 2016 +0200
+++ b/kallithea/tests/test.ini	Mon Jul 18 13:32:34 2016 +0200
@@ -502,6 +502,13 @@
 sqlalchemy.db1.pool_recycle = 3600
 
 ################################
+### ALEMBIC CONFIGURATION   ####
+################################
+
+[alembic]
+script_location = kallithea:alembic
+
+################################
 ### LOGGING CONFIGURATION   ####
 ################################