from .sqla_compat import ( # noqa
sqla_07, sqla_079, sqla_08, sqla_083, sqla_084, sqla_09, sqla_092,
sqla_094, sqla_099, sqla_100, sqla_105, sqla_110)
-
-
-class CommandError(Exception):
- pass
+from .exc import CommandError
if not sqla_07:
--- /dev/null
+class CommandError(Exception):
+ pass
import re
from .compat import load_module_py, load_module_pyc
from mako.template import Template
+from mako import exceptions
+import tempfile
+from .exc import CommandError
def template_to_file(template_file, dest, output_encoding, **kw):
- with open(dest, 'wb') as f:
- template = Template(filename=template_file)
- f.write(
- template.render_unicode(**kw).encode(output_encoding)
- )
+ template = Template(filename=template_file)
+ try:
+ output = template.render_unicode(**kw).encode(output_encoding)
+ except:
+ with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as ntf:
+ ntf.write(
+ exceptions.text_error_template().
+ render_unicode().encode(output_encoding))
+ fname = ntf.name
+ raise CommandError(
+ "Template rendering failed; see %s for a "
+ "template-oriented traceback." % fname)
+ else:
+ with open(dest, 'wb') as f:
+ f.write(output)
def coerce_resource_to_filename(fname):
"""Given a source path, run the EDITOR for it"""
import editor
- from . import CommandError
try:
editor.edit(path)
except Exception as exc:
.. changelog::
:version: 0.8.6
+ .. change::
+ :tags: bug, commands
+ :tickets: 367
+
+ Errors which occur within the Mako render step are now intercepted
+ and raised as CommandErrors like other failure cases; the Mako
+ exception itself is written using template-line formatting to
+ a temporary file which is named in the exception message.
+
.. change::
:tags: bug, postgresql
:tickets: 365
import datetime
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector
+from alembic.util import CommandError
+import re
env, abc, def_ = None, None, None
)
+
+
class CustomizeRevisionTest(TestBase):
def setUp(self):
self.env = staging_env()
with open(rev.path) as f:
text = f.read()
assert "somearg: somevalue" in text
+
+ def test_bad_render(self):
+ env_file_fixture("""
+context.configure(dialect_name='sqlite', template_args={"somearg":"somevalue"})
+""")
+ script_file_fixture("""
+ <% z = x + y %>
+""")
+
+ try:
+ command.revision(self.cfg, message="some rev")
+ except CommandError as ce:
+ m = re.match(
+ r"^Template rendering failed; see (.+?) "
+ "for a template-oriented",
+ str(ce)
+ )
+ assert m, "Command error did not produce a file"
+ contents = open(m.group(1)).read()
+ os.remove(m.group(1))
+ assert "<% z = x + y %>" in contents