Viewing File: /opt/alt/python35/lib/python3.5/site-packages/peewee_migrate/__pycache__/migrator.cpython-35.pyc



/Xp*@sddlZddlmZmZmZm	Z
mZmZm
Z
mZmZmZmZmZddlmZGdddeZGdddeeZGdd	d	eeZGd
ddee
Z	dd
ZGdddeZdS)N)
MySQLMigratorPostgresqlMigratorSchemaMigratorSqliteMigrator	OperationSQLEntityClausePostgresqlDatabase	operationSqliteDatabase
MySQLDatabase)LOGGERcseZdZdZefddZdddZeddZd	d
Z	eddZ
ed
dZS)rzImplement migrations.csat|trt|St|tr2t|St|trKt|Stt|j	|S)zInitialize migrator by db.)

isinstancer
rrrr
rsuperr
from_database)clsdatabase)	__class__/migrator.pyrs


zSchemaMigrator.from_databaseTcsfddS)NcsjdS)Ncascade)
drop_tabler)rmodelrr<lambda>sz+SchemaMigrator.drop_table.<locals>.<lambda>r)selfrrr)rrrrszSchemaMigrator.drop_tablecCsA|j|||g}|js=|j|j||g|S)zChange column.)alter_change_columnnullextendadd_not_null)rtablecolumn_namefieldZ
operationsrrr
change_columns	zSchemaMigrator.change_columncCsY|jd}|_|jjj|}||_ttdt|td|S)zSupport change columns.TzALTER TABLEzALTER COLUMN)rrcompilerfield_definitionr	rr)rr columnr"
field_nullfield_clauserrrr's	z"SchemaMigrator.alter_change_columncGstt||S)zExecute raw SQL.)r	r)rsqlparamsrrrr).szSchemaMigrator.sqlcCs|jd}|_||_|jjj|}||_tdt|td|g}t|tj	r|j
|j|n	||_t
|S)zKeep fieldname unchanged.TzALTER TABLEz
ADD COLUMN)r	db_columnrr$r%rrrpwForeignKeyFieldrZget_inline_fk_sqlnamer	)rr r!r"r'r(partsrrralter_add_column3s							zSchemaMigrator.alter_add_column)__name__
__module____qualname____doc__classmethodrrrr#rr)r0rr)rrr
srcs(eZdZdZfddZS)rz Support the migrations in MySQL.csHtt|j|||}|jd}|jjdtd|S)zSupport change columns.TYPE)rrrnodesinsertr)rr r!r"clauser()rrrrKs
z!MySQLMigrator.alter_change_column)r1r2r3r4rrr)rrrGsrcs(eZdZdZfddZS)rz%Support the migrations in postgresql.csHtt|j|||}|jd}|jjdtd|S)zSupport change columns.r6r7r8)rrrr9r:r)rr r!r"r;r()rrrrWs
z&PostgresqlMigrator.alter_change_column)r1r2r3r4rrr)rrrSsrc@s1eZdZdZdddZddZdS)rz!Support the migrations in sqlite.TcsfddS)z0SQLite doesnt support cascade syntax by default.csjddS)NrF)rr)rrrresz+SqliteMigrator.drop_table.<locals>.<lambda>r)rrrr)rrrcszSqliteMigrator.drop_tablecs(fdd}j|||S)zSupport change columns.cs7jj}|j}|j|\}}|S)N)rr$r%Z
parse_node)r!Z
column_defr$r;r)_)r"rrr_changeisz3SqliteMigrator.alter_change_column.<locals>._change)Z_update_column)rr r&r"r=r)r"rrrgsz"SqliteMigrator.alter_change_columnN)r1r2r3r4rrrrrrr_srcsfdd}|S)zConvert string to model class.cs<t|tr)||j|||S||||S)N)rstrorm)migratorrargskwargs)methodrrwrappersszget_model.<locals>.wrapperr)rCrDr)rCr	get_modelqsrEc@sWeZdZdZddZddZddZdd	Zd
dZdd
Z	e	Z
edddZeZ
eddZeZeddZeZeddZeZddZeddZeZeddZeddZedd Zed!d"Zed#d$Zed%d&Zd'S)(MigratorzProvide migrations.cCsUt|tjr|j}||_t|_t|_t	j
|j|_dS)zInitialize the migrator.N)rr,ZProxyobjrdictr?listopsrrr@)rrrrr__init__~s		zMigrator.__init__cCs^xM|jD]B}t|trEtjd|j|j|jq
|q
W|jdS)zRun operations.z%s %sN)	rJrrrinforCrArunclean)rZopnrrrrMs
zMigrator.runcs&|jjfdddS)zRun python code.cs
S)Nrr)rAfuncrBrrrsz!Migrator.python.<locals>.<lambda>N)rJappend)rrOrArBr)rArOrBrpythonszMigrator.pythoncGs#|jj|jj||dS)zExecure raw SQL.N)rJrPr@r))rr)r*rrrr)szMigrator.sqlcCst|_dS)zClean the operations.N)rIrJ)rrrrrNszMigrator.cleancCs9||j|jj<|j|j_|jj|j|S)zUCreate model and table in database.

        >> migrator.create_table(model)
        )r?_metadb_tablerrJrPcreate_table)rrrrrrTszMigrator.create_tableTcCs3|j|jj=|jj|jj||dS)zaDrop model and table from database.

        >> migrator.drop_table(model, cascade=True)
        N)r?rRrSrJrPr@r)rrrrrrrszMigrator.drop_tablecKsx|jD]\}}|j|||jj|jj|jj|j||j	r
