yet another refactor
This commit is contained in:
@@ -1 +0,0 @@
|
||||
"""Database helper for application-specific persistence operations."""
|
||||
@@ -1,22 +0,0 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.db_utils import upsert
|
||||
from src.models.category import Category, CategoryDb
|
||||
|
||||
|
||||
def upsert_category(
|
||||
session: Session, category: Category | None, do_commit: bool = True
|
||||
) -> CategoryDb | None:
|
||||
if category is None:
|
||||
return None
|
||||
|
||||
parent = upsert_category(session, category.parent, do_commit=False)
|
||||
parameters = {"name": category.name, "parent": parent}
|
||||
return upsert(
|
||||
session,
|
||||
CategoryDb,
|
||||
parameters,
|
||||
parameters,
|
||||
do_update=False,
|
||||
do_commit=do_commit,
|
||||
)
|
||||
+1
-1
@@ -3,10 +3,10 @@ from pathlib import Path
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.db_utils import upsert
|
||||
from src.helper.category import upsert_category
|
||||
from src.ingest.common import _reader_raw
|
||||
from src.ingest.transactions import consolidate_transactions, map_b4w_to_transaction
|
||||
from src.models.b4w import B4wDb, B4wExport
|
||||
from src.models.category import upsert_category
|
||||
|
||||
|
||||
def reader_b4w(path: Path) -> list[B4wExport]:
|
||||
|
||||
+22
-2
@@ -1,7 +1,9 @@
|
||||
from pydantic import Field, computed_field
|
||||
from sqlalchemy import ForeignKey, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.orm import Mapped, Session, mapped_column, relationship
|
||||
|
||||
from src.db_utils import upsert
|
||||
from src.models.category_budget import CategoryBudgetDb
|
||||
from src.models.common import BaseModelDb, ExportBaseModel, hashify
|
||||
|
||||
|
||||
@@ -53,4 +55,22 @@ class CategoryDb(BaseModelDb):
|
||||
"CategoryDb", remote_side="CategoryDb.id", back_populates="children"
|
||||
)
|
||||
children = relationship("CategoryDb", back_populates="parent")
|
||||
budgets = relationship("CategoryBudgetDb", back_populates="category")
|
||||
budgets: Mapped[list[CategoryBudgetDb]] = relationship(back_populates="category")
|
||||
|
||||
|
||||
def upsert_category(
|
||||
session: Session, category: Category | None, do_commit: bool = True
|
||||
) -> CategoryDb | None:
|
||||
if category is None:
|
||||
return None
|
||||
|
||||
parent = upsert_category(session, category.parent, do_commit=False)
|
||||
parameters = {"name": category.name, "parent": parent}
|
||||
return upsert(
|
||||
session,
|
||||
CategoryDb,
|
||||
parameters,
|
||||
parameters,
|
||||
do_update=False,
|
||||
do_commit=do_commit,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user