Add leaves reset, split holidays/leave resets, support multi-table reset
This commit is contained in:
+12
-6
@@ -1936,19 +1936,25 @@ async def reset_section(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Reset (truncate) a specific data section. Creates auto-backup first."""
|
||||
valid = {"attendance": AttendanceRecord, "holidays": Holiday, "leaves": LeaveRecord, "accident": Accident}
|
||||
if section not in valid:
|
||||
single_models = {"attendance": AttendanceRecord, "accident": Accident}
|
||||
if section not in single_models and section not in ("holidays", "leaves"):
|
||||
raise HTTPException(status_code=400, detail=f"Unknown section: {section}")
|
||||
# Auto-backup before reset
|
||||
src = Path(DB_PATH)
|
||||
auto_dst = None
|
||||
if src.exists():
|
||||
auto_dst = _backup_path(f"auto_pre_{section}_reset")
|
||||
shutil.copy2(src, auto_dst)
|
||||
# Truncate table
|
||||
model = valid[section]
|
||||
db.execute(model.__table__.delete())
|
||||
# Truncate table(s)
|
||||
if section == "holidays":
|
||||
db.execute(Holiday.__table__.delete())
|
||||
db.execute(LeaveRecord.__table__.delete())
|
||||
elif section == "leaves":
|
||||
db.execute(LeaveRecord.__table__.delete())
|
||||
else:
|
||||
db.execute(single_models[section].__table__.delete())
|
||||
db.commit()
|
||||
return {"reset": section, "auto_backup": auto_dst.name}
|
||||
return {"reset": section, "auto_backup": auto_dst.name if auto_dst else None}
|
||||
|
||||
|
||||
@app.get("/api/version")
|
||||
|
||||
Reference in New Issue
Block a user