sqlalchemy / sqlalchemy/alembic

Consider Removing psql type specifiers when Comparing Computed Constructs

Open
#1,462 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug postgresql
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
I am using a Computed column in a Postgresql database and created a migration. The next migration I create with automigration returns a warning: alembic\autogenerate\compare.py:1034: UserWarning: Computed default on table.search_vector cannot be modified

The string generated for comparison by _normalize_computed_default:
setweightto_tsvectorenglish,title,a
setweightto_tsvectorenglish::regconfig,title::text,a::char

The code responsible for this:

https://github.com/sqlalchemy/alembic/blob/44965f05e91ee5d424d9dde6566650c1bf26b516/alembic/autogenerate/compare.py#L979-L986

Proposed solution
Remove groups of text starting with :: by adding another regex substitution.

 def _normalize_computed_default(sqltext: str) -> str: 
     """we want to warn if a computed sql expression has changed.  however 
     we don't want false positives and the warning is not that critical. 
     so filter out most forms of variability from the SQL text. 
     """ 
  
    normalized_sqltext = re.sub(r"[ \(\)'\"`\[\]]", "", sqltext).lower()
    return re.sub(r"::\w+", "", normalized_sqltext)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in alembic/autogenerate/compare.py at _normalize_computed_default, which currently normalizes computed SQL text before comparison. Verify the PostgreSQL example, update the normalization so :: type specifiers are ignored, and confirm that equivalent computed expressions no longer produce the modification warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.