Open nstrong-scw opened 2 years ago
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 recreate_dropped
and it looks good to me. It's certainly a nicer way to handle the issue, although it'd still be cool if the need for it could be auto-detected at generate time.
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.
it'd still be cool if the need for it could be auto-detected at generate time.
the tricky part w/ that is that the alembic migrations need to be executed for the schema diffing alembic_utils
uses to work. That would be fine 80% of the time but some failure cases are:
create index concurrently
) -> exceptionThe 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
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 solve_resolution_order
little bit:
https://github.com/olirice/alembic_utils/pull/90
I'm getting an error KeyError: 'formatters'
when I try to run tests. The error is coming from trying to parse the config file (alembic.ini), but I'm not sure why it can't find it. So, I don't know if the tests still pass or not--I'd appreciate guidance.
Here are the steps to run the tests
Requirements:
git clone git@github.com:olirice/alembic_utils.git
cd alembic_utils
python -m venv venv
source venv/bin/activate
pip install -U pip
pip install -e ".[dev]"
pytest
If that fails, please add a comment with:
pip freeze
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 pypy
to the latest version because the old version pulls in a version of check-ast
that doesn't build against python 3.9 and later.
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 in this function https://github.com/olirice/alembic_utils/blob/5f670039a9a10722c0e5522370d8591831855e54/src/alembic_utils/testbase.py#L36
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: