-
Notifications
You must be signed in to change notification settings - Fork 49
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
gracefully handle when a migrator needs to modify a column that a view depends on #89
Comments
There is an experimental context manager for this use case: from alembic_utils.depends import recreate_dropped
def upgrade() -> None:
my_view = PGView(...)
with recreate_dropped(op.get_bind()) as conn:
op.drop_entity(my_view)
# you could also do `op.execute("drop view myview")` here
# change an integer column to a bigint
op.alter_column(
table_name="account",
column_name="id",
schema="public"
type_=sa.BIGINT()
existing_type=sa.Integer(),
) when the context manager goes out of scope, anything you drop gets recreated Please note that it is not currently part of the public API so it is use-at-your-own-risk |
Thanks for this! I looked over the code behind The logging tends to be a little chatty (the "Resolving entities with dependencies" message gets repeated across each round), but that's a very minor complaint. |
the tricky part w/ that is that the alembic migrations need to be executed for the schema diffing
|
If you'd like to open a PR I'm happy for that log line to more outside the loop
|
I took a stab at optimizing I'm getting an error |
Here are the steps to run the tests Requirements:
If that fails, please add a comment with:
so i can try to reproduce |
No dice.
As a side note, I'm not sure if it is relevant to the tests failing, but I had to re-pin |
unfortunately i can not reproduce that error with 3.10.3 and the lib versions from pip freeze It looks like its failing to find this key https://github.com/olirice/alembic_utils/blob/master/alembic.ini#L59
if you track it down I'd be happy to fix. In the meantime, please feel free to open PRs and let CI run the test suite for you as needed (mark as draft) thanks |
Alembic version: 1.7.7
Alembic_utils version: 0.7.7
db: postgres 12
Here's a basic setup:
Now, suppose you wish to change
some_field
to atext
column. I change the class, runalembic revision -m 'update table' --autogenerate
.Expected behavior: the column should upgrade successfully.
Actual behavior: Postgres throws an error: "cannot alter type of a column used by a view or rule"
So, in other words, alembic_utils will successfully recreate views if the view definition is changed, but it doesn't catch when table columns associated with those views get modified.
I've devised a workaround, but it would be nice if it could be done automatically somehow.
Here is my (admittedly naive) workaround to solve this problem in the current code:
The text was updated successfully, but these errors were encountered: