
    +hV                       d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ ddlm	Z
 dd	lmZ dd
lmZ ddlmZ erd dlmZ d dlmZ d dlmZ ddlmZ ddZ	 	 d 	 	 	 	 	 	 	 	 	 d!dZ	 	 	 	 	 	 	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#dZd$dZ	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 d&dZ	 	 d'	 	 	 	 	 	 	 	 	 d(dZ	 	 d'	 	 	 	 	 	 	 	 	 d(dZd)dZ	 	 	 d*	 	 	 	 	 	 	 	 	 d+dZ	 d,	 	 	 	 	 	 	 d-dZ d.d/dZ!d.d/dZ"	 	 	 d0	 	 	 	 	 	 	 	 	 	 	 d1dZ#d)dZ$d.d2dZ%y)3    )annotationsN)List)Optional)TYPE_CHECKING)Union   )autogenerate)util)EnvironmentContext)ScriptDirectory)Config)Script)
_RevIdType)ProcessRevisionDirectiveFnc                   | j                  d       t        j                  | j                               D ]o  }t	        t        j
                  j                  | j                         |d            5 }t        |      j                         }ddd       | j                  d|       q | j                  d       | j                  d       y# 1 sw Y   AxY w)zNList available templates.

    :param config: a :class:`.Config` object.

    zAvailable templates:
READMENz%s - %sz1
Templates are used via the 'init' command, e.g.:z,
  alembic init --template generic ./scripts)	print_stdoutoslistdirget_template_directoryopenpathjoinnextrstrip)configtempnamereadmesynopsiss       B/var/www/html/venv/lib/python3.12/site-packages/alembic/command.pylist_templatesr!      s     01JJv<<>? ;GGLL668(HM
 	-F|**,H	- 	Ix:; LM
GH	- 	-s   0C

C	c                   t        j                  |t         j                        r-t        j                  |      rt	        j
                  d|z        t         j                  j                  | j                         |      }t        j                  |t         j                        st	        j
                  d|z        t        j                  |t         j                        s^t	        j                  dt         j                  j                  |      fi | j                  5  t        j                  |       ddd       t         j                  j                  |d      }t	        j                  dt         j                  j                  |      fi | j                  5  t        j                  |       ddd       t        |      }d}t        j                  |      D ]  }t         j                  j                  ||      }	|dk(  r| j                  J t         j                  j                  | j                        }t        j                  |t         j                        r%t	        j                  d|dfi | j                   |j!                  |	||	       t         j                  j#                  |	      st         j                  j                  ||      }
|j%                  |	|
        |rt         j                  j                  t         j                  j                  |      d
      t         j                  j                  t         j                  j                  |      d
      fD ]D  }t	        j                  d|fi | j                  5  t'        |d      5  	 ddd       ddd       F |J t	        j                  d|dfi | j                   y# 1 sw Y   xY w# 1 sw Y   8xY w# 1 sw Y   VxY w# 1 sw Y   xY w)ak  Initialize 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 emptyzNo such template %rzCreating directory Nversionszalembic.ini.makozFile z already exists, skipping)script_locationz__init__.pyzAdding wz9Please edit configuration/connection/logging settings in z before proceeding.)r   accessF_OKr   r
   CommandErrorr   r   r   statusabspathmessaging_optsmakedirsr   config_file_namemsg_generate_templateisfile
