diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-14 22:44:33 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-14 22:44:33 -0400 |
commit | f4653729a6ff952555c0192a5b0f0851c4d40ad5 (patch) | |
tree | b6778b1455c76d58f61dc3aa4937cf0593ca86df /lib | |
parent | dbc8bbfba3591d2ea704673d90e95014765d0f10 (diff) | |
download | sqlalchemy-f4653729a6ff952555c0192a5b0f0851c4d40ad5.tar.gz |
- wrestle with conv() and tests some more
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 48 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/naming.py | 47 |
2 files changed, 53 insertions, 42 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 1447787ce..4f58f6141 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -3202,6 +3202,52 @@ class _truncated_label(quoted_name): return self +class conv(_truncated_label): + """Mark a string indicating that a name has already been converted + by a naming convention. + + This is a string subclass that indicates a name that should not be + subject to any further naming conventions. + + E.g. when we create a :class:`.Constraint` using a naming convention + as follows:: + + m = MetaData(naming_convention={ + "ck": "ck_%(table_name)s_%(constraint_name)s" + }) + t = Table('t', m, Column('x', Integer), + CheckConstraint('x > 5', name='x5')) + + The name of the above constraint will be rendered as ``"ck_t_x5"``. + That is, the existing name ``x5`` is used in the naming convention as the + ``constraint_name`` token. + + In some situations, such as in migration scripts, we may be rendering + the above :class:`.CheckConstraint` with a name that's already been + converted. In order to make sure the name isn't double-modified, the + new name is applied using the :func:`.schema.conv` marker. We can + use this explicitly as follows:: + + + m = MetaData(naming_convention={ + "ck": "ck_%(table_name)s_%(constraint_name)s" + }) + t = Table('t', m, Column('x', Integer), + CheckConstraint('x > 5', name=conv('ck_t_x5'))) + + Where above, the :func:`.schema.conv` marker indicates that the constraint + name here is final, and the name will render as ``"ck_t_x5"`` and not + ``"ck_t_ck_t_x5"`` + + .. versionadded:: 0.9.4 + + .. seealso:: + + :ref:`constraint_naming_conventions` + + """ + + class _defer_name(_truncated_label): """mark a name as 'deferred' for the purposes of automated name generation. @@ -3210,6 +3256,8 @@ class _defer_name(_truncated_label): def __new__(cls, value): if value is None: return _NONE_NAME + elif isinstance(value, conv): + return value else: return super(_defer_name, cls).__new__(cls, value) diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py index eb017eb25..053db3e34 100644 --- a/lib/sqlalchemy/sql/naming.py +++ b/lib/sqlalchemy/sql/naming.py @@ -14,49 +14,9 @@ from .schema import Constraint, ForeignKeyConstraint, PrimaryKeyConstraint, \ UniqueConstraint, CheckConstraint, Index, Table, Column from .. import event, events from .. import exc -from .elements import _truncated_label, _defer_name, _defer_none_name +from .elements import _truncated_label, _defer_name, _defer_none_name, conv import re -class conv(_truncated_label): - """Mark a string indicating that a name has already been converted - by a naming convention. - - This is a string subclass that indicates a name that should not be - subject to any further naming conventions. - - E.g. when we create a :class:`.Constraint` using a naming convention - as follows:: - - m = MetaData(naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}) - t = Table('t', m, Column('x', Integer), - CheckConstraint('x > 5', name='x5')) - - The name of the above constraint will be rendered as ``"ck_t_x5"``. That is, - the existing name ``x5`` is used in the naming convention as the ``constraint_name`` - token. - - In some situations, such as in migration scripts, we may be rendering - the above :class:`.CheckConstraint` with a name that's already been - converted. In order to make sure the name isn't double-modified, the - new name is applied using the :func:`.schema.conv` marker. We can - use this explicitly as follows:: - - - m = MetaData(naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}) - t = Table('t', m, Column('x', Integer), - CheckConstraint('x > 5', name=conv('ck_t_x5'))) - - Where above, the :func:`.schema.conv` marker indicates that the constraint - name here is final, and the name will render as ``"ck_t_x5"`` and not - ``"ck_t_ck_t_x5"`` - - .. versionadded:: 0.9.4 - - .. seealso:: - - :ref:`constraint_naming_conventions` - - """ class ConventionDict(object): def __init__(self, const, table, convention): @@ -147,7 +107,10 @@ def _get_convention(dict_, key): def _constraint_name_for_table(const, table): metadata = table.metadata convention = _get_convention(metadata.naming_convention, type(const)) - if convention is not None and ( + + if isinstance(const.name, conv): + return const.name + elif convention is not None and ( const.name is None or not isinstance(const.name, conv) and "constraint_name" in convention ): |