Commits

Bryce Lohr committed 58c0d93 Merge

Merged revision 32fb7e8aabec from upstream default tip

Comments (0)

Files changed (4)

alembic/command.py

     """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:
         util.requires_07("autogenerate")

alembic/migration.py

             if self.as_sql and not rev:
                 self._version.drop(self.connection)
 
-    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):

alembic/templates/multidb/env.py

+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
+
 USE_TWOPHASE = False
 
-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
-logging.fileConfig(options.config_file)
+# Interpret the config file for Python logging. 
+# This line sets up loggers basically.
+fileConfig(config.config_file_name)
+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 env.py,
+# can be acquired:
+# my_important_option = config.get_main_option("my_important_option")
+# ... etc.
+
 def run_migrations_offline():
     """Run migrations in 'offline' mode.
 
                                             "sqlalchemy.url")
 
     for name, rec in engines.items():
+        logger.info("Migrating database %s" % name)
         file_ = "%s.sql" % name
-        sys.stderr.write("Writing output to %s\n" % file_)
+        logger.info("Writing output to %s" % file_)
         context.configure(
                     url=rec['url'],
                     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.
 
     try:
         for name, rec in engines.items():
+            logger.info("Migrating database %s" % name)
             context.configure(
                         connection=rec['connection'],
                         upgrade_token="%s_upgrades",
                         downgrade_token="%s_downgrades",
                         target_metadata=target_metadata.get(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():

alembic/templates/multidb/script.py.mako

-"""${message}
+<%!
+import re
+
+%>"""${message}
 
 Revision ID: ${up_revision}
 Revises: ${down_revision}
 import sqlalchemy as sa
 ${imports if imports else ""}
 
-def upgrade(engine):
-    eval("upgrade_%s" % engine.name)()
+def upgrade(engine_name):
+    eval("upgrade_%s" % engine_name)()
 
 
-def downgrade(engine):
-    eval("upgrade_%s" % engine.name)()
+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