
    i                        S SK Jr   S SKJr  S SKrS SKrS SKrS SK	J
r
Jr  \R                  " \5      rSr\R                   " S\R"                  5      rS
S jrSS jrSS	 jrg! \ a	    S SKJr   Ncf = f)    )annotations)TraversableN)import_module	resourcesz"workflows.server.sqlite.migrationsz!pragma\s+user_version\s*=\s*(\d+)c                     [        [        5      n [        R                  " U 5      nS UR	                  5        5       n[        US S9$ )z:Yield packaged SQL migration files in lexicographic order.c              3  h   #    U  H(  oR                   R                  S 5      (       d  M$  Uv   M*     g7f)z.sqlN)nameendswith).0ps     Y/home/james-whalen/.local/lib/python3.13/site-packages/workflows/server/sqlite/migrate.py	<genexpr>(_iter_migration_files.<locals>.<genexpr>   s     B1&&//&*AQQs   #2	2c                    U R                   $ )N)r	   )r   s    r   <lambda>'_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                    U (       a  U R                  5       S   OSn[        R                  U5      nU(       a  [        UR	                  S5      5      $ 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                $   U R                  5       nUR                  S5      R                  5       nU(       a  [        US   5      OSn[	        5        Hw  nUR                  5       n[        U5      =(       d    SnXc::  a  M.   [        R                  SUR                  U5        UR                  SU-   5        UR                  S5        UnMy     g! [         a8  n[        R                  SUR                  U5        UR                  S5        e SnAff = f)	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_migrationsr;   #   s     ++-C++&;<EEG5Hc-a01aO%'>>#.x8=A,	-LL=tyy. j834 KK!,O# (  	LL2DIIsCKK
#	s    5C
D3D

D)returnzlist[Traversable])r$   strr<   z
int | None)r4   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'   r;    r   r   <module>rL      sp    "*3  	  . 
		8	$ 7

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