73 lines
2.9 KiB
Python
73 lines
2.9 KiB
Python
class StaleRepository:
|
|
_TABLES = {
|
|
"UNIT_COST": "b08_unit_costs",
|
|
"EQUIPMENT": "b08_equipment_rates",
|
|
"COST_BASIS": "b08_cost_basis",
|
|
}
|
|
|
|
@classmethod
|
|
async def mark_all(cls, cursor, project_id: str) -> None:
|
|
for table in cls._TABLES.values():
|
|
await cursor.execute(
|
|
f"UPDATE {table} SET status='STALE' "
|
|
"WHERE project_id=%s AND status='CONFIRMED'",
|
|
(project_id,),
|
|
)
|
|
|
|
@classmethod
|
|
async def mark_dependents(
|
|
cls, cursor, project_id: str, source_type: str, source_id: str
|
|
) -> None:
|
|
queue = [(source_type, source_id)]
|
|
visited: set[tuple[str, str]] = set()
|
|
while queue:
|
|
reference_type, reference_id = queue.pop(0)
|
|
key = (reference_type, reference_id)
|
|
if key in visited:
|
|
continue
|
|
visited.add(key)
|
|
dependents = await cls._find_dependents(
|
|
cursor, project_id, reference_type, reference_id
|
|
)
|
|
for dependent_type, dependent_id in dependents:
|
|
if (dependent_type, dependent_id) in visited:
|
|
continue
|
|
table = cls._TABLES[dependent_type]
|
|
await cursor.execute(
|
|
f"UPDATE {table} SET status='STALE' "
|
|
"WHERE project_id=%s AND id=%s AND status='CONFIRMED'",
|
|
(project_id, dependent_id),
|
|
)
|
|
if cursor.rowcount:
|
|
queue.append((dependent_type, dependent_id))
|
|
|
|
@staticmethod
|
|
async def _find_dependents(
|
|
cursor, project_id: str, reference_type: str, reference_id: str
|
|
) -> list[tuple[str, str]]:
|
|
await cursor.execute(
|
|
"""SELECT 'UNIT_COST' AS dependent_type,u.id AS dependent_id
|
|
FROM b08_unit_costs u
|
|
JOIN b08_unit_cost_components c ON c.unit_cost_id=u.id
|
|
WHERE u.project_id=%s AND u.status='CONFIRMED'
|
|
AND c.component_type=%s AND c.reference_id=%s
|
|
UNION
|
|
SELECT 'EQUIPMENT',e.id
|
|
FROM b08_equipment_rates e
|
|
JOIN b08_equipment_rate_components c ON c.equipment_rate_id=e.id
|
|
WHERE e.project_id=%s AND e.status='CONFIRMED'
|
|
AND c.component_type=%s AND c.item_id=%s
|
|
UNION
|
|
SELECT 'COST_BASIS',b.id
|
|
FROM b08_cost_basis b
|
|
JOIN b08_cost_basis_components c ON c.cost_basis_id=b.id
|
|
WHERE b.project_id=%s AND b.status='CONFIRMED'
|
|
AND c.component_type=%s AND c.reference_id=%s""",
|
|
(project_id, reference_type, reference_id,
|
|
project_id, reference_type, reference_id,
|
|
project_id, reference_type, reference_id),
|
|
)
|
|
return [
|
|
(row["dependent_type"], row["dependent_id"])
|
|
for row in await cursor.fetchall()
|
|
] |