Commits

Bryce Lohr committed 5534090

Updated the 'revision' command to pass the current Alembic config to the script templates, so it could use it to generate blocks based on current config. Useful for multidb configurations, where you want each database accounted for in the migrations.

  • Participants
  • Parent commits 3994a6c

Comments (0)

Files changed (2)

File 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")

File alembic/templates/multidb/script.py.mako

-"""${message}
+<%!
+import re
+
+%>"""${message}
 
 Revision ID: ${up_revision}
 Revises: ${down_revision}
 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