]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support mssql_clustered option on UniqueConstraint (plus docs and test)
authordonkopotamus <derek.harland@finq.co.nz>
Thu, 16 Jan 2014 21:46:16 +0000 (10:46 +1300)
committerdonkopotamus <derek.harland@finq.co.nz>
Thu, 16 Jan 2014 21:46:16 +0000 (10:46 +1300)
lib/sqlalchemy/dialects/mssql/base.py
test/dialect/mssql/test_compiler.py

index 213e00d79fa91734ed21fd8f5ff269501dda1542..c024b62304dc2e7799fde51f88bef91ce5e956b2 100644 (file)
@@ -107,8 +107,8 @@ Clustered Index Support
 -----------------------
 
 The MSSQL dialect supports clustered indexes (and primary keys) via the
-``mssql_clustered`` option.  This option is available to :class:`.Index`
-and :class:`.PrimaryKeyConstraint`.
+``mssql_clustered`` option.  This option is available to :class:`.Index`,
+:class:`.UniqueConstraint`. and :class:`.PrimaryKeyConstraint`.
 
 To generate a clustered index::
 
@@ -129,7 +129,16 @@ which will render the table, for example, as::
 
   CREATE TABLE my_table (x INTEGER NOT NULL, y INTEGER NOT NULL, PRIMARY KEY CLUSTERED (x, y))
 
-.. versionadded:: 0.9
+Similarly, we can generate a clustered unique constraint using::
+
+    Table('my_table', metadata,
+          Column('x', ...),
+          Column('y', ...),
+          PrimaryKeyConstraint("x"),
+          UniqueConstraint("y", mssql_clustered=True),
+          )
+
+  .. versionadded:: 0.9
 
 MSSQL-Specific Index Options
 -----------------------------
@@ -1055,7 +1064,23 @@ class MSDDLCompiler(compiler.DDLCompiler):
             text += "CLUSTERED "
 
         text += "(%s)" % ', '.join(self.preparer.quote(c.name)
-                                       for c in constraint)
+                                   for c in constraint)
+        text += self.define_constraint_deferrability(constraint)
+        return text
+
+    def visit_unique_constraint(self, constraint):
+        text = ""
+        if constraint.name is not None:
+            text += "CONSTRAINT %s " % \
+                    self.preparer.format_constraint(constraint)
+        text += "UNIQUE "
+
+        # support clustered option
+        if constraint.kwargs.get("mssql_clustered"):
+            text += "CLUSTERED "
+
+        text += "(%s)" % ', '.join(self.preparer.quote(c.name)
+                                   for c in constraint)
         text += self.define_constraint_deferrability(constraint)
         return text
 
index f1cf6924cd3f375598180c94d8186a888124ebde..f12ab0330fec78eb4f8ceb6b31ea66d2f38884b4 100644 (file)
@@ -521,6 +521,18 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                             "PRIMARY KEY CLUSTERED (x, y))"
                             )
 
+    def test_table_uc_clustering(self):
+        metadata = MetaData()
+        tbl = Table('test', metadata,
+                    Column('x', Integer, autoincrement=False),
+                    Column('y', Integer, autoincrement=False),
+                    PrimaryKeyConstraint("x"),
+                    UniqueConstraint("y", mssql_clustered=True))
+        self.assert_compile(schema.CreateTable(tbl),
+                            "CREATE TABLE test (x INTEGER NOT NULL, y INTEGER NULL, "
+                            "PRIMARY KEY (x), UNIQUE CLUSTERED (y))"
+                            )
+
     def test_index_clustering(self):
         metadata = MetaData()
         tbl = Table('test', metadata,