mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
aurweb.db: add commit(), add() and autocommit arg
With the addition of these two, some code has been swapped to use these in some of the other db wrappers with an additional autocommit kwarg in create and delete, to control batch transactions. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
bd8f528011
commit
40448ccd34
2 changed files with 41 additions and 5 deletions
21
aurweb/db.py
21
aurweb/db.py
|
@ -59,24 +59,35 @@ def query(model, *args, **kwargs):
|
|||
return session.query(model).filter(*args, **kwargs)
|
||||
|
||||
|
||||
def create(model, *args, **kwargs):
|
||||
def create(model, autocommit: bool = True, *args, **kwargs):
|
||||
instance = model(*args, **kwargs)
|
||||
session.add(instance)
|
||||
session.commit()
|
||||
add(instance)
|
||||
if autocommit is True:
|
||||
commit()
|
||||
return instance
|
||||
|
||||
|
||||
def delete(model, *args, **kwargs):
|
||||
def delete(model, *args, autocommit: bool = True, **kwargs):
|
||||
instance = session.query(model).filter(*args, **kwargs)
|
||||
for record in instance:
|
||||
session.delete(record)
|
||||
session.commit()
|
||||
if autocommit is True:
|
||||
commit()
|
||||
|
||||
|
||||
def rollback():
|
||||
session.rollback()
|
||||
|
||||
|
||||
def add(model):
|
||||
session.add(model)
|
||||
return model
|
||||
|
||||
|
||||
def commit():
|
||||
session.commit()
|
||||
|
||||
|
||||
def get_sqlalchemy_url():
|
||||
"""
|
||||
Build an SQLAlchemy for use with create_engine based on the aurweb configuration.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue