o
    Jh[                     @   s  d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d d	l
mZ 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Zd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dlm Z  edZ!dZ"ej#Z$dej%fddZ&dd Z'ej(e)e)f Z*e)Z+e)Z,de)dej-e*e+e,f fddZ.de)dej-e*e+ej/e) f fddZ0G dd de1Z2G dd de2Z3G d d! d!e1Z4G d"d# d#e4Z5G d$d% d%e4Z6G d&d' d'e4Z7G d(d) d)e7Z8dej9ej-e)ej/e) f  fd*d+Z:d,d- Z;G d.d/ d/ej<Z=G d0d1 d1e1Z>d2d3 Z?d4d5 Z@d6d7 ZAeAZBd8d9 ZCd:d; ZDd<d= ZEd>ej9e2 dej9e2 fd?d@ZFdS )A    )Counter)OrderedDict)abc)	ExitStack)copy)glob)	resources)chain)count)zip_longest)	getLoggerN)
exceptions)pluralzyoyo.migrations_yoyo_migrationpathc                 C   s.   ddl m} |  o| jdv o| j|j S )zH
    Return True if the given path matches a migration file pattern
    r   )newmigration>   .py.sql)yoyo.scriptsr   is_filesuffixname
startswithtempfile_prefix)r   r    r   Q/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/yoyo/migrations.py_is_migration_file1   s   r   c                 C   s   | du rdS t | d S )z
    Return a unique hash given a migration_id, that can be used as a database
    key.

    :param migration_id: a migration id (ie filename without extension), or
                         ``None`` if this is a new migration
    Nzutf-8)hash_functionencode	hexdigest)migration_idr   r   r   get_migration_hash>   s   r!   sreturnc              	   C   s  ddg}t dj}t ddtt j|}t d| d d}t	| 
|}i }g }g }|D ]<}	||	}
|
rW|
 \}}||v rR||  d	|7  < q3|||< q3||	rj|	 d
 }|| q3||	  || |t||||fS )Ntransactionaldependsz^(\s*|\s*--.*)$z^\s*--\s*({})\s*:\s*(.*)$|z
\n|\r\n|\r
r   z {}z--)recompilematchformatjoinmapescapesearchgroupitersplitgroupsstriplstripappendextendtextwrapdedent)r"   directive_namescomment_or_emptydirective_pattern
lineendinglines
directivesleading_commentssqlliner*   kvdecommentedr   r   r    parse_metadata_from_sql_commentsS   s6   



rF   c                 C   s   i }d}g }t j| r9t| ddd}t| }|r*t|d \}}}||d< W d    n1 s4w   Y  dd |D }|||fS )N rzUTF-8)encodingr   c                 S   s   g | ]}|  r|qS r   )r4   .0r"   r   r   r   
<listcomp>       z&read_sql_migration.<locals>.<listcomp>)osr   existsopensqlparser2   readrF   )r   r?   leading_comment
statementsfrA   r   r   r   read_sql_migrationw   s"   
	
rV   c                   @   sf   e Zd ZU i Zejed f ed< dd Zdd Z	dd Z
edd	 Zed
d Zdd ZdddZdS )	Migration_Migration__all_migrationsc                 C   sH   || _ t|| _|| _d | _d | _|| _d| _d | _| | j	|< d | _
d S )NT)idr!   hashr   stepssource
source_diruse_transactions_dependsrX   module)selfrY   r   r]   r   r   r   __init__   s   


zMigration.__init__c                 C   s   d | jj| j| jS )Nz<{} {!r} from {}>)r+   	__class____name__rY   r   ra   r   r   r   __repr__      zMigration.__repr__c                 C   s   | j dS )Nr   )r   endswithre   r   r   r   
is_raw_sql      zMigration.is_raw_sqlc                 C   s
   | j d uS Nr[   re   r   r   r   loaded   s   
zMigration.loadedc                 C   s   |    | jS rk   )loadr_   re   r   r   r   r%      s   zMigration.dependsc              
      sV   j rd S t d}t jd}|  _W d    n1 s w   Y    r1t j _	nt
