Spaces:
Sleeping
Sleeping
File size: 3,170 Bytes
940d3bc |
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 |
"""
Check for orphaned user references in project_team table
"""
from sqlalchemy import create_engine, text
from app.config import settings
def check_orphaned_users():
"""Find project_team records with missing or deleted users"""
engine = create_engine(settings.DATABASE_URL)
with engine.connect() as conn:
# Find project_team records where user doesn't exist or is deleted
query = text("""
SELECT
pt.id as team_id,
pt.user_id,
pt.project_id,
u.email,
u.deleted_at,
u.is_active
FROM project_team pt
LEFT JOIN users u ON pt.user_id = u.id
WHERE pt.deleted_at IS NULL
AND pt.removed_at IS NULL
AND (u.id IS NULL OR u.deleted_at IS NOT NULL)
ORDER BY pt.created_at DESC
""")
result = conn.execute(query)
orphaned = result.fetchall()
if orphaned:
print(f"\n๐ Found {len(orphaned)} orphaned team member records:\n")
for row in orphaned:
print(f" Team ID: {row.team_id}")
print(f" User ID: {row.user_id}")
print(f" Project ID: {row.project_id}")
print(f" Email: {row.email or 'N/A'}")
print(f" Deleted: {row.deleted_at or 'N/A'}")
print(f" Active: {row.is_active if row.is_active is not None else 'N/A'}")
print(" ---")
else:
print("\nโ
No orphaned team member records found")
# Check specifically for the problematic user
query2 = text("""
SELECT id, email, deleted_at, is_active
FROM users
WHERE id = '129753d3-fdcd-4c28-9c10-b6e9f58891e0'
""")
result2 = conn.execute(query2)
user = result2.fetchone()
print(f"\n๐ Checking specific user 129753d3-fdcd-4c28-9c10-b6e9f58891e0:")
if user:
print(f" โ User exists")
print(f" Email: {user.email}")
print(f" Deleted: {user.deleted_at}")
print(f" Active: {user.is_active}")
else:
print(f" โ User does NOT exist in database")
# Find where this user is referenced
query3 = text("""
SELECT 'project_team' as table_name, COUNT(*) as count
FROM project_team
WHERE user_id = '129753d3-fdcd-4c28-9c10-b6e9f58891e0'
AND deleted_at IS NULL
AND removed_at IS NULL
UNION ALL
SELECT 'asset_assignments', COUNT(*)
FROM asset_assignments
WHERE user_id = '129753d3-fdcd-4c28-9c10-b6e9f58891e0'
AND deleted_at IS NULL
""")
result3 = conn.execute(query3)
references = result3.fetchall()
print(f"\n๐ References to this user:")
for ref in references:
if ref.count > 0:
print(f" {ref.table_name}: {ref.count} records")
if __name__ == "__main__":
check_orphaned_users()
|