U ofV@sddlmZddlZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd l m Z dd lmZerdd lmZdd lmZddlmZddl mZdddddZdGddddddddZdHdddddddddddd d! d"d#Zd$ddd%d&ZdIdd'dddd(d)d*d+ZdJdddddd,d-d.ZdKdddddd,d/d0Zdddd1d2d3ZdLdddddd4d5d6ZdMddddd7d8d9Z dNdddd:d;d<Z!dOdddd:d=d>Z"dPdd'ddddd?d@dAZ#dddd1dBdCZ$dQddddDdEdFZ%dS)R) annotationsN)List)Optional) TYPE_CHECKING)Union) autogenerate)util)EnvironmentContext)ScriptDirectory)Config)Script) _RevIdType)ProcessRevisionDirectiveFnr None)configreturnc Cst|dt|D]B}ttj||d}t|}W5QRX|d||q|d|ddS)zNList available templates. :param config: a :class:`.Config` object. zAvailable templates: ZREADMEz%s - %sz1 Templates are used via the 'init' command, e.g.:z, alembic init --template generic ./scriptsN) print_stdoutoslistdirget_template_directoryopenpathjoinnextrstrip)rtempnameZreadmeZsynopsisr=/opt/hc_python/lib/python3.8/site-packages/alembic/command.pylist_templatess  rgenericFstrbool)r directorytemplatepackagerc Cs(t|tjr&t|r&td|tj||}t|tjsTtd|t|tjstj dtj |f|j t |W5QRXtj|d}tj dtj |f|j t |W5QRXt |}d}t|D]}tj||} |dkrd|jdk sttj |j}t|tjrRtjd|df|j n|j| ||d qtj| rtj||} || | q|rtjtj |d tjtj |d fD]>} tj d | f|j t| d W5QRXW5QRXq|dk s ttjd |df|j dS)akInitialize a new scripts directory. :param config: a :class:`.Config` object. :param directory: string path of the target directory. :param template: string name of the migration environment template to use. :param package: when True, write ``__init__.py`` files into the environment location as well as the versions/ location. z,Directory %s already exists and is not emptyzNo such template %rzCreating directory versionsNzalembic.ini.makozFile z already exists, skipping)Zscript_locationz __init__.pyzAdding wz9Please edit configuration/connection/logging settings in z before proceeding.)raccessF_OKrr CommandErrorrrrstatusabspathZmessaging_optsmakedirsr Zconfig_file_nameAssertionErrormsgZ_generate_templateisfileZ _copy_filer) rr#r$r%Z template_dirr&script config_filefile_ file_pathZ output_filerrrrinit*sn     r5headz Optional[str]zOptional[_RevIdType]z$Optional[ProcessRevisionDirectiveFn]z/Union[Optional[Script], List[Optional[Script]]]) rmessagersqlr6splice branch_label version_pathrev_id depends_onprocess_revision_directivesrc  st|} t||||||||| d } tj|| | | dt|d} |rjd} |r\tdfdd}n | r|fdd}n|rtd | rt || ||j d | W5QRXd d D}t |d kr|dS|SdS)aCreate a new revision file. :param config: a :class:`.Config` object. :param message: string message to apply to the revision; this is the ``-m`` option to ``alembic revision``. :param autogenerate: whether or not to autogenerate the script from the database; this is the ``--autogenerate`` option to ``alembic revision``. :param sql: whether to dump the script out as a SQL string; when specified, the script is dumped to stdout. This is the ``--sql`` option to ``alembic revision``. :param head: head revision to build the new revision upon as a parent; this is the ``--head`` option to ``alembic revision``. :param splice: whether or not the new revision should be made into a new head of its own; is required when the given ``head`` is not itself a head. This is the ``--splice`` option to ``alembic revision``. :param branch_label: string label to apply to the branch; this is the ``--branch-label`` option to ``alembic revision``. :param version_path: string symbol identifying a specific version path from the configuration; this is the ``--version-path`` option to ``alembic revision``. :param rev_id: optional revision identifier to use instead of having one generated; this is the ``--rev-id`` option to ``alembic revision``. :param depends_on: optional list of "depends on" identifiers; this is the ``--depends-on`` option to ``alembic revision``. :param process_revision_directives: this is a callable that takes the same form as the callable described at :paramref:`.EnvironmentContext.configure.process_revision_directives`; will be applied to the structure generated by the revision process where it can be altered programmatically. Note that unlike all the other parameters, this option is only available via programmatic use of :func:`.command.revision`. r7rr8r6r9r:r;r<r=)r>revision_environmentTz7Using --sql with --autogenerate does not make any sensecs||gSNZrun_autogeneraterevcontextrevision_contextrrretrieve_migrationss z%revision..retrieve_migrationscs||gSrA)Zrun_no_autogeneraterCrFrrrHs ziUsing --sql with the revision command when revision_environment is not configured does not make any sensefnas_sql template_argsrGcSsg|]}|qSrr).0r1rrr szrevision..rrN)r from_configdictautogenRevisionContextr asboolget_main_optionr*r rLrun_envZgenerate_scriptslen)rr7rr8r6r9r:r;r<r=r>script_directory command_args environmentrHscriptsrrFrrevision{s\:   r[z'Config'c st|}tdddddddddd }t|||fdd}t|||djd|W5QRXjd }g}|j D]}| | q||rt d |n |d dS) zCheck if revision command with autogenerate has pending upgrade ops. :param config: a :class:`.Config` object. .. versionadded:: 1.9.0 NTFr6r?cs||gSrArBrCrFrrrHs z"check..retrieve_migrationsrIz!New upgrade operations detected: z#No new upgrade operations detected.)r rOrPrQrRr rLrUZgenerated_revisionsZupgrade_ops_listextendZas_diffsr ZAutogenerateDiffsDetectedr)rrWrXrHZmigration_scriptZdiffsZ upgrade_opsrrFrchecksH     r^rzOptional[Script])r revisionsr7r:r<rc Csxt|}d|i}t|d}|rTdd}t|||d|d|W5QRX|j|pbt|fd||d|S) aMerge two revisions together. Creates a new migration file. :param config: a :class:`.Config` instance :param revisions: The revisions to merge. :param message: string message to apply to the revision. :param branch_label: string label name to apply to the new revision. :param rev_id: hardcoded revision identifier instead of generating a new one. .. seealso:: :ref:`branches` rr@cSsgSrArrCrrrnothingWszmerge..nothingF)rJrKrLT)refreshr6Z branch_labels) r rOr rSrTr rUZgenerate_revisionr<) rr_r7r:r<r1rLrYr`rrrmerge3s2  rb)rr[r8tagrc spt|d}dkr4|s$tddd\}fdd}t|||||dW5QRXdS)aUpgrade to a later version. :param config: a :class:`.Config` instance. :param revision: string revision target or range for --sql mode. May be ``"heads"`` to target the most recent revision(s). :param sql: if True, use ``--sql`` mode. :param tag: an arbitrary "tag" that can be intercepted by custom ``env.py`` scripts via the :meth:`.EnvironmentContext.get_tag_argument` method. N:Range revision not allowedcs |SrA)Z _upgrade_revsrCr[r1rrupgradeszupgrade..upgraderJrK starting_revdestination_revrcr rOr r*splitr rU)rr[r8rcrjrhrrgrrhms"   rhc st|d}dkr6|s$tddd\}n|rDtdfdd}t|||||dW5QRXdS) aRevert to a previous version. :param config: a :class:`.Config` instance. :param revision: string revision target or range for --sql mode. May be ``"base"`` to target the first revision. :param sql: if True, use ``--sql`` mode. :param tag: an arbitrary "tag" that can be intercepted by custom ``env.py`` scripts via the :meth:`.EnvironmentContext.get_tag_argument` method. Nrdrerfz/downgrade with --sql requires :cs |SrA)Z_downgrade_revsrCrgrr downgradeszdowngrade..downgraderirl)rr[r8rcrjrnrrgrrns*   rn)rrDrc sdt|dkrDfdd}t|dW5QRXn|D]}|jqNdS)zShow the revision(s) denoted by the given symbol. :param config: a :class:`.Config` instance. :param rev: string revision target. May be ``"current"`` to show the revision(s) currently applied in the database. currentcs |D]}|jq gSrA) get_revisionsr log_entryrDrEscrr1rr show_currentszshow..show_currentrJN)r rOr rUrprrq)rrDrursrrtrshows rw)r rev_rangeverboseindicate_currentrc st|}|dk r8d|kr$td|d\}}nd}}t|dpR}d fdd fdd }|d ks|d ks|r|||||n||||dS) zList changeset scripts in chronological order. :param config: a :class:`.Config` instance. :param rev_range: string revision range. :param verbose: output in verbose mode. :param indicate_current: indicate current revision. Nrdz9History range requires [start]:[end], [start]:, or :[end]r@rc sH|j|p d|pddD],}r*|j|k|_||jddddqdS)Nbaseheads)r{r6T)ryinclude_branches include_docZinclude_parents)walk_revisionsr[Z_db_current_indicatorr cmd_format)rr1r{r6Zcurrentsrs)rzryrr_display_historys  z!history.._display_historyc s:fdd}t|dW5QRXdS)NcsHdkr||n*dkr4||n|gS)NrorrC)rr{rr6r1rr_display_current_historys zMhistory.._display_history_w_current.._display_current_historyrv)r rU)rr1r{r6r)r)r{rr6r1r_display_history_w_currents z+history.._display_history_w_currentro)r)r rOr r*striprmrSrT) rrxryrzr1r{r6rYrr)rrzryrhistorys   r)rryresolve_dependenciesrcCsLt|}|r|d}n||}|D]}||j|dddq,dS)zShow current available heads in the script directory. :param config: a :class:`.Config` instance. :param verbose: output in verbose mode. :param resolve_dependencies: treat dependency version as down revisions. r|TF)r}Ztree_indicatorsN)r rOrpZ get_headsrr)rryrr1r|rDrrrr|'s  r|)rryrc s`t|D]Hjr|djdddfddfddjDDqdS) zShow current branch points. :param config: a :class:`.Config` instance. :param verbose: output in verbose mode. z%s %s T)r} c3s4|],}ddttj|jdddfVqdS)z%s -> %s FT)r}r~N)rVr!r[r)rMZrev_obj)rsryrr Pszbranches..c3s|]}|VqdSrA) get_revision)rMrDr1rrrXsN)r rOrZis_branch_pointrrrZnextrev)rryr)rsr1ryrbranchesBs    rc sBtfdd}t|ddW5QRXdS)zDisplay the current revision for a database. :param config: a :class:`.Config` instance. :param verbose: output in verbose mode. cs@rdt|jjj|D]}|q&gS)NzCurrent revision(s) for %s:)rr Zobfuscate_url_pw connectionZengineurlZget_all_currentrrCrr1ryrrdisplay_versionjsz current..display_versionT)rJZ dont_mutateNr rOr rU)rryrrrrro_s  ro)rr[r8rcpurgerc st||rjgd}t|D]F}d|kr\|dd\}}||kr\|dkrR|}n td|q n t|fdd}t||||r|ndt||d W5QRXdS)a'stamp' the revision table with the given revision; don't run any migrations. :param config: a :class:`.Config` instance. :param revision: target revision or list of revisions. May be a list to indicate stamping of multiple branch heads; may be ``"base"`` to remove all revisions from the table or ``"heads"`` to stamp the most recent revision(s). .. note:: this parameter is called "revisions" in the command line interface. :param sql: use ``--sql`` mode :param tag: an arbitrary "tag" that can be intercepted by custom ``env.py`` scripts via the :class:`.EnvironmentContext.get_tag_argument` method. :param purge: delete all entries in the version table before stamping. NrdrfzMStamp operation with --sql only supports a single starting revision at a timecst|SrA)Z _stamp_revsr to_tuplerCZdestination_revsr1rrdo_stampszstamp..do_stamp)rJrKrjrkrcr) r rOr Zto_listrmr*appendr rrU) rr[r8rcrrjZ _revisionZsrevrrrrstamp{s6    rc st||dkrBfdd}t||dW5QRXn:|}|s^td||D]}|sntt|j qbdS)zEdit revision script(s) using $EDITOR. :param config: a :class:`.Config` instance. :param rev: target revision. rocs.|std|D]}t|jqgS)NzNo current revisions)r r*rpopen_in_editorrrrrrr edit_currents  zedit..edit_currentrvz*No revision files indicated by symbol '%s'N) r rOr rUrpr r*r.rr)rrDrrevsrsrrredits   r)rr8rc Cs:t|}dd}t||||d|W5QRXdS)zCreate the alembic version table if it doesn't exist already . :param config: a :class:`.Config` instance. :param sql: use ``--sql`` mode. .. versionadded:: 1.7.6 cSs |gSrA)Z_ensure_version_tablerCrrrdo_ensure_versionsz)ensure_version..do_ensure_version)rJrKNr)rr8r1rrrrensure_versions r)r F) NFFr6FNNNNN)NNN)FN)FN)NFF)FF)F)F)FNF)F)& __future__rrtypingrrrrrrQr Zruntime.environmentr r1r Zalembic.configr Zalembic.script.baser Zalembic.script.revisionrrrr5r[r^rbrhrnrwrr|rrorrrrrrrsp             S&<=//FC!