mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
use djangos method of wiping sqlite3 tables
Django uses a reference graph to determine the order in table deletions that occur. Do the same here. This commit also adds in the `REGEXP` sqlite function, exactly how Django uses it in its reference graphing. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
5de7ff64df
commit
d18cfad63e
3 changed files with 62 additions and 1 deletions
24
aurweb/db.py
24
aurweb/db.py
|
@ -1,4 +1,8 @@
|
|||
import functools
|
||||
import math
|
||||
import re
|
||||
|
||||
from sqlalchemy import event
|
||||
|
||||
import aurweb.config
|
||||
import aurweb.util
|
||||
|
@ -129,13 +133,31 @@ def get_engine(echo: bool = False):
|
|||
|
||||
if engine is None:
|
||||
connect_args = dict()
|
||||
if aurweb.config.get("database", "backend") == "sqlite":
|
||||
|
||||
db_backend = aurweb.config.get("database", "backend")
|
||||
if db_backend == "sqlite":
|
||||
# check_same_thread is for a SQLite technicality
|
||||
# https://fastapi.tiangolo.com/tutorial/sql-databases/#note
|
||||
connect_args["check_same_thread"] = False
|
||||
|
||||
engine = create_engine(get_sqlalchemy_url(),
|
||||
connect_args=connect_args,
|
||||
echo=echo)
|
||||
|
||||
if db_backend == "sqlite":
|
||||
# For SQLite, we need to add some custom functions as
|
||||
# they are used in the reference graph method.
|
||||
def regexp(regex, item):
|
||||
return bool(re.search(regex, str(item)))
|
||||
|
||||
@event.listens_for(engine, "begin")
|
||||
def do_begin(conn):
|
||||
create_deterministic_function = functools.partial(
|
||||
conn.connection.create_function,
|
||||
deterministic=True
|
||||
)
|
||||
create_deterministic_function("REGEXP", 2, regexp)
|
||||
|
||||
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
session = Session()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue