From 0a47420a6156cb21b5a00c3f8dabb1a809381576 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 30 Nov 2014 18:50:03 -0500 Subject: - name the include_object hook "foreign_key_constraint" - changelog and other doc updates, fixes #178 - fix drop_constraint() unit tests and add two more for FKs --- alembic/autogenerate/compare.py | 4 ++-- alembic/environment.py | 9 +++++++-- docs/build/autogenerate.rst | 6 +++++- docs/build/changelog.rst | 10 ++++++++++ tests/test_autogen_fks.py | 9 ++++++--- tests/test_autogen_render.py | 40 ++++++++++++++++++++++++++++++++++++---- 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/alembic/autogenerate/compare.py b/alembic/autogenerate/compare.py index 6577025..2aae962 100644 --- a/alembic/autogenerate/compare.py +++ b/alembic/autogenerate/compare.py @@ -638,7 +638,7 @@ def _compare_foreign_keys(schema, tname, object_filters, conn_table, def _add_fk(obj, compare_to): if _run_filters( - obj.const, obj.name, "foreignkey", False, + obj.const, obj.name, "foreign_key_constraint", False, compare_to, object_filters): diffs.append(('add_fk', const.const)) @@ -651,7 +651,7 @@ def _compare_foreign_keys(schema, tname, object_filters, conn_table, def _remove_fk(obj, compare_to): if _run_filters( - obj.const, obj.name, "foreignkey", True, + obj.const, obj.name, "foreign_key_constraint", True, compare_to, object_filters): diffs.append(('remove_fk', obj.const)) log.info( diff --git a/alembic/environment.py b/alembic/environment.py index dab31e8..adf6ac6 100644 --- a/alembic/environment.py +++ b/alembic/environment.py @@ -486,16 +486,21 @@ class EnvironmentContext(object): as a :class:`~sqlalchemy.schema.Table`, :class:`~sqlalchemy.schema.Column`, :class:`~sqlalchemy.schema.Index` - or :class:`~sqlalchemy.schema.UniqueConstraint` object + :class:`~sqlalchemy.schema.UniqueConstraint`, + or :class:`~sqlalchemy.schema.ForeignKeyConstraint` object * ``name``: the name of the object. This is typically available via ``object.name``. * ``type``: a string describing the type of object; currently - ``"table"``, ``"column"``, ``"index"`` or ``"unique_constraint"``. + ``"table"``, ``"column"``, ``"index"``, ``"unique_constraint"``, + or ``"foreign_key_constraint"`` .. versionadded:: 0.7.0 Support for indexes and unique constraints within the :paramref:`~.EnvironmentContext.configure.include_object` hook. + .. versionadded:: 0.7.1 Support for foreign keys within the + :paramref:`~.EnvironmentContext.configure.include_object` hook. + * ``reflected``: ``True`` if the given object was produced based on table reflection, ``False`` if it's from a local :class:`.MetaData` object. diff --git a/docs/build/autogenerate.rst b/docs/build/autogenerate.rst index 9c7417b..ee9ccb9 100644 --- a/docs/build/autogenerate.rst +++ b/docs/build/autogenerate.rst @@ -119,6 +119,10 @@ Autogenerate **will detect**: .. versionadded:: 0.6.1 Support for autogenerate of indexes and unique constraints. +* Basic changes in foreign key constraints + +.. versionadded:: 0.7.1 Support for autogenerate of foreign key constraints. + Autogenerate can **optionally detect**: * Change of column type. This will occur if you set @@ -167,7 +171,7 @@ Autogenerate **can not detect**: Autogenerate can't currently, but **will eventually detect**: * Some free-standing constraint additions and removals, - like CHECK, FOREIGN KEY, PRIMARY KEY - these are not fully implemented. + like CHECK, PRIMARY KEY - these are not fully implemented. * Sequence additions, removals - not yet implemented. diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 1179e55..6d19992 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -6,6 +6,16 @@ Changelog .. changelog:: :version: 0.7.1 + .. change:: + :tags: feature, autogenerate + :tickets: 178 + :pullreq: bitbucket:32 + + Support for autogenerate of FOREIGN KEY constraints has been added. + These are delivered within the autogenerate process in the same + manner as UNIQUE constraints, including ``include_object`` support. + Big thanks to Ann Kamyshnikova for doing the heavy lifting here. + .. change:: :tags: bug, batch :pullreq: bitbucket:34 diff --git a/tests/test_autogen_fks.py b/tests/test_autogen_fks.py index 307cca7..6b5c4c1 100644 --- a/tests/test_autogen_fks.py +++ b/tests/test_autogen_fks.py @@ -278,7 +278,8 @@ class IncludeHooksTest(AutogenFixtureTest, TestBase): def include_object(object_, name, type_, reflected, compare_to): return not ( isinstance(object_, ForeignKeyConstraint) and - type_ == 'foreignkey' and reflected and name == 'fk1') + type_ == 'foreign_key_constraint' + and reflected and name == 'fk1') diffs = self._fixture(m1, m2, object_filters=[include_object]) @@ -316,7 +317,8 @@ class IncludeHooksTest(AutogenFixtureTest, TestBase): def include_object(object_, name, type_, reflected, compare_to): return not ( isinstance(object_, ForeignKeyConstraint) and - type_ == 'foreignkey' and not reflected and name == 'fk1') + type_ == 'foreign_key_constraint' + and not reflected and name == 'fk1') diffs = self._fixture(m1, m2, object_filters=[include_object]) @@ -380,7 +382,8 @@ class IncludeHooksTest(AutogenFixtureTest, TestBase): def include_object(object_, name, type_, reflected, compare_to): return not ( isinstance(object_, ForeignKeyConstraint) and - type_ == 'foreignkey' and name == 'fk1' + type_ == 'foreign_key_constraint' + and name == 'fk1' ) diffs = self._fixture(m1, m2, object_filters=[include_object]) diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index ddd108e..1e25e6b 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -220,7 +220,7 @@ unique=False, """ "['code'], schema='CamelSchema')" ) - def test_drop_constraint(self): + def test_drop_unique_constraint(self): """ autogenerate.render._drop_constraint """ @@ -233,10 +233,10 @@ unique=False, """ uq = UniqueConstraint(t.c.code, name='uq_test_code') eq_ignore_whitespace( autogenerate.render._drop_constraint(uq, self.autogen_context), - "op.drop_constraint('uq_test_code', 'test')" + "op.drop_constraint('uq_test_code', 'test', type_='unique')" ) - def test_drop_constraint_schema(self): + def test_drop_unique_constraint_schema(self): """ autogenerate.render._drop_constraint using schema """ @@ -250,7 +250,39 @@ unique=False, """ uq = UniqueConstraint(t.c.code, name='uq_test_code') eq_ignore_whitespace( autogenerate.render._drop_constraint(uq, self.autogen_context), - "op.drop_constraint('uq_test_code', 'test', schema='CamelSchema')" + "op.drop_constraint('uq_test_code', 'test', " + "schema='CamelSchema', type_='unique')" + ) + + def test_drop_fk_constraint(self): + m = MetaData() + Table('a', m, Column('id', Integer, primary_key=True)) + b = Table('b', m, Column('a_id', Integer, ForeignKey('a.id'))) + fk = ForeignKeyConstraint(['a_id'], ['a.id'], name='fk_a_id') + b.append_constraint(fk) + eq_ignore_whitespace( + autogenerate.render._drop_constraint(fk, self.autogen_context), + "op.drop_constraint('fk_a_id', 'b', type_='foreignkey')" + ) + + def test_drop_fk_constraint_schema(self): + m = MetaData() + m = MetaData() + Table( + 'a', m, Column('id', Integer, primary_key=True), + schema="CamelSchemaTwo") + b = Table( + 'b', m, Column('a_id', Integer, ForeignKey('a.id')), + schema="CamelSchemaOne") + fk = ForeignKeyConstraint( + ["a_id"], + ["CamelSchemaTwo.a.id"], name='fk_a_id') + b.append_constraint(fk) + + eq_ignore_whitespace( + autogenerate.render._drop_constraint(fk, self.autogen_context), + "op.drop_constraint('fk_a_id', 'b', schema='CamelSchemaOne', " + "type_='foreignkey')" ) def test_render_table_upgrade(self): -- cgit v1.2.1