
    yj                          d dl Zd dlmZ d dlmZ defdZddedej        dz  de	fdZ
	 dded	edej        dz  de	fd
Z	 ddededej        dz  de	fdZ	 ddededej        dz  de	fdZ	 ddedededej        dz  de	f
dZded	eddfdZdS )    N)op)settingsreturnc                  $    t           j        j        S N)r   DBSCHEMA     8/DATA/AppData/hermes/projects/honcho/migrations/utils.py
get_schemar      s    ;r   
table_name	inspectorc                     |%t          j        t          j                              }t	                      }|                    | |          S )z(Check if a table exists in the database.Nschema)sainspectr   get_bindr   	has_table)r   r   r   s      r   table_existsr      s?    Jr{}}--	\\Fz&999r   column_namec                     |%t          j        t          j                              }t	                      }d |                    | |          D             }||v S )z$Check if a column exists in a table.Nc                     g | ]
}|d          S )namer
   ).0cols     r   
<listcomp>z!column_exists.<locals>.<listcomp>   s)       F  r   r   )r   r   r   r   r   get_columns)r   r   r   r   existing_columnss        r   column_existsr!      sh     Jr{}}--	\\F (44Z4OO   ***r   fk_namec                     |%t          j        t          j                              }t	                      }|                    | |          }t          fd|D                       S )z)Check if a foreign key exists in a table.Nr   c              3   J   K   | ]}|                     d           k    V  dS r   N)get)r   fkr"   s     r   	<genexpr>zfk_exists.<locals>.<genexpr>(   s2      @@Rrvvf~~(@@@@@@r   )r   r   r   r   r   get_foreign_keysany)r   r"   r   r   foreign_keyss    `   r   	fk_existsr,       sd     Jr{}}--	\\F--j-HHL@@@@<@@@@@@r   
index_namec                     |%t          j        t          j                              }t	                      }|                    | |          }t          fd|D                       S )z$Check if an index exists in a table.Nr   c              3   0   K   | ]}|d          k    V  dS r%   r
   )r   idxr-   s     r   r(   zindex_exists.<locals>.<genexpr>3   s,      <<Ss6{j(<<<<<<r   )r   r   r   r   r   get_indexesr*   )r   r-   r   r   indexess    `   r   index_existsr3   +   sd     Jr{}}--	\\F##Jv#>>G<<<<G<<<<<<r   constraint_nametypec                    |%t          j        t          j                              }t	                      }|dk    r|                    | |          }nw|dk    r|                    | |          }nY|dk    r#|                    | |          }|d         k    S |dk    r|                    | |          }nt          d|           t          fd	|D                       S )
z(Check if a constraint exists in a table.Ncheckr   uniqueprimaryr   
foreignkeyzInvalid constraint type: c              3   0   K   | ]}|d          k    V  dS r%   r
   )r   
constraintr4   s     r   r(   z$constraint_exists.<locals>.<genexpr>K   s-      SSz&!_4SSSSSSr   )r   r   r   r   r   get_check_constraintsget_unique_constraintsget_pk_constraintr)   
ValueErrorr*   )r   r4   r5   r   r   constraintsr<   s    `     r   constraint_existsrB   6   s    Jr{}}--	\\Fw55j5PP			66z&6QQ			00F0KK
&!_44			00F0KK;T;;<<<SSSS{SSSSSSr   c                    t                      }t          j                    }|  d d}t          j        t          j                              }|                    | |          }t          fd|D             d          }|t          d|  d d          |d	         st          d|  d d
           dS |j	        }|j
        }	|	                    |          }
|	                    |           }|	                    |          }|	                              }t          | |d          s4|                    t          j        d|
 d| d| d| d	                     |                    t          j        d|
 d| d| d                     t          j        | d|           |                    t          j        d|
 d| d| d                     dS )a  
    Make a column non-nullable using a non-blocking approach to minimize lock duration.

    WARNING: Only use this if you can guarantee that:
        1. No NULL values currently exist in the column
        2. The application code is already writing non-NULL values to this column or
        3. The column has never accepted NULLs in practice

    This uses a 4-step process to avoid long exclusive locks:
    1. Add CHECK constraint with NOT VALID (instant, no scan)
    2. Validate the constraint (scans but allows concurrent read/writes to the table)
    3. Set column NOT NULL (fast since we've validated the constraint)
    4. Drop the redundant CHECK constraint

    Args:
        table_name: The name of the table
        column_name: The name of the column to make non-nullable
    _	_not_nullr   c              3   4   K   | ]}|d          k    |V  dS r%   r
   )r   r   r   s     r   r(   z0make_column_non_nullable_safe.<locals>.<genexpr>h   s1      MM#f+2L2L2L2L2L2LMMr   NzColumn .z does not existnullablez% is already non-nullable, skipping...r7   z
                ALTER TABLE z 
                ADD CONSTRAINT z
                CHECK (z8 IS NOT NULL)
                NOT VALID
                z
            ALTER TABLE z!
            VALIDATE CONSTRAINT z
            F)rH   r   z
          ALTER TABLE z
          DROP CONSTRAINT z
          )r   r   r   r   r   r   nextr@   printdialectidentifier_preparerquoterB   executetextalter_column)r   r   r   connr4   r   columnscolumn_inforK   preparerquoted_schemaquoted_tablequoted_constraintquoted_columns    `            r   make_column_non_nullable_saferY   N   s   & \\F;==D#<<k<<<O 
2;==))I##Jv#>>GMMMMwMMMtTTKL:LLLLLMMMz" W
WW[WWWXXX lG*HNN6**M>>*--L 77NN;//M Z'BB 

G* -9  1  &   		
 		
 		
 	LL
& )5 !2  	
 	
   O	    	LL
$ '3 ,  	
 	
    r   r   )
sqlalchemyr   alembicr   
src.configr   strr   	Inspectorboolr   r!   r,   r3   rB   rY   r
   r   r   <module>r`      s                  C    : :S :R\D-@ :D : : : : IM
+ 
+
+"%
+24,2E
+	
+ 
+ 
+ 
+ EIA AA!A.0lT.AA	A A A A HL= ==!$=131D=	= = = = &*	T TTT T |d"	T
 
T T T T0Uc U U U U U U U Ur   