U of @sUddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd lm Z dd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddl!m"Z"ddl#m$Z$ddl#m%Z%ddl&m'Z'ddl"m(Z(erddlm)Z)ddl*m+Z+dd l,m-Z-dd!l,m.Z.dd"lm/Z/dd#lm0Z0dd$l1m2Z2dd%l3m4Z4dd&l5m6Z6dd'l5m7Z7dd(l5m8Z8dd)l5m9Z9dd*l#m:Z:e;e<Z=d+d,d-d.d/d0Z>e"j?d1d2Z@d+d3d-d4d5d6ZAe@Bd7d+d3d8d-d9d:d;ZCdd?d@ZDe"EejFejGejHejIejHejIdAZJdBeKdC<dDdEdFdGdHdIdJZLdDdEdFdKdHdLdMZMdEdFdNdOdPdQZNejOdRdSdFdFdTd+d=dUdVdWdXZPedYeeeefdZZQe@Bd[d+dTdRdSd\d\d-d]d^d_ZRd`daZSe@Bdbd+dcdRdSdSddddd-dedfdgZTe@Bdbd+dcdRdSdhddddd-dedidjZUe@Bdbd+dcdRdSdSddddd-dedkdlZVdmd+dRdndodpZWdqdqdrdsdtZXd+dcdRdqdqddddd-dedudvZYdqdqd-dwdxdyZZdzd{Z[e@Bdbd+dcdRdSdSddddd|ded}d~Z\e@Bdbd+dcdRdSdhddddddeddZ]e@Bd[d+dTdRdSdFdFd-dddZ^e@Bd[d+dTdRdSd\d\d-dddZ_dS)) annotationsN)Any)cast)Dict)Iterator)Mapping)Optional)Set)Tuple) TYPE_CHECKING)TypeVar)Union)event)inspectschema)text)types) expression)ForeignKeyConstraint)Index)UniqueConstraint) OrderedSet)util) is_index_sig) is_uq_sig)ops) sqla_compat)Literal) Inspector) quoted_name) TextClause)Column)Table)AutogenContext) DefaultImpl) AlterColumnOp)MigrationScript)ModifyTableOps) UpgradeOps)_constraint_sigr%r(None)autogen_contextmigration_scriptreturncCs,|jd}|jd}t||||dS)N)Zupgrade_ops_listZdowngrade_ops_list_produce_net_changesZ reverse_into)r-r. upgrade_opsZ downgrade_opsr3J/opt/hc_python/lib/python3.8/site-packages/alembic/autogenerate/compare.py_populate_migration_script;s   r5T)Zuselistr*)r-r2r/csj}|dk stjdd}t|}|jj}|r`t|}| d| || dndh}fdd|D}jdk stt djj ||dS)Ninclude_schemasFZinformation_schemacsh|]}|dir|qSrrun_name_filters).0sr-r3r4 ]sz'_produce_net_changes..r) connectionAssertionErroroptsgetrdialectdefault_schema_namesetZget_schema_namesdiscardadd comparatorsdispatchname)r-r2r=r6 inspectordefault_schemaschemasr3r;r4r1Hs(      r1rz$Union[Set[None], Set[Optional[str]]])r-r2rKr/c sj}t}jj}jj}|D]Ft|jd}|krL|jjg}|fdd|Dq tddj D||fg}t ||||dS)Nrc3s(|] }|ddir|fVqdS)table schema_nameNr7)r9tnamer-rMr3r4 }sz&_autogen_for_tables..cSsg|]}|j|jfqSr3)rrH)r9rLr3r3r4 sz'_autogen_for_tables..) rIrCmigration_contextversion_table_schema version_tableZget_table_names differenceupdaterZ sorted_tables_compare_tables) r-r2rKrIconn_table_namesrSrTtablesmetadata_table_namesr3rOr4_autogen_for_tablesgs4 r[rCr )rXrZrIr2r-r/c s|jjjtfdd|D}fddt||D}|}||D]\}}|r^d||fn|} |||f} | |dddrF|jtj | t d| tj |g|d } td| ||d| | sF|j| qFt} ||D]\}}t||} | | jk} tj|| |d }| sHt|d jj|t||||dd drtj |g|d } td| |||d| s|j| |jtj |t d | q||}t}i}|D]f\}}t||} | |jk} tj|||d }| s,t|d jj|t||||||f<qt|d ddD]\}}|p\d}|rpd||fn|} |||f} |j| }| |dd|rLtj |g|d } t |||| | |td| |||| W5QRX| sL|j| qLdS)Ncs$g|]\}}|kr|nd|fqSNr3)r9rrN)rJr3r4rQsz#_compare_tables..cs(i|] \}\}}|jt||qSr3)Ztable_key_to_table sa_schema_get_table_key)r9Zno_dflt_schemarrNr;r3r4 s   z#_compare_tables..%s.%srLFzDetected added table %rrZcolumn_reflectTzDetected removed table %rcSs|dp d|dfS)Nrr3)xr3r3r4z!_compare_tables..)key)!bindrArBrziprUrun_object_filtersrappendZ CreateTableOpZ from_tableloginfor)rFrGZis_emptyr]ZMetaDatar^rYr$rlistenrRimplZ_compat_autogen_column_reflectrZ_reflect_tableZ DropTableOp intersectionsorted_compare_columns)rXrZrIr2r-Z#metadata_table_names_no_dflt_schemaZtname_to_tabler:rNrHmetadata_tablemodify_table_opsZremoval_metadataexiststZexisting_tablesZexisting_metadataZconn_column_info conn_tabler3)r-rJr4rWs                     rW)ascdescZ nulls_firstZ nulls_last nullsfirst nullslastzMapping[str, Any]_IndexColumnSortingOpsr&zDict[str, Any]r$zOptional[Index])rnparamsrvr/c Csg}|d}t|dD]x\}}|dkrLd|ks6t|d|}t|}n|}|j|}|r||kr||D]} | tkrnt| |}qn||qtj|df||d|d| |d} d|kr|d| j d<| S) NZcolumn_sorting column_namesZ expressionsrHunique)r~_tableindexZduplicates_constraint) r@ enumerater>rcr{rjr]r adjust_reflected_dialect_optionsrl) rnr|rvZexprsZsortingnumZcol_namerHitemoperatorixr3r3r4 _make_index(s6         rrcsLtjfdd|dDd|di||d}d|krH|d|jd<|S)Ncsg|]}j|qSr3rr9cnamervr3r4rQLsz+_make_unique_constraint..r}rHunique_constraintduplicates_index)r]rrrl)rnr|rvuqr3rr4_make_unique_constraintHs rr)r|rvr/c s|d|dr d|df|di}tjfdd|dDfdd|d D|d |d |d |d |dd}||S)NZreferred_tableZreferred_schemar`optionscsg|]}j|qSr3rrrr3r4rQ`sz%_make_foreign_key..Zconstrained_columnscsg|]}d|fqS)r`r3)r9n)rNr3r4rQasZreferred_columnsonupdateondelete deferrable initiallyrH)rrrrrH)r@r]rZappend_constraint)r|rvrconstr3)rvrNr4_make_foreign_keyVs  rz Optional[str]zUnion[quoted_name, str]r)zIterator[None])rrNrvrrrsr-rIr/c #szrdfn}tdd|jD}dd|jD} fdd|jD} || D]D} | | | dddrZ|jtj| | t d || qZ| | D]h} | | } |j| }| | dd|sqtj | d }t d|| || |r|j|qdVt| |D]L} |j| | dd dr(|jtj|j| t d || q(dS) Nr`css|]}|js|jVqdSr\systemrHr9rr3r3r4rPxsz#_compare_columns..cSsi|]}|js|j|qSr3rrr3r3r4r_{sz$_compare_columns..c s*i|]"}|jddr|j|qS)columnZ table_namerM)r8rHrr-rrNr3r4r_srFzDetected added column '%s.%s'rTzDetected removed column '%s.%s')rrrUrirrjZ AddColumnOpZfrom_column_and_tablenamerkrlror'rFrGZ has_changesrCZ DropColumnOp)rrNrvrrrsr-rIrHZmetadata_col_namesZmetadata_cols_by_nameZconn_col_namesrZcolname metadata_colconn_colalter_column_opr3rr4rqms   rq_C)boundrLzOptional[Table])r- modify_opsrrNrvrrr/c$sdj}dk|dkjj|dk rBdd|jD}t|j}n t}t}t} } d d} dk rTz|j d} d Wn&tk rYnBt k rYn0X fdd| D} | D]} | drd} qz|j d} Wntk rYnX fd d| D} r"t} nfd d| D} d dfd d | DD} | rnt | | ||j | | ||fdd|D} fdd|D}fdd| D}fdd| D}fdd| |D}dd|Ddd|Ddd||D}fddtD}dd|D}dd| D}fdd| D}dd fdd }dd fdd }ddd!d" fd#d$ }tt||D]~}||}t|r|j|krqnT||kr0||\}tfd%d |Dr8|j|kr8|||n||qtt||D]}||}||kr||\}t|r}n|}n||}t|t|kr||||n^||}|jr||||jn<|jrt|rd&nd'} td(| |j|jn |jsNt qNtt||D]}!||!}"||"q&|D]}#|#|krB|||#qBdS))NcSsh|]}t|tjr|qSr3) isinstancer]rr9rr3r3r4r<s z/_compare_indexes_and_uniques..FrTc s(g|] }|dddr|qS)rHrrr7rrr3r4rQsz0_compare_indexes_and_uniques..rc s(g|] }|dddr|qS)rHrrr7r9rrr3r4rQscsh|]}t|qSr3)r)r9Zuq_defrvrnr3r4r< scSsh|]}|dk r|qSr\r3)r9rr3r3r4r<sc3s|]}t|VqdSr\)rrrr3r4rPsz/_compare_indexes_and_uniques..csh|]}|qSr3_create_metadata_constraint_sigrrnr3r4r<3scsh|]}|qSr3rrrr3r4r<8scsh|]}|qSr3 _create_reflected_constraint_sigrrr3r4r<<scsh|]}|qSr3rrrr3r4r<@scs$i|]}|jrtt||qSr3)Zis_namedrstrZmd_name_to_sql_namerr;r3r4r_Esz0_compare_indexes_and_uniques..cSsi|] }|j|qSr3rHrr3r3r4r_NscSsi|] }|j|qSr3rrr3r3r4r_OscSs i|]}t|jr|j|qSr3)rZconstraint_name_stringrHrr3r3r4r_Ps csi|]}|||fqSr3r3)r9rH)conn_indexes_by_nameconn_uniques_by_namer3r4r_VscSsi|] }|j|qSr3unnamedrr3r3r4r__scSsi|] }|j|qSr3rrr3r3r4r_`scs$i|]}t|jjs|j|qSr3)rZ_constraint_is_namedrrArrr;r3r4r_csr+objcst|rH|j|jdddrjtj|jt d|j|j ndt |rsXdSs`rddS|j|jdddrjtj |jt d|j|j ndstdS)NrFz!Detected added index '%r' on '%s'rz+Detected added unique constraint %r on '%s')rrirrHrrj CreateIndexOp from_indexrkrlr}rAddConstraintOpfrom_constraintr>r)r-is_create_table is_drop_tablersupports_unique_constraintsr3r4 obj_addedssF z/_compare_indexes_and_uniques..obj_addedcst|rT|jrsdS|j|jdddrjtj|jt d|jnZt |rsdrhdS|j|jdddrjtj |jt d|jndstdS)NrTzDetected removed index %r on %rrz+Detected removed unique constraint %r on %rF)rZ is_uniquerirrHrrj DropIndexOprrkrlrDropConstraintOprr>r)r-rrrrrNr3r4 obj_removeds>  z1_compare_indexes_and_uniques..obj_removedroldnewmsgcst|rlt|st|j|jdd|jrtd|j|jtj |jjtj |jntt |rt |st|j|jdd|jrtd|j|jtj |jjtj|jndstdS)NrFz#Detected changed index %r on %r: %srz/Detected changed unique constraint %r on %r: %s)rr>rirrHrkrlrrjrrrrrrrr)r-rrNr3r4 obj_changedsN    z1_compare_indexes_and_uniques..obj_changedc3s|]}j|jkVqdSr\r)r9Zmeta_idx)conn_idxr3r4rPsrzunique constraintz5Cannot compare %s %r, assuming equal and skipping. %s)!rIrRrn constraintsrCZindexes frozensetZget_unique_constraintsNotImplementedError TypeErrorr@Z get_indexes_correct_for_uq_duplicates_uixrAZcorrect_for_autogen_constraintsunionrorprUrrallrtypeZcompare_to_reflectedZ is_differentmessageZis_skiprkrlrHZis_equalr>)$r-rrrNrvrrrImetadata_unique_constraintsmetadata_indexesZ conn_uniques conn_indexesZ+unique_constraints_duplicate_unique_indexesrZmetadata_unique_constraints_sigZmetadata_indexes_sigconn_unique_constraintsZconn_indexes_sigZmetadata_namesZ conn_namesZdoubled_constraintsZconn_uniques_by_sigZmetadata_uniques_by_sigZunnamed_metadata_uniquesrrrZ removed_nameZconn_objZconn_uqZ existing_nameZ metadata_objZ comparisonthingZ added_namerZuq_sigr3) r-rrrvrrnrrrrrrNr4_compare_indexes_and_uniquess*                     ! %            rc sfdd|D}dd|D}fdd|D}fdd|D} dd|D} d d|D} | D]F} | |kr| | j|kr|| | qd| | krd|| | qddS) Ncsg|]}t||fqSr3)r_get_constraint_final_namer9consrAr3r4rQ.sz2_correct_for_uq_duplicates_uix..cSsh|]\}}|dk r|qSr\r3r9rHrr3r3r4r<3sz1_correct_for_uq_duplicates_uix..cs$h|]\}}|dkr|jqSr\)rrrrr3r4r<7scsh|]}|jrt|qSr3)r~rrrrr3r4r<=scSsi|]}|jr|j|qSr3)r~rHrr3r3r4r_Esz2_correct_for_uq_duplicates_uix..cSsi|]}|jdr|j|qS)r)rlrHrr3r3r4r_Gs )rrrD) rrrrrArnZmetadata_cons_namesZmetadata_uq_namesZunnamed_metadata_uqsZmetadata_ix_namesZ conn_ix_namesZuqs_dupe_indexesZoverlapr3)rArnr4rs6   rrr'z Column[Any])r-rrrNrrrr/c Csv|j}|j}||_||k rrt|j|jr4t|sDt|j|jrTtd||n||_ td|rhdnd||dS)Nz3Ignoring nullable change on identity column '%s.%s'zDetected %s on column '%s.%s'ZNULLzNOT NULL) ZnullableZexisting_nullabler_server_default_is_computedserver_defaultZ_nullability_might_be_unset_server_default_is_identityrkrlZmodify_nullable) r-rrrNrrrZmetadata_col_nullableZconn_col_nullabler3r3r4_compare_nullableZs8   rr!cCsF|jj|krd|jd<n*|jdkr.d|jd<n|jdkrBd|jd<dS)NT autoincrementF)rLZ_autoincrement_columnkwr)r-rrrNrrrr3r3r4_setup_autoincrements     rc Cs||j}||_|j}|jtjkr0td||dS|jtjkrNtd||dS|j||} | rx||_ td||||dS)Nz3Couldn't determine database type for column '%s.%s'z:Column '%s.%s' has no type within the model; can't comparez-Detected type change from %r to %r on '%s.%s') rZ existing_typeZ_type_affinitysqltypesZNullTyperkrlrR _compare_typeZ modify_type) r-rrrNrrrZ conn_typeZ metadata_typeZisdiffr3r3r4rs<   rz Optional[Any])metadata_defaultr-r/cCsPt|tjr:t|jtr |j}nt|jj|jddid}t|trH|SdSdS)N literal_bindsTrAcompile_kwargs)rr]Z DefaultClauseargrcompilerA)rr-r3r3r4"_render_server_default_for_compares   rr)sqltextr/cCstdd|S)zwe want to warn if a computed sql expression has changed. however we don't want false positives and the warning is not that critical. so filter out most forms of variability from the SQL text. z[ \(\)'\"`\[\]\t\r\n]ra)resublower)rr3r3r4_normalize_computed_defaultsrc Cstttj|jjj|jddid}t|}t |jtjrnt|jjj|jddid}|dkrdd}qrt|}nd}||krt ||dS)NrTrra) rrr]ZComputedrrrrArr_warn_computed_not_supported) r-rrrNrrrrendered_metadata_defaultrendered_conn_defaultr3r3r4_compare_computed_defaults0  r)rNrr/cCstd||fdS)Nz,Computed default on %s.%s cannot be modified)rwarn)rNrr3r3r4r src Cs&|jj}||j|j\}} } || fSr\)rRrn_compare_identity_defaultr) r-rrrNrrrrndiffZ ignored_attris_alterr3r3r4r s  rzOptional[bool]cCs|j}|j}|dkr |dkr dSt|rHtjs4dSt|||||||St|r`t||dSt||r||_t|||||||\} } | r||_ | rt d||t | nRt ||} |rtt|jjnd} ||_|j||| | } | r||_ t d||dS)NFzIDetected server default on column '%s.%s': identity options attributes %sz)Detected server default on column '%s.%s')rrrZhas_computed_reflectionrrrZexisting_server_defaultrZmodify_server_defaultrkrlrprrrrrrR_compare_server_default)r-rrrNrrrrZconn_col_defaultrrrrZis_diffr3r3r4rsx     rzOptional[Literal[False]]c Cs`|jdk st|jjsdS|j}|j}|dkr:|dkr:dS||_||kr\||_td||dS)NFzDetected column comment '%s.%s')rAr>supports_commentscommentexisting_commentZmodify_commentrkrl) r-rrrNrrrZmetadata_commentZconn_col_commentr3r3r4_compare_column_commentvs r)r-rsrrNrvrrr/csdks|dkrdSj}dd|jD}fdd|jdD}fdd|D} jjjj| |fdd|D} fdd| D} |rd |d krd d | D} d d | D} ndd | D} dd | D} dd | D}dd | D}fdd}fdd}t| | D]>}| ||| kr*j|krX|jj nd}||q*t| | D]>}| ||| krxj|kr|jj nd}||qxdS)NcSsh|]}t|tjr|qSr3)rr]rr9Zfkr3r3r4r<s z(_compare_foreign_keys..c s(g|] }|dddr|qS)rHforeign_key_constraintrr7rrr3r4rQsz)_compare_foreign_keys..rcsh|]}t|qSr3)r)r9rrr3r4r<scsh|]}|qSr3rrrr3r4r<scsh|]}|qSr3rrrr3r4r<srrcSsi|] }|j|qSr3rrr3r3r4r_sz)_compare_foreign_keys..cSsi|] }|j|qSr3rrr3r3r4r_scSsi|] }|j|qSr3Zunnamed_no_optionsrr3r3r4r_scSsi|] }|j|qSr3rrr3r3r4r_scSsi|]}|jdk r|j|qSr\rrr3r3r4r_s cSsi|]}|jdk r|j|qSr\rrr3r3r4r_s csd|j|jdd|r`jtjjtdd |j d |j |j rVd|j nd|j dS)NrFz1Detected added foreign key (%s)(%s) on table %s%s, %s.ra)rirrHrrjZCreateForeignKeyOprrkrljoinsource_columnstarget_columns source_schema source_tabler compare_to)r-rrsr3r4_add_fks"   z&_compare_foreign_keys.._add_fkcsd|j|jdd|r`jtj|jtdd |j d |j |j rVd|j nd|j dS)NrTz3Detected removed foreign key (%s)(%s) on table %s%srrra)rirrHrrjrrrkrlrrrrrr)r-rsr3r4 _remove_fks"   z)_compare_foreign_keys.._remove_fk) rIrZget_foreign_keysrRrnZcorrect_for_autogen_foreignkeysrCrUrHr)r-rsrrNrvrrrIZ metadata_fksZ conn_fks_listZconn_fksZmetadata_fks_sigZ conn_fks_sigZconn_fks_by_sigZmetadata_fks_by_sigZmetadata_fks_by_nameZconn_fks_by_namer r Z removed_sigrZ added_sigr3)r-rvrrnrsrrNr4_compare_foreign_keyssh       r cCs|jdk st|jjsdS|dks*|dkr.dS|jdkrF|jdkrFdS|jdkrv|jdk rv|jtj||j|dn*|j|jkr|jtj||j|j|ddS)N)rr)rAr>rrrrjZDropTableCommentOpZCreateTableCommentOp)r-rsrrNrvrrr3r3r4_compare_table_comments0  r )` __future__r contextlibloggingrtypingrrrrrrr r r r r Z sqlalchemyrrrr]rrrZsqlalchemy.sqlrZsqlalchemy.sql.schemarrrZsqlalchemy.utilrrarZ ddl._autogenrr operationsrrrZsqlalchemy.engine.reflectionr Zsqlalchemy.sql.elementsr!r"r#r$Zalembic.autogenerate.apir%Zalembic.ddl.implr&Zalembic.operations.opsr'r(r)r*r+ getLogger__name__rkr5Z DispatcherrFr1Z dispatch_forr[rWZ immutabledictrwrxryrzr{__annotations__rrrcontextmanagerrqrrrrrrrrrrrrrr r r3r3r3r4s                                          *   Hc?(* ,W{