|jj|jj
|jj|jfddq
W|S)zCreate new fields.uniqueT)itemsadd_to_classrJrPr@Z
add_columnrRrSr+rU	add_index)rrfieldsr.r"rrradd_columnss+	#zMigrator.add_columnscKsx|jD]\}}|j|||jj|jj|jj|j||j	r
|jj|jj
|jj|jfddq
W|S)zChange fields.rUT)rVrWrJrPr@r#rRrSr+rUrX)rrrYr.r"rrrchange_columnss	#zMigrator.change_columnsc	sfdd|jjjD}|jdd}x|D]}|j|||jr|jj}|j|jj	|j
f}|jj|j
j|jj	||jj|j
j|jj	|j
d|q>W|S)zRemove fields from model.cs%g|]}|jkr|qSr)r.).0r")namesrr
<listcomp>s	z)Migrator.drop_columns.<locals>.<listcomp>rT)rRrYvaluespop
__del_field__rUrr$
index_namerSr+rJrPr@
drop_indexZdrop_column)	rrr]rBrYrr"r$rbr)r]rdrop_columnss%
	%	)zMigrator.drop_columnscCs_|jj|jt||jt|tjr[t|j|j|jjj	|j=dS)zDelete field from model.N)
rRZremove_fieldr.delattrrr,r-Z	rel_modelZrelated_nameZreverse_rel)rrr"rrrras
zMigrator.__del_field__cCs|jj|}t|tjr+|j}|j||||_|_|j||t|tjr|jd|_}|j	j
|jj|jj
|||S)zRename field in model._id)rRrYrr,r-r+rar.rWrJrPr@
rename_columnrS)rrZold_namenew_namer"rrrrgs	(zMigrator.rename_columncCsX|j|jj=||j_||j|jj<|jj|jj|jj||S)zRename table in database.)r?rRrSrJrPr@rename_table)rrrhrrrris
%zMigrator.rename_tablecOs|jdd}|jjj||fg}xL|D]D}|jjj|}t|tjro|d}|j|q8W|j	j|j
j|jj|d||S)zCreate indexes.rUFrf)
r`rRindexesrPrYgetrr,r-rJr@rXrS)rrcolumnsrBrUcolumns_colr"rrrrXs

+zMigrator.add_indexcsg}xLD]D}|jjj|}t|tjrD|d}|j|q
W|jjj	j
|jj|}fdd|jjD|j_|j
j|jj|jj||S)z
Drop indexes.rfcs.g|]$\}}|kr||fqSrr)r\Zcolsr<)rlrrr^s	z'Migrator.drop_index.<locals>.<listcomp>)rRrYrkrr,r-rPr@rr$rbrSrjrJrc)rrrlrmrnr"rbr)rlrrcs

$%%zMigrator.drop_indexcGsVxO|D]G}|jj|}d|_|jj|jj|jj|jqW|S)z
Add not null.F)	rRrYrrJrPr@rrSr+)rrr]r.r"rrrrs

	,zMigrator.add_not_nullcGsVxO|D]G}|jj|}d|_|jj|jj|jj|jqW|S)zDrop not null.T)	rRrYrrJrPr@
drop_not_nullrSr+)rrr]r.r"rrrro's

	,zMigrator.drop_not_nullcCsS|jj|}||jj|<|_|jj|jj|jj|||S)zAdd default.)	rRrYZdefaultsdefaultrJrPr@Z
apply_defaultrS)rrr.rpr"rrradd_default0s(zMigrator.add_defaultN)r1r2r3r4rKrMrQr)rNrTZcreate_modelrErZremove_modelrZZ
add_fieldsr[Z
change_fieldsrdZ
remove_fieldsrargZrename_fieldrirXrcrrorqrrrrrFzs4


			rF)Zpeeweer,Zplayhouse.migraterZMqMrZPgMrZScMrZSqMrrrr	r
rrr
Zpeewee_migraterrEobjectrFrrrr<module>sR:	
Back to Directory File Manager