sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.89k stars 247 forks source link

Support if_exists / if_not_exists on drop_table and create_table operations #1520

Closed agriffin-grow closed 2 months ago

agriffin-grow commented 3 months ago

Describe the use case This is similar to #524, which appears to at least be implemented for indexes.

SqlAlchemy already supports these operations on DropTable and CreateTable so this would essentially be a pass-through in a similar manner.

Databases / Backends / Drivers targeted

Postgresql

Example Use Much like drop_index, I'd expect

op.drop_table('foobar', if_exists=True)

to produce

DROP TABLE IF EXISTS foobar

Additional context This is just some insurance for edge cases where a migration partially applies due to external circumstances. i.e. a migration containing two drop_table operations could fail after the first succeeds, but then would require some surgery to work again

Have a nice day!

CaselIT commented 3 months ago

Hi,

I was convinced it was already supported, but it seems not.

A PR with an initial implementation would be appreciated!

agriffin-grow commented 3 months ago

@CaselIT https://github.com/sqlalchemy/alembic/pull/1521

Hope that's everything

agriffin-grow commented 3 months ago

@CaselIT - do you know when we could expect this to get merged and released?

CaselIT commented 3 months ago

sorry, been on holiday this past week. Will try taking a look in the weekend

sqla-tester commented 2 months ago

Aaron Griffin has proposed a fix for this issue in the main branch:

Support if_exists and if_not_exists on create/drop table commands https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/5455

CaselIT commented 2 months ago

1434 was a duplicate of this issue