Files
service-finder/backend/app/scripts/fix_asset_person_ids.py
2026-03-30 06:32:22 +00:00

100 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Fix asset person IDs - update owner_person_id from User ID to Person ID.
"""
import asyncio
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import AsyncSessionLocal
from app.models.vehicle.asset import Asset
from app.models.identity.identity import User
async def fix_asset_person_ids():
async with AsyncSessionLocal() as db:
# Get User 28
user_stmt = select(User).where(User.id == 28)
user_result = await db.execute(user_stmt)
test_user = user_result.scalar_one_or_none()
if not test_user:
print("❌ User 28 not found")
return
print(f"✅ Found User 28:")
print(f" - User ID: {test_user.id}")
print(f" - Person ID: {test_user.person_id}")
print(f" - Email: {test_user.email}")
if not test_user.person_id:
print("❌ User has no person_id")
return
# Find assets with owner_person_id = 28 (User ID)
asset_stmt = select(Asset).where(
Asset.owner_person_id == 28
)
asset_result = await db.execute(asset_stmt)
assets = asset_result.scalars().all()
print(f"\n✅ Found {len(assets)} assets with owner_person_id = 28:")
for asset in assets:
print(f" - {asset.license_plate} (Asset ID: {asset.id})")
if assets:
# Update them to have person_id = 29
update_stmt = (
update(Asset)
.where(Asset.owner_person_id == 28)
.values(owner_person_id=test_user.person_id)
)
result = await db.execute(update_stmt)
await db.commit()
print(f"\n✅ Updated {result.rowcount} assets:")
print(f" - Changed owner_person_id from 28 to {test_user.person_id}")
# Verify the update
asset_stmt = select(Asset).where(
Asset.owner_person_id == test_user.person_id
)
asset_result = await db.execute(asset_stmt)
updated_assets = asset_result.scalars().all()
print(f"\n✅ Verification - Found {len(updated_assets)} assets with owner_person_id = {test_user.person_id}:")
for asset in updated_assets:
print(f" - {asset.license_plate}")
else:
print("\n No assets found with owner_person_id = 28")
# Also check for operator_person_id = 28
operator_asset_stmt = select(Asset).where(
Asset.operator_person_id == 28
)
operator_asset_result = await db.execute(operator_asset_stmt)
operator_assets = operator_asset_result.scalars().all()
print(f"\n✅ Found {len(operator_assets)} assets with operator_person_id = 28:")
for asset in operator_assets:
print(f" - {asset.license_plate} (Asset ID: {asset.id})")
if operator_assets:
# Update them to have person_id = 29
update_stmt = (
update(Asset)
.where(Asset.operator_person_id == 28)
.values(operator_person_id=test_user.person_id)
)
result = await db.execute(update_stmt)
await db.commit()
print(f"\n✅ Updated {result.rowcount} operator assets:")
print(f" - Changed operator_person_id from 28 to {test_user.person_id}")
if __name__ == "__main__":
asyncio.run(fix_asset_person_ids())