
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    ՙ_                  	   @   s  d dl Z d dlZd dlmZ d dlmZ d dlZd dlZd dl	m	Z	 d dl
mZmZ d dlmZmZ d dlmZmZ d dlmZ ed	ejZe je  d
Zdd Zee je je je dZ!e!" Z#W 5 Q R X G dd de$Z%G dd de%Z&G dd de%Z'dS )    N)import_module)
ModuleType)cached_property)LOGGERMigrateHistory)	diff_manyNEWLINE)string_typesexec_in)Migratorz\s+$
migrationsc                 C   s   d S N )mdr   r   F/opt/alt/python38/lib/python3.8/site-packages/peewee_migrate/router.py<lambda>       r   ztemplate.txtc                   @   s   e Zd ZdZdefddZedd Zedd Z	d ddZ
d!ddZdd Zedd Zedd Zedd Zd"ddZd#ddZdd ZdS )$
BaseRouterzAbstract base class for router.Zmigratehistoryc                 C   s6   || _ || _|| _t| j tjtjfs2td| d S )NzInvalid database: %s)databasemigrate_tablelogger
isinstancepwZDatabaseZProxyRuntimeError)selfr   r   r   r   r   r   __init__   s
    zBaseRouter.__init__c                 C   s"   | j tj_ | jtj_td tS )z+Ensure that migrations has prepared to run.T)r   r   Z_metar   Zdb_tableZcreate_tabler   r   r   r   model"   s    


zBaseRouter.modelc                 C   s   t d S r   NotImplementedErrorr   r   r   r   todo+   s    zBaseRouter.todoautoFc           	         sP  d }} r"t  trHzt  W n" tk
rF   | jd  Y S X t  trvttdd  fddt	 D  | j
D ]}| || j q| }t| jj }t||| jd}|s| jdS ttd	|d
 }td
|}t||| jdd}ttd	|d
 }td
|}| jd| | |||}| jd| |S )Create a migration. zCan't import models module: %sc                 S   s   t | tot| tjS r   )r   type
issubclassr   ZModel)r   r   r   r   r   ;   r   z#BaseRouter.create.<locals>.<lambda>c                 3   s   | ]}t  |V  qd S r   )getattr).0r   r"   r   r   	<genexpr><   s     z$BaseRouter.create.<locals>.<genexpr>)migratorzNo changes found.z


T)r+   reversezCreating migration "%s"zMigration created %s)r   r	   r   ImportErrorr   errorr   listfilterdirdiffrun_oner+   Zormvaluesr   warnr   joinsplitCLEAN_REsubinfo_create)	r   namer"   migraterollbackZ	migrationZmodels1Zmodels2pathr   r)   r   create/   s8    


zBaseRouter.creater$   c                 C   s   t d S r   r   )r   r=   r>   r?   r   r   r   r<   T   s    zBaseRouter._createc                 C   s   t d S r   r   )r   r=   r   r   r   readW   s    zBaseRouter.readc                 C   s   dd | j  D S )zScan migrations in database.c                 S   s   g | ]
}|j qS r   r=   )r(   Zmmr   r   r   
<listcomp>]   s     z#BaseRouter.done.<locals>.<listcomp>)r   selectr   r   r   r   doneZ   s    zBaseRouter.donec                    s   t | j  fdd| jD S )z'Calculate difference between fs and db.c                    s   g | ]}| kr|qS r   r   )r(   r=   rF   r   r   rD   c   s      z#BaseRouter.diff.<locals>.<listcomp>)setrF   r!   r   r   rG   r   r3   _   s    
zBaseRouter.diffc                 C   s&   t | j}| jD ]}| || q|S )z2Create migrator and setup it with fake migrations.)r   r   rF   r4   )r   r+   r=   r   r   r   r+   e   s    

zBaseRouter.migratorTc           	   
   C   st  z.|  |\}}|rtd, td ||| j|d W 5 Q R X W 5 Q R X |rr| jj|d | jd| |  |W S | jd| | j	  |s||| j|d |
  | jj|d | jd| nP| jd| ||| j|d |
  | j | jj|k  | jd| W 5 Q R X W n> tk
