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),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
"""Reset (truncate) a specific data section. Creates auto-backup first."""
|
"""Reset (truncate) a specific data section. Creates auto-backup first."""
|
||||||
valid = {"attendance": AttendanceRecord, "holidays": Holiday, "leaves": LeaveRecord, "accident": Accident}
|
single_models = {"attendance": AttendanceRecord, "accident": Accident}
|
||||||
if section not in valid:
|
if section not in single_models and section not in ("holidays", "leaves"):
|
||||||
raise HTTPException(status_code=400, detail=f"Unknown section: {section}")
|
raise HTTPException(status_code=400, detail=f"Unknown section: {section}")
|
||||||
# Auto-backup before reset
|
# Auto-backup before reset
|
||||||
src = Path(DB_PATH)
|
src = Path(DB_PATH)
|
||||||
|
auto_dst = None
|
||||||
if src.exists():
|
if src.exists():
|
||||||
auto_dst = _backup_path(f"auto_pre_{section}_reset")
|
auto_dst = _backup_path(f"auto_pre_{section}_reset")
|
||||||
shutil.copy2(src, auto_dst)
|
shutil.copy2(src, auto_dst)
|
||||||
# Truncate table
|
# Truncate table(s)
|
||||||
model = valid[section]
|
if section == "holidays":
|
||||||
db.execute(model.__table__.delete())
|
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()
|
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")
|
@app.get("/api/version")
|
||||||
|
|||||||
@@ -18,10 +18,16 @@ const RESET_SECTIONS = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "holidays",
|
key: "holidays",
|
||||||
label: "假期 Holidays + Leave",
|
label: "假期 Holidays",
|
||||||
description: "刪除所有公眾假期 + 員工請假記錄",
|
description: "刪除所有公眾假期記錄",
|
||||||
color: "amber",
|
color: "amber",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: "leaves",
|
||||||
|
label: "請假 Leaves",
|
||||||
|
description: "刪除所有員工請假記錄",
|
||||||
|
color: "lime",
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
export default function Settings() {
|
export default function Settings() {
|
||||||
|
|||||||
Reference in New Issue
Block a user