diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-02-04 15:50:29 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-02-06 22:53:16 -0500 |
commit | 30307c4616ad67c01ddae2e1e8e34fabf6028414 (patch) | |
tree | 6e8edd4cb13132aa19f916409f3a3f3dcba7fd0c /test/dialect/postgresql/test_compiler.py | |
parent | 11845453d76e1576f637161e660160f0a6117af6 (diff) | |
download | sqlalchemy-30307c4616ad67c01ddae2e1e8e34fabf6028414.tar.gz |
Remove all remaining text() coercions and ensure identifiers are safe
Fully removed the behavior of strings passed directly as components of a
:func:`.select` or :class:`.Query` object being coerced to :func:`.text`
constructs automatically; the warning that has been emitted is now an
ArgumentError or in the case of order_by() / group_by() a CompileError.
This has emitted a warning since version 1.0 however its presence continues
to create concerns for the potential of mis-use of this behavior.
Note that public CVEs have been posted for order_by() / group_by() which
are resolved by this commit: CVE-2019-7164 CVE-2019-7548
Added "SQL phrase validation" to key DDL phrases that are accepted as plain
strings, including :paramref:`.ForeignKeyConstraint.on_delete`,
:paramref:`.ForeignKeyConstraint.on_update`,
:paramref:`.ExcludeConstraint.using`,
:paramref:`.ForeignKeyConstraint.initially`, for areas where a series of SQL
keywords only are expected.Any non-space characters that suggest the phrase
would need to be quoted will raise a :class:`.CompileError`. This change
is related to the series of changes committed as part of :ticket:`4481`.
Fixed issue where using an uppercase name for an index type (e.g. GIST,
BTREE, etc. ) or an EXCLUDE constraint would treat it as an identifier to
be quoted, rather than rendering it as is. The new behavior converts these
types to lowercase and ensures they contain only valid SQL characters.
Quoting is applied to :class:`.Function` names, those which are usually but
not necessarily generated from the :attr:`.sql.func` construct, at compile
time if they contain illegal characters, such as spaces or punctuation. The
names are as before treated as case insensitive however, meaning if the
names contain uppercase or mixed case characters, that alone does not
trigger quoting. The case insensitivity is currently maintained for
backwards compatibility.
Fixes: #4481
Fixes: #4473
Fixes: #4467
Change-Id: Ib22a27d62930e24702e2f0f7c74a0473385a08eb
Diffstat (limited to 'test/dialect/postgresql/test_compiler.py')
-rw-r--r-- | test/dialect/postgresql/test_compiler.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index a9c9722c9..696078cc4 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -595,6 +595,40 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): "WITH (buffering = off)", ) + def test_create_index_with_using_unusual_conditions(self): + m = MetaData() + tbl = Table("testtbl", m, Column("data", String)) + + self.assert_compile( + schema.CreateIndex( + Index("test_idx1", tbl.c.data, postgresql_using="GIST") + ), + "CREATE INDEX test_idx1 ON testtbl " "USING gist (data)", + ) + + self.assert_compile( + schema.CreateIndex( + Index( + "test_idx1", + tbl.c.data, + postgresql_using="some_custom_method", + ) + ), + "CREATE INDEX test_idx1 ON testtbl " + "USING some_custom_method (data)", + ) + + assert_raises_message( + exc.CompileError, + "Unexpected SQL phrase: 'gin invalid sql'", + schema.CreateIndex( + Index( + "test_idx2", tbl.c.data, postgresql_using="gin invalid sql" + ) + ).compile, + dialect=postgresql.dialect(), + ) + def test_create_index_with_tablespace(self): m = MetaData() tbl = Table("testtbl", m, Column("data", String)) @@ -789,6 +823,27 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): "(room::TEXT WITH =)", ) + def test_exclude_constraint_colname_needs_quoting(self): + m = MetaData() + cons = ExcludeConstraint(("Some Column Name", "=")) + Table("testtbl", m, Column("Some Column Name", String), cons) + self.assert_compile( + schema.AddConstraint(cons), + "ALTER TABLE testtbl ADD EXCLUDE USING gist " + '("Some Column Name" WITH =)', + ) + + def test_exclude_constraint_with_using_unusual_conditions(self): + m = MetaData() + cons = ExcludeConstraint(("q", "="), using="not a keyword") + Table("testtbl", m, Column("q", String), cons) + assert_raises_message( + exc.CompileError, + "Unexpected SQL phrase: 'not a keyword'", + schema.AddConstraint(cons).compile, + dialect=postgresql.dialect(), + ) + def test_exclude_constraint_cast(self): m = MetaData() tbl = Table("testtbl", m, Column("room", String)) |