rn } z| j  | jd	|  W 5 d
}~X Y nX d
S )zRun a migration.zpeewee.Model.selectzpeewee.Query._execute)fakerC   zDone %szRunning "%s"zRolling back %szRolled back %szMigration failed: %sN)rB   mockZpatchr   r   rA   r   r;   ZcleanZtransactionrundeletewherer=   execute	Exceptionr?   Z	exception)	r   r=   r+   rI   Z	downgradeforcer>   r?   excr   r   r   r4   m   s6    $
zBaseRouter.run_oneNc                 C   sj   | j d g }| j}|s*| j d |S | j}|D ]0}| j||||d || |r4||kr4 qfq4|S )zRun migrations.zStarting migrationszThere is nothing to migrate)rI   rP   )r   r;   r3   r+   r4   append)r   r=   rI   rF   r3   r+   Zmnamer   r   r   rK      s    
zBaseRouter.runc                 C   sV   |  }| j}|std||d kr.td| j}| ||dd | jd| d S )NzNo migrations are found.z$Only last migration can be canceled.FTzDowngraded migration: %s)striprF   r   r+   r4   r   r6   )r   r=   rF   r+   r   r   r   r?      s    zBaseRouter.rollback)r"   F)r$   r$   )TFF)NF)__name__
__module____qualname____doc__r   r   r   r   propertyr!   rA   r<   rB   rF   r3   r+   r4   rK   r?   r   r   r   r   r      s$   


%




$
r   c                       sH   e Zd ZedZef fdd	Zedd Z	dddZ
d	d
 Z  ZS )Routerz[\d]{3}_[^\.]+\.py$c                    s   t t| j|f| || _d S r   )superrZ   r   migrate_dir)r   r   r\   kwargs	__class__r   r   r      s    zRouter.__init__c                    sH   t j js* jd j t  j t fddt  jD S )zScan migrations in file system.z'Migration directory: %s does not exist.c                 3   s,   | ]$} j |rd |dd V  qdS )r$   N)filemaskmatchr7   )r(   fr   r   r   r*      s     zRouter.todo.<locals>.<genexpr>)	osr@   existsr\   r   r6   makedirssortedlistdirr   r   r   r   r!      s    
zRouter.todor$   c              	   C   sb   t | j}d|d }|| d }tj| j|}t|d}|t	j||d W 5 Q R X |S )r#   z{:03}_   .pyw)r>   r?   )
lenr!   formatrd   r@   r7   r\   openwriteMIGRATE_TEMPLATE)r   r=   r>   r?   Znumprefixr@   rc   r   r   r   r<      s    
zRouter._createc              
   C   s^   t tj| j|d <}| }i }t|| |dt|dtfW  5 Q R  S Q R X dS )zRead migration from file.rj   r>   r?   N)	rn   rd   r@   r7   r\   rB   r
   getVOID)r   r=   rc   codeZscoper   r   r   rB      s
    
zRouter.read)r$   r$   )rU   rV   rW   recompilera   MIGRATE_DIRr   rY   r!   r<   rB   __classcell__r   r   r^   r   rZ      s   


rZ   c                       s&   e Zd Zd fdd	Zdd Z  ZS )ModuleRouterr   c                    s0   t t| j|f| t|tr&t|}|| _d S r   )r[   ry   r   r   r	   r   migrate_module)r   r   rz   r]   r^   r   r   r      s    
zModuleRouter.__init__c                 C   s$   t | j|}t |dtt |dtfS )Nr>   r?   )r'   rz   rs   )r   r=   modr   r   r   rB      s    zModuleRouter.read)r   )rU   rV   rW   r   rB   rx   r   r   r^   r   ry      s   ry   )(rd   ru   	importlibr   typesr   rJ   Zpeeweer   r   Zpeewee_migrater   r   Zpeewee_migrate.autor   r   Zpeewee_migrate.compatr	   r
   Zpeewee_migrate.migratorr   rv   Mr9   r@   r7   getcwdrw   rs   rn   abspathdirname__file__trB   rp   objectr   rZ   ry   r   r   r   r   <module>   s&   $ %