Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/peewee_migrate/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/peewee_migrate/__pycache__/router.cpython-35.pyc



���]�@s}ddlZddlZddlmZddlmZddlZddlZddl	m	Z	ddl
mZmZddl
mZmZddlmZmZddlmZejd	ej�Zejjej�d
�Zdd�Zeejjejjejje ��d
���Z!e!j"�Z#WdQRXGdd�de$�Z%Gdd�de%�Z&Gdd�de%�Z'dS)�N)�
import_module)�
ModuleType)�cached_property)�LOGGER�MigrateHistory)�	diff_many�NEWLINE)�string_types�exec_in)�Migratorz\s+$�
migrationscCsdS)N�)�m�dr
r
�F/opt/alt/python35/lib/python3.5/site-packages/peewee_migrate/router.py�<lambda>srztemplate.txtc@s�eZdZdZdedd�Zedd��Zedd��Z	d	d
dd�Z
d
d
dd�Zdd�Zedd��Z
edd��Zedd��Zdd
d
dd�Zdd
dd�Zdd�ZdS) �
BaseRouterzAbstract base class for router.ZmigratehistorycCsM||_||_||_t|jtjtjf�sItd|��dS)NzInvalid database: %s)�database�
migrate_table�logger�
isinstance�pwZDatabaseZProxy�RuntimeError)�selfrrrr
r
r�__init__s
			zBaseRouter.__init__cCs/|jtj_|jtj_tjd�tS)z+Ensure that migrations has prepared to run.T)rr�_metarZdb_tableZcreate_table)rr
r
r�model"s
zBaseRouter.modelcCs
t�dS)N)�NotImplementedError)rr
r
r�todo+szBaseRouter.todo�autoFc	s�d}}�r�t�t�rWyt���Wn%tk
rV|jjd��SYnXt�t�r�ttdd��fdd�t	��D����x$|j
D]}|j||j�q�W�}t|jj
j��}t||d|j�}|s
|jjd�Sttjd	j|�jd
��}tjd
|�}t||d|jdd�}ttjd	j|�jd
��}tjd
|�}|jjd
|�|j|||�}|jjd|�|S)zCreate a migration.�zCan't import models module: %scSst|t�ot|tj�S)N)r�type�
issubclassrZModel)rr
r
rr;sz#BaseRouter.create.<locals>.<lambda>c3s|]}t�|�VqdS)N)�getattr)�.0r)rr
r�	<genexpr><sz$BaseRouter.create.<locals>.<genexpr>�migratorzNo changes found.z

�
�reverseTzCreating migration "%s"zMigration created %s)rr	r�ImportErrorr�errorr�list�filter�dir�diff�run_oner&Zorm�valuesr�warnr�join�split�CLEAN_RE�sub�info�_create)	r�namer�migrate�rollbackZ	migrationZmodels1Zmodels2�pathr
)rr�create/s6

	%%%zBaseRouter.creater cCs
t�dS)N)r)rr8r9r:r
r
rr7TszBaseRouter._createcCs
t�dS)N)r)rr8r
r
r�readWszBaseRouter.readcCsdd�|jj�D�S)zScan migrations in database.cSsg|]}|j�qSr
)r8)r$�mmr
r
r�
<listcomp>]s	z#BaseRouter.done.<locals>.<listcomp>)r�select)rr
r
r�doneZszBaseRouter.donecs)t|j���fdd�|jD�S)z'Calculate difference between fs and db.cs"g|]}|�kr|�qSr
r
)r$r8)rAr
rr?cs	z#BaseRouter.diff.<locals>.<listcomp>)�setrAr)rr
)rArr._szBaseRouter.diffcCs7t|j�}x!|jD]}|j||�qW|S)z2Create migrator and setup it with fake migrations.)rrrAr/)rr&r8r
r
rr&eszBaseRouter.migratorTc	 Cs�y�|j|�\}}|r�tjd��2tjd��|||jd|�WdQRXWdQRX|r�|jjd|�|jjd|�|j�|S|jjd|�|jj	���|s|||jd|�|j
�|jjd|�|jjd|�nn|jjd|�|||jd|�|j
�|jj�j|jj
|k�j�|jjd	|�WdQRXWnHtk
r�}z(|jj�|jjd
|��WYdd}~XnXdS)zRun a migration.zpeewee.Model.selectzpeewee.Query._execute�fakeNr8zDone %szRunning "%s"zRolling back %szRolled back %szMigration failed: %s)r=�mock�patchrrr<rr6�cleanZtransaction�run�delete�wherer8�execute�	Exceptionr:�	exception)	rr8r&rCZ	downgrade�forcer9r:�excr
r
rr/ms6$


