-
-
Notifications
You must be signed in to change notification settings - Fork 252
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
conditional ddl support; add autogeneration support for ddf_if()
#1491
Comments
hi - this is very special use case so you would need to manually adjust the autogenerated file for now. logic would need to be added to autogenerate to look for these ddl_ifs but it would likely then not be able to correctly generate each dialect-specific index, since autogen is only against one database. |
Fair enough, just checking whether this was a known issue, bug, not supported etc. For the case of autogen, it would logically follow that it should be easier because it's a single dialect, so just ignore those not for the current dialect? I'll see if i can understand the autogen code and maybe submit a PR for this. Might consider updating https://alembic.sqlalchemy.org/en/latest/autogenerate.html#what-does-autogenerate-detect-and-what-does-it-not-detect to denote this edge case |
well it would render the Index() for just one of the databases, and not the other, meaning your migration file will only work on that one database. that is, it wouldnt generate a cross-compatible migration file even though your model is cross compatible. |
So for my specific use case, the indexing options are different for PSQL vs SQLite, and SQLite is only used for quick integration tests. I only ever autogenerate for PSQL. Would you also not be able to follow this guidance and just generate multiple migration file sets for each dialect type: |
if you really wanted multiple separate environments, sure, usually people want one migration set for a certain kind of schema and they use cross-compatibility features for things like special indexes. anyway, PRs to improve the behavior are welcome as long as you can make some tests for them |
how could a direct support api look like? pass a kw arg with a dict of kw similar to https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.HasConditionalDDL.ddl_if ? op.create_index('my_pg_index_psql', 'user_account', ['name'], unique=False, ddl_if={'dialect': 'postgresql'})
op.create_index('my_pg_index_sqlite', 'user_account', ['name'], unique=False ddl_if={'dialect': 'sqlite'}) something else like a conditional? if op.ddl_if(dialect='postgresql'):
op.create_index('my_pg_index_psql', 'user_account', ['name'], unique=False)
if op.ddl_if(dialect='sqlite'):
op.create_index('my_pg_index_sqlite', 'user_account', ['name'], unique=False) |
I like the second form better, but it might be more challenging from a python rendering perspective (or it likely doesnt matter much). I'm pretty sure we talked about, or I had in my head at some point, that we should have some formal "if " construct in alembic (annnd....I'm pretty sure we dont yet, but not 100% sure) |
it's for sure more general, since it could be used by user to make conditional sections. |
ddf_if()
Describe the bug
Using conditional Indices with
ddl_if
, alembic ignores the target dialect and creates all the indices, not just the index that matches the conditional constraint.Generate python file contains two create_index statements, should be a single stmt:
Expected behavior
Only the index for the target dialect should be created, not both. Note if you use the same index name for both Index defs, you will get a single op.create_index but the configuration will be random.
To Reproduce
As above, using postgres as the db in alembic.ini:
Versions.
The text was updated successfully, but these errors were encountered: