]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- Added support for the ``initially``, ``match`` keyword arguments
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 14 Mar 2014 20:41:21 +0000 (16:41 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 14 Mar 2014 20:41:21 +0000 (16:41 -0400)
as well as dialect-specific keyword arguments to
:meth:`.Operations.create_foreign_key`.
fixes #190

alembic/operations.py
docs/build/changelog.rst
tests/test_op.py

index 828b420039322a051790c0a1a69f41de27bf303e..f1d06a5fd85f51804cf6f6b562566c4bcb53c456 100644 (file)
@@ -73,7 +73,8 @@ class Operations(object):
                                     local_cols, remote_cols,
                                     onupdate=None, ondelete=None,
                                     deferrable=None, source_schema=None,
-                                    referent_schema=None):
+                                    referent_schema=None, initially=None,
+                                    match=None, **dialect_kw):
         m = self._metadata()
         if source == referent:
             t1_cols = local_cols + remote_cols
@@ -95,7 +96,10 @@ class Operations(object):
                                             name=name,
                                             onupdate=onupdate,
                                             ondelete=ondelete,
-                                            deferrable=deferrable
+                                            deferrable=deferrable,
+                                            initially=initially,
+                                            match=match,
+                                            **dialect_kw
                                             )
         t1.append_constraint(f)
 
@@ -514,10 +518,12 @@ class Operations(object):
                                 schema)
                 )
 
+
     def create_foreign_key(self, name, source, referent, local_cols,
                            remote_cols, onupdate=None, ondelete=None,
-                           deferrable=None, source_schema=None,
-                           referent_schema=None):
+                           deferrable=None, initially=None, match=None,
+                           source_schema=None, referent_schema=None,
+                           **dialect_kw):
         """Issue a "create foreign key" instruction using the
         current migration context.
 
@@ -567,7 +573,8 @@ class Operations(object):
                             local_cols, remote_cols,
                             onupdate=onupdate, ondelete=ondelete,
                             deferrable=deferrable, source_schema=source_schema,
-                            referent_schema=referent_schema)
+                            referent_schema=referent_schema,
+                            initially=initially, match=match, **dialect_kw)
                 )
 
     def create_unique_constraint(self, name, source, local_cols,
index 0b142df6edd5142616242dbdfd438b6d42a0a5bc..8fae97e2b39fcd3c06ed09c3ca548c1294bfe1ac 100644 (file)
@@ -6,6 +6,13 @@ Changelog
     :version: 0.6.4
 
     .. change::
+      :tags: bug
+      :tickets: 190
+
+      Added support for the ``initially``, ``match`` keyword arguments
+      as well as dialect-specific keyword arguments to
+      :meth:`.Operations.create_foreign_key`.
+
       :tags: feature
       :tickets: 163
 
index a243aeb5ec26dded51d1ab68c69350f5ed0beecb..1b82af30e3ed40e0f4c70981d662fb95203ba484 100644 (file)
@@ -6,7 +6,8 @@ from sqlalchemy.sql import column, func, text
 from sqlalchemy import event
 
 from alembic import op
-from . import op_fixture, assert_raises_message, requires_094
+from . import op_fixture, assert_raises_message, requires_094, eq_
+import mock
 
 @event.listens_for(Table, "after_parent_attach")
 def _add_cols(table, metadata):
@@ -397,6 +398,38 @@ def test_add_foreign_key_deferrable():
             "REFERENCES t2 (bat, hoho) DEFERRABLE"
     )
 
+def test_add_foreign_key_initially():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    initially='INITIAL')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) INITIALLY INITIAL"
+    )
+
+def test_add_foreign_key_match():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    match='SIMPLE')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) MATCH SIMPLE"
+    )
+
+def test_add_foreign_key_dialect_kw():
+    context = op_fixture()
+    with mock.patch("alembic.operations.sa_schema.ForeignKeyConstraint") as fkc:
+        op.create_foreign_key('fk_test', 't1', 't2',
+                        ['foo', 'bar'], ['bat', 'hoho'],
+                        foobar_arg='xyz')
+        eq_(fkc.mock_calls[0],
+                mock.call(['foo', 'bar'], ['t2.bat', 't2.hoho'],
+                    onupdate=None, ondelete=None, name='fk_test',
+                    foobar_arg='xyz',
+                    deferrable=None, initially=None, match=None))
+
 def test_add_foreign_key_self_referential():
     context = op_fixture()
     op.create_foreign_key("fk_test", "t1", "t1", ["foo"], ["bar"])