
    Ji                        d dl mZ 	 d dlmZ d dlZd dlZd dlZd dl	m
Z
mZ  ej                  e      ZdZ ej                   dej"                        Zd
dZddZdd	Zy# e$ r	 d dlmZ Y _w xY w)    )annotations)TraversableN)import_module	resourcesz"workflows.server.sqlite.migrationsz!pragma\s+user_version\s*=\s*(\d+)c                     t        t              } t        j                  |       }d |j	                         D        }t        |d       S )z:Yield packaged SQL migration files in lexicographic order.c              3  X   K   | ]"  }|j                   j                  d       s| $ yw)z.sqlN)nameendswith).0ps     q/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/workflows/server/sqlite/migrate.py	<genexpr>z(_iter_migration_files.<locals>.<genexpr>   s     B1!&&//&*AQBs    **c                    | j                   S )N)r	   )r   s    r   <lambda>z'_iter_migration_files.<locals>.<lambda>   s
    qvv     )key)r   _MIGRATIONS_PKGr   filesiterdirsorted)pkgrootr   s      r   _iter_migration_filesr      s8    

(C??3DBBE%-..r   c                    | r| j                         d   nd}t        j                  |      }|rt        |j	                  d            S dS )zGReturn target schema version declared in the first PRAGMA line, if any.r       N)
splitlines_USER_VERSION_PATTERNsearchintgroup)sql_text
first_linematchs      r   _parse_target_versionr%      sC    -5$$&q)2J!((4E"'3u{{1~1T1r   c                   | j                         }|j                  d      j                         }|rt        |d         nd}t	               D ]p  }|j                         }t        |      xs d}||k  r(	 t        j                  d|j                  |       |j                  d|z          |j                  d       |}r y# t        $ r8}t        j                  d|j                  |       |j                  d        d}~ww xY w)	zApply pending migrations found under the migrations package.

    Each migration file should start with a `PRAGMA user_version=N;` line.
    Files are applied in lexicographic order and only when N > current_version.
    zPRAGMA user_versionr   u+   Applying migration %s → target version %szBEGIN;
COMMITzFailed migration %s: %sROLLBACKN)cursorexecutefetchoner    r   	read_textr%   loggerdebugr	   executescript	Exceptionerror)conncurcurrent_version_rowcurrent_versionpathr"   target_versionexcs           r   run_migrationsr9   "   s     ++-C++&;<EEG5Hc-a01aO%' ->>#.x8=A_,	-LL=tyy. j834 KK!,O#-  	LL2DIIsCKK
#	s   55C  	D	3C<<D)returnzlist[Traversable])r"   strr:   z
int | None)r2   zsqlite3.Connectionr:   None)
__future__r   importlib.resources.abcr   ImportErrorimportlib.abcloggingresqlite3	importlibr   r   	getLogger__name__r-   r   compile
IGNORECASEr   r   r%   r9    r   r   <module>rJ      sr    "*3  	  .			8	$ 7"

#GW /2-;  *)*s   A   A.-A.