j j j}|d us@J t
j| _	|j j	_|j j	_|j j	_| j	_  rt j\}}}ttj jd d \}}}|  t||d d}	|	D ]
\}
}||
| q| j	_t j	dddd	|d
d   t j	ddd |dd D  n8z|r|jr|j j	 nt !d j t"# jW n t$y } zt !d j| t"# j|d }~ww t% j	dg }t&|t't(fr|g} fdd|D  _)t% j	dd _*d  j)v r"t"#d+ j|, j* _-d S )N)	migrationrH   r   .rollback.sql)	fillvalue__transactional__TF)truefalser$   rs   __depends__c                 S   s   h | ]}|r|qS r   r   )rK   dr   r   r   	<setcomp>       z!Migration.load.<locals>.<setcomp>r%   rG   zECould not import migration from %r: ModuleSpec has no loader attachedz&Could not import migration from %r: %rc                    s   h | ]	} j |d qS rk   )rX   get)rK   rY   re   r   r   rw          z$Could not resolve dependencies in {}).rm   StepCollectorrP   r   rR   r\   ri   types
ModuleTyper`   	importlibutilspec_from_file_locationmodule_from_specadd_stepstepadd_step_groupr0   transaction__yoyo_collector__rV   rN   splitextreverser   __doc__setattrry   lowerr2   loaderexec_modulelogger	exceptionr   BadMigration	Exceptiongetattr
isinstancestrbytesr_   r^   r+   create_stepsr[   )ra   	collectorrU   specr?   rS   rT   _rollback_statementsstatements_with_rollbackr"   rH   er%   r   re   r   rn      s~   





	
zMigration.loadFc           
      C   s0  |    ddd| }| j}|d usJ |dkrt|}g }| jr%|j}n|j}| c |D ]W}zt|||| || W q. |jy   t	
 }	|jrQ| jstzt|D ]	}t||| qVW n |jys   td||j Y nw |	d d ur|	d |	d Y q.w W d    d S 1 sw   Y  d S )Napplyrollback)r   r   zCould not %s step %s      )rn   r[   reversedr^   r   disable_transactionsr   r6   DatabaseErrorsysexc_infohas_transactional_ddlr   r   rY   with_traceback)
ra   backend	directionforcer   r[   executed_stepsr   r   r   r   r   r   process_steps   s>   "zMigration.process_stepsNF)rd   
__module____qualname__rX   tDictr   __annotations__rb   rf   ri   propertyrm   r%   rn   r   r   r   r   r   rW      s   
 

GrW   c                   @   s   e Zd ZdZdS )PostApplyHookMigrationz
    A special migration that is run after successfully applying a set of
    migrations. Unlike a normal migration this will be run every time
    migrations are applied script is called.
    N)rd   r   r   r   r   r   r   r   r     s    r   c                   @   s,   e Zd ZdZdd Zd	ddZd	ddZdS )
StepBaseNc                 C      d | jj| jS )Nz<{} #{}>)r+   rc   rd   rY   re   r   r   r   rf        zStepBase.__repr__Fc                 C      t  rk   NotImplementedErrorra   r   r   r   r   r   r   "     zStepBase.applyc                 C   r   rk   r   r   r   r   r   r   %  r   zStepBase.rollbackr   )rd   r   r   rY   rf   r   r   r   r   r   r   r     s
    
r   c                   @   6   e Zd ZdZdddZdd Zdd	d
ZdddZdS )TransactionWrapperz
    A :class:~`yoyo.migrations.TransactionWrapper` object causes a step to be
    run within a single database transaction. Nested transactions are
    implemented via savepoints.
    Nc                 C      |dv sJ || _ || _d S N)Nallr   r   r   ignore_errorsra   r   r   r   r   r   rb   0     
zTransactionWrapper.__init__c                 C      d | jS Nz<TransactionWrapper {!r}>r+   r   re   r   r   r   rf   5  rj   zTransactionWrapper.__repr__Fr   c                 C   s~   z"|   t| j||| W d    W d S 1 sw   Y  W d S  |jy>   |s3| j|dfv r=td| j Y d S  w Nr   zIgnored error in %r)r   r   r   r   r   r   r   ra   r   r   r   r   r   r   r   8  s   
&zTransactionWrapper.applyc                 C      |  ||d d S Nr   r   r   r   r   r   r   B  r   zTransactionWrapper.rollbackrk   Fr   r   rd   r   r   r   rb   rf   r   r   r   r   r   r   r   )  s    


r   c                   @   r   )Transactionlessz~
    A :class:~`yoyo.migrations.TransactionWrapper` object causes a step to be
    run outside of a database transaction.
    Nc                 C   r   r   r   r   r   r   r   rb   L  r   zTransactionless.__init__c                 C   r   r   r   re   r   r   r   rf   Q  rj   zTransactionless.__repr__Fr   c                 C   sR   zt | j||| W d S  |jy(   |s| j|dfv r'td| j Y d S  w r   )r   r   r   r   r   r   r   r   r   r   r   T  s   zTransactionless.applyc                 C   r   r   r   r   r   r   r   r   ^  r   zTransactionless.rollbackrk   r   r   r   r   r   r   r   r   F  s    


r   c                   @   s6   e Zd ZdZdd ZdddZddd	Zdd
dZdS )MigrationStepz
    Model a single migration.

    Each migration step comprises apply and rollback steps of up and down SQL
    statements.
    c                 C   s   || _ || _|| _d S rk   )rY   	_rollback_apply)ra   rY   r   r   r   r   r   rb   j  s   
zMigrationStep.__init__Nc                 C   s<  |du rt j}t|trtd|dd ntd| || |jrdd |	 D }dd |jD }dd |D }|D ]}t
|D ]\}}	t|	|| krWt|	||< qEq?d	d
d |D }
|
d7 }
||
t|  |ddd |D d  |D ]}||
t|  q|tt|ddd  dS dS )zp
        Execute the given statement. If rows are returned, output these in a
        tabulated format.
        Nz - executing %rasciireplacec                 S   s   g | ]	}d d |D qS )c                 S      g | ]}t |qS r   )r   )rK   valuer   r   r   rL   |  rx   z5MigrationStep._execute.<locals>.<listcomp>.<listcomp>r   )rK   rowr   r   r   rL   |  rz   z*MigrationStep._execute.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   )rK   descr   r   r   rL   }  rx   c                 S   r   r   )len)rK   cr   r   r   rL   ~  rx   r&   c                 s   s    | ]}d | V  qdS )z	 %%- %ds Nr   rK   sizer   r   r   	<genexpr>  s    z)MigrationStep._execute.<locals>.<genexpr>r'   +c                 s   s    | ]	}d |d  V  qdS )-r   Nr   r   r   r   r   r     s    z(%d row)z	(%d rows))r   stdoutr   r   r   debugr   executedescriptionfetchall	enumerater   r,   writetupler   )ra   cursorstmtoutresultcolumn_namescolumn_sizesr   ixr   r+   r   r   r   _executeo  s0   

zMigrationStep._executeFc                 C   sd   t d| j | jsdS t| jtr*| }z| || j W |  dS |  w | |j	 dS )zl
        Apply the step.

        :param force: If true, errors will be logged but not be re-raised
        z - applying step %dN)
r   inforY   r   r   r   r   r   close
connectionra   r   r   r   r   r   r   r     s   zMigrationStep.applyc                 C   sh   t d| j | jdu rdS t| jtr,| }z| || j W |  dS |  w | |j	 dS )z$
        Rollback the step.
        z - rolling back step %dN)
r   r   rY   r   r   r   r   r   r   r   r   r   r   r   r     s   
zMigrationStep.rollbackrk   r   )rd   r   r   r   rb   r   r   r   r   r   r   r   r   b  s    

r   c                   @   s4   e Zd ZdZdd Zdd ZdddZdd	d
ZdS )	StepGroupz,
    Multiple steps aggregated together
    c                 C   s
   || _ d S rk   rl   )ra   r[   r   r   r   rb        
zStepGroup.__init__c                 C   r   )Nz{}({!r}))r+   rc   rd   r[   re   r   r   r   rf     r   zStepGroup.__repr__Fc                 C   s   | j D ]}||| qd S rk   )r[   r   ra   r   r   itemr   r   r   r     s   
zStepGroup.applyc                 C   s    t | jD ]}||| qd S rk   )r   r[   r   r   r   r   r   r     s   zStepGroup.rollbackNr   r   r   r   r   r   r     s    
r   c           
   	   #   s    t dj}t  t j | D ]`}||}|rU|d}|d}z't	|
