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:
Kevin Morris 2021-06-13 12:16:13 -07:00
parent bd8f528011
commit 40448ccd34
2 changed files with 41 additions and 5 deletions

View file

@ -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.