Skip to content

Commit 3aa86eb

Browse files
pauliyobocary-rowen
authored andcommitted
Enable sqlite3 transaction
1 parent 2573c89 commit 3aa86eb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

alembic/env.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from logging.config import fileConfig
22

3-
from sqlalchemy import create_engine
3+
from sqlalchemy import create_engine, event
44
from sqlalchemy import pool
55
from sqlalchemy.orm import configure_mappers
66

@@ -60,6 +60,7 @@ def run_migrations_offline() -> None:
6060
target_metadata=target_metadata,
6161
literal_binds=True,
6262
dialect_opts={"paramstyle": "named"},
63+
render_as_batch=True,
6364
)
6465

6566
with context.begin_transaction():
@@ -74,12 +75,22 @@ def run_migrations_online() -> None:
7475
7576
"""
7677
connectable = create_engine(config.get_main_option("sqlalchemy.url"))
78+
# see: https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#sqlite-transactions
79+
@event.listens_for(connectable, "connect")
80+
def do_connect(dbapi_connection, connection_record):
81+
dbapi_connection.isolation_level = None
82+
83+
84+
@event.listens_for(connectable, "begin")
85+
def do_begin(conn):
86+
conn.exec_driver_sql("BEGIN")
7787

7888
with connectable.connect() as connection:
7989
context.configure(
8090
connection=connection,
8191
target_metadata=target_metadata,
8292
process_revision_directives=writer,
93+
render_as_batch=True,
8394
)
8495

8596
with context.begin_transaction():

0 commit comments

Comments
 (0)