mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
[FastAPI] Refactor db modifications
For SQLAlchemy to automatically understand updates from the external world, it must use an `autocommit=True` in its session. This change breaks how we were using commit previously, as `autocommit=True` causes SQLAlchemy to commit when a SessionTransaction context hits __exit__. So, a refactoring was required of our tests: All usage of any `db.{create,delete}` must be called **within** a SessionTransaction context, created via new `db.begin()`. From this point forward, we're going to require: ``` with db.begin(): db.create(...) db.delete(...) db.session.delete(object) ``` With this, we now get external DB modifications automatically without reloading or restarting the FastAPI server, which we absolutely need for production. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
b52059d437
commit
a5943bf2ad
37 changed files with 998 additions and 902 deletions
|
@ -1,6 +1,6 @@
|
|||
import pytest
|
||||
|
||||
from aurweb.db import create, query
|
||||
from aurweb import db
|
||||
from aurweb.models.account_type import AccountType
|
||||
from aurweb.models.ssh_pub_key import SSHPubKey, get_fingerprint
|
||||
from aurweb.models.user import User
|
||||
|
@ -19,19 +19,18 @@ def setup():
|
|||
|
||||
setup_test_db("Users", "SSHPubKeys")
|
||||
|
||||
account_type = query(AccountType,
|
||||
AccountType.AccountType == "User").first()
|
||||
user = create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=account_type)
|
||||
account_type = db.query(AccountType,
|
||||
AccountType.AccountType == "User").first()
|
||||
with db.begin():
|
||||
user = db.create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=account_type)
|
||||
|
||||
assert account_type == user.AccountType
|
||||
assert account_type.ID == user.AccountTypeID
|
||||
|
||||
ssh_pub_key = create(SSHPubKey,
|
||||
UserID=user.ID,
|
||||
Fingerprint="testFingerprint",
|
||||
PubKey="testPubKey")
|
||||
with db.begin():
|
||||
ssh_pub_key = db.create(SSHPubKey,
|
||||
UserID=user.ID,
|
||||
Fingerprint="testFingerprint",
|
||||
PubKey="testPubKey")
|
||||
|
||||
|
||||
def test_ssh_pub_key():
|
||||
|
@ -43,9 +42,10 @@ def test_ssh_pub_key():
|
|||
|
||||
def test_ssh_pub_key_cs():
|
||||
""" Test case sensitivity of the database table. """
|
||||
ssh_pub_key_cs = create(SSHPubKey, UserID=user.ID,
|
||||
Fingerprint="TESTFINGERPRINT",
|
||||
PubKey="TESTPUBKEY")
|
||||
with db.begin():
|
||||
ssh_pub_key_cs = db.create(SSHPubKey, UserID=user.ID,
|
||||
Fingerprint="TESTFINGERPRINT",
|
||||
PubKey="TESTPUBKEY")
|
||||
|
||||
assert ssh_pub_key_cs.Fingerprint == "TESTFINGERPRINT"
|
||||
assert ssh_pub_key_cs.PubKey == "TESTPUBKEY"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue