import logging
import re
+from ..compat import StringIO
+
+from mako.pygen import PythonPrinter
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.util import OrderedSet
from .compare import _compare_tables
diffs = []
_produce_net_changes(connection, metadata, diffs,
autogen_context, object_filters, include_schemas)
- template_args[opts['upgrade_token']] = \
- _indent(_produce_upgrade_commands(diffs, autogen_context))
- template_args[opts['downgrade_token']] = \
- _indent(_produce_downgrade_commands(diffs, autogen_context))
+
+ template_args[opts['upgrade_token']] = _indent(_render_cmd_body(
+ _produce_upgrade_commands, diffs, autogen_context))
+ template_args[opts['downgrade_token']] = _indent(_render_cmd_body(
+ _produce_downgrade_commands, diffs, autogen_context))
template_args['imports'] = "\n".join(sorted(imports))
+def _indent(text):
+ text = re.compile(r'^', re.M).sub(" ", text).strip()
+ return text
+
+
+def _render_cmd_body(fn, diffs, autogen_context):
+
+ buf = StringIO()
+ printer = PythonPrinter(buf)
+
+ printer.writeline(
+ "### commands auto generated by Alembic - "
+ "please adjust! ###"
+ )
+
+ for line in fn(diffs, autogen_context):
+ printer.writeline(line)
+
+ printer.writeline("### end Alembic commands ###")
+
+ return buf.getvalue()
+
+
def _get_object_filters(
context_opts, include_symbol=None, include_object=None):
include_symbol = context_opts.get('include_symbol', include_symbol)
}, connection
-def _indent(text):
- text = "### commands auto generated by Alembic - "\
- "please adjust! ###\n" + text
- text += "\n### end Alembic commands ###"
- text = re.compile(r'^', re.M).sub(" ", text).strip()
- return text
-
###################################################
# walk structures
def _produce_upgrade_commands(diffs, autogen_context):
- buf = []
- for diff in diffs:
- buf.append(_invoke_command("upgrade", diff, autogen_context))
- if not buf:
- buf = ["pass"]
- return "\n".join(buf)
+ if diffs:
+ for diff in diffs:
+ yield _invoke_command("upgrade", diff, autogen_context)
+ else:
+ yield "pass"
def _produce_downgrade_commands(diffs, autogen_context):
- buf = []
- for diff in reversed(diffs):
- buf.append(_invoke_command("downgrade", diff, autogen_context))
- if not buf:
- buf = ["pass"]
- return "\n".join(buf)
+ if diffs:
+ for diff in reversed(diffs):
+ yield _invoke_command("downgrade", diff, autogen_context)
+ else:
+ yield "pass"
def _invoke_command(updown, args, autogen_context):