|L i.ddlmZddlmZddlZddlZddlmZddlZddl Z ddl Z ddl m Z ddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!er0ddlm"Z"ddlm#Z#ddlm$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.ej`d#Z1ej`d$Z2ej`d%Z3ej`d&Z4d'Z5Gd(d)Z6Gd*d+ejHZ7y#e/$rdZ-YswxYw),) annotations)contextmanagerN)Path) ModuleType)Any)cast)Iterator)List)Optional)Sequence)Set)Tuple) TYPE_CHECKING)Unionrevision) write_hooks)util) migration)compat)not_none)_preserving_path_as_str) _GetRevArg) _RevIdType)Revision)Config)MessagingOptions)PostWriteHookConfig) RevisionStep) StampStep)ZoneInfo)ZoneInfoNotFoundErrorz (?!\.\#|__init__)(.*\.py)(c|o)?$z(?!\.\#|__init__)(.*\.py)$z([a-f0-9]+)\.py$z\w+z%(rev)s_%(slug)sc peZdZdZedddddgdedejf d$dZe d%dZ ejd&d Z ejd'd Z d(d Zed)d Ze d* d+d Z d, d-dZd.dZd/dZd0dZ d1dZ d2dZd3dZd4dZd3dZd4dZ d5dZ d6dZ d7dZd8dZ e d%dZ!d9dZ"d9dZ#d:dZ$d;d Z%dd"Z' d?d#Z(y)@ScriptDirectoryaHProvides operations upon an Alembic script directory. This object is useful to get information as to current revisions, most notably being able to get at the "head" revision, for schemes that want to test if the current revision in the database is the most recent:: from alembic.script import ScriptDirectory from alembic.config import Config config = Config() config.set_main_option("script_location", "myapp:migrations") script = ScriptDirectory.from_config(config) head_revision = script.get_current_head() (NFutf-8rc t||_|xsdD cgc] } t| c} |_||_|xsd|_||_||_tj|j|_ ||_ ||_ | |_ | |_tj |tj"st%j&d|dycc} w)Nr'zPath doesn't exist: z@. Please use the 'init' command to create a new scripts folder.)rdirversion_locations file_templatetruncate_slug_length sourcelessoutput_encodingr RevisionMap_load_revisions revision_maptimezonehooksrecursive_version_locationsmessaging_optsosaccessF_OKr CommandError) selfr+r-r.r,r/r0r4r5r6r7ps Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/alembic/script/base.py__init__zScriptDirectory.__init__Is"+3/0A0GR" +, #A &" +$8$>B!$.$001E1EF   +F(,yybgg&##&se,"" '" sCc,t|jS)zreturn a single version location based on the sole path passed within version_locations. If multiple version locations are configured, an error is raised. )str_singular_version_locationr<s r>versionszScriptDirectory.versionsos42233cj|j}t|dkDrtjd|dS)Nrz"Multiple version_locations presentr)_version_locationslenrr;)r<locs r>rBz*ScriptDirectory._singular_version_locationzs3%% s8a<##$HI Iq6MrEc|jr;|jDcgc]%}tj|j'c}St |j djgScc}w)NrD)r,rcoerce_resource_to_filenameabsoluterr+)r<locations r>rGz"ScriptDirectory._version_locationssf  ! !!% 6 600:CCE  :.779: : s*A-c#zK|jDcgc]}|js|}}t}|D]y}tj ||D]^}|j }||vrt jd|d1|j|tj||}|[|`{ycc}ww)NzFile zC loaded twice! ignoring. Please ensure version_locations is unique.) rGexistssetScript _list_py_dirresolverwarnadd _from_path)r<verspathsdupes file_path real_pathscripts r>r2zScriptDirectory._load_revisionss"&"9"9K$T[[]KK D#00t<  %--/ %II {+EE )$**4;>   LsB;B6B6BB;c  |jd}|tjd|jd}| t|}nd}|j }|r|t j dd|jd}ttj||jdt||jd|jd d |j|jd |j||j S) zProduce a new :class:`.ScriptDirectory` given a :class:`.Config` instance. The :class:`.Config` need only have the ``script_location`` key present. script_locationNz0No 'script_location' key found in configuration.r.rr6r-r/r0r(r4) r-r.r/r0r,r4r5r6r7)get_alembic_optionrr;intget_prepend_sys_paths_listsyspathget_alembic_boolean_optionr&rK_default_file_templateget_version_locations_listget_hooks_listr7)clsconfigr^tslr.prepend_sys_pathrvls r> from_configzScriptDirectory.from_configs!334EF  "##B ''(>? ?#&s8 #' !<<> +CHHRaL//0MN  , ,_ = 33!7"688F"55!7%??A..z:'')(+!00  rEc#K dy#tj$r_}|tt|j}|tt|j }|sd}|||dz}t j||d}~wtj$rR}|sd}||xs |jt j|jdz}t j||d}~wtj$r,}|d|jz}t j||d}~wtj$r(} t j| jd| d} ~ wwxYww)NzgRequested range %(start)s:%(end)s does not refer to ancestor/descendant revisions along the same branchstartendzMultiple head revisions are present for given argument '%(head_arg)s'; please specify a specific target revision, '@%(head_arg)s' to narrow to a specific head, or 'heads' for all heads)head_argheadsz(Can't locate revision identified by '%s'r)rRangeNotAncestorErrorrrlowerupperrr; MultipleHeadsargumentformat_as_commarsResolutionError RevisionErrorargs) r<ancestormultiple_headsrprq resolutionrnamhreerrs r>_catch_revision_errorsz&ScriptDirectory._catch_revision_errorssE# : -- 7}S#)),{3 *J E#">>H##H-3 6%% <!J,.2;;--bhh7/N##N3 ;'' 8!GKK ##J/R 7%% :##CHHQK0c 9 :sJE EEAA66E A CE/'DE,#EEEc#K|j||5|jj||ddD]}tt| dddy#1swYyxYww)a-Iterate through all revisions. :param base: the base revision, or "base" to start from the empty revision. :param head: the head revision; defaults to "heads" to indicate all head revisions. May also be "head" to indicate a single head revision. roTF) inclusiveassert_relative_lengthN)rr3iterate_revisionsrrQ)r<baseheadrevs r>walk_revisionszScriptDirectory.walk_revisionssl ( (t ( > (((::dd5; (63'' ( ( ( (sA#7A A#A A#c|j5tttdf|jj |cdddS#1swYyxYw)zReturn the :class:`.Script` instance with the given rev identifier, symbolic name, or sequence of identifiers. .N)rrrrQr3 get_revisionsr<id_s r>rzScriptDirectory.get_revisions sM  ( ( * fck"!!//4   s 2A  Ac|j5ttt|jj |cdddS#1swYyxYwN)rrr rQr3_get_all_currentrs r>get_all_currentzScriptDirectory.get_all_currentsF  ( ( * NF T%6%6%G%G%LM N N Ns 0A  Ac|j5tt|jj |cdddS#1swYyxYw)zReturn the :class:`.Script` instance with the given rev id. .. seealso:: :meth:`.ScriptDirectory.get_revisions` N)rrrQr3 get_revisionrs r>rzScriptDirectory.get_revisionsD ( ( * E 1 1 > >s CD E E Es )AA c|j5|jj|\}}dddsy|dk(r|S|dS#1swYxYw)z[Convert a symbolic revision, i.e. 'head' or 'base', into an actual revision number.Nrsr)rr3_resolve_revision_number)r<rr branch_names r>as_revision_numberz"ScriptDirectory.as_revision_number'sa  ( ( * O#00II#N C O G^Jq6M O Os AAc httt|jj||fi|S)aIterate through script revisions, starting at the given upper revision identifier and ending at the lower. The traversal uses strictly the `down_revision` marker inside each migration script, so it is a requirement that upper >= lower, else you'll get nothing back. The iterator yields :class:`.Script` objects. .. seealso:: :meth:`.RevisionMap.iterate_revisions` )rr rQr3r)r<rvrukws r>rz!ScriptDirectory.iterate_revisions8s6* V  /D   / /u C C  rEc|jd5|jjcdddS#1swYyxYw)aGReturn the current head revision. If the script directory has multiple heads due to branching, an error is raised; :meth:`.ScriptDirectory.get_heads` should be preferred. :return: a string revision number. .. seealso:: :meth:`.ScriptDirectory.get_heads` z}The script directory has multiple heads (due to branching).Please use get_heads(), or merge the branches using alembic merge.r~N)rr3get_current_headrCs r>rz ScriptDirectory.get_current_headRsE ( (!)  8$$557 8 8 8s 7Ac@t|jjS)aUReturn all "versioned head" revisions as strings. This is normally a list of length one, unless branches are present. The :meth:`.ScriptDirectory.get_current_head()` method can be used normally when a script directory has only one head. :return: a tuple of string revision numbers. )listr3rsrCs r> get_headszScriptDirectory.get_headsjsD%%++,,rEcx|j}t|dkDrtjd|r|dSy)a#Return the "base" revision as a string. This is the revision number of the script that has a ``down_revision`` of None. If the script directory has multiple bases, an error is raised; :meth:`.ScriptDirectory.get_bases` should be preferred. rz@The script directory has multiple bases. Please use get_bases().rN) get_basesrHrr;)r<basess r>get_basezScriptDirectory.get_basewsD  u:>##* 8OrEc@t|jjS)zreturn all "base" revisions as strings. This is the revision number of all scripts that have a ``down_revision`` of None. )rr3rrCs r>rzScriptDirectory.get_basessD%%++,,rEc|jd|5|j||d}tt|Dcgc],}tj j |j|.c}cdddScc}w#1swYyxYw)NzFDestination %(end)s is not a valid upgrade target from current head(s)r}rqT) implicit_base)rrreversedrr MigrationStepupgrade_from_scriptr3r< destination current_revrevsr\s r> _upgrade_revszScriptDirectory._upgrade_revss ( (*)  ))[*D'tDz2 '';;%%v    s+B1A<0B<BB c|jd|5|j||d}|Dcgc],}tjj |j |.c}cdddScc}w#1swYyxYw)NzHDestination %(end)s is not a valid downgrade target from current head(s)rT)select_for_downgrade)rrrrdowngrade_from_scriptr3rs r>_downgrade_revszScriptDirectory._downgrade_revss ( (*)  ))*D# ''==%%v    sA/1A*A/*A//A8cN|jd5|j|}g}|sd}g}tj|D]G}|s|j |j j ttt||dItj|}|j|xsdg}|D]}|K|j |D cgc]/} tj| jddd|j 1c} Q||vrVt|j j|g} t|j j!|g} | j#|rl| j#|rJ|D cgc]} | j} } tj| |jdd|j } |j%| | j#|rY|D cgc]} | j} } tj| |jdd|j } |j%| tjd|jdd|j } |j%| |cdddScc} wcc} wcc} w#1swYyxYw)NzCMultiple heads are present; please specify a single target revisionrrT)include_dependenciesFr*)rrrto_tupleextendr3filter_for_lineagerr rQ unique_listrr"rrP_get_descendant_nodes_get_ancestor_nodes intersectionappend)r<rrs heads_revsstepsfiltered_headsrdestsdestr descendants ancestors todo_headssteps r> _stamp_revszScriptDirectory._stamp_revss ( (%) Y ++E2JE!+-N}}X. "))))<< &!1:>15= "--n=N&&x0:TFE> <LL)7 !%&// $ $ % $ $ 1 1   ^+"%%;;TFC   1 1 E Etf MN ++N; )55nEEE @sY Y < 6"L"LMY Y s?4JBJ4J A?JJAJ+J>BJ JJ$cDtj|jdy)zRun the script environment. This basically runs the ``env.py`` script present in the migration environment. It is called exclusively by the command functions in :mod:`alembic.command`. env.pyN)rload_python_filer+rCs r>run_envzScriptDirectory.run_envs dhh1rEc@tt|jdS)Nr)rArr+rCs r>env_py_locationzScriptDirectory.env_py_location#s4(+,,rEc tjd|jfi|j5tj|||j fddi|dddy#1swYyxYw)NzAppending to existing append_with_newlinesTrstatusrLr7template_to_filer0r<srcrrs r>_append_templatez ScriptDirectory._append_template'su [[$T]]_$5 6 !!   ! !$$ &*        s %A  A)c tjd|jfi|j5tj|||j fi|dddy#1swYyxYwNz Generating rrs r>_generate_templatez"ScriptDirectory._generate_template4se [[$--/* + /3/B/B  I  ! !#tT-A-A HR H I I Is #AA'ctjd|jfi|j5t j ||dddy#1swYyxYwr)rrrLr7shutilcopy)r<rrs r> _copy_filezScriptDirectory._copy_file:sR [[$--/* + /3/B/B  # KKT " # # #s AAc|j}|jsBtjd|fi|j5t j |dddyy#1swYyxYw)NzCreating directory )rLrOrrr7r8makedirs)r<rcs r>_ensure_directoryz!ScriptDirectory._ensure_directory@sg}}{{}%dV,040C0C " D! " " " "s A$$A-c|jttjd t|j}|$ t|jj }t j jt jjj|}|St j j}|S#t$rd}YwxYw#t$r$tjd|jzdwxYw)NzePython >= 3.9 is required for timezone support or the 'backports.zoneinfo' package must be installed.zCan't locate timezone: %s)tz) r4r#rr;r$rvdatetimenowutc astimezone)r<tzinfo create_dates r>_generate_create_datez%ScriptDirectory._generate_create_dateHs == $''J  !$--0~ %dmm&9&9&;t+|j@d|ftC|tjDtGd| Dt jH|tjD| tjJ||ndd| |jL}|rtOjP|| tjS||}|y|r?|j<s3t j d|jd|d|jTd|jjW||S#tj$r(} t j | j d| d} ~ wwxYw#1swYxYwcc}wcc}}w#1swYxYw#tj$r(} t j | j d| d} ~ wwxYw)aGenerate a new revision file. This runs the ``script.py.mako`` template, given template arguments, and creates a new file. :param revid: String revision id. Typically this comes from ``alembic.util.rev_id()``. :param message: the revision message, the one passed by the -m argument to the ``revision`` command. :param head: the head revision to generate against. Defaults to the current "head" if no branches are present, else raises an exception. :param splice: if True, allow the "head" version to not be an actual head; otherwise, the selected head must be a head (e.g. endpoint) revision. Nrrz{Multiple heads are present; please specify the head revision on which the new revision should be based, or perform a merge.rr.rz"Duplicate head revisions specifiedrzAMultiple version locations present, please specify --version-pathzPath z0 is not represented in current version locationszeRevision %s is not a head revision; please specify --splice to create a new branch from this revisionzscript.py.makoc3<K|]}| |jndywrr).0hs r> z4ScriptDirectory.generate_revision..sKAMajjt;Ksz empty message) up_revision down_revision branch_labels depends_onrcommamessagezVersion z specified branch_labels z, however the migration file zb does not have them; have you upgraded your script.py.mako to include the 'branch_labels' section?),rQ verify_rev_idrr{rr;r|rrrr r3rrHrPrrG isinstance _script_pathparentrBrrLr,r _rev_pathis_headto_listrrrrr+rAtuple_rev_as_scalartuplerryr5r _run_hooksrVrc add_revision)r<revidrrsplicer version_pathr-rrrrsrrhead_ norm_path vers_pathrcdeprresolved_depends_onpost_write_hooksr\s r>generate_revisionz!ScriptDirectory.generate_revisioncsl: <D :   ' ( (&) #hz*C/0!!//5E #F{"{ # # s5z?c%j (##$HI I002  4**+a/" E()%888','9'9'@'@   ++8 $>>  -L,--- ))+ 00 I!!#y0 ## ~&$$   ! !  " "< 0~~lE7KH $U]]++M..)  ,,. $(<< #;%"$"3"3"@"@"EFL% ' !S#"3"33 \\* '# '  #'  + ,  E "66KUKK-- 6334GH#&&&2G   ::   " "4)9 : :&&tT2F > !5!5## ??M6;; @  &&v. g%% :##CHHQK0c 9 : # #L% '  H%% :##CHHQK0c 9 :skOAP.PP/-PP"&PPP)P#O<<PP PP&)Q$<#QQ$c t|j}djtj |xsdj }t ||jkDr%|d|jjddddz}d|j||||j|j|j|j|j|jd zz}t!||z S)N_rrz%s.py) rslugepochyearmonthdayhourminutesecond)r` timestampjoin_slug_refindallrurHr.rsplitr-rrrrrrr)r<rcrev_idrrrrfilenames r>rzScriptDirectory._rev_pathsK))+,xx((B78>>@ t9t00 03$334;;CCAFLD   #(($**"#((%,,%,,    DzH$$rE)r+Union[str, os.PathLike[str]]r-rAr.z Optional[int]r,z0Optional[Sequence[Union[str, os.PathLike[str]]]]r/boolr0rAr4 Optional[str]r5zlist[PostWriteHookConfig]r6rr7rreturnNoner!rAr!r)r!zSequence[Path])r!Iterator[Script])rirr!r&)NNNNN) r}r r~r rpr rqr rr r!zIterator[None])rrs)rrArrAr!r%)rrr!zTuple[Script, ...])rzTuple[str, ...]r!z Set[Script])rrAr!rQ)rr r!z%Optional[Union[str, Tuple[str, ...]]])rv!Union[str, Tuple[str, ...], None]rur&rrr!r%)r!r )r!z List[str])rrArrAr!List[RevisionStep])rrArr r!r')rrrsrr!zList[StampStep])r!r")rrrrrrr!r")rrrrr!r")rcrr!r")r!datetime.datetime)NFNNNN)rrArr rOptional[_RevIdType]rOptional[bool]rr)rz"Union[str, os.PathLike[str], None]r-r rr)rrr!Optional[Script]) rcrrrArr rr(r!r))__name__ __module__ __qualname____doc__rerr EMPTY_DICTr?propertyrDmemoized_propertyrBrGr2 classmethodrmrrrrrrrrrrrrrrrrrrrrrrr rr*rEr>r&r&5s:,4.0  &"&+-,1+/ , $ )$$, $  $$$ $)$&*$)$ !$L44  ;;(( ( T#'(,#!$( +:+:&+: +:  +: " +: +:+:\/6(((+( (& N E  ." 0 1     480 -,--0 $-: $\"\+5\ \| 2-- I # ">&*!&.2;?'++/UUU# U  U , U9U%U)UU Un%*%% % ' %  %rEr&c@eZdZUdZ dfd Zded< ded< eddZdZd ed < edd Z edd Z edd Z ddZ d ddZ d ddZddZe ddZe ddZxZS)rQzRepresent a single revision file in a ``versions/`` directory. The :class:`.Script` instance is returned by methods such as :meth:`.ScriptDirectory.iterate_revisions`. modulerrArcc ||_t||_t|||j t jt|dddt jt|dddy)Nrr*)defaultr)r dependencies) r5rrcsuperr?rrrgetattr)r<r5rrc __class__s r>r?zScript.__init__sl  +D1     --6 d3R  rEc,t|jSr)rrcrCs r>rzScript._script_path7sDIIrENr*_db_current_indicatorcHtjd|jdS))Return the docstring given in the script.z r)rsplitlongdocrCs r>docz Script.doc?sxx -a00rEc|jj}|rKt|jdr%|j|jj}|j Sy)r?_alembic_source_encodingr)r5r/hasattrdecoderDstrip)r<rBs r>rAzScript.longdocEsQkk!! t{{$>?jjKK8899; rEcd|j|jrdnd|jrdnd|jrdnd|jrdndd}|jr|d|j dz }n|d |j dz }|j r%|d tj|j zz }|jr%|d tj|jzz }|jr&|d tj|jdz }|d |jdz }|ddjd|jjDzz }|S)NzRev:  (head)r (branchpoint) (mergepoint) (current) zMerges: zParent: zAlso depends on: %s zBranches into: %s zBranch names: zPath: z %s c3&K|] }d|z yw)z %sNr*)rparas r>rz#Script.log_entry..ssL$hoLs)rris_branch_pointis_merge_pointr=_format_down_revisionr8rrynextrevrrcrrA splitlines)r<entrys r> log_entryzScript.log_entrySs^ MMI2 - $ 4 4 " <#22O : 66LB >      t'A'A'CE EE t'A'A'CE EE    ,$$T%6%67 E    *$$T\\2 E    $$T%7%78 E ,,  IIL$,,2I2I2KL L   rEc|jd|j|jrdnd|jrdnd|jrdndd|j S)N -> rIrrJrKz, )rRrrrPrQrBrCs r>__str__zScript.__str__wsU  & & ( MMI2 - $ 4 4 " <#22O : HH   rEc>|j}|rW|jr6|jdtj|jd|}n|jd|}|J|r1|j r%|dtj|j zz }|s|rA||j rdnd|jr|j sdnd|jrdndz }|r%||jrd nd|jrd ndz }|r|d |jzz }|S) Nz (z) -> rXz (%s)rIrz (effective head)rLrJrKz, %s) rr8rRrryr _is_real_headrr=rPrQrB)r<include_branches include_docinclude_parentstree_indicatorshead_indicatorstexts r> _head_onlyzScript._head_onlys5}}   ..0(():):; &*%?%?%A4H  2 2 Gd2243E3EFF FD o !// R7||D,>,>(!% : : B D  $($8$8 b@#'#6#6B> D  FTXX% %D rEcF|r |jS|j||||Sr)rVrb)r<verboser\r]r^r_s r> cmd_formatzScript.cmd_formats+ >> !?? + rEcZ|jsytj|jS)Nz)rrry_versioned_down_revisionsrCs r>rRzScript._format_down_revisions%!!''(F(FG GrEc g}tj|dD]\}}}|jjdr#t |D]}|j ||z |j rs|dz jr^|Dchc])}t|jjdd+c}|j fd jD|js|S|j|Scc}w)NT)top_down __pycache__.rc3hK|])}|jjddvr|z +yw)rkrN)namer@)rpycnames py_cache_paths r>rz&Script._list_py_dir..s8!88>>#.q1>&+!s/2)r path_walkrmendswithsortedrr/rOrr@riterdirr6sort) rh scriptdirrcrXrootdirsfilesrrorps @@r>rRzScript._list_py_dirs !'!1!1$!F  D$yy!!-0"5M . TH_- .## $} 4  '') KP>FX++11#6q9ELL!#0#8#8#:! 88  IIKA D %s.Dct|}|j|j}}|jrtj |}nt j |}|sy|jd}|jr)|jddk(}|jddk(}ndx}}|s|r0||z j} ||dzz j} | s|r| rytj||} t| dsBtj |} | stjd|d| jd} n | j} t| | ||z S) NrrcoFrz.Could not determine revision id from filename z. Be sure the 'revision' variable is declared inside the script (please see 'Upgrading from Alembic 0.1 to 0.2' in the documentation).)rrrmr/_sourceless_rev_filematch_only_source_rev_filegrouprOrrrE _legacy_revr;rrQ)rhrvrcdir_rpy_match py_filenameis_cis_o py_exists pyc_existsr5mrs r>rVzScript._from_pathsU Dzdiih   +11(;H,228>!$+D>>!$+D D4 4 +335I+"34<<>J DZ&&tX6vz*!!(+A'' (z*FF771:Hfhx88rE)r5rrrArcrr$r#)FFFTT) r\rr]rr^rr_rr`rr!rA)FFFT) rdrr\rr]rr^rr_rr!rA)rvr&rcrr!z List[Path])rvr&rcrr!r+)r,r-r.r/r?__annotations__r1rr=rBrArVrYrbrerRr3rRrV __classcell__)r;s@r>rQrQs   +  & B I( -1>0411   !!F "'! % $ $ &&& &  &  & &V"'! % $          H &'&/3& &&P39'39/K39 3939rErQ)8 __future__r contextlibrrr8pathlibrrrrbtypesrtypingrrr r r r r rrrrrrrruntimerrr util.pyfilesrrrrrirrr runtime.migrationr!r"zoneinfor#r$ ImportErrorcompiler}rrrrer&rQr*rEr>rs"%   2$$"),0-!."rzz"EF" #@Abjj,- 2::f +^ %^ %BE9X  E9WHs D<<EE