]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- [feature] Informative error message when op.XYZ
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Feb 2012 22:14:03 +0000 (17:14 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Feb 2012 22:14:03 +0000 (17:14 -0500)
  directives are invoked at module import time.

CHANGES
alembic/util.py
tests/test_op.py

diff --git a/CHANGES b/CHANGES
index 7c27ecf76b04cbf97818c3ba768fe6c7ff91ac16..e1cd2bf87e7a0f05b5de4ca661d51ec65ee485de 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,8 @@
 0.2.2
 =====
+- [feature] Informative error message when op.XYZ
+  directives are invoked at module import time.
+  
 - [bug] setup.py won't install argparse if on
   Python 2.7/3.2
 
index 3599531d7ac6833c2a76fdd972dfcb3295ec73e2..eb2d11e7b4fd28d6b31a1907c235ce590b70809a 100644 (file)
@@ -90,10 +90,25 @@ def create_module_class_proxy(cls, globals_, locals_):
                                 defaulted_vals,
                                 formatvalue=lambda x: '=' + x)
 
+        def _name_error(name):
+            raise NameError(
+                    "Can't invoke function '%s', as the proxy object has "\
+                    "not yet been "
+                    "established for the Alembic '%s' class.  "
+                    "Try placing this code inside a callable." % (
+                        name, cls.__name__
+                    ))
+        globals_['_name_error'] = _name_error
+
         func_text = textwrap.dedent("""\
         def %(name)s(%(args)s):
             %(doc)r
+            try:
+                p = _proxy
+            except NameError:
+                _name_error('%(name)s')
             return _proxy.%(name)s(%(apply_kw)s)
+            e
         """ % {
             'name':name,
             'args':args[1:-1],
index 58f19cb8c0c6871fcac0f9ff4ed516a8ea660956..2d6a29b1218b5d5bf78baf5c2cfcc2e910dba328 100644 (file)
@@ -1,6 +1,6 @@
 """Test against the builders in the op.* module."""
 
-from tests import op_fixture
+from tests import op_fixture, assert_raises_message
 from alembic import op
 from sqlalchemy import Integer, Column, ForeignKey, \
             UniqueConstraint, Table, MetaData, String,\
@@ -294,3 +294,15 @@ def test_inline_literal():
         "UPDATE account SET name='account 2' WHERE account.name = 'account 1'",
         "UPDATE account SET id=2 WHERE account.id = 1"
     )
+
+def test_cant_op():
+    if hasattr(op, '_proxy'):
+        del op._proxy
+    assert_raises_message(
+        NameError,
+        "Can't invoke function 'inline_literal', as the "
+        "proxy object has not yet been established "
+        "for the Alembic 'Operations' class.  "
+        "Try placing this code inside a callable.",
+        op.inline_literal, "asdf"
+    )
\ No newline at end of file