init
This commit is contained in:
31
backend/core/db_utils.py
Normal file
31
backend/core/db_utils.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# backend/core/db_utils.py
|
||||
|
||||
from sqlalchemy import insert as sa_insert
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from core.db import engine
|
||||
|
||||
_IS_PG = engine.url.get_backend_name().startswith("postgres")
|
||||
|
||||
def upsert(
|
||||
tbl,
|
||||
insert_values: dict,
|
||||
conflict_cols: list[str],
|
||||
update_values: dict | None = None,
|
||||
):
|
||||
|
||||
if _IS_PG:
|
||||
stmt = (
|
||||
pg_insert(tbl)
|
||||
.values(**insert_values)
|
||||
.on_conflict_do_update(
|
||||
index_elements=conflict_cols,
|
||||
set_=update_values or insert_values,
|
||||
)
|
||||
)
|
||||
else:
|
||||
stmt = (
|
||||
sa_insert(tbl)
|
||||
.values(**insert_values)
|
||||
.prefix_with("OR REPLACE") # SQLite
|
||||
)
|
||||
return stmt
|
||||
Reference in New Issue
Block a user