Skip to content

Commit

Permalink
Fixed db migrations dropping tables content because of ForeignKey con…
Browse files Browse the repository at this point in the history
…straints. #2489
  • Loading branch information
morpheus65535 committed May 9, 2024
1 parent d686ab7 commit 4815313
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions migrations/env.py
@@ -1,6 +1,7 @@
from flask import current_app

from alembic import context
from sqlalchemy import text

import logging

Expand Down Expand Up @@ -95,8 +96,22 @@ def process_revision_directives(context, revision, directives):
)

with context.begin_transaction():
bind = context.get_bind()

if bind.engine.name == 'sqlite':
bind.execute(text("PRAGMA foreign_keys=OFF;"))
elif bind.engine.name == 'postgresql':
bind.execute(text("SET CONSTRAINTS ALL DEFERRED;"))

context.run_migrations()

if bind.engine.name == 'sqlite':
bind.execute(text("PRAGMA foreign_keys=ON;"))
elif bind.engine.name == 'postgresql':
bind.execute(text("SET CONSTRAINTS ALL IMMEDIATE;"))

bind.close()


if context.is_offline_mode():
run_migrations_offline()
Expand Down

0 comments on commit 4815313

Please sign in to comment.