mf/UdZddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlmZmZmZmZmZmZmZmZmZdd lmZddlZd d lmZmZd d l m!Z!d d l"m#Z#d dl$m%Z%m&Z&d dl'm(Z(erddl)m*Z*ej+dej,Z-de.d<e j/Z0de.d<e0dz Z1de.d<d+dZ2Gdde3Z4Gdde4Z5Gdde4Z6d,d"Z7d-d$Z8d%Z9d&d'd.d*Z:dS)/zMigration router.) annotationsN)cached_property) import_module)Path) ModuleType) TYPE_CHECKINGAnyFinalIterableListOptionalSetTypeUnion)mock)NEWLINE diff_many)logger)Migrator) MIGRATE_TABLEMigrateHistory)TEMPLATE)Loggerz\s+$r CLEAN_RECURDIR migrationsDEFAULT_MIGRATE_DIRcdSN)mdfakes L/opt/imunify360/venv/lib64/python3.11/site-packages/peewee_migrate/router.pyvoidr& s 4ceZdZdZeddefd2d Zed3dZe d4dZ e d5dZ e d5dZ ed6dZ dddd7dZd8d9dZdZ d:d;d%Zd9d&Zd'ddd(dd0Zd1ZdS)? BaseRouterzAbstract base class for router.NdatabaseUnion[pw.Database, pw.Proxy]ignoreOptional[Iterable[str]]schema Optional[str]rrc||_||_||_||_||_t |jt jt jfstd|zdS)Initialize the router.zInvalid database: %sN) r* migrate_tabler.r,r isinstancepwDatabaseProxy TypeError)selfr*r2r,r.rs r%__init__zBaseRouter.__init__'sc! *   $-"+rx)@AA ?2X=>> > ? ?r'returnType[MigrateHistory]ctj}|j|_|j|_|j|_tjdtS)z,Initialize and cache MigrationHistory model.T)safe)r_metar*r2 table_namer. create_table)r8metas r%modelzBaseRouter.model8sB#  ,k #....r' Iterable[str]ct)zGet migrations to run.NotImplementedErrorr8s r%todozBaseRouter.todoBs "!r' List[str]cd|j|jjDS)zScan migrations in database.cg|] }|j Sr!name).0mms r% z#BaseRouter.done..JsNNNBNNNr')rBselectorder_byidrGs r%donezBaseRouter.doneGs9ON$*"3"3"5"5">">tz}"M"MNNNNr'cRt|jfd|jDS)z'Calculate difference between fs and db.cg|]}|v| Sr!r!)rNrMrTs r%rPz#BaseRouter.diff..Ps#???d$.>.>.>.>.>r')setrTrH)r8rTs @r%diffzBaseRouter.diffLs.49~~????????r'rcnt|j}|jD]}||||S)z2Create migrator and setup it with fake migrations.)rr*rTrun_one)r8migratorrMs r%r[zBaseRouter.migratorRs@DM**I ) )D LLx ( ( ( (r'autoF)r\rMstrr cXdx}}|rTtjtt |t r|n|g}t d|Dsv |}t |tr%dtj tgD}d|D}n,#t$rj d|YdSwxYwj rfd|D}jD]}|jd  t#j|}|sj d dSt#j|d }j d ||||}j d ||S)zYCreate a migration. :param auto: Python module path to scan for models. c34K|]}t|VdSr ) _check_model)rNr"s r% z$BaseRouter.create..ds(771|A777777r'cg|] \}}}|| Sr!r!)rN_r"ispkgs r%rPz%BaseRouter.create..hs4###"-!QX]####r'c6g|]}t|D]}|Sr!) load_models)rNmoduler"s r%rPz%BaseRouter.create..ks-SSSF{6?R?RSS!aSSSSr'zCan't import models module: %sNc:g|]}|jjjv|Sr!)r>rMr,)rNr"r8s r%rPz%BaseRouter.create..rs)OOOqw|4;/N/N!/N/N/Nr'Tr$zNo changes found.reversezCreating migration "%s"z"Migration has been created as "%s")syspathappendrr3listallboolpkgutil iter_modules ImportErrorr exceptionr,rXrZr[compile_migrationswarninginfocompile)r8rMr\migraterollbackmodelsmodules migrations` r%createzBaseRouter.createZs  (  O HOOM * * *'d33?TT$F7777777   $G!$--##181E&{m1T1T###TSgSSSFF"   K))*JDQQQ44 { POOOOVOOO!Y B B  Y D AAAA(??G  ##$7888t)$-NNNH 2D999||D'844 =tDDD s"AB++%CCinitialct|j}t|t|jj}|s|jdS||j d|t|jg}| |||d}t|j}| ||dd|j d|dS)zMerge migrations into one.zCan't merge migrationszMerge migrations into "%s"rTr$forcez$Migrations has been merged into "%s"N) rr*rwrpr[ormrerrorclearryrzrZ)r8rMr[r{r|s r%mergezBaseRouter.mergesDM**$XtDM4E/F/FGG ?;$$%=>> >  5t<<<%dmR88||D'8Q77DM** T8$d ;;; ?FFFtr'c\|jdS)zClear migrations.N)rBdeleteexecuterGs r%rzBaseRouter.clears( ##%%%%%r'r_r{r|num Optional[int]ct)Create a migration.rE)r8rMr{r|rs r%rzzBaseRouter.compiles "!r'ct)Read migration from file.rE)r8rMs r%readzBaseRouter.reads!!r'T)r$ downgraderr[r$rrrrc ||\}}|rtj}d|j_tjd5tjd|5|||j|dddn #1swxYwYdddn #1swxYwY|r6|j||j d|g|_ |S|j 5|sT|j d||||j|||j|n|j d ||||j|||j |jj|k|j d||cdddS#1swxYwYdS#t"$r=|j|sd nd } |j d | |wxYw) z(Run/emulate a migration with given name.Nzpeewee.Model.selectzpeewee.Database.execute_sql) return_valuerjrLzDone %sz Migrate "%s"zRolling back %s MigrationRollbackz %s failed: %s)rrMock fetch_onerpatchr*rBrrry__ops__ transactionrwhererMr Exceptionr|rv) r8rMr[r$rrr{r| mocked_cursor operations r%rZzBaseRouter.run_onesX$  $ $ GX  $ 7; '4Z 566@@ 1 999@@GHdm$????@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 6J%%4%000K$$Y555#%  **,,   QK$$^T:::GHdm$????HJJJJ%%4%0000K$$%6===HXt}4@@@@HJJJJ%%''--djo.EFFNNPPP   D111                      M " " $ $ $+4D *I K ! !/9d C C C   sA HB&B: BB B B B HB!!H$B!%AH)HC7H9 HH  H H HAIrjc  |jdg}|j}|s|jd|S|j}|D]8}|||||||r||krn9|S)zRun migrations.zStarting migrationszThere is nothing to migrater)rryrXr[rorZ)r8rMr$rTrXr[mnames r%runzBaseRouter.runs .///y  K  : ; ; ;K=  E KK UH4t LL M M M   r'c|j}|sd}t||d}|j}|||dd|jd|dS)zRollback the latest migration.zThere is nothing to rollbackFT)r$rzDowngraded migration: %sN)rT RuntimeErrorr[rZrrx)r8rTmsgrMr[s r%r|zBaseRouter.rollbacksmy $0Cs## #Bx= T8%4 @@@ 6=====r')r*r+r,r-r.r/rr)r:r;)r:rC)r:rI)r:r)rMr]r\r r:r/)r)rMr]r_r_N) rMr]r{r]r|r]rrr:r]) rMr]r[rr$rrrrrrrrr:r]r )rMr/r$rrr:rI)__name__ __module__ __qualname____doc__rrr9rrBpropertyrHrTrXr[rrrrzrrZrr|r!r'r%r)r)$s)) $*. $ ?????"_"""X"OOOXO@@@X@ _'''''''R$&&&! """""""""......`e$ > > > > >r'r)cxeZdZdZejdZ ddfd ZedZ ddd Zd Z fd Z xZ S)RouterzFile system router.z[\d]{3}_[^\.]+\.py$N migrate_dirOptional[Union[str, Path]]c tt|j|fi||t}n$t |t rt |}||_dSr1N)superrr9rr3r]rr)r8r*rkwargs __class__s r%r9zRouter.__init__sd %fd$X88888  -KK  S ) ) ,{++K&r'cjs;jdjjdt fdt jjDS)zScan migrations in file system.z'Migration directory: %s does not exist.T)parentsc3`K|](}j||ddV)dS)N)filemaskmatch)rNfr8s r%rbzRouter.todo..sA]]dmFYFYZ[F\F\]af]]]]]]r')rexistsrrxmkdirsortedoslistdirrGs`r%rHz Router.todo s&&(( 1 K   I4K[ \ \ \   " "4 " 0 0 0]]]]bj1A&B&B]]]]]]r'r_r:r]c4|t|j}d|dz|z}|dz}|j|z }|d5}|t j|||dddn #1swxYwY|S)rNz{:03}_r.pyw)r{r|rM)lenrHformatropenwriter)r8rMr{r|rfilenamernrs r%rzzRouter.compiles ;di..CsQw''$.%<(* YYs^^ Xq GGHOGhXVVV W W W X X X X X X X X X X X X X X X s+B  BBcZ|j|dzz }|d5}|}i}t|ddd}t ||d|dt |d t fcdddS#1swxYwYdS) rrrzexecT) dont_inheritNr{r|)rrrrzrgetr&)r8rMrnrcodescopes r%rz Router.reads4%<0 YYs^^ Kq6688DE4V$GGGD ud # # #99Y--uyyT/J/JJ  K K K K K K K K K K K K K K K K K KsA0B  B$'B$ctt||jD]#}|j|dzz }|$dS)zRemove migrations from fs.rN)rrrrHrunlink)r8rMrnrs r%rz Router.clear(sZ fd!!###I  D#te|4D KKMMMM  r'r )rrr)r:r]) rrrrrerzrr9rrHrr __classcell__rs@r%rrsrz011H 37 ' ' ' ' ' ' '^^X^     KKKr'rc*eZdZdZdfd ZdZxZS) ModuleRouterzModule based router.rc tt|j|fi|t|trt |}||_dSr)rrr9r3r]rmigrate_module)r8r*rrrs r%r9zModuleRouter.__init__3sU*lD!!*8>>v>>> nc * * ;*>::N,r'ct|j|}t|dtt|dtfS)zRead migrations from a module.r{r|)getattrrr&)r8rMmods r%rzModuleRouter.read<s7d)400sIt,,gc:t.L.LLLr')r)rrrrr9rrrs@r%rr0sY------MMMMMMMr'rrhUnion[str, ModuleType]r:Set[Type[pw.Model]]cht|tr|gnt|}d|DS)zLoad models from given module.c vh|]5ttfdtDD]}|6S)c38K|]}t|VdSr )r)rNrMrhs r%rbz(load_models...Is-&U&Uwvt'<'<&U&U&U&U&U&Ur')filterradir)rNr"rhs @r% zload_models..Fse     &U&U&U&UV&U&U&UVV        r')r3r_import_submodules)rhr~s r%rgrgBsI%VZ88Xvhh>PQW>X>XG     r'.c|durt}t|trt|}t |ds|hSg}t|j|zr|S|t|jz}t j|j|jdzD]i\}}}| |d}||j &|j |}| ||r|t|z }j|S)N.__path__.)rWr3r]rhasattrrrs walk_packagesr find_specloader load_moduleror)packagepassedr~rrMis_pkgspecrhs r%rrMs! }}'3)(( 7J ' 'yG 7 v% c'"###F ' 5g6FHX[^H^ _ _22fd++ <4;. ((..v  2 )&11 1G Nr'ct|to)t|tjot |dS)z/Check object if it's a peewee model and unique.r>)r3type issubclassr4Modelr)objs r%rarais3 c4 XZRX%>%> X73PWCXCXXr'Frkr[rct|j}|r||}}t||||}|sdSttjd|dz}t d|S)z$Compile migrations for given models.rkr_z  )rprrrjoinsplitrsub)r[r}rlsourcerrs r%rwrwns (,  F(668WEEEJ r W\&++j"9"9"?"?"E"EFF FD <<d # ##r'r )rhrr:r).)r[r);r __future__rrrsrrm functoolsr importlibrpathlibrtypesrtypingrr r r r r rrrunittestrpeeweer4r\rrlogsrr[rr}rrtemplaterloggingrrzMr__annotations__cwdrrr&objectr)rrrgrrarwr!r'r%r s""""""  %%%%%%######XXXXXXXXXXXXXXXXXXXXXX$$$$$$$$11111111"*Wbd++++++ #l22222O>O>O>O>O>O>O>O>d77777Z777tMMMMM:MMM$8YYY ?D $ $ $ $ $ $ $ $r'