mfu=ddZddlmZddlmZmZmZmZmZm Z m Z ddl Z ddl mZmZmZmZmZmZmZmZddl mZddl mZddl mZdd l mZd d lmZerd d l m!Z!m"Z"Gd deZGddeeZGddeeZGddeeZGddZ#GddZ$dS)zRun migrations.) annotations) TYPE_CHECKINGAnyCallableListUnioncastoverloadN)SQLContext MySQLDatabase OperationPostgresqlDatabaseSqliteDatabasemake_index_name operation) MySQLMigrator)PostgresqlMigrator)SchemaMigrator)SqliteMigrator)logger) TModelType TVModelTypeceZdZdZeddZdddd ZeddZd dZ ed!dZ d"fd Z xZ S)#rzImplement migrations.databaseUnion[pw.Database, pw.Proxy]returnct|trt|St|trt |St|t rt |Std|z)zInitialize migrator by db.zUnsupported database: %s) isinstancerrrrr r ValueError)clsrs N/opt/imunify360/venv/lib64/python3.11/site-packages/peewee_migrate/migrator.py from_databasezSchemaMigrator.from_database(sy h 2 3 3 0%h// / h / / ,!(++ + h . . + ** *3h>???Tcascademodelrr'boolCallable[[], Any]cfdS)z Drop table.c0S)Nr& drop_table)r'r(sr#z+SchemaMigrator.drop_table..8su'''88r%selfr(r's ``r#r.zSchemaMigrator.drop_table6s888888r%tablestr column_namefieldpw.FieldList[Union[Context, Operation]]c||||g}|js*||||g|S)zChange column.)alter_change_columnnullextend add_not_null)r2r3r5r6 operationss r# change_columnzSchemaMigrator.change_column:sZ  $ $UK ? ?7 z G   t00 DDE F F Fr%columnr c|}|jdc}|_|||d||}||_|S)Support change columns.Tz ALTER COLUMN  make_contextr; _alter_tableliteralsqlddlr2r3r@r6ctx field_nulls r#r:z"SchemaMigrator.alter_change_columnFsl!!!&T EJU++334DEEII%))TW..YY  r%rGr ct|g|RS)Execute raw SQL.)r )r2rGparamss r#rGzSchemaMigrator.sqlNs3     r%rc |j}tt|j|||fi|}t |t jr||_|S)zFix fieldname for ForeignKeys.)namesuperralter_add_columnr pwForeignKeyField)r2r3r5r6kwargsrPop __class__s r#rRzSchemaMigrator.alter_add_columnSsVz 9U>4 ( ( 9%e ^ ^W] ^ ^ eR/ 0 0 EJ r%)rrrr)r(rr'r)rr*)r3r4r5r4r6r7rr8r3r4r@r4r6r7rr )rGr4rr )r3r4r5r4r6r7rr) __name__ __module__ __qualname____doc__ classmethodr$r.rr?r:rGrR __classcell__rWs@r#rr%s @ @ @[ @@D999999   Y !!!Y!r%rceZdZdZd d Zd S) rzSupport MySQL.r3r4r@r6r7rr c|}|jdc}|_|||d||}||_|S)rBTz MODIFY COLUMN rCrIs r#r:z!MySQLMigrator.alter_change_columnasl!!!&T EJU++334EFFJJ599UX>>ZZ  r%NrX)rYrZr[r\r:r0r%r#rr^s.r%rc$eZdZdZd fd ZxZS) rz%Support the migrations in postgresql.r3r4r5r6r7rr ctt||||}|jdd|jdd|S)rBTYPE )rQrr:_sqlinsert)r2r3r5r6contextrWs r#r:z&PostgresqlMigrator.alter_change_columnms[*D11EEe[Z_`` B''' B$$$r%)r3r4r5r4r6r7rr )rYrZr[r\r:r^r_s@r#rrjsC//r%rc(eZdZdZdddd ZddZdS)rz!Support the migrations in sqlite.Tr&r(pw.Modelr'r)rrcfdS)z0Sqlite doesnt support cascade syntax by default.c0dS)NFr&r-)r(sr#r/z+SqliteMigrator.drop_table..zsu'''66r%r0r1s ` r#r.zSqliteMigrator.drop_tablexs66666r%r3r4r@r6r7rc@fd}|||S)rBc}|||dS)Nr)rDrGrHquery)c_namec_defrJr6r2s r#fnz.SqliteMigrator.alter_change_column..fnsB##%%C GGEIIcNN # # #99;;q> !r%)_update_column)r2r3r@r6rss` ` r#r:z"SqliteMigrator.alter_change_column|s< " " " " " " ""5&"555r%N)r(rkr'r)rr)r3r4r@r4r6r7rr)rYrZr[r\r.r:r0r%r#rrusO++=A777777666666r%rc>eZdZdZdZddZddZdd Zdd Zd Z d S)ORM __tables__ __models__c"i|_i|_dSNrwr2s r#__init__z ORM.__init__sr%r(rcL||j|j<||j|jj<dSr{ryrYrx_meta table_namer2r(s r#addzORM.adds'*/'27 .///r%cD|j|j=|j|jj=dSr{rrs r#removez ORM.removes# OEN + OEK2 3 3 3r%rPr4rc|j|Sr{)ryr2rPs r# __getattr__zORM.__getattr__t$$r%c|j|Sr{)rxrs r# __getitem__zORM.__getitem__rr%cNt|jSr{)iterryvaluesr|s r#__iter__z ORM.__iter__sDO**,,---r%N)r(r)rPr4rr) rYrZr[ __slots__r}rrrrrr0r%r#rvrvs,I88884444%%%%%%%%.....r%rvceZdZdZd5dZdZdZed6d Zed7d Zd8dZd9dZ d:dZ d6dZ e Z ddd;dZ e Zdd$Zd?d'ZeZd@d(Zd)d*dAd,ZdAd-ZdBd.ZdBd/ZdCd3Zd4S)DMigratorzProvide migrations.rrct|_t|tjr|j}||_g|_t ||_ dS)zInitialize the migrator.N) rvormr rSProxyobj __database____ops__rr$ __migrator__)r2rs r#r}zMigrator.__init__sS h ) ) $|H$9; *88BBr%c|jD]p}t|tr5tjd|j|j|Ltjd|j|qg|_dS)zRun operations.z%s %szRun %sN) rr rrinfomethodargsrunrY)r2rVs r#__call__zMigrator.__call__sx,  B"i((  GRY888 Hbk222 r%c*t|jS)zIterate over models.)rrr|s r#rzMigrator.__iter__sDH~~r%r(rrcdSr{r0rs r# __get_model__zMigrator.__get_model__ r%r4rcdSr{r0rs r#rzMigrator.__get_model__rr%Union[TVModelType, str]Union[TVModelType, TModelType]ct|trM||jjvr|jj|S||jjvr |j|St d|z|S)zGet model by name.zModel %s not found)r r4rryrxr!rs r#rzMigrator.__get_model__si eS ! ! ;+++x*511+++x&1E9:: : r%rGctt|jj|g|R}|j|dS)rMN)r rrrGrappend)r2rGrNrVs r#rGz Migrator.sqlsF )2T.23@@@@ A A Br%funcrcJ|jfddS)zRun a python function.ciSr{r0)rrrUsr#r/z!Migrator.python..sDD$$9&$9$9r%N)rr)r2rrrUs ```r#pythonzMigrator.pythons1 999999:::::r%c|j}|j||j|_|j|j|S)zUCreate model and table in database. >> migrator.create_table(model) )rrrrrrr create_table)r2r(metas r#rzMigrator.create_tablesG {  U)  E./// r%Tr&Union[str, TModelType]r'r)c||}|j||j|j||dS)zaDrop model and table from database. >> migrator.drop_table(model, cascade=True) r&N)rrrrrrr.r1s r#r.zMigrator.drop_tables] ""5))  D-888PPQQQQQr%fieldsr7c ||}|j}|D]\}}||||j|j|j|j ||j r@|j|j |j|j fd|S)zCreate new fields.Tunique) rritems add_fieldrrr add_columnrr5r add_index)r2r(rrrPr6s r# add_columnszMigrator.add_columnss""5)){!<<>>  KD% NN4 ' ' ' L  !,,OU%6    |  ##%//%BSAU^b/cc r%c |||}|j}|D]\}}|j||}|o|j}|||t|tj r8|j |j |j|||jkr>|j |j |j||jt|tj r||jr|jnd}|jr|jnd} |j |j |j|j|jjj|jj|| q|j |j |j|j||j|jkr|jr[|jf|jf} |j |j j|jg| R|j | #|j} |j |j |j| |j|jf|jf|S)zChange fields.RESTRICT)rrrrgetr5rr rSrTrrrdrop_foreign_key_constraintr rename_column on_delete on_updateadd_foreign_key_constraint rel_model rel_fieldrPr?rrindexes drop_indexr) r2r(rrrPr6 old_fieldold_column_namerrindexs r#change_columnszMigrator.change_columnss""5)){!<<>>1 N1 NKD% e44I'AI,AO NN4 ' ' ')R%788  ##%AA$/Sbcc%"333 ##%33%:K %!344 /4NEOOJ /4NEOOJ  ##%@@)-8,!!     L  !//OU%6    |y///| N*,el: ##$?D$5$?$XRW$X$X$XYYY ##E****) ##D$5$@$@RW$X$XYYY ##e&7%99;K$LMMMM r%namesc||}|j}fd|jD}|D]}||||jrSt |j|jg}|j |j |j||j |j |j|j||S)zRemove fields from model.c&g|] }|jv |Sr0)rP).0r6rs r# z)Migrator.drop_columns..Gs%QQQEUZ5=P=P%=P=P=Pr%r&)rrrr __del_field__rrrr5rrrr drop_column)r2r(r'rrrr6 index_names ` r# drop_columnszMigrator.drop_columnsAs""5)){QQQQT[%7%7%9%9QQQ  E   ue , , ,| _,T_u?P>QRR  ##D$5$@$@R\$]$]^^^ L  !--OU%6.      r%r6cV|j}||jt||jt |t jrX|j}|j|jkr|dz }t||rt||t|j |j dSdS)zDelete field from model._idN) r remove_fieldrPdelattrr rSrTr5hasattrrbackref)r2r(r6r obj_id_names r#rzMigrator.__del_field__Vs{ %*%%%uz""" eR/ 0 0 4+K EJ..u$ uk** ,{+++ EOU] 3 3 3 3 3  4 4r%old_namenew_namec||}|j}|j|}t|tjr|j}||||x|_|_t|tjr|jdz|_| |||j |j |j||j|S)zRename field in model.r)rrrr rSrTr5rrPrrrrrr)r2r(rrrr6s r#rzMigrator.rename_columncs""5)){ H% eR/ 0 0 )(H 5%((()11 U& eR/ 0 0 : % 1E 9E  x'''    + +DOXuGX Y Y    r%c(||}|j}|j}|j|||_|j||j|j |||S)zRename table in database.) rrrrrrrrr rename_table)r2r(rrrs r#rzMigrator.rename_tablexs""5)){? "  U D-::8XNNOOO r%Frcolumnsc||}|j}|j||fg}|D]X}|j|}t |dkr||_| |_||j Y|j |j |j |||S)zCreate indexes.rr)rrrrrrlenrrr5rrrr)r2r(rrrcolumns_colr6s r#rzMigrator.add_indexs""5)){ Wf-... / /CKOOC((E7||q  % "(j OOE- . . . . D-77Z`7aabbb r%c||}|j}g}D]Z}|j|}|st dkrdx|_|_||j[t|j |}fd|j D|_ |j |j |j ||S)z Drop indexes.rFc*g|]\}}|k ||fSr0r0)rcols_rs r#rz'Migrator.drop_index..s&SSSitQ7d??q ???r%)rrrrrrrrr5rrrrrr)r2r(rrrrr6rs ` r#rzMigrator.drop_indexs""5)){ / /CKOOC((E 7||q  -22 u{ OOE- . . . .$T_h?? SSSS4<SSS  D-88*UUVVV r%c||}|j}|D]S}|j|}d|_|j|j|j|j T|S)z Add not null.F) rrrr;rrrr=rr5r2r(rrrPr6s r#r=zMigrator.add_not_nullsw""5)){ d dDK%EEJ L   1 > >tPUPa b b c c c c r%c||}|j}|D]S}|j|}d|_|j|j|j|j T|S)zDrop not null.T) rrrr;rrr drop_not_nullrr5rs r#rzMigrator.drop_not_nullsw""5)){ e eDK%EEJ L   1 ? ?QVQb c c d d d d r%rPdefaultrc||}|j}|j|}|x|j|<|_|j|j|j |||S)z Add default.) rrrdefaultsrrrr apply_defaultr)r2r(rPrrr6s r# add_defaultzMigrator.add_defaultsn""5)){ D!/66 eu} D-;;DOTSXYYZZZ r%N)rr)r(rrr)r(r4rr)r(rrr)rGr4)rr)r(rr'r))r(rrr7rr)r(rrr4r'r)rr)r(rr6r7)r(rrr4rr4rr)r(rrr4rr)r(rrr4rr)r(rrr4rr)r(rrPr4rrrr)rYrZr[r\r}rrr rrGrr create_modelr. remove_modelr add_fieldsr change_fieldsr remove_fieldsrr rename_fieldrrrr=rrr0r%r#rrs0 C C C C      X    X         ;;;;     LKORRRRRRL"J7777r#MKO&!M 4 4 4 4&!L    NS$(r%r)%r\ __future__rtypingrrrrrr r peeweerSplayhouse.migrater r r rrrrrrMqMrPgMrScMrSqMlogsrtypesrrrvrr0r%r#rsx""""""                    322222777777333333333333/........66666S666r     NC   66666^S666$........2ddddddddddr%