fix: Fix parallel execution of alembic with SQlite (#5404)

Fix parallel execution of alembic with SQlite
This commit is contained in:
Christophe Bornet 2024-12-30 19:10:33 +01:00 committed by GitHub
commit 870aedf74e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@ from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool, text
from sqlalchemy.event import listen
from langflow.services.database.service import SQLModel
@ -51,6 +52,21 @@ def run_migrations_offline() -> None:
context.run_migrations()
def _sqlite_do_connect(
dbapi_connection,
connection_record, # noqa: ARG001
):
# disable pysqlite's emitting of the BEGIN statement entirely.
# also stops it from emitting COMMIT before any DDL.
dbapi_connection.isolation_level = None
def _sqlite_do_begin(conn):
# emit our own BEGIN
conn.exec_driver_sql("PRAGMA busy_timeout = 60000")
conn.exec_driver_sql("BEGIN EXCLUSIVE")
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
@ -64,6 +80,11 @@ def run_migrations_online() -> None:
poolclass=pool.NullPool,
)
if connectable.dialect.name == "sqlite":
# See https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#serializable-isolation-savepoints-transactional-ddl
listen(connectable, "connect", _sqlite_do_connect)
listen(connectable, "begin", _sqlite_do_begin)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True)