Bryce Lohr  committed 58c0d93 Merge

Merged revision 32fb7e8aabec from upstream default tip

  • Participants
  • Parent commits 32fb7e8, d8ec56e

Comments (0)

Files changed (4)

File alembic/

     """Create a new revision file."""
     script = ScriptDirectory.from_config(config)
-    template_args = {}
+    template_args = {
+        'config': config # Let templates use config for e.g. multiple databases
+    }
     imports = set()
     if autogenerate:

File alembic/

             if self.as_sql and not rev:
-    def execute(self, sql):
+    def execute(self, sql, execution_options=None):
         """Execute a SQL construct or string statement.
         The underlying execution mechanics are used, that is
         the current SQLAlchemy connection.
-        self.impl._exec(sql)
+        self.impl._exec(sql, execution_options)
     def _stdout_connection(self, connection):
         def dump(construct, *multiparams, **params):

File alembic/templates/multidb/

+from __future__ import with_statement
+from alembic import context
+from sqlalchemy import engine_from_config, pool
+from logging.config import fileConfig
+import logging
+import re
-from alembic import context
-from sqlalchemy import engine_from_config, pool
-import re
-import sys
+# this is the Alembic Config object, which provides
+# access to the values within the .ini file in use.
+config = context.config
-import logging
+# Interpret the config file for Python logging. 
+# This line sets up loggers basically.
+logger = logging.getLogger(__name__)
 # gather section names referring to different 
 # databases.  These are named "engine1", "engine2"
 # in the sample .ini file.
-db_names = options.get_main_option('databases')
+db_names = config.get_main_option('databases')
 # add your model's MetaData objects here
 # for 'autogenerate' support.  These must be set 
 target_metadata = {}
+# other values from the config, defined by the needs of,
+# can be acquired:
+# my_important_option = config.get_main_option("my_important_option")
+# ... etc.
 def run_migrations_offline():
     """Run migrations in 'offline' mode.
     for name, rec in engines.items():
+"Migrating database %s" % name)
         file_ = "%s.sql" % name
-        sys.stderr.write("Writing output to %s\n" % file_)
+"Writing output to %s" % file_)
                     output_buffer=open(file_, 'w')
         with context.begin_transaction():
-            context.run_migrations(engine=name)
+            context.run_migrations(engine_name=name)
 def run_migrations_online():
     """Run migrations in 'online' mode.
         for name, rec in engines.items():
+  "Migrating database %s" % name)
-            context.execute("--running migrations for engine %s" % name)
-            context.run_migrations(engine=name)
+            context.execute("-- running migrations for database %s" % name)
+            context.run_migrations(engine_name=name)
         if USE_TWOPHASE:
             for rec in engines.values():

File alembic/templates/multidb/

+import re
 Revision ID: ${up_revision}
 Revises: ${down_revision}
 import sqlalchemy as sa
 ${imports if imports else ""}
-def upgrade(engine):
-    eval("upgrade_%s" %
+def upgrade(engine_name):
+    eval("upgrade_%s" % engine_name)()
-def downgrade(engine):
-    eval("upgrade_%s" %
+def downgrade(engine_name):
+    eval("downgrade_%s" % engine_name)()
+    db_names = context.get("config").get_main_option("databases")
-% for engine in ["engine1", "engine2"]:
+% for db_name in re.split(r',\s*', db_names):
-def upgrade_${engine}():
-    ${context.get("%s_upgrades" % engine, "pass")}
+def upgrade_${db_name}():
+    ${context.get("%s_upgrades" % db_name, "pass")}
-def downgrade_${engine}():
-    ${context.get("%s_downgrades" % engine, "pass")}
+def downgrade_${db_name}():
+    ${context.get("%s_downgrades" % db_name, "pass")}
-% endfor
+% endfor