sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.76k stars 241 forks source link

Consider Removing psql type specifiers when Comparing Computed Constructs #1462

Open tcommandeur opened 5 months ago

tcommandeur commented 5 months ago

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)
CaselIT commented 4 months ago

Hi,

seems like a sensible solution. There is already some code that's trying to compare the index expression in the postgresql dialect, so maybe that could be re-used.

Could you provided a PR for it?