_copy_filer   )r   	directorytemplatepackagetemplate_dirr#   scriptconfig_filefile_	file_pathoutput_filer   s               r    initr;   *   s>   ( 
yyBGG$I)>:YF
 	
 77<< = = ?JL99\277+ 5 @AA99Y([[!"''//)"<!?@
##
 	# KK	"		# ww||Iz2H	
bggooh7:;




  	H	 Y'F"KL) 6GGLLu5	&&**666''//&*A*ABKyybgg.K?*CD++
 )){I *  WW^^I&'',,y%8Ki5!6$ GGLL3]CGGLL2MB
 	D wth/I63H3HI $_  		 """HH	"o%8	: 

Y	# 	# B  s<    OO*P O7"PO'*O47P <PP	c                B   t        j                  |       }t        |||||||||		      }t        j                  | |||
      t        j                  | j                  d            }|rd}|rt        j                  d      fd}n|rfd}n|rt        j                  d      |r4t        | ||j                  	      5  |j                          d
d
d
       j                         D cg c]  }| }}t        |      dk(  r|d   S |S # 1 sw Y   ;xY wc c}w )a  Create 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`.

    	messager	   sqlheadsplicebranch_labelversion_pathrev_id
depends_on)process_revision_directivesrevision_environmentTz7Using --sql with --autogenerate does not make any sensec                ,    j                  | |       g S Nrun_autogeneraterevcontextrevision_contexts     r    retrieve_migrationsz%revision.<locals>.retrieve_migrations   s    --c7;I    c                ,    j                  | |       g S rI   )run_no_autogeneraterL   s     r    rP   z%revision.<locals>.retrieve_migrations   s    00g>IrQ   ziUsing --sql with the revision command when revision_environment is not configured does not make any sensefnas_sqltemplate_argsrO   Nr   r   )r   from_configdictautogenRevisionContextr
   asboolget_main_optionr(   r   rW   run_envgenerate_scriptslen)r   r>   r	   r?   r@   rA   rB   rC   rD   rE   rF   script_directorycommand_argsenvironmentrP   r6   scriptsrO   s                    @r    revisionre   {   sK   t '226:!!!
L ..$?	 ++f445KLMK##I 	 
	 
M
 	

 "*88-
 	' $$&	' %5$E$E$GH&vHGH
7|qqz'	' 	' Is   D0	DDc                   t        j                  |       }t        ddddddddd	      }t        j                  | ||      fd}t        | ||dj                        5  |j                          ddd       j                  d   }g }|j                  D ]!  }|j                  |j                                # |rt        j                  d	| |
      | j                  d       y# 1 sw Y   yxY w)zCheck if revision command with autogenerate has pending upgrade ops.

    :param config: a :class:`.Config` object.

    .. versionadded:: 1.9.0

    NTFr@   r=   c                ,    j                  | |       g S rI   rJ   rL   s     r    rP   z"check.<locals>.retrieve_migrations  s    ))#w7	rQ   rT   z!New upgrade operations detected: )rO   diffsz#No new upgrade operations detected.)r   rX   rY   rZ   r[   r   rW   r^   generated_revisionsupgrade_ops_listextendas_diffsr
   AutogenerateDiffsDetectedr   )r   ra   rb   rP   migration_scriptri   upgrade_opsrO   s          @r    checkrq      s    '226:
L .. 
&44)
 # 	  "# (;;B?E'88 -[))+,- ,,/w7-
 	
 	AB1# #s   "C++C4c                L   t        j                  |       }d| i}t        j                  | j	                  d            }|r,d }t        | ||d|      5  |j                          ddd        |j                  |xs t        j                         |fd||d|S # 1 sw Y   7xY w)	a  Merge 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`

    r   rG   c                    g S rI    rM   rN   s     r    nothingzmerge.<locals>.nothingY  s    IrQ   F)rU   rV   rW   NT)refreshr@   branch_labels)	r   rX   r
   r\   r]   r   r^   generate_revisionrD   )	r   	revisionsr>   rB   rD   r6   rW   rc   rv   s	            r    merger{   5  s    4 ((0F&M
 ++f445KLMK	  '
 	 NN	 $6##$++- "  	 	s   BB#c           	        t        j                  |       d}dv r,|st        j                  d      j	                  dd      \  }fd}t        | ||||      5  j                          ddd       y# 1 sw Y   yxY w)a  Upgrade 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 allowed   c                (    j                  |       S rI   )_upgrade_revsrM   rN   re   r6   s     r    upgradezupgrade.<locals>.upgrade  s    ##Hc22rQ   rU   rV   starting_revdestination_revtagr   rX   r
   r(   splitr   r^   )r   re   r?   r   r   r   r6   s    `    @r    r   r   o  s    * ((0FL
h##$@AA!)Q!7h3 
! 
 	 		 	 	s   "A<<Bc           	     @   t        j                  |       d}dv r-|st        j                  d      j	                  dd      \  }n|rt        j                  d      fd}t        | ||||      5  j                          ddd       y# 1 sw Y   yxY w)a  Revert 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.

    Nr}   r~   r   z/downgrade with --sql requires <fromrev>:<torev>c                (    j                  |       S rI   )_downgrade_revsr   s     r    	downgradezdowngrade.<locals>.downgrade  s    %%h44rQ   r   r   )r   re   r?   r   r   r   r6   s    `    @r    r   r     s    * ((0FL
h##$@AA!)Q!7h	=
 	
5 
! 
 	 		 	 	s   :BBc                    t        j                         |dk(  r. fd}t         |      5  j                          ddd       yj	                  |      D ]  } j                  |j                          y# 1 sw Y   yxY w)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.

    currentc                j    j                  |       D ]  }j                  |j                          g S rI   )get_revisionsr   	log_entry)rM   rN   scr   r6   s      r    show_currentzshow.<locals>.show_current  s4    **3/ 2##BLL12IrQ   rU   N)r   rX   r   r^   r   r   r   )r   rM   r   r   r6   s   `   @r    showr     s     ((0F
i	
  <@ 	NN	 	 &&s+ 	.B-	.	 	s   A==Bc                f  	 t        j                  |       }|<d|vrt        j                  d      |j	                         j                  d      \  }}ndx}}t        j                  | j                  d            xs }dfd			fd}|dk(  s|dk(  s|r || |||       y 	| |||       y)	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.

    Nr}   z9History range requires [start]:[end], [start]:, or :[end]rG   c           	         |j                  |xs d|xs d      D ];  }r|j                  |v |_        | j                  |j	                  ddd             = y )Nbaseheads)r   r@   T)verboseinclude_branchesinclude_docinclude_parents)walk_revisionsre   _db_current_indicatorr   
cmd_format)r   r6   r   r@   currentsr   indicate_currentr   s         r    _display_historyz!history.<locals>._display_history  sp    ''dog ( 
 	B  +-;;(+B(#%) $$(	  	rQ   c                      fd}t         |      5  j                          d d d        y # 1 sw Y   y xY w)Nc                l    dk(  r | |        g S dk(  r | |        g S  |        g S )Nr   rt   )rM   rN   r   r   r   r@   r6   s     r    _display_current_historyzMhistory.<locals>._display_history_w_current.<locals>._display_current_history  sZ    y  sC@
 I	 " dC@ I !tSAIrQ   r   )r   r^   )r   r6   r   r@   r   r   s   ```` r    _display_history_w_currentz+history.<locals>._display_history_w_current  s;    	 	  3KL 	NN	 	 	s	   7A r   )rt   )r   rX   r
   r(   stripr   r\   r]   )
r   	rev_ranger   r   r6   r   r@   rc   r   r   s
     ``     @r    historyr     s    & ((0Fi##N  __&,,S1
dt 	F**+ABC 	 
  yDI-"664>t4rQ   c                    t        j                  |       }|r|j                  d      }n|j                  |j                               }|D ]%  }| j	                  |j                  |dd             ' y)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   tree_indicatorsN)r   rX   r   	get_headsr   r   )r   r   resolve_dependenciesr6   r   rM   s         r    r   r   )  su     ((0F$$W-$$V%5%5%78 
NN$  	

rQ   c           
        t        j                  |       j                         D ]_  j                  s| j	                  dj                  d      dj                  fdfdj                  D        D                     a y)zShow current branch points.

    :param config: a :class:`.Config` instance.

    :param verbose: output in verbose mode.

    z%s
%s
T)r   
c              3     K   | ]<  }d t        t        j                              z  d|j                  dd       > yw) z -> FT)r   r   N)r`   strre   r   ).0rev_objr   r   s     r    	<genexpr>zbranches.<locals>.<genexpr>R  sM         c#bkk"233**!Dg + s   AAc              3  @   K   | ]  }j                  |        y wrI   )get_revision)r   rM   r6   s     r    r   zbranches.<locals>.<genexpr>Z  s!      $58++C0$s   N)r   rX   r   is_branch_pointr   r   r   nextrev)r   r   r   r6   s    `@@r    branchesr   D  s~     ((0F##% g=		 $<>JJ$ rQ   c                     t        j                          fd}t         |d      5  j                          ddd       y# 1 sw Y   yxY w)zDisplay the current revision for a database.

    :param config: a :class:`.Config` instance.

    :param verbose: output in verbose mode.

    c                    rCj                  dt        j                  |j                  j                  j
                               j                  |       D ]"  } j                  | j                               $ g S )NzCurrent revision(s) for %s:)r   r
   obfuscate_url_pw
connectionengineurlget_all_currentr   )rM   rN   r   r6   r   s     r    display_versionz current.<locals>.display_versionl  sq    -%%g&8&8&?&?&C&CD ))#. 	9Cw 78	9 	rQ   T)rU   dont_mutateNr   rX   r   r^   )r   r   r   r6   s   `` @r    r   r   a  sN     ((0F	 
?
  	  s   A		Ac           
       	
 t        j                  |       
|rhg 	d}t        j                  |      D ]K  }d|v r4|j	                  dd      \  }}||k7  r||}nt        j
                  d      	j                  |       M nt        j                  |      		
fd}t        | 
|||rndt        j                  	      ||      5  
j                          ddd       y# 1 sw Y   yxY w)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.

    Nr}   r   zMStamp operation with --sql only supports a single starting revision at a timec                N    j                  t        j                        |       S rI   )_stamp_revsr
   to_tuple)rM   rN   destination_revsr6   s     r    do_stampzstamp.<locals>.do_stamp  s     !!$--0@"A3GGrQ   )rU   rV   r   r   r   purge)
r   rX   r
   to_listr   r(   appendr   r   r^   )r   re   r?   r   r   r   	_revisionsrevr   r   r6   s            @@r    stampr   }  s    < ((0F
h/ 	/Ii"+//#q"9i4'#+'+"//A  ##I.	/  <<1H 
%(\d&67	
 
 	
 
 
s   C!!C*c                V   t        j                  |       |dk(  r-fd}t        | |      5  j                          ddd       yj	                  |      }|st        j                  d|z        |D ]%  }|sJ t        j                  |j                         ' y# 1 sw Y   yxY w)zEdit revision script(s) using $EDITOR.

    :param config: a :class:`.Config` instance.

    :param rev: target revision.

    r   c                    | st        j                  d      j                  |       D ]!  }t        j                  |j                         # g S )NzNo current revisions)r
   r(   r   open_in_editorr   )rM   rN   r   r6   s      r    edit_currentzedit.<locals>.edit_current  sI    ''(>??**3/ -##BGG,-IrQ   r   Nz*No revision files indicated by symbol '%s')	r   rX   r   r^   r   r
   r(   r   r   )r   rM   r   revsr   r6   s        @r    editr     s     ((0F
i	  <@ 	NN	 	 ##C(##<sB   	)BI2(	)	 	s   BB(c                    t        j                  |       }d }t        | |||      5  |j                          ddd       y# 1 sw Y   yxY w)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

    c                &    |j                          g S rI   )_ensure_version_tableru   s     r    do_ensure_versionz)ensure_version.<locals>.do_ensure_version  s    %%'	rQ   )rU   rV   Nr   )r   r?   r6   r   s       r    ensure_versionr     sP     ((0F 
	
  	  s   AA)r   r   returnNone)genericF)
r   r   r2   r   r3   r   r4   boolr   r   )
NFFr@   FNNNNN)r   r   r>   Optional[str]r	   r   r?   r   r@   r   rA   r   rB   Optional[_RevIdType]rC   r   rD   r   rE   r   rF   z$Optional[ProcessRevisionDirectiveFn]r   z/Union[Optional[Script], List[Optional[Script]]])r   z'Config'r   r   )NNN)r   r   rz   r   r>   r   rB   r   rD   r   r   zOptional[Script])FN)
r   r   re   r   r?   r   r   r   r   r   )r   r   rM   r   r   r   )NFF)
r   r   r   r   r   r   r   r   r   r   )FF)r   r   r   r   r   r   r   r   )F)r   r   r   r   r   r   )FNF)r   r   re   r   r?   r   r   r   r   r   r   r   )r   r   r?   r   r   r   )&
__future__r   r   typingr   r   r   r    r	   rZ   r
   runtime.environmentr   r6   r   alembic.configr   alembic.script.baser   alembic.script.revisionr   r   r!   r;   re   rq   r{   r   r   r   r   r   r   r   r   r   r   rt   rQ   r    <module>r      s   # 	      %  3 #%*2?I, 	NNN N 	N
 
Nf ")-"&  $HL||| | 
	|
 | | '|  | | | "F| 5|~8C| ")- 777 7 '	7
 7 7z 	))) 
) 
	)
 
)^ 	,,, 
, 
	,
 
,^.8  $"	B5B5B5 B5 	B5
 
B5L IN

!
AE
	
6:> @@@ 
@ 
	@
 @ 
@F)BrQ   