(
zBaseRouter.run_oneNcCs�|jjd�g}|j}|s9|jjd�|S|j}xJ|D]B}|j||d|d|�|j|�|rI||krIPqIW|S)zRun migrations.zStarting migrationszThere is nothing to migraterCrM)rr6r.r&r/�append)rr8rCrAr.r&Zmnamer
r
rrG�s		

zBaseRouter.runcCsy|j�}|j}|s'td��||dkrCtd��|j}|j||dd�|jjd|�dS)NzNo migrations are found.�z$Only last migration can be canceled.FTzDowngraded migration: %s���)�striprArr&r/rr1)rr8rAr&r
r
rr:�s		zBaseRouter.rollback)�__name__�
__module__�__qualname__�__doc__rrrr�propertyrr<r7r=rAr.r&r/rGr:r
r
r
rrs	%$rcsdeZdZejd�Ze�fdd�Zedd��Z	dddd�Z
d	d
�Z�S)�Routerz[\d]{3}_[^\.]+\.py$cs&tt|�j||�||_dS)N)�superrXr�migrate_dir)rrrZ�kwargs)�	__class__r
rr�szRouter.__init__csdtjj�j�s;�jjd�j�tj�j�t�fdd�tj�j�D��S)zScan migrations in file system.z'Migration directory: %s does not exist.c3s:|]0}�jj|�rdj|dd��VqdS)r N����)�filemask�matchr2)r$�f)rr
rr%�szRouter.todo.<locals>.<genexpr>)	�osr;�existsrZrr1�makedirs�sorted�listdir)rr
)rrr�s
zRouter.todor c
Cs�t|j�}dj|d�}||d}tjj|j|�}t|d��&}|jt	jd|d|��WdQRX|S)zCreate a migration.z{:03}_rPz.py�wr9r:N)
�lenr�formatrbr;r2rZ�open�write�MIGRATE_TEMPLATE)rr8r9r:�num�prefixr;rar
r
rr7�s&zRouter._createcCsqttjj|j|d���H}|j�}i}t||�|jdt�|jdt�fSWdQRXdS)zRead migration from file.z.pyr9r:N)	rjrbr;r2rZr=r
�get�VOID)rr8ra�codeZscoper
r
rr=�s
%
zRouter.read)rSrTrU�re�compiler_�MIGRATE_DIRrrWrr7r=r
r
)r\rrX�s
	rXcs1eZdZd�fdd�Zdd�Z�S)�ModuleRouterrcsAtt|�j||�t|t�r4t|�}||_dS)N)rYrurrr	r�migrate_module)rrrvr[)r\r
rr�szModuleRouter.__init__cCs4t|j|�}t|dt�t|dt�fS)Nr9r:)r#rvrp)rr8�modr
r
rr=�szModuleRouter.read)rSrTrUrr=r
r
)r\rru�sru)(rbrr�	importlibr�typesrrDZpeeweerr�peewee_migraterrZpeewee_migrate.autorrZpeewee_migrate.compatr	r
Zpeewee_migrate.migratorrrs�Mr4r;r2�getcwdrtrprj�abspath�dirname�__file__�tr=rl�objectrrXrur
r
r
r�<module>s$6�%