]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- name the include_object hook "foreign_key_constraint"
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Nov 2014 23:50:03 +0000 (18:50 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Nov 2014 23:52:32 +0000 (18:52 -0500)
- changelog and other doc updates, fixes #178
- fix drop_constraint() unit tests and add two more for FKs

alembic/autogenerate/compare.py
alembic/environment.py
docs/build/autogenerate.rst
docs/build/changelog.rst
tests/test_autogen_fks.py
tests/test_autogen_render.py

index 65770253400245a379d17a97d043993ea7aee483..2aae96214a941baf86af64b652320b3014b08a5e 100644 (file)
@@ -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(
index dab31e8fd0a1fc3b04ed1a80f29dd93d2a069957..adf6ac62e27a676ee1ceedf3563c213265a57c74 100644 (file)
@@ -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.
index 9c7417b498590ca0db105cf5e03a39ca7b1ca3db..ee9ccb91ce304619fcffbb775accfcd19d9b2332 100644 (file)
@@ -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.
 
 
index 1179e55784e257743f1c2db49ad3ef984d1c5507..6d19992a3f1eedad9e070f4080427eb7806b4046 100644 (file)
@@ -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
index 307cca7fcc8ca15c2bc3c39f2f71d7a1118d570a..6b5c4c16af5f886dc414b78f950be41f4f2b76ae 100644 (file)
@@ -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])
index ddd108eaf55750974671760578e2acae1e7f1ae2..1e25e6b5d909ceb0ef484bcdc6f7632d6719c830 100644 (file)
@@ -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):