From: AlonM Date: Sat, 14 Nov 2020 16:14:15 +0000 (+0200) Subject: Fix and add tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f6e7faa9c01c9c84aa102c48a127039f02f306f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix and add tests --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 772f9f553c..0ba3d0a638 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2418,7 +2418,7 @@ class PGDDLCompiler(compiler.DDLCompiler): for expr, name, op in constraint._render_exprs: kw["include_table"] = False exclude_element = self.sql_compiler.process(expr, **kw) + ( - (" " + constraint.ops[expr.key] + " ") + (" " + constraint.ops[expr.key]) if hasattr(expr, "key") and expr.key in constraint.ops else "" ) diff --git a/lib/sqlalchemy/dialects/postgresql/ext.py b/lib/sqlalchemy/dialects/postgresql/ext.py index 42e1a5c70f..d2c6e81a32 100644 --- a/lib/sqlalchemy/dialects/postgresql/ext.py +++ b/lib/sqlalchemy/dialects/postgresql/ext.py @@ -209,8 +209,7 @@ class ExcludeConstraint(ColumnCollectionConstraint): if where is not None: self.where = coercions.expect(roles.StatementOptionRole, where) - # TODO: add docs, name? - self.ops = kw.get("ops") + self.ops = kw.get("ops", {}) def _set_parent(self, table, **kw): super(ExcludeConstraint, self)._set_parent(table) diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index dad7ccd3cb..a031c3df93 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -820,13 +820,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): where="room > 100", deferrable=True, initially="immediate", + ops={"room": "my_opclass"}, ) tbl.append_constraint(cons) self.assert_compile( schema.AddConstraint(cons), "ALTER TABLE testtbl ADD CONSTRAINT my_name " "EXCLUDE USING gist " - "(room WITH =, during WITH " + "(room my_opclass WITH =, during WITH " "&&) WHERE " "(room > 100) DEFERRABLE INITIALLY immediate", dialect=postgresql.dialect(), @@ -935,6 +936,24 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): dialect=postgresql.dialect(), ) + def test_exclude_constraint_ops_many(self): + m = MetaData() + tbl = Table( + "testtbl", m, Column("room", String), Column("during", TSRANGE) + ) + cons = ExcludeConstraint( + ("room", "="), + ("during", "&&"), + ops={"room": "first_opsclass", "during": "second_opclass"}, + ) + tbl.append_constraint(cons) + self.assert_compile( + schema.AddConstraint(cons), + "ALTER TABLE testtbl ADD EXCLUDE USING gist " + "(room first_opsclass WITH =, during second_opclass WITH &&)", + dialect=postgresql.dialect(), + ) + def test_substring(self): self.assert_compile( func.substring("abc", 1, 2),