File size: 5,084 Bytes
6ea020b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
NexLab LIS — Clean Patients Tool
=================================
Deletes ALL patients and their related data from the database.

Affected tables (deleted in order):
  1. ResultDetail  (child of Result)
  2. Result        (child of Order)
  3. Payment       (child of PatientVisit)
  4. Attachment    (child of Patient)
  5. Order         (child of Patient / PatientVisit)
  6. PatientVisit  (child of Patient)
  7. Patient

NOT affected:
  - Users, Departments, Tests, Devices, Parameters, Packages, Partners
  - Provinces, Regions, LabInfo, PrintTemplates
  - Formulas, Supplies, Inventory, CalControl
  - AuditLog, ActivityLog, DeletedRecord (historical records preserved)
  - Chat, Messages, Expenses

Usage:
  python tools/clean_patients.py
"""

import sys
import os

# Ensure project root is in the path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sqlmodel import Session, select, func
from app.database import engine
from app.models import (
    Patient, PatientVisit, Order, Result, ResultDetail,
    Payment, Attachment,
)


def count_records(session):
    """Count all patient-related records."""
    counts = {
        "Patient": session.exec(select(func.count(Patient.id))).one(),
        "PatientVisit": session.exec(select(func.count(PatientVisit.id))).one(),
        "Order": session.exec(select(func.count(Order.id))).one(),
        "Result": session.exec(select(func.count(Result.id))).one(),
        "ResultDetail": session.exec(select(func.count(ResultDetail.id))).one(),
        "Payment": session.exec(select(func.count(Payment.id))).one(),
        "Attachment": session.exec(select(func.count(Attachment.id))).one(),
    }
    return counts


def clean_patients():
    """Delete all patient data in the correct FK order."""

    with Session(engine) as session:
        # --- Show what will be deleted ---
        counts = count_records(session)
        total = sum(counts.values())

        if total == 0:
            print("\n  No patient data found. The system is already clean.")
            return

        print("\n  ============================================")
        print("    NexLab LIS - Clean Patients Tool")
        print("  ============================================")
        print()
        print("  The following records will be PERMANENTLY deleted:\n")
        for table, count in counts.items():
            print(f"    {table:<20} {count:>6} records")
        print(f"    {'':-<20} {'':->6}---------")
        print(f"    {'TOTAL':<20} {total:>6} records")
        print()

        # --- Safety confirmation ---
        print("  WARNING: This action is IRREVERSIBLE!")
        print("  Tip: Run 'python tools/backup_db.py' first to create a backup.\n")

        confirm = input("  Type 'DELETE ALL PATIENTS' to confirm: ").strip()
        if confirm != "DELETE ALL PATIENTS":
            print("\n  Cancelled. No data was deleted.")
            return

        # --- Delete in FK order (children first) ---
        print("\n  Deleting records...")

        # 1. ResultDetail (depends on Result)
        deleted = session.exec(select(ResultDetail)).all()
        for r in deleted:
            session.delete(r)
        print(f"    ResultDetail:  {len(deleted)} deleted")

        # 2. Result (depends on Order)
        deleted = session.exec(select(Result)).all()
        for r in deleted:
            session.delete(r)
        print(f"    Result:        {len(deleted)} deleted")

        # 3. Payment (depends on PatientVisit / Patient)
        deleted = session.exec(select(Payment)).all()
        for r in deleted:
            session.delete(r)
        print(f"    Payment:       {len(deleted)} deleted")

        # 4. Attachment (depends on Patient)
        deleted = session.exec(select(Attachment)).all()
        for r in deleted:
            session.delete(r)
        print(f"    Attachment:    {len(deleted)} deleted")

        # 5. Order (depends on Patient / PatientVisit)
        deleted = session.exec(select(Order)).all()
        for r in deleted:
            session.delete(r)
        print(f"    Order:         {len(deleted)} deleted")

        # 6. PatientVisit (depends on Patient)
        deleted = session.exec(select(PatientVisit)).all()
        for r in deleted:
            session.delete(r)
        print(f"    PatientVisit:  {len(deleted)} deleted")

        # 7. Patient
        deleted = session.exec(select(Patient)).all()
        for r in deleted:
            session.delete(r)
        print(f"    Patient:       {len(deleted)} deleted")

        # --- Commit ---
        session.commit()
        print("\n  All patient data has been deleted successfully.")

        # --- Verify ---
        remaining = count_records(session)
        total_remaining = sum(remaining.values())
        if total_remaining == 0:
            print("  Verification: OK - all patient tables are empty.")
        else:
            print(f"  WARNING: {total_remaining} records still remain. Check for orphaned data.")

        print()


if __name__ == "__main__":
    clean_patients()