Skip to content

feat: add drop constraint if exists to compiler#8161

Closed
miketheman wants to merge 3 commits into
sqlalchemy:mainfrom
miketheman:miketheman/support-drop-constraint-8141
Closed

feat: add drop constraint if exists to compiler#8161
miketheman wants to merge 3 commits into
sqlalchemy:mainfrom
miketheman:miketheman/support-drop-constraint-8141

Conversation

@miketheman
Copy link
Copy Markdown
Contributor

@miketheman miketheman commented Jun 23, 2022

Description

Add DROP CONSTRAINT ... IF EXISTS behavior to the compiler.

Fixes #8141

Checklist

This pull request is:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Jun 23, 2022

this looks like the general idea, I wonder though since "IF EXISTS" / " IF NOT EXISTS" is generic enough that it should be in the base compiler (makes the PR even easier then). It looks like the base compiler is already doing "CASCADE" which certainly isn't covered by most backends either.

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Jun 23, 2022

youd add a few tests to test/sql/test_constraints in that case, compile tests similar to test_render_drop_constraint_cascade

@miketheman
Copy link
Copy Markdown
Contributor Author

Thanks! I thought about that first, but then backed out, since it seemed dialect-specific and didn't want to break any compiler rules. Will re-work the change.

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Jun 23, 2022

this one is a gray area. we like to do only "SQL Standard " things in Core but I dont think create/drop of constraints is part of SQL standard anyway.

the main thing is i think if someone does "if_exists" and runs it on a backend that doesnt support it, it should fail, otherwise why did they add "if_exists". so by just running that SQL, if the backend doesnt support it, we get that error for free.

been making this kind of choice for about 17 years now...

@miketheman miketheman force-pushed the miketheman/support-drop-constraint-8141 branch 2 times, most recently from 3ee6a1f to ca90184 Compare June 25, 2022 00:19
@miketheman
Copy link
Copy Markdown
Contributor Author

miketheman commented Jun 25, 2022

Thanks for the pointers - I've moved the code around to the base compiler, and rewrote the test to be similar to other tests in the module.

I did some more reading of the Postgres docs, and it seems that the only constraint operation that supports the EXISTS behavior is drop constraint, so no need to implement the ADD side.

Let me know what you think!

@miketheman miketheman marked this pull request as ready for review June 25, 2022 00:30
@miketheman miketheman force-pushed the miketheman/support-drop-constraint-8141 branch from ca90184 to 659dc57 Compare June 25, 2022 00:34
@miketheman miketheman changed the title feat: add drop constraint if exists to postgres feat: add drop constraint if exists to compiler Jun 25, 2022
Add `DROP CONSTRAINT ... IF EXISTS` behavior to the compiler.

Fixes sqlalchemy#8141

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman miketheman force-pushed the miketheman/support-drop-constraint-8141 branch from 659dc57 to 120171a Compare June 25, 2022 00:35
Comment thread lib/sqlalchemy/sql/compiler.py Outdated
Comment on lines 5215 to 5217
drop.if_exists and "IF EXISTS " or "",
formatted_name,
drop.cascade and " CASCADE" or "",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these could be replaced with inline if-else (these probably date back to a python version that did not yet support it )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I tried to remain consistent with the existing patterns rather than diverge for my changes.
Origin of this pattern cfa1a42
If it’s desirable to convert to newer patterns, happy to do so!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really required, but I think it's safe to convert to the new syntax since we are modifying this part anyway.

If you make the change, could you also add a changelog for this pr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes updated.
Changelog added as well - wasn't really certain if the location (2.0) and tags we correct - I was basing it off the opened ticket labels.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look great, thanks!

@CaselIT CaselIT requested a review from sqla-tester July 2, 2022 21:57
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of CaselIT to try to get revision 120171a of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

New Gerrit review created for change 120171a: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3970

Use single-line ternary if/else introduced in Python 2.5.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@CaselIT CaselIT requested a review from sqla-tester July 3, 2022 21:56
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of CaselIT to try to get revision 43276e2 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

Patchset 43276e2 added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3970

@miketheman
Copy link
Copy Markdown
Contributor Author

@zzzeek I'm not super familiar with the code review process for sqlalchemy, and having read the docs, I'm not sure if there's another action I'm meant to take?

@CaselIT
Copy link
Copy Markdown
Member

CaselIT commented Jul 6, 2022

Currently just wait for a review :)

@sqla-tester
Copy link
Copy Markdown
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3970 has been merged. Congratulations! :)

@miketheman miketheman deleted the miketheman/support-drop-constraint-8141 branch July 28, 2022 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support DROP CONSTRAINT IF EXISTS for Postgres Dialect

4 participants