Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion alembic/autogenerate/compare/server_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def _normalize_computed_default(sqltext: str) -> str:

"""

return re.sub(r"[ \(\)'\"`\[\]\t\r\n]", "", sqltext).lower()
normalized_sqltext = re.sub(r"[ \(\)'\"`\[\]\t\r\n]", "", sqltext).lower()
# strip postgresql type cast specifiers, e.g. ``::regconfig``, ``::text``,
# which can appear inconsistently and cause false-positive warnings.
return re.sub(r"::\w+", "", normalized_sqltext)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can't be here since this is generic for all databases.



def _compare_computed_default(
Expand Down
10 changes: 10 additions & 0 deletions docs/build/unreleased/1462.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. change::
:tags: bug, autogenerate, postgresql
:tickets: 1462

Fixed false-positive "Computed default cannot be modified" warning emitted
during autogenerate when comparing PostgreSQL ``Computed`` columns whose
reflected SQL expression includes type cast specifiers such as
``::regconfig`` or ``::text``. These cast specifiers are now stripped
before comparison so that semantically equivalent expressions no longer
trigger the warning.
17 changes: 17 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
from alembic.autogenerate.compare.server_defaults import (
_dialect_impl_compare_server_default as _compare_server_default,
)
from alembic.autogenerate.compare.server_defaults import (
_normalize_computed_default,
)


class PostgresqlOpTest(TestBase):
Expand Down Expand Up @@ -395,6 +398,20 @@ def test_alter_column_computed_not_supported(self, sd, esd):
existing_server_default=esd(),
)

def test_normalize_computed_default_strips_type_casts(self):
# type cast specifiers such as ``::regconfig`` only appear on the
# reflected (connection) side and would otherwise cause a
# false-positive "cannot be modified" warning. issue #1462
eq_(
_normalize_computed_default(
"setweight(to_tsvector('english', title), 'a')"
),
_normalize_computed_default(
"setweight(to_tsvector('english'::regconfig, "
"title::text), 'a'::\"char\")"
),
)

@combinations(
({}, None),
(dict(always=True), None),
Expand Down