housekeep(fastapi): rework aurweb.db session API

Changes:
-------
- Add aurweb.db.get_session()
    - Returns aurweb.db's global `session` instance
    - Provides us a way to change the implementation of the session
      instance without interrupting user code.
- Use aurweb.db.get_session() in session API methods
- Add docstrings to session API methods
- Refactor aurweb.db.delete
    - Normalize aurweb.db.delete to an alias of session.delete
- Refresh instances in places we depend on their non-PK columns
  being up to date.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-14 15:36:06 -08:00
parent f8ba2c5342
commit 4103ab49c9
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
22 changed files with 212 additions and 138 deletions

View file

@ -19,7 +19,7 @@ def references_graph(table):
"regexp_1": r'(?i)\s+references\s+("|\')?',
"regexp_2": r'("|\')?\s*\(',
}
cursor = aurweb.db.session.execute(query, params=params)
cursor = aurweb.db.get_session().execute(query, params=params)
return [row[0] for row in cursor.fetchall()]
@ -51,7 +51,7 @@ def setup_test_db(*args):
db_backend = aurweb.config.get("database", "backend")
if db_backend != "sqlite": # pragma: no cover
aurweb.db.session.execute("SET FOREIGN_KEY_CHECKS = 0")
aurweb.db.get_session().execute("SET FOREIGN_KEY_CHECKS = 0")
else:
# We're using sqlite, setup tables to be deleted without violating
# foreign key constraints by graphing references.
@ -59,10 +59,10 @@ def setup_test_db(*args):
references_graph(table) for table in tables))
for table in tables:
aurweb.db.session.execute(f"DELETE FROM {table}")
aurweb.db.get_session().execute(f"DELETE FROM {table}")
if db_backend != "sqlite": # pragma: no cover
aurweb.db.session.execute("SET FOREIGN_KEY_CHECKS = 1")
aurweb.db.get_session().execute("SET FOREIGN_KEY_CHECKS = 1")
# Expunge all objects from SQLAlchemy's IdentityMap.
aurweb.db.session.expunge_all()
aurweb.db.get_session().expunge_all()