|}| rJ fdd| D }dd t|D }||fV  W q tyT   Y qw ttjt|D ]}	dd |	 D }t|	t|fV  q]qd S )	Nz^package:([^\s\/:]+):(.*)$r   r   c                 3   s(    | ]}|  r t|V  qd S rk   )r   enter_contextr   as_file)rK   traversablefilecontextr   r   r     s    
z"_expand_sources.<locals>.<genexpr>c                 S      g | ]
}t |rt|qS r   r   r   rK   rU   r   r   r   rL         z#_expand_sources.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   rL     r  )r(   r)   r*   r   atexitregisterr   r0   r   filesjoinpathis_diriterdirsortedFileNotFoundErrorr-   pathlibPathr   r   )
sourcespackage_matchr\   mopackage_nameresource_dir	pkg_files	all_filespaths	directoryr   r   r   _expand_sources  s4   



r  c            
      G   s   t  }t| D ]L\}}|D ]E}|drqtjtj|d }t}|dr*t	}|tjtj|d ||d}|
|t }|t	u rM|j| q|| qqtt|  tdd | D  }	|	S )zP
    Return a ``MigrationList`` containing all migrations from ``sources``.
    rp   r   z
post-apply)r]   c                 s       | ]}|j V  qd S rk   )
post_applyrK   mr   r   r   r         z"read_migrations.<locals>.<genexpr>)r   r  rh   rN   r   r   basenamerW   r   r   
setdefaultMigrationListr  r6   r	   values)
r  
migrationsr\   r  r   filenamemigration_classro   mlmerged_migrationsr   r   r   read_migrations  s0   


