-
-
Notifications
You must be signed in to change notification settings - Fork 253
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 EXISTS/IF NOT EXISTS directives #151
Comments
Michael Bayer (@zzzeek) wrote: this is great but mostly needs to be implemented within SQLAlchemy, alembic here would only need to refer to new API. sqlalchemy.schema.CreateTable, CreateSequence and I suppose CreateIndex (also DropTable, DropSequence, DropIndex) would need a new flag "exists=True" which instructs the dialect to render the appropriate IF [NOT] EXISTS directives on a per-dialect basis. |
Changes by Michael Bayer (@zzzeek):
|
Rémy Roy (@remyroy) wrote: I've created an enhancement for the SQLAlchemy project: http://www.sqlalchemy.org/trac/ticket/2843#ticket |
Patrick Lee (@pllee) wrote: It looks like this feature is supported by SQLAlchemy now with checkfirst=True. Is there a way to use this when generating upgrade scripts in offline mode? |
Patrick Lee (@pllee) wrote: @zzzeek thanks for the quick response. Is there a way to force it to do it? If not I would gladly submit a pull request if I can figure it out. I have an application where the tables could be at any state in the migration and it wouldn't matter if all of the statements had if not exists appended to all the create statements. Instead of keeping track for the particular instance what was the last upgrade run it would just run all of the upgrades since the beginning. I can go into more details if need be on why this functionality makes sense for us. |
Michael Bayer (@zzzeek) wrote: a PR against SQLAlchemy would be part of it but none of that is getting released soon enough for you to solve your immediate issues. here's a recipe that will work fine for now.
|
EirN (@eirnym) wrote: Created GIST for this solution for different types: |
Zac Witte (@zacwitte) wrote: Has there been any update on this? If I have the op |
Michael Bayer (@zzzeek) wrote:
an update by definition would be listed here....
for now:
|
Michael Bayer (@zzzeek) wrote: the comment right above has another way to extend the directive: https://gist.github.com/eirnym/afe8afb772a79407300a |
dx wrote: So, is this a thing that is supposed to be implemented on the sqlalchemy side, or on this side? A comment dated 2013-10-19 (a few days after the creation of this ticket) in the referenced sqlalchemy ticket says:
Most of the comments in this ticket seem to say the opposite - that this needs to be implemented on the sqlalchemy side. But I don't see how it would make sense to implement it there if its ddl.py isn't used by alembic. |
Michael Bayer (@zzzeek) wrote: alembic uses SQLAlchemy's ddl.py for all the CREATE / DROP directives, everything that is not ALTER. |
Michael Bayer (@zzzeek) wrote:
I don't see anything in our lists of PRs so...no ? :)
I'm not following what you mean.
"IF EXISTS" is not standard SQL so it won't work in all cases, and it doesn't make sense to have some DBs arbitrarily behave differently. "IF EXISTS" is not needed unless your target database is not correctly in sync with your migrations which would mean someone is manually altering a database. I would think one would want migrations to fail as soon as possible if that's the case since it means the target database is in an undefined state. |
since sqlalchemy implemented sqlalchemy/sqlalchemy#7354 this should now be pretty straight forward |
Адриан Максим Александрович has proposed a fix for this issue in the main branch: Added parameters if_exists and if_not_exists for index operations. https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4782 |
Migrated issue, originally created by Rémy Roy (@remyroy)
IF EXISTS and IF NOT EXISTS are already part of many DBMSs DDL vocabulary. They have many use cases which are highly useful in some situations.
For instance, creating a table if it does not already exists or dropping a column if it exists.
It would be nice to have those directives with op in alembic. I guess they could be implemented using the native support for DBMSs that support them or with some introspection for those that do not.
Thanks
The text was updated successfully, but these errors were encountered: