11from logging .config import fileConfig
22
3- from sqlalchemy import create_engine
3+ from sqlalchemy import create_engine , event
44from sqlalchemy import pool
55from 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