sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.61k stars 234 forks source link

Render `if_not_exists` option for CreateIndexOp and DropIndexOp #1446

Open lachaib opened 4 months ago

lachaib commented 4 months ago

Fixes: #151

Description

Without opinion on whether or not the option if_not_exists or if_exists should be set by default (or configurable via AutogenContext), I think a follow-up on

151 should be that if_not_exists or if_exists attributes should be that they are rendered during autogen steps.

How I intend to use it: specify the attribute during rewrites of the pipeline

@writer.rewrites(ops.CreateIndexOp)
def create_index_if_not_exist(_autogen_context, _revision, op: ops.CreateIndexOp):
    op.if_not_exists = True
    return op

@writer.rewrites(ops.DropIndexOp)
def drop_index_if_exist(_autogen_context, _revision, op: ops.DropIndexOp):
    op.if_exists = True
    return op

Checklist

This pull request is:

Have a nice day!