U of?i@sddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd l m Z dd l mZdd l mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&dd lm'Z'dd!lm(Z(dd"lm)Z)dd#lm*Z*erdd$lm+Z+dd%l,m-Z-dd&l.m/Z/dd'l.m0Z0dd(l1m2Z2dd)lm3Z3dd*l4m5Z5dd+l6m7Z7Gd,d-d-Z8Gd.d/d/Z9d0S)1) annotations)Any)Dict)List)Optional)Tuple) TYPE_CHECKING)Union)CheckConstraint)Column)ForeignKeyConstraint)Index)MetaData)PrimaryKeyConstraintschema)Table)types)SchemaEventTarget) OrderedDict) topological)exc)_columns_for_constraint)_copy)_copy_expression)_ensure_scope_for_ddl)_fk_is_self_referential)_idx_table_bound_expressions)_insert_inline)_is_type_bound)_remove_column_from_collection)_resolve_for_variant)_select)constraint_name_defined)constraint_name_string)Literal)Dialect) ColumnClause) quoted_name)Function) Constraint) TypeEngine) DefaultImplc@seZdZddZeddddZedddd Zd dd d Zd dddZd dddZ d dddZ d dddZ dd dddZ dd dddZ ddZddd d d!d"Zddd d d#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/S)0BatchOperationsImplc Cs||_||_||_|dkr"td||_||_||_t||_||_ t| |_ |j dt |j dd|j dd|jjf| |_| |_g|_dS)N)autoalwaysneverz4recreate may be one of 'auto', 'always', or 'never'.Z listenersZcolumn_reflect) operations table_namer ValueErrorrecreate copy_from table_argsdict table_kwargs reflect_argsreflect_kwargs setdefaultlistgetappendimplZautogen_column_reflectnaming_conventionpartial_reorderingbatch) selfr3r4rr6r7r8r:r;r<rBrCr2r2F/opt/hc_python/lib/python3.8/site-packages/alembic/operations/batch.py__init__8s.    zBatchOperationsImpl.__init__r'returncCs |jjjSN)r3rAdialectrEr2r2rFrK]szBatchOperationsImpl.dialectr-cCs|jjSrJ)r3rArLr2r2rFrAaszBatchOperationsImpl.implboolcCs.|jdkr|jj|S|jdkr&dSdSdS)Nr/r0TF)r6r3rAZrequires_recreate_in_batchrLr2r2rF_should_recreatees   z$BatchOperationsImpl._should_recreateNonec Cs.|}t|jj|sH|jD]"\}}}t|jj|}|||q"n|jr\t|jd}nt}|j dk rx|j }d}nZ|jj j rt d|jj jjd|jdt|j|f|j|j|jd|j}d}t|j||j|j||jd} |jD]\}}}t| |}|||q| |jW5QRXdS) N)rBFzEThis operation cannot proceed in --sql mode; batch mode with dialect zF requires a live database connection with which to reflect the table "z". To generate a batch SQL migration script using table "move and copy", a complete Table object should be passed to the "copy_from" argument of the batch_alter_table() method so that table reflection can be skipped.)rZ autoload_withT)rC)rNrrA connectionrDgetattrr3rBrr7Zmigration_contextZas_sqlr CommandErrorrKnamer4rr;rZget_bindr<ApplyBatchImplr8r:rC_create) rEZshould_recreateopnameargkwfnm1Zexisting_table reflectedZ batch_implr2r2rFflushmsP    zBatchOperationsImpl.flushcOs|jd||fdS)N alter_columnrDr@rErWrXr2r2rFr]sz BatchOperationsImpl.alter_columncOs8d|ksd|kr"|s"td|jd||fdS)N insert_before insert_afterz^Can't specify insert_before or insert_after when using ALTER; please specify recreate='always' add_column)rNrrRrDr@r_r2r2rFrbszBatchOperationsImpl.add_columncOs|jd||fdS)N drop_columnr^r_r2r2rFrcszBatchOperationsImpl.drop_columnr+constrIcCs|jd|fifdS)Nadd_constraintr^rErer2r2rFrfsz"BatchOperationsImpl.add_constraintcCs|jd|fifdS)Ndrop_constraintr^rgr2r2rFrhsz#BatchOperationsImpl.drop_constraintcOs|jd||fdS)N rename_tabler^r_r2r2rFrisz BatchOperationsImpl.rename_tabler r)idxrXrIcKs|jd|f|fdS)N create_indexr^rErjrXr2r2rFrksz BatchOperationsImpl.create_indexcKs|jd|f|fdS)N drop_indexr^rlr2r2rFrmszBatchOperationsImpl.drop_indexcCs|jd|fifdS)Ncreate_table_commentr^rEtabler2r2rFrnsz(BatchOperationsImpl.create_table_commentcCs|jd|fifdS)Ndrop_table_commentr^ror2r2rFrqsz&BatchOperationsImpl.drop_table_commentcCs tddS)Nz Can't create table in batch modeNotImplementedErrorror2r2rF create_tablesz BatchOperationsImpl.create_tablecCs tddS)NzCan't drop table in batch moderrror2r2rF drop_tableszBatchOperationsImpl.drop_tablecCs|jd|fifdS)Ncreate_column_commentr^rEcolumnr2r2rFrvsz)BatchOperationsImpl.create_column_commentN)__name__ __module__ __qualname__rGpropertyrKrArNr\r]rbrcrfrhrirkrmrnrqrtrurvr2r2r2rFr.7s(%9 r.c @sTeZdZdLddddddddd d Zed d d ddZddddZddddZddddZddddZ ddddddZ dddd d!Z dMd d d$d%d&d'd(d)dd* d+d,Z d d&d&dd-d.d/Z dNd d0d&d&dd1d2d3Zd d4dd5d6d7Zd8d9Zd:d;Zddd?d@dAZd>dd?dBdCZdDddEdFdGZdDddEdHdIZdJdKZd"S)OrTr2r-rtuplezDict[str, Any]rMrO)rArpr8r:r[rCrIcCsn||_||_||_||_||j|_d|_||_d|_ t dd|jj D|_ t |j |_||_|dS)Nr2css|]}|jd|ifVqdSexprN)rS.0cr2r2rF sz*ApplyBatchImpl.__init__..)rArpr8r:_calc_temp_namerStemp_table_name new_tablerCadd_col_orderingrrcolumn_transfersr>existing_orderingr[_grab_table_elements)rErArpr8r:r[rCr2r2rFrGs   zApplyBatchImpl.__init__zUnion[quoted_name, str]str) tablenamerIcCsd|ddS)Nz_alembic_tmp_%sr2r2)clsrr2r2rFrszApplyBatchImpl._calc_temp_namerHcCsP|jj}t|_|jjD]D}t||d}d|_|_t|j t rP|j |j k sPt ||j|j <qi|_ g|_i|_i|_i|_|jjD]L}t|rqq|jrt|tr|j sqt|j r||j |j <q|j|q|js |jjD]&}|jD]}|j r||f|j|j <qq|jjD]}||j|j <q|jjD]}|j||jj|q.dS)NrF)rprrcolumnsrruniqueindex isinstancetyperAssertionErrorrSnamed_constraintsunnamed_constraintscol_named_constraintsindexes new_indexes constraintsr r[r r%r@kwargsr:r=)rErrZc_copyrecolrjkr2r2rFrsF          z#ApplyBatchImpl._grab_table_elementscst}tj}jrTjD]4}t|D]&\}}|dkr(|||d|fq(qn2tjD]&\}}|dkr^|||d|fq^|jdd|D}tt j ||dd}t fdd|D_t fd d|D_ dS) NrcSs g|]}|d|dkr|qS)rrr2)rpr2r2rF 9szNApplyBatchImpl._adjust_self_columns_for_partial_reordering..T)Zdeterministic_orderc3s|]}|j|fVqdSrJ)rrrrLr2rFr>szMApplyBatchImpl._adjust_self_columns_for_partial_reordering..c3s|]}|j|fVqdSrJ)rrrLr2rFr?s) setr>rrC enumerateaddrupdaterrsortrr)rEpairsZ col_by_idxtuple_relemZ pairs_listZsorted_r2rLrF+_adjust_self_columns_for_partial_reordering%s&    z:ApplyBatchImpl._adjust_self_columns_for_partial_reorderingcCs |jdkstdt}|jj}|js,|jr4|t|j |ft |j t |j d|i|j|_}t |j |jD]}ddt|D}||jsqt|trt|rt|||jd}qt||d}nt|||d}t|tr|||||qdS)NzCan only create new table oncercSsh|] }|jqSr2)keyrr2r2rF WszAApplyBatchImpl._transfer_elements_to_new_table..)rZ target_tabler)rrrrprrCrrrrr>rvaluesr8r:rrrissubsetrrr rr_setup_referentZappend_constraint)rEmrrreZ const_columnsZ const_copyr2r2rF_transfer_elements_to_new_tableCsH      z.ApplyBatchImpl._transfer_elements_to_new_tablez List[Index]csjdk stg}jD]J}|jr(qt|jffddt|D|jjd|j }| |qj D]<}| t|jffdd|j Dd|ji|j qr|S)Ncsg|]}t|jqSr2)rr)rrrLr2rFrszCApplyBatchImpl._gather_indexes_from_both_tables..)r_tablecsg|]}jj|qSr2)rr)rrrLr2rFrsr)rrrrZ _column_flagr rSrrrr@rrkeys)rErjZ idx_existingZidx_copyrr2rLrF _gather_indexes_from_both_tablesus>  z/ApplyBatchImpl._gather_indexes_from_both_tablesrr )metadata constraintrIc s|jd}|d}|d}t|dkr6|d}nd}||jkrt||}dddd||jkr|j|}|jD]2} | dd } | |jkrx| t | t j qxn0t ||fd d fd d |jDDd |idS)Nr.rrcSs|SrJ) _get_colspecrr2r2rFcolspecsz/ApplyBatchImpl._setup_referent..colspeccSsg|]}t|tjqSr2)r sqltypesNULLTYPE)rnr2r2rFrsz2ApplyBatchImpl._setup_referent..csg|]}|ddqS)rr)split)rrrr2rFrsr)elementsrrlenr sql_schemaZ_get_table_keyZtablesr append_columnr rrr) rErrspecpartsZtnameZreferent_schemartrcolnamer2rrFrs6           zApplyBatchImpl._setup_referent)op_implrIc Cs||||j|jdk s$t||jzP|t|jt dd|j Dt dd|j D||jWn||jYnRX|j|j|jj|jjd|jj|j_z|D]}||qW5|j|j_XdS)Ncss|]\}}d|kr|VqdSr~r2)rrtransferr2r2rFrsz)ApplyBatchImpl._create..cSsg|]}d|kr|dqS)rr2)rrr2r2rFrsz*ApplyBatchImpl._create..r)rZprep_table_for_batchrprrrt_execrZ from_selectr>ritemsr#rrurirrSrrrk)rErrjr2r2rFrUs>     zApplyBatchImpl._createNFzOptional[bool]z)Optional[Union[Function[Any], str, bool]]z Optional[str]zOptional[TypeEngine]z&Optional[Union[bool, Literal['auto']]]zUnion[str, Literal[False]]) r4 column_namenullableserver_defaultrStype_ autoincrementcommentrIc Ks|j|} |j|} |dk rt||krt|| _|| d<| dd} | rtt| d|jj} t| trt| jrt|j | jd|dk rt |}t| j trd| j _| j _|j| | ||| _ |dk r|| _|dk r|dkrd| _nt|| |dk rt|| _|dk r|| _dS)NrS existing_typeF)rrrSr?r"rArKrrrpoprZ to_instancerZ_create_eventsZcreate_constraintZcast_for_batch_migraterrrZ DefaultClauseZ _set_parentrMrr)rEr4rrrrSrrrrXexistingZexisting_transferrZresolved_existing_typer2r2rFr]sZ        zApplyBatchImpl.alter_column)rr`rarIcCs|j}ddt|D}|js|r^|s^||krP||d}|t|kr^||}nt|j|}|r|s||kr||d}|dkr||}ndd|jD|}|r|j||ff7_|r|j||ff7_|js|s|s|r|j|d|ff7_dS)NcSsi|]\}}||qSr2r2)rirSr2r2rF 4szEApplyBatchImpl._setup_dependencies_for_add_column..rrcSsi|]\}}||qSr2r2)rabr2r2rFrLsr)rrrCrr9r)rErr`raZ index_colsZ col_indexesrjr2r2rF"_setup_dependencies_for_add_column-sH     z1ApplyBatchImpl._setup_dependencies_for_add_columnz Column[Any])r4rxr`rarIcKs8||j||t||jjd|j|j<i|j|j<dS)Nr)rrSrrprrr)rEr4rxr`rarXr2r2rFrb]szApplyBatchImpl.add_columnz%Union[ColumnClause[Any], Column[Any]])r4rxrIcKsz|j|jjjkr t|jjj||j|j=|j|j=|j|jd|krvt|dt rv|djrv|j |djddS)Nr) rSrp primary_keyrr!rrremoverrrr)rEr4rxrXr2r2rFrcms$   zApplyBatchImpl.drop_columncCsdS)a^the batch table creation function will issue create_column_comment on the real "impl" as part of the create table process. That is, the Column object will have the comment on it already, so when it is received by add_column() it will be a normal part of the CREATE TABLE and doesn't need an extra step here. Nr2rwr2r2rFrvsz$ApplyBatchImpl.create_column_commentcCsdS)zthe batch table creation function will issue create_table_comment on the real "impl" as part of the create table process. Nr2ror2r2rFrnsz#ApplyBatchImpl.create_table_commentcCsdS)zthe batch table creation function will issue drop_table_comment on the real "impl" as part of the create table process. Nr2ror2r2rFrqsz!ApplyBatchImpl.drop_table_commentr+rdcCsdt|jstdt|tjr<|jj|jkr<|j |jjt |jrT||j |j<n |j |dS)NConstraint must have a name) r$rSr5rrrrprrrr%rr@rgr2r2rFrfs   zApplyBatchImpl.add_constraintcCs|jstdz|j|jkrh|j|j\}}t|j|jjD]$}|j|jkr@|j|jj|q@n0t|jr|j |j}n||j kr|j |Wn0t k rt |rYdStd|jYn&Xt |tr|jD]}d|j|j_qdS)NrzNo such constraint: '%s'F)rSr5rrr>rrrr%rrKeyErrorr rrr)rErerZ col_constr2r2rFrhs&      zApplyBatchImpl.drop_constraintr )rjrIcCs||j|j<dSrJ)rrSrErjr2r2rFrkszApplyBatchImpl.create_indexcCs6z|j|j=Wn"tk r0td|jYnXdS)NzNo such index: '%s')rrSrr5rr2r2rFrmszApplyBatchImpl.drop_indexcOs tddS)NTODOrrr_r2r2rFriszApplyBatchImpl.rename_table)r2)NFNNNF)NN)ryrzr{rG classmethodrrrrrrrUr]rrbrcrvrnrqrfrhrkrmrir2r2r2rFrTs<.2!%+ K4  rTN): __future__rtypingrrrrrrr Z sqlalchemyr r r r rrrrrrrZsqlalchemy.sql.schemarZsqlalchemy.utilrrutilrZutil.sqla_compatrrrrrrrr r!r"r#r$r%r&Zsqlalchemy.enginer'Zsqlalchemy.sql.elementsr(r)Zsqlalchemy.sql.functionsr*r+Zsqlalchemy.sql.type_apir,Zddl.implr-r.rTr2r2r2rFsZ