U
    h                  
   @   s   d dl mZmZ d dlmZ d dlZd dlmZ e  ede	 Z
ee
Zdd Zdd	 Zed
kred ze  e  W n@ ek
r Z z"ede  d dlZe  W 5 dZ[X Y nX dS )    )create_enginetext)ConfigN)load_dotenvDATABASE_URLc                  C   s  t  p} td | td}t|}|sDtd W 5 Q R  dS tdt| d |D ]\}}td| d| d	 q\td
}| dkrtd W 5 Q R  dS td z"| td | 	  td W n2 t
k
r } ztd|  W 5 d}~X Y nX td td}| |}| 	  td|j d td | td }|dkrftd ntd| d W 5 Q R X dS )z7Fix any integer developer_ids in activity_records tablez9Checking for integer developer_ids in activity_records...z
            SELECT DISTINCT developer_id, pg_typeof(developer_id) as type
            FROM activity_records
            WHERE developer_id IS NOT NULL
              AND developer_id ~ '^[0-9]+$'
            LIMIT 10
        u8   ✅ No numeric developer_ids found. Database looks good!NzFound z( numeric developer_ids that need fixing:  - 'z	' (type: )zD
Do you want to fix these by converting to string format? (yes/no): Zyesz
Cancelled.z'
Creating backup of activity_records...z
                CREATE TABLE IF NOT EXISTS activity_records_backup AS 
                SELECT * FROM activity_records
            u/   ✅ Backup created as 'activity_records_backup'u#   ⚠️ Backup might already exist: z
Fixing developer_ids...z
            UPDATE activity_records
            SET developer_id = CONCAT('dev_', developer_id)
            WHERE developer_id ~ '^[0-9]+$'
        u   ✅ Updated  recordsz
Verifying fix...zs
            SELECT COUNT(*)
            FROM activity_records
            WHERE developer_id ~ '^[0-9]+$'
        r   u1   ✅ All developer_ids are now properly formatted!u   ⚠️ Still found z numeric developer_ids)engineconnectprintexecuter   listleninputlowerZcommit	ExceptionZrowcountZscalar)connresultZnumeric_idsdev_idZdtypeZresponseeZupdate_query r   ./fix_database_types.pyfix_developer_ids   s>     


r   c               	   C   s   t  } td | td}t|}|rftdt| d |D ]\}}td| d| d qDntd | td	}t|}|rtd
t| d |D ]\}}td| d| d qntd W 5 Q R X dS )z%Check data consistency between tablesz"
=== Checking data consistency ===aE  
            SELECT DISTINCT ar.developer_id, COUNT(*) as count
            FROM activity_records ar
            LEFT JOIN developers d ON ar.developer_id = d.developer_id
            WHERE ar.developer_id IS NOT NULL
              AND d.developer_id IS NULL
            GROUP BY ar.developer_id
            LIMIT 10
        u#   
⚠️ Found activity records for z non-existent developers:r   z': r	   u2   
✅ All activity records have matching developersz
            SELECT d.developer_id, d.name
            FROM developers d
            LEFT JOIN activity_records ar ON d.developer_id = ar.developer_id
            WHERE d.active = true
              AND ar.id IS NULL
            LIMIT 10
        u   
⚠️ Found z( active developers without any activity:z  - z (ID: r   u0   
✅ All active developers have activity recordsN)r
   r   r   r   r   r   r   )r   r   Zorphanedr   countZinactivenamer   r   r   check_data_consistencyV   s     

	r   __main__z$=== Timesheet Database Fix Tool ===
u   
❌ Error: )Z
sqlalchemyr   r   Zconfigr   osZdotenvr   getenvZget_database_urlr   r
   r   r   __name__r   r   r   	traceback	print_excr   r   r   r   <module>   s"   G*
