Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IF (NOT) EXISTS for ALTER TABLE sub-commands like constraints etc #524

Open
EMCain opened this issue Jan 9, 2019 · 3 comments
Open

IF (NOT) EXISTS for ALTER TABLE sub-commands like constraints etc #524

EMCain opened this issue Jan 9, 2019 · 3 comments
Labels
external SQLAlchemy issues the issue is in SQLAlchemy, not here in alembic feature op directives

Comments

@EMCain
Copy link

EMCain commented Jan 9, 2019

Hello, forgive me if an issue already exists for this, I have seen a related one here but that is several years old and status is unclear.

There doesn't seem to be an option to add IF (NOT) EXISTS or CASCADE flags to queries generated by e.g. op.create_unique_constraint or op.drop_index. Here are some raw queries I've added and directly executed lately:

DROP INDEX ix_host_ip 
CASCADE;

or

ALTER TABLE host 
DROP CONSTRAINT IF EXISTS vuln_occurrence_unique_constraint
CASCADE;

I would like to be able to create these with alembic, for example:

op.drop_index('ix_host_ip', table_name='host', cascade=True)
op.drop_constraint_if_exists('vuln_occurrence_host_ip_fkey', 'vuln_occurrence', type_='foreignkey', cascade=True)

(The syntax could be different to better fit the style of existing commands, if needed.)

I haven't contributed yet but I might be able to work on this issue if it is deemed a worthwhile change to the project.

@zzzeek
Copy link
Member

zzzeek commented Jan 9, 2019

I think these would be good contributions, although I would still consider this to be #151, and additionally because the DDL for these constructs comes from SQLAlchemy it would be cleaner to be on the SQLAlhcemy side added to the CreateIndex, DropIndex etc constructs.

The good news is that CASCADE, while not really supported by any major DB except Postgresql, is in the SQL standard, so it can go right in as is. "IF (NOT) EXISTS" seems like it's not part of the standard but it seems to be in every database these days.

anyway the issue you want to be mainly working on is in SQLAlchemy over at: sqlalchemy/sqlalchemy#2843 . CASCADE can be part of it too for those constructs where it applies. you will note how often in that bug, people offer to work on the issue, then disappear, or I ask people to provide a PR, then they disappear, etc. I've gotten PRs for some very complex features in this area like support for schema comments and such so I know it's doable.

@zzzeek
Copy link
Member

zzzeek commented Jan 9, 2019

to clarify, the change has to be both in SQLAlchemy and Alembic - SQLAlchemy's CreateIndex/DropIndex/CreateTable/DropTable/AddConstraint/DropConstraint all get CASCADE and IF (NOT) EXISTS as is available on databases, then Alembic's op directives gain those flags and pass them through. I dont think Alembic's autogenerate feature needs anything at all here as these directives don't apply to a automatically managed schema.

@zzzeek
Copy link
Member

zzzeek commented Jan 9, 2019

also let's have only one issue open, if you want it to be #524 then lets close #151, or the other way around, unless you can point out some difference in the requests I'm not seeing (other than support CASCADE as well),

@mikeywaites mikeywaites added feature op directives external SQLAlchemy issues the issue is in SQLAlchemy, not here in alembic awaiting info waiting for the submitter to give more information labels Jan 11, 2019
@CaselIT CaselIT removed the awaiting info waiting for the submitter to give more information label May 11, 2023
@zzzeek zzzeek changed the title no "if (not) exists" or "cascade" arguments available on create/drop index, constraint commands IF (NOT) EXISTS for ALTER TABLE sub-commands like constraints etc Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external SQLAlchemy issues the issue is in SQLAlchemy, not here in alembic feature op directives
Projects
None yet
Development

No branches or pull requests

4 participants