r$  c                   @   sj   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r  z(
    A list of database migrations.
    Nc                 C   sF   |rt |ng | _|rt |ng | _tdd | jD | _|   d S )Nc                 s   r  rk   rY   rK   r   r   r   r   r   	  r  z)MigrationList.__init__.<locals>.<genexpr>)listitemsr  setkeyscheck_conflicts)ra   r(  r  r   r   r   rb     s   zMigrationList.__init__c                 C   s   d | jjt| jS )Nz{}({}))r+   rc   rd   reprr(  re   r   r   r   rf     rg   zMigrationList.__repr__c                 C   s@   t  }| D ]}||j  d7  < ||j dkrt|jqd S )Nr   )r   rY   r   MigrationConflict)ra   r   r   r   r   r   r+    s   zMigrationList.check_conflictsc                 C   s(   t |tr| | j|S | j|S rk   )r   slicerc   r(  __getitem__)ra   nr   r   r   r/    s   
zMigrationList.__getitem__c                 C   s   | j | }t|tstdd |D }|jh}ntdd |D }dd |D }|D ]}|| jv r:||vr:t|q*| j| | j	| | j 
||S )Nc                 S   s   g | ]}|j qS r   r%  r&  r   r   r   rL         z-MigrationList.__setitem__.<locals>.<listcomp>c                 s   r  rk   r%  r&  r   r   r   r   !  r  z,MigrationList.__setitem__.<locals>.<genexpr>c                 S   s   h | ]}|j qS r   r%  r&  r   r   r   rw   "  r1  z,MigrationList.__setitem__.<locals>.<setcomp>)r(  r   r'  r)  rY   r*  r   r-  difference_updateupdate__setitem__)ra   r0  obremoving
remove_idsnew_idsrY   r   r   r   r4    s   



zMigrationList.__setitem__c                 C   s
   t | jS rk   )r   r(  re   r   r   r   __len__,  r   zMigrationList.__len__c                 C   s$   | j | j| j | j| d S rk   )r*  remover(  rY   __delitem__)ra   ir   r   r   r;  /  s   zMigrationList.__delitem__c                 C   s4   |j | jv rt|j | j|j  | j||S rk   )rY   r*  r   r-  addr(  insert)ra   r<  xr   r   r   r>  3  s   zMigrationList.insertc                 C   s   t | }|| |S rk   )r   r7   )ra   otherr5  r   r   r   __add__9  s   
zMigrationList.__add__c                    s   |   fdd| D | jS )Nc                    s   g | ]} |r|qS r   r   r  	predicater   r   rL   ?  rM   z(MigrationList.filter.<locals>.<listcomp>rc   r  )ra   rC  r   rB  r   filter>  s   zMigrationList.filterc                 C   s   |  || jS rk   rD  )ra   newmigrationsr   r   r   r   A  s   zMigrationList.replaceNN)rd   r   r   r   rb   rf   r+  r/  r4  r9  r;  r>  rA  rE  r   r   r   r   r   r    s    
r  c                   @   s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )r{   z
    Provide the ``step`` and ``transaction`` functions used in migration
    scripts.

    Each call to step/transaction updates the StepCollector's ``steps`` list.
    c                 C   s   || _ t | _td| _d S )Nr   )ro   r   r[   r
   step_id)ra   ro   r   r   r   rb   M  s   zStepCollector.__init__Nc                    s     fdd}dj |< |S )z
        Wrap the given apply and rollback code in a transaction, and add it
        to the list of steps.
        Return the transaction-wrapped step.
        c                    s,   | rt nt}ttj }||}|S rk   )r   r   r   nextrH  )r^   wrapperr   r   r   r   ra   r   r   do_addY  s   
