U of@stddlmZddlmZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZe rddlmZddlm Z ddlm!Z!ddlm"Z"ddl#m$Z$ddl#m%Z%ddl#m&Z&ddl#m'Z'ddl#m(Z(ddl#m)Z)dd l#m*Z*dd!l#m+Z+dd"l#m,Z,dd#l#m-Z-dd$l.m/Z/dd%l0m1Z1dd&l2m3Z3dd'l4m5Z5dd(l6m7Z7dd)l6m8Z8dd*l9m:Z:dd+l9m;Z;d,Zd-d7d8d9d:d;d<Z?e@ZAZBd=d-d3d>d?d@ZCd-dAdBdCdDdEZDd-dAd3dCdFdGZEeBFej8d-dHdBdCdIdJZGeBFejHd-dKd3dCdLdMZIeBFejJd-dNd3dCdOdPZKeBFejLd-dQd3dCdRdSZMeBFejNd-dTd3dCdUdVZOd-dWdXdYdZd[ZPeBFejQd-d\d3dCd]d^ZReBFejSd-d_d3dCd`daZTeBFejUd-dbdBdCdcddZVeBFejWd-ded3dCdfdgZXeBFejYdhdiZZeBFej[djdkZ\eBFej]d-dld3dCdmdnZ^eBFej_d-dod3dCdpdqZ`eBFejad-drd3dCdsdtZbeBFejcd-dud3dCdvdwZdGdxdydyZedzd{d|d}d~Zfdddddd-dddd3dddZgdd-dBdddZhdd-dd3dddZiddZjd-d3dddZkd-d3dddZld3dd-ddddZmdd-d3dddZndddddZoddd-dd{dddZpdd-d3dddZqdd-d3dddZrddd-dd3dddZsdd-d3dddZtdd-d3dddZuddd-d3d3d{ddddZve@Zwdd-dd{dddZxewFej,dd-dd{ddd„Zydd{dd3dŜddDŽZzddd9dʜdd̄Z{ewFej)dd-dd{ddd΄Z|ewFej-dd-dd3dddЄZ}ewFej$dd-dd{dddӄZ~eBFejd-dd3dCddքZeAZBdS)) annotations)StringION)Any)cast)Dict)List)Optional)Tuple) TYPE_CHECKING)Union) PythonPrinter)schema)sql)types)conv) quoted_name)util)ops) sqla_compat)Literal) DialectKWArgs) ColumnElement) TextClause)CheckConstraint)Column) Constraint) FetchedValue) ForeignKey)ForeignKeyConstraint)Index)MetaData)PrimaryKeyConstraint)UniqueConstraint)ARRAY) TypeEngine)AutogenContext)Config)MigrationScript)ModifyTableOps)Computed)Identityr&zsqla_compat._ConstraintNamez*Optional[Union[quoted_name, str, _f_name]])autogen_contextnamereturncCs&t|trtt||St|SdSN) isinstancer_f_name_alembic_autogenerate_prefixrZconstraint_name_or_none)r-r.r4I/opt/hc_python/lib/python3.8/site-packages/alembic/autogenerate/render.py_render_gen_name:s r6str)textr/cCs4tdtjd|}tdtjd|}|S)N^ z +$)recompileMsubstrip)r8r4r4r5_indentDsrAr(zDict[str, Union[str, Config]]None)r-migration_script template_argsr/cCs\|j}t|j|jD]0\}}tt||||j<tt||||j<qdt ||d<dS)N imports) rFzipZupgrade_ops_listZdowngrade_ops_listrA_render_cmd_bodyZ upgrade_tokenZdowngrade_tokenjoinsorted)r-rCrDrFZ upgrade_opsZ downgrade_opsr4r4r5 _render_python_into_templatevarsJs   rKzops.OpContainer) op_containerr-r/cCsrt}t|}|dd}|jD].}t||}|p:t|}|D]}||q@q"|s`|d|d|S)Nz=# ### commands auto generated by Alembic - please adjust! ###Fpassz# ### end Alembic commands ###)rr Z writeliner render_opboolgetvalue)rLr-bufprinterZ has_linesoplinesliner4r4r5rH`s     rHzops.MigrateOperationz List[str])r-rSr/cCst|}t|||}|Sr0) renderersdispatchrZto_list)r-rSrendererrTr4r4r5rN{s rNcCsdt||S)NrE)rIrNr-rSr4r4r5render_op_textsrZr)c Cs|j}|dd}|jrg}|rv|F|d|j|jf|jD]}t||}||qF|dW5QRXn |jD]}t||}||q||SgSdS)Nrender_as_batchFz5with op.batch_alter_table(%r, schema=%r) as batch_op:r;) optsgetrZ _within_batchappend table_namer rNextend)r-rSr\r[rTZt_opZt_linesr4r4r5_render_modify_tables(         razops.CreateTableCommentOpcCsh|jr d}nd}|jt||j|jdk r2d|jnd|jdk rHd|jnd|jdk r^d|jndddS)NzV{prefix}create_table_comment( {indent}{comment}, {indent}existing_comment={existing} )z{prefix}create_table_comment( {indent}'{tname}', {indent}{comment}, {indent}existing_comment={existing}, {indent}schema={schema} )%r'%s'r:)prefixtnamecommentexistingr indent) _has_batchformatr3r_rfexisting_commentr r-rSZtemplr4r4r5_render_create_table_comments rmzops.DropTableCommentOpcCsR|jr d}nd}|jt||j|jdk r2d|jnd|jdk rHd|jndddS)NzA{prefix}drop_table_comment( {indent}existing_comment={existing} )zm{prefix}drop_table_comment( {indent}'{tname}', {indent}existing_comment={existing}, {indent}schema={schema} )rbrcr:)rdrergr rh)rirjr3r_rkr rlr4r4r5_render_drop_table_comments rnzops.CreateTableOpc s<}ddfdd|jDDtddfdd|jDD}t|tkrjdd|d}n d|}d tjt |d }j r|d tj 7}|j }|r|d t|7}|j }|r|d |7}tj D]"}|d|ddj |f7}q|jr0ddd|jD} |d| 7}|d7}|S)NcSsg|] }|r|qSr4r4.0colr4r4r5 sz_add_table..csg|]}t|qSr4)_render_columnror-r4r5rrscSsg|]}|dk r|qSr0r4)rpZrconsr4r4r5rrscsg|]}t|jqSr4)_render_constraintZ_namespace_metadata)rpZconsrYr4r5rrs z*[z, ]z.%(prefix)screate_table(%(tablename)r, %(args)s)Z tablenamerdargsz , schema=%rz , comment=%rz, info=z, %s=%r _, css|]}d|VqdS)rcNr4)rppr4r4r5 sz_add_table..z, prefixes=[%s]z ))Zto_tablecolumnsrJ constraintslenMAX_PYTHON_ARGSrI_identr_r3r rfinfokwreplace _prefixes) r-rStablerwargs_strr8rfrkprefixesr4rYr5 _add_tablesJ      rzops.DropTableOpcCs<dt|t|jd}|jr0|dt|j7}|d7}|S)Nz%(prefix)sdrop_table(%(tname)r)rdre , schema=%r))r3rr_r )r-rSr8r4r4r5 _drop_tablesrrz list[str])r-itemr/csfdd|jDS)Ncs$g|]\}}|dt|qS)=_render_potential_exprrpkeyvalrtr4r5rr-sz0_render_dialect_kwargs_items..)dialect_kwargsitems)r-rr4rtr5_render_dialect_kwargs_items*s rzops.CreateIndexOpc Cs|}|j}|rd}nd}|jdk s*tt||}|t|t||jt|jjd t |||j pfd|jj r~dt|jj nd|rdd |ndd}|S)NzL%(prefix)screate_index(%(name)r, [%(columns)s], unique=%(unique)r%(kwargs)s)za%(prefix)screate_index(%(name)r, %(table)r, [%(columns)s], unique=%(unique)r%(schema)s%(kwargs)s)rzFrr;)rdr.rr}uniquer kwargs) to_indexrirAssertionErrorrr3r6r.rrI_get_index_rendered_expressionsrr r-rSindex has_batchtmplr\r8r4r4r5 _add_index3s.   rzops.DropIndexOpc Csv|}|j}|rd}nd}t||}|t|t||jt|j|jrTdt|jnd|rhdd |ndd}|S)Nz(%(prefix)sdrop_index(%(name)r%(kwargs)s)zM%(prefix)sdrop_index(%(name)r, table_name=%(table_name)r%(schema)s%(kwargs)s)rr;rz)rdr.r_r r) rrirr3r6Z index_namerr_r rIrr4r4r5 _drop_indexYs  rzops.CreateUniqueConstraintOpcCst||dgS)NT)_uq_constraintZ to_constraintrYr4r4r5_add_unique_constraintqsrzops.CreateForeignKeyOpcCstt||jg}|js,|tt|j|tt|jtdd|j Dtdd|j Dgdddddd d g}|js| d d |D]2}||j kr|j |}|dk r|d ||fqdt |d|dS)NcSsg|] }t|qSr4rror4r4r5rrsz&_add_fk_constraint..cSsg|] }t|qSr4rror4r4r5rrsZreferent_schemaonupdateondelete initially deferrable use_altermatchrZ source_schema%s=%rz&%(prefix)screate_foreign_key(%(args)s)rzrdrw)reprr6constraint_namerir^rZ source_tabler`Zreferent_tableZ local_colsZ remote_colsinsertrr3rI)r-rSrwrrvaluer4r4r5_add_fk_constraintxs8     rcCs tdSr0NotImplementedError constraintr-r4r4r5_add_pk_constraintsrcCs tdSr0rrr4r4r5_add_check_constraintsrzops.DropConstraintOpcCst|}t||j}|jr$t|jnd}|jr8t|jnd}g}|t||js|tt|j |dk r|d||dk r|d||dd |dS)Nzschema=ztype_=zdrop_constraint(rzr) r3r6rr rZconstraint_typer^rrir_rI)r-rSrdr.r type_Z params_strsr4r4r5_drop_constraints rzops.AddColumnOpcCsZ|j|j|j}}}|jr"d}nd}|r2|d7}|d7}|t||t|||d}|S)Nz %(prefix)sadd_column(%(column)s)z*%(prefix)sadd_column(%(tname)r, %(column)s, schema=%(schema)rr)rdrecolumnr )r r_rrir3rs)r-rSr rertemplater8r4r4r5 _add_columnsrzops.DropColumnOpcCs`|j|j|j}}}|jr"d}nd}|r2|d7}|d7}|t|t|t|t|d}|S)Nz %(prefix)sdrop_column(%(cname)r)z*%(prefix)sdrop_column(%(tname)r, %(cname)rrr)rdrecnamer )r r_ column_namerir3r)r-rSr rerrr8r4r4r5 _drop_columnsrzops.AlterColumnOpcCs|j}|j}|j}|j}|j}|j}|jdd}|j} |j } |j } |j } |j } d}|j r`d}nd}|t|||d}| dk r|d|t| |f7}|dk rt||}|d||f7}|dk r|d |t||f7}|dk r|d ||f7}|dk r|d ||f7}| dk r"|d || f7}|dkrF| dk rF|d || f7}|dk r`|d||f7}|dkr| rt| |}|d||f7}| r|j s|d|| f7}|d7}|S)N autoincrementz z %(prefix)salter_column(%(cname)rz+%(prefix)salter_column(%(tname)r, %(cname)r)rdrerz, %sexisting_type=%sFz, %sserver_default=%sz , %stype_=%sz, %snullable=%rz, %scomment=%rz, %sexisting_comment=%rz, %sexisting_nullable=%rz, %sautoincrement=%rz, %sexisting_server_default=%sz , %sschema=%rr)r_rZmodify_server_defaultZ modify_typeZmodify_nullableZmodify_commentrr] existing_typeexisting_nullablerkexisting_server_defaultr rir3 _repr_type_render_server_default)r-rSrerserver_defaultrnullablerfrrrrkrr rhrr8renderedr4r4r5 _alter_columnsf    rc@s,eZdZddddddZdddd Zd S) r2r7rrB)rdr.r/cCs||_||_dSr0)rdr.)selfrdr.r4r4r5__init__$sz_f_name.__init__)r/cCsd|jt|jfS)Nz%sf(%r))rdrr.)rr4r4r5__repr__(sz_f_name.__repr__N)__name__ __module__ __qualname__rrr4r4r4r5r2#sr2z!Optional[Union[quoted_name, str]]z Optional[str])r.r/cCs0|dkr |St|trt|St|tr,|SdS)zproduce a __repr__() object for a string identifier that may use quoted_name() in SQLAlchemy 0.9 and greater. The issue worked around here is that quoted_name() doesn't have very good repr() behavior by itself when unicode is involved. N)r1rr7)r.r4r4r5r,s   rTF) wrap_in_textis_server_defaultis_indexrrO)rr-rrrr/cCsHt|tjr<|rd}nd}|t||jjj|||ddSt|SdS)Nz%(prefix)stext(%(sql)r)z%(sql)r)rr)rdr)r1rZ ClauseElement_sqlalchemy_autogenerate_prefixmigration_contextimplZrender_ddl_sql_exprr)rr-rrrrr4r4r5r<s rr )idxr-r/csfdd|jDS)Ncs:g|]2}t|tjr(ttt|ddn t|ddqS)r.NT)r)r1 sa_schemarrrgetattrr)rpexprtr4r5rrXs z3_get_index_rendered_expressions..)Z expressions)rr-r4rtr5rUs rr#)rr-alterr/cCsZg}|j}|jr$|dt|jf|jr>|dt|jf|sd|rd|jjrd|dt|jjf|s|jr|dt ||jft ||}|rt t ||jg}|s|t t|jjg7}|t dd|j D| dd|D| |dt|d |d Sd d|j D}| d d|D| |d t|d |d SdS)Nrrr r.cSsg|]}t|jqSr4)rr.ror4r4r5rr{sz"_uq_constraint..cSsg|]\}}d||fqSrr4rprvr4r4r5rr|sz,%(prefix)screate_unique_constraint(%(args)s)rzrcSsg|]}tt|jqSr4)rrr.ror4r4r5rrscSsg|]\}}d||fqSrr4rr4r4r5rrsz$%(prefix)sUniqueConstraint(%(args)s))rirr^r7rrr rr.r6rrr}r`r3rIr)rr-rr\rZdialect_optionsrwr4r4r5rbs>    rcCs$|jd}|dkrd|jS|SdS)NZuser_module_prefixz%s.)r\r)r-targetrdr4r4r5_user_autogenerate_prefixs  r)r-r/cCs|jdp dS)NZsqlalchemy_module_prefixr;r\rtr4r4r5rsrcCs|jr dS|jdpdSdS)Nz batch_op.Zalembic_module_prefixr;)rir\rtr4r4r5r3sr3zUnion[str, Literal[False]])robject_r-r/cCs4d|jkr0|jd}|r0||||}|dk r0|SdS)NZ render_itemFr)rrr-renderrr4r4r5_user_defined_renders   rz Column[Any])rr-r/c s<td|}|dk r|Sg}g}|jrZt|j}|rZt|jrL||n|d|f|jdk r|jtjkr|d|jf|jdk r|d|jf|j r|d|j f|j }|r|dd|fd t t |j t|j|rd d d |Dd nd d dd |Dfdd t|DdS)NrFrrrsystemrfrbz8%(prefix)sColumn(%(name)r, %(type)s, %(args)s%(kwargs)s)rzcSsg|] }t|qSr4)r7)rpargr4r4r5rrsz"_render_column..r;cSsg|]\}}d||fqS%s=%sr4rpZkwnamerr4r4r5rrscs"g|]\}}d|t|fqSrrrrtr4r5rrs )rdr.typerwr)rrr*_should_render_server_default_positionallyr^rrZAUTOINCREMENT_DEFAULTrrrfrrr.rrrIZ_column_kwargsr)rr-rrwr\rfr4rtr5rssR         rs)rr/cCst|pt|Sr0)r_server_default_is_computed_server_default_is_identity)rr4r4r5rs rzBOptional[Union[FetchedValue, str, TextClause, ColumnElement[Any]]])defaultr-repr_r/cCstd||}|dk r|St|r2ttd||St|rLttd||St|tj r|t|j t rl|j }nt |j |ddSt|t r|rt tdd|}tt |S) NrFr*r+T)rz^'|'$r;)rrr_render_computedrr_render_identityr1rZ DefaultClauserr7rrr<r?)rr-rrr4r4r5rs$     rr*)computedr-r/cCsPt|j|dd}i}|jdk r(|j|d<dt||ddd|DdS) NFr persistedz(%(prefix)sComputed(%(text)s, %(kwargs)s)rzcss|]}d|VqdSrNr4rppairr4r4r5r|sz#_render_computed..)rdr8r)rsqltextrrrIr)rr-r8rr4r4r5rs  rr+)identityr-r/cCs4tj|dd}dt|ddd|DdS)NT)rz%(prefix)sIdentity(%(kwargs)s)rzcss|]}d|VqdSrr4rr4r4r5r|!sz#_render_identity..)rdr)rZ_get_identity_options_dictrrIr)rr-rr4r4r5rsrr%)rr-_skip_variantsr/c Cs*td||}|dk r|St|jdr6|jj||}nd}t|j}|j}|drt d|}|dk slt | d}|dk r| d||r|Sd||fSn|r|S|st|rt||S|d rd |jtkrtd |j} | ||St|} d | |fSnt||} d | |fSdS) NrFrsqlalchemy.dialectssqlalchemy\.dialects\.(\w+)z"from sqlalchemy.dialects import %sz%s.%rz sqlalchemy.z_render_%s_typez%s%r)rhasattrrrZ render_typerrrF startswithr<rrgroupaddrZ_type_has_variants_render_Variant_typeZ__visit_name__globalsrr) rr-rrZimpl_rtmodrFrdnamefnrdr4r4r5r%s@           rr$)rr-r/cCsttt||ddS)NZ item_typez(.+?\())rr7_render_type_w_subtype)rr-r4r4r5_render_ARRAY_typeQsr cCsdt|\}}t||dd}|dk r,|dk s0tt|D]&}||}|dt||dd|f7}q8|S)NT)rFz.with_variant(%s, %r))rZ_get_variant_mappingrrrJ)rr-Z base_typeZvariant_mappingbasedialecttypr4r4r5rZs   rz$Union[Optional[str], Literal[False]])rr-attrnameregexprdr/c Cst|}t||d}|dkr dSt|}tdd|}tt|||}t||d||} |rld|| fSt|j} | drtd| } | dk st | d} d | | fS| d rt |}d|| fSdSdS) NFz([\(\)])z\\\1z\1%sz%s%srrr%s.%s sqlalchemy) rrr<r?rrrrrrrr) rr-r rrdZ outer_reprZ inner_typeZ inner_reprZsub_typeZ outer_typerrrr4r4r5ris(          rrzOptional[MetaData])rr-namespace_metadatar/cCsJzt|}Wn*tk r8td|d|YSX||||SdS)Nz(No renderer is established for object %rz[Unknown Python object %r])_constraint_renderersrW ValueErrorrwarn)rr-rrXr4r4r5rus rur"cCsxtd||}|dk r|S|js"dSg}|jrF|dtt||jfdt|ddd|jDdd|Dd S) NZ primary_keyFr.z(%(prefix)sPrimaryKeyConstraint(%(args)s)rzcSsg|]}t|jqSr4)rr.)rpcr4r4r5rrsz'_render_primary_key..cSsg|]\}}d||fqSrr4rr4r4r5rrsr)rr}r.r^rr6rrIrr-rrr\r4r4r5_render_primary_keys$  rrr!)fkmetadata_schemarr/c Cs|}|d}|dd\}}|dk rDt|dkrDd||f}nd|dd}|js|jdk r|jjdk r||jkr|j|j |}|dk rt |j }d||f}|S)zxImplement a 'safe' version of ForeignKey._get_colspec() that won't fail if the remote table can't be resolved. .Nrrr) Z _get_colspecsplitrrIZ link_to_nameparentrZtablesrr]rr.) rrrZcolspectokensreZcolnameZtable_fullnamerqr4r4r5 _fk_colspecs$      r rzList[Tuple[str, str]])rr\r/cCs|jr|dt|jf|jr4|dt|jf|jrN|dt|jf|jrh|dt|jf|jr|dt|jf|jr|dt|jfdS)Nrrrrrr)rr^rrrrrr)rr\r4r4r5_populate_render_fk_optssr!cstd||}|dk r|Sg}|jr<|dtt||jft||jdt|ddd|j Ddfdd|j Ddd d |Dd S) NZ foreign_keyFr.zC%(prefix)sForeignKeyConstraint([%(cols)s], [%(refcols)s], %(args)s)rzcss|]}tt|jjVqdSr0)rrrr.rpfr4r4r5r|sz&_render_foreign_key..c3s|]}tt|VqdSr0)rr r"Zapply_metadata_schemarr4r5r|scSsg|]\}}d||fqSrr4rr4r4r5rr sz'_render_foreign_key..)rdcolsZrefcolsrw) rr.r^rr6r!r rrIelementsrr4r$r5_render_foreign_keys0    r'cCs$td||}|dk r|St||dS)NrF)rr)rr-rrr4r4r5_render_unique_constraints r(rcCstd||}|dk r|S|jr>t|jdr>t|jjtjr>dSg}|jrb|dt t ||jfdt ||rdd dd|Dnd t |j|dd d S) NcheckFrr.z.%(prefix)sCheckConstraint(%(sqltext)s%(opts)s)rzcss|]\}}d||fVqdSrr4rr4r4r5r|?sz+_render_check_constraint..r;r)rdr\r)rZ _create_rulerr1rsqltypesr%r.r^rr6rrIrrrr4r4r5_render_check_constraint s:  r+zops.ExecuteSQLOpcCst|jtstdd|jS)NznAutogenerate rendering of SQL Expression language constructs not supported here; please use a plain SQL stringzop.execute(%r))r1rr7rrYr4r4r5 _execute_sqlIs  r,)T)F)N) __future__riorr<typingrrrrrr r r Z mako.pygenr rr rrrr*Zsqlalchemy.sql.elementsrrr;r operationsrrrZsqlalchemy.sql.baserrrZsqlalchemy.sql.schemarrrrrrr r!r"r#Zsqlalchemy.sql.sqltypesr$Zsqlalchemy.sql.type_apir%Zalembic.autogenerate.apir&Zalembic.configr'Zalembic.operations.opsr(r)Zalembic.util.sqla_compatr*r+rr6rArKZ DispatcherZdefault_renderersrVrHrNrZZ dispatch_forraZCreateTableCommentOprmZDropTableCommentOprnZ CreateTableOprZ DropTableOprrZ CreateIndexOprZ DropIndexOprZCreateUniqueConstraintOprZCreateForeignKeyOprZCreatePrimaryKeyOprZCreateCheckConstraintOprZDropConstraintOprZ AddColumnOprZ DropColumnOprZ AlterColumnOprr2rrrrrrr3rrsrrrrrr rrrrurr r!r'r(r+Z ExecuteSQLOpr,branchr4r4r4r5s                                             !  6   %   &        <  * 9 , " % %  (