From: donkopotamus Date: Thu, 16 Jan 2014 21:46:16 +0000 (+1300) Subject: Support mssql_clustered option on UniqueConstraint (plus docs and test) X-Git-Tag: rel_0_9_2~53^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=35935489608c6a896790ed0c51c3ea4b4eb6186f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support mssql_clustered option on UniqueConstraint (plus docs and test) --- diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 213e00d79f..c024b62304 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -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 diff --git a/test/dialect/mssql/test_compiler.py b/test/dialect/mssql/test_compiler.py index f1cf6924cd..f12ab0330f 100644 --- a/test/dialect/mssql/test_compiler.py +++ b/test/dialect/mssql/test_compiler.py @@ -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,