No need for session.begin in db.create_all.

This commit is contained in:
Fabio Manganiello 2023-04-24 23:55:50 +02:00
parent d33d760361
commit f4e13d0cb0
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -139,7 +139,7 @@ class DbPlugin(Plugin):
engine=None, engine=None,
data: Optional[dict] = None, data: Optional[dict] = None,
*args, *args,
**kwargs **kwargs,
): ):
""" """
Returns rows (as a list of hashes) given a query. Returns rows (as a list of hashes) given a query.
@ -219,7 +219,7 @@ class DbPlugin(Plugin):
query = table.select() query = table.select()
if filter: if filter:
for (k, v) in filter.items(): for k, v in filter.items():
query = query.where(self._build_condition(table, k, v)) query = query.where(self._build_condition(table, k, v))
if query is None: if query is None:
@ -246,7 +246,7 @@ class DbPlugin(Plugin):
key_columns=None, key_columns=None,
on_duplicate_update=False, on_duplicate_update=False,
*args, *args,
**kwargs **kwargs,
): ):
""" """
Inserts records (as a list of hashes) into a table. Inserts records (as a list of hashes) into a table.
@ -394,7 +394,7 @@ class DbPlugin(Plugin):
values = {k: v for (k, v) in record.items() if k not in key_columns} values = {k: v for (k, v) in record.items() if k not in key_columns}
update = table.update() update = table.update()
for (k, v) in key.items(): for k, v in key.items():
update = update.where(self._build_condition(table, k, v)) update = update.where(self._build_condition(table, k, v))
update = update.values(**values) update = update.values(**values)
@ -503,13 +503,13 @@ class DbPlugin(Plugin):
table, engine = self._get_table(table, engine=engine, *args, **kwargs) table, engine = self._get_table(table, engine=engine, *args, **kwargs)
delete = table.delete() delete = table.delete()
for (k, v) in record.items(): for k, v in record.items():
delete = delete.where(self._build_condition(table, k, v)) delete = delete.where(self._build_condition(table, k, v))
connection.execute(delete) connection.execute(delete)
def create_all(self, engine, base): def create_all(self, engine, base):
with (self.get_session(engine, locked=True) as session, session.begin()): with self.get_session(engine, locked=True) as session:
base.metadata.create_all(session.connection()) base.metadata.create_all(session.connection())
@contextmanager @contextmanager
@ -523,7 +523,7 @@ class DbPlugin(Plugin):
# Mock lock # Mock lock
lock = RLock() lock = RLock()
with (lock, engine.connect() as conn, conn.begin()): with lock, engine.connect() as conn, conn.begin():
session = scoped_session( session = scoped_session(
sessionmaker( sessionmaker(
expire_on_commit=False, expire_on_commit=False,