]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Added :func:`.operations.create_primary_key`
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Apr 2013 19:56:04 +0000 (15:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Apr 2013 19:56:04 +0000 (15:56 -0400)
operation, will genenerate an ADD CONSTRAINT
for a primary key.
#93

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

index f97ec6e67a3f2a21e2f5df9c6a4e58543b29085e..1e21bc777266d449097a85db34cd8546bdcd775e 100644 (file)
@@ -50,6 +50,17 @@ class Operations(object):
         yield op
         alembic.op._remove_proxy()
 
+
+    def _primary_key_constraint(self, name, table_name, cols, schema=None):
+        m = sa_schema.MetaData()
+        columns = [sa_schema.Column(n, NULLTYPE) for n in cols]
+        t1 = sa_schema.Table(table_name, m,
+                *columns,
+                schema=schema)
+        p = sa_schema.PrimaryKeyConstraint(*columns, name=name)
+        t1.append_constraint(p)
+        return p
+
     def _foreign_key_constraint(self, name, source, referent,
                                     local_cols, remote_cols,
                                     onupdate=None, ondelete=None,
@@ -388,6 +399,46 @@ class Operations(object):
         )
 
 
+    def create_primary_key(self, name, table_name, cols, schema=None):
+        """Issue a "create primary key" instruction using the current
+        migration context.
+
+        e.g.::
+
+            from alembic import op
+            op.create_primary_key(
+                        "pk_my_table", "my_table",
+                        ["id", "version"]
+                    )
+
+        This internally generates a :class:`~sqlalchemy.schema.Table` object
+        containing the necessary columns, then generates a new
+        :class:`~sqlalchemy.schema.PrimaryKeyConstraint`
+        object which it then associates with the :class:`~sqlalchemy.schema.Table`.
+        Any event listeners associated with this action will be fired
+        off normally.   The :class:`~sqlalchemy.schema.AddConstraint`
+        construct is ultimately used to generate the ALTER statement.
+
+        .. versionadded:: 0.5.0
+
+        :param name: Name of the primary key constraint.  The name is necessary
+         so that an ALTER statement can be emitted.  For setups that
+         use an automated naming scheme such as that described at
+         `NamingConventions <http://www.sqlalchemy.org/trac/wiki/UsageRecipes/NamingConventions>`_,
+         ``name`` here can be ``None``, as the event listener will
+         apply the name to the constraint object when it is associated
+         with the table.
+        :param table_name: String name of the target table.
+        :param cols: a list of string column names to be applied to the
+         primary key constraint.
+        :param schema: Optional schema name of the table.
+
+        """
+        self.impl.add_constraint(
+                    self._primary_key_constraint(name, table_name, cols,
+                                schema)
+                )
+
     def create_foreign_key(self, name, source, referent, local_cols,
                            remote_cols, onupdate=None, ondelete=None,
                            source_schema=None, referent_schema=None):
index aee64a7593839a1ac5c1b0021557c54276a0b064..f60ac595ed758a2bc40a0ca7bd404170f3e26273 100644 (file)
@@ -6,6 +6,14 @@ Changelog
 .. changelog::
     :version: 0.5.0
 
+    .. change::
+        :tags: feature
+        :tickets: 93
+
+      Added :func:`.operations.create_primary_key`
+      operation, will genenerate an ADD CONSTRAINT
+      for a primary key.
+
     .. change::
         :tags: bug, mssql
         :tickets: 109
index d5a6abe9696133df54cf2320212788bf16f39d70..1118ae49503d5c09645b9eaf91b1089183fe19de 100644 (file)
@@ -338,7 +338,7 @@ def test_add_foreign_key_schema():
     context = op_fixture()
     op.create_foreign_key('fk_test', 't1', 't2',
                     ['foo', 'bar'], ['bat', 'hoho'],
-                   source_schema='foo2', referent_schema='bar2')
+                    source_schema='foo2', referent_schema='bar2')
     context.assert_(
         "ALTER TABLE foo2.t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
             "REFERENCES bar2.t2 (bat, hoho)"
@@ -372,6 +372,20 @@ def test_add_foreign_key_self_referential():
         "FOREIGN KEY(foo) REFERENCES t1 (bar)"
     )
 
+def test_add_primary_key_constraint():
+    context = op_fixture()
+    op.create_primary_key("pk_test", "t1", ["foo", "bar"])
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT pk_test PRIMARY KEY (foo, bar)"
+    )
+
+def test_add_primary_key_constraint_schema():
+    context = op_fixture()
+    op.create_primary_key("pk_test", "t1", ["foo"], schema="bar")
+    context.assert_(
+        "ALTER TABLE bar.t1 ADD CONSTRAINT pk_test PRIMARY KEY (foo)"
+    )
+
 def test_add_check_constraint():
     context = op_fixture()
     op.create_check_constraint(