diff --git a/src/backend/base/langflow/alembic/env.py b/src/backend/base/langflow/alembic/env.py index f3e107eee..8cf6002c4 100644 --- a/src/backend/base/langflow/alembic/env.py +++ b/src/backend/base/langflow/alembic/env.py @@ -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)