z&StepCollector.add_step.<locals>.do_addr   rl   )ra   r   r   r   rL  r   rK  r   r   R  s   
zStepCollector.add_stepc                    sb   d v r|r
t d d nttdd |D  D ]}| j|= q fdd}d| j|< |S )z8
        Create a ``StepGroup`` group of steps.
        r[   zHsteps cannot be called with both keyword and positional 'steps' argumentc                 s   s&    | ]}t |tjr|n|gV  qd S rk   )r   r   IterablerJ   r   r   r   r   p  s   $ z/StepCollector.add_step_group.<locals>.<genexpr>c                    s8    dd } r
tnt}t fddD }|||S )Nr   c                       g | ]}| qS r   r   rK   create_stepr^   r   r   rL   y  rx   z@StepCollector.add_step_group.<locals>.do_add.<locals>.<listcomp>)popr   r   r   )r^   r   rJ  r0   kwargsr[   rQ  r   rL  u  s   
z,StepCollector.add_step_group.<locals>.do_addr   )
ValueErrorr'  r	   r[   )ra   argsrT  r"   rL  r   rS  r   r   b  s   


zStepCollector.add_step_groupc                    s    fdd| j D S )Nc                    rN  r   r   rO  rQ  r   r   rL     rx   z.StepCollector.create_steps.<locals>.<listcomp>rl   )ra   r^   r   rQ  r   r     s   zStepCollector.create_stepsrG  )rd   r   r   r   rb   r   r   r   r   r   r   r   r{   E  s    
r{   c                  C   sX   t  } z| d urd| jv r| jd W S | j} | d us	W d S W d S  ty+   tdw )Nr   zAExpected to be called in the context of a migration module import)inspectcurrentframe	f_globalsf_backKeyErrorAssertionError)framer   r   r   _get_collector  s   
r^  c                  O      t  j| i |S rk   )r^  r   rV  rT  r   r   r   r     r   r   c                  O   r_  rk   )r^  r   r`  r   r   r   r0     r   r0   c                 C   sj   t  }| jD ]}|| qt  }|r3| }|| |jD ]}||v r&q|| || q|s|S )z
    Return the dependencies for ``migration`` from ``population``.

    :param migration: a :class:`~yoyo.migrations.Migration` object
    :param population: a collection of migrations
    )r)  r%   r=  rR  )ro   
population
to_processr  depsrv   r   r   r   	ancestors  s   



rd  c                 C   sT   t |}| h}	 d}|| D ]}t |j|@ r|| d}q|s"nq||  |S )z
    Return all descendants of ``migration`` from ``population``.

    :param migration: a :class:`~yoyo.migrations.Migration` object
    :param population: a collection of migrations
    TF)r)  r%   r=  r:  )ro   ra  descendantsfoundr  r   r   r   re    s   

re  c                 C   s    t | }| D ]}||j8 }q|S )zF
    Return the set of migrations that have no child dependencies
    )r)  r%   )migration_listheadsr  r   r   r   rh    s   rh  r  c                    s   ddl m} ddl m} t| }t|  fdd|D }z|||W S  |yB } ztdddd	 |j	d
 D d }~ww )Nr   )topological_sort)
CycleErrorc                    s   i | ]}||j  @ qS r   )r%   r  all_migrationsr   r   
<dictcomp>  rM   z$topological_sort.<locals>.<dictcomp>z/Circular dependencies among these migrations {}z, c                 s   r  rk   r%  r  r   r   r   r     r  z#topological_sort.<locals>.<genexpr>r   )
yoyo.topologicalsortri  rj  r'  r)  r   r   r+   r,   rV  )r  topological_sort_implrj  rg  dependency_graphr   r   rk  r   ri    s   ri  )Gcollectionsr   r   r   
contextlibr   r   r   r~   r   	itertoolsr	   r
   r   loggingr   r  typingr   hashlibimportlib.utilrN   r
  r(   r   rW  r|   r8   rQ   yoyor   
yoyo.utilsr   r   default_migration_tablesha256r   r  r   r!   r   r   DirectivesTypeLeadingCommentTypeSqlTypeTuplerF   ListrV   objectrW   r   r   r   r   r   r   rM  r  r$  MutableSequencer  r{   r^  r   r0   r   rd  re  rh  ri  r   r   r   r   <module>   s~   
$
 
M$!D>"
