Skip to content

Commit

Permalink
Fix parallel execution of alembic with SQlite
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Dec 23, 2024
1 parent b2e7476 commit 7ab8e67
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Empty file.
22 changes: 21 additions & 1 deletion src/backend/base/langflow/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from alembic import context
from sqlalchemy import engine_from_config, pool
from sqlalchemy.event import listen

from langflow.services.database.models import *
from langflow.services.database.service import SQLModel

# this is the Alembic Config object, which provides
Expand Down Expand Up @@ -52,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.
Expand All @@ -65,6 +80,11 @@ def run_migrations_online() -> None:
poolclass=pool.NullPool,
)

if connectable.url.drivername.startswith("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)

Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ignore_missing_imports = true

[tool.ruff]
target-version = "py310"
exclude = ["langflow/alembic"]
exclude = ["langflow/alembic/versions"]
line-length = 120

[tool.ruff.lint]
Expand Down

0 comments on commit 7ab8e67

Please sign in to comment.