Files
docqube_backend/scripts/delete_project_accesses.py
2026-09-08 11:00:05 +05:30

46 lines
1.9 KiB
Python

import os
import sys
# Add backend directory to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy import text
from app.db.database import SessionLocal
def purge_project_accesses():
db = SessionLocal()
try:
# 1. Delete all role_accesses linked to project.*
res_role_acc = db.execute(text("DELETE FROM role_accesses WHERE access_id IN (SELECT id FROM accesses WHERE access_code LIKE 'project.%')"))
print(f"Deleted {res_role_acc.rowcount} from role_accesses.")
# 2. Nullify parent_id for any accesses that use project.* as parent (unlikely, but safe)
try:
res_parent = db.execute(text("UPDATE accesses SET parent_id = NULL WHERE parent_id IN (SELECT id FROM accesses WHERE access_code LIKE 'project.%')"))
print(f"Nullified parent_id for {res_parent.rowcount} accesses.")
except Exception:
try:
# Fallback if the column is named parent_access_id
res_parent = db.execute(text("UPDATE accesses SET parent_access_id = NULL WHERE parent_access_id IN (SELECT id FROM accesses WHERE access_code LIKE 'project.%')"))
print(f"Nullified parent_access_id for {res_parent.rowcount} accesses.")
except Exception:
pass
# 3. Delete the rows themselves
res_del = db.execute(text("DELETE FROM accesses WHERE access_code LIKE 'project.%'"))
print(f"Deleted {res_del.rowcount} from accesses.")
db.commit()
print("Success! All 'project.*' rows have been completely purged from the database.")
except Exception as e:
db.rollback()
import traceback
traceback.print_exc()
print(f"Failed to delete: {e}")
finally:
db.close()
if __name__ == "__main__":
purge_project_accesses()