Deadlock while updating a column, updated the env.py file to replicate same set of migrations on different schemas #1308
Replies: 4 comments 1 reply
-
hi - I'm not sure where the exact source of your deadlock is, however Alembic does not connect to the database, your own code in env.py is where that happens. I see you are mixing up the use of ORM sessions coming from Flask bound to some other engine somewhere, along with connections you are starting up locally in your env.py. this will cause deadlocks and this is expected. I think you'd be better off ensuring all the operations you are doing share a single connection and transaction, something like this: with connectable.connect() as connection:
for schema, schema_type in customer_list:
if schema != "public":
connection.execute(text(f'CREATE SCHEMA IF NOT EXISTS "{schema}";'))
connection.commit()
context.configure(
include_object=include_object,
connection=connection,
target_metadata=get_metadata(),
process_revision_directives=process_revision_directives,
version_table_schema=schema,
**current_app.extensions["migrate"].configure_args,
)
with context.begin_transaction():
context.run_migrations(schema=schema, schema_type=schema_type) |
Beta Was this translation helpful? Give feedback.
-
I tried but did not work Let me give more clarity My requirement is that I have a series of migrations and I apply the same set of migrations on multiple database schemas (a typical multitenant requirement). I have updated my env.py file to do this customer behavior in this way
This works perfectly fine when I run the command
In this process everything works fine until there are 2 revisions lets say revision1 and revision2 and in revision1 we are creating a constraint revision1.py
and drop that in revision2 revision2.py
when I try to apply these revisions from the API it goes into deadlock but works correctly when triggered via cli with |
Beta Was this translation helpful? Give feedback.
-
Your new code example still refers to "db.session" throughout, being mixed with a
this is not really relevant to the actual error you are getting, which is that you are running statements in multiple transactions at once and getting deadlocks. Using multiple schemas does not necessitate the use of a
the Flask db.session is probably still opened within a transaction. It doesn't really matter until the programming patterns in the
remove the use of |
Beta Was this translation helpful? Give feedback.
-
Thanks @zzzeek and @CaselIT for the help!! Means a lot Seems Like our issue was different This is what I was doing in the Flask API
app.py
but because I was importing the But after discussing this issue here with @zzzeek and @CaselIT I feel like the |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
I have updated the env.py (will be attaching the updated file below) file to replicate the same set of revisions on multiple schemas. And in that if I have 2 revisions lets call them revision1 and revision2 and I create a column in revision1 and alter the column in revision2 and I am triggering the upgrading of the migrations from the code not from the cli.
env.py
revision1.py
revision2.py
This causes a deadlock when the migration is triggered via code something like this
Expected behavior
Migration should happen without the deadlock anyways each revision is expected to run in a different transaction
The deadlock causing queries in postgreSQL:
ALTER TABLE "23d85593-8f70-4915-b5cb-445dff4ea0b8".appointment DROP CONSTRAINT created_by_id_fk
Versions.
Additional context
I have tried adding these 2 parameters
transaction_per_migration=True
transactional_ddl=True
in configure with these params it gave an error that the constraint created_by_id_fk does not existsHave a nice day!
Beta Was this translation helpful? Give feedback.
All reactions