
    +h                    >   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	 d d	lm
Z
 d
dlmZ d
dlmZ e	rBd
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 d
dlmZ d
dlmZ d
dlmZ edded   gdf   Z G d d      Zy)    )annotations)Any)Callable)Iterator)List)Tuple)Type)TYPE_CHECKING)Union   )util)ops)AddColumnOp)AlterColumnOp)CreateTableOp)DowngradeOps)MigrateOperation)MigrationScript)ModifyTableOps)OpContainer)
UpgradeOps)MigrationContext)
_GetRevArgr   r   r   Nc                     e Zd ZU dZ ej
                         ZdZded<   ddZ		 	 	 	 ddZ
	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 dd	Zej                  ej                         	 	 	 	 	 	 	 	 dd
       Zej                  ej$                        	 	 	 	 	 	 	 	 dd       Zej                  ej(                        	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)Rewritera5  A helper object that allows easy 'rewriting' of ops streams.

    The :class:`.Rewriter` object is intended to be passed along
    to the
    :paramref:`.EnvironmentContext.configure.process_revision_directives`
    parameter in an ``env.py`` script.    Once constructed, any number
    of "rewrites" functions can be associated with it, which will be given
    the opportunity to modify the structure without having to have explicit
    knowledge of the overall structure.

    The function is passed the :class:`.MigrationContext` object and
    ``revision`` tuple that are passed to the  :paramref:`.Environment
    Context.configure.process_revision_directives` function normally,
    and the third argument is an individual directive of the type
    noted in the decorator.  The function has the choice of  returning
    a single op directive, which normally can be the directive that
    was actually passed, or a new directive to replace it, or a list
    of zero or more directives to replace it.

    .. seealso::

        :ref:`autogen_rewriter` - usage example

     z7Tuple[Union[ProcessRevisionDirectiveFn, Rewriter], ...]_chainedc                6    t        j                         | _        y N)r   
Dispatcherdispatch)selfs    P/var/www/html/venv/lib/python3.12/site-packages/alembic/autogenerate/rewriter.py__init__zRewriter.__init__?   s    )    c                    | j                   j                  | j                         }|j                  j                  | j                         |xj                  |fz  c_        |S )a  Produce a "chain" of this :class:`.Rewriter` to another.

        This allows two or more rewriters to operate serially on a stream,
        e.g.::

            writer1 = autogenerate.Rewriter()
            writer2 = autogenerate.Rewriter()


            @writer1.rewrites(ops.AddColumnOp)
            def add_column_nullable(context, revision, op):
                op.column.nullable = True
                return op


            @writer2.rewrites(ops.AddColumnOp)
            def add_column_idx(context, revision, op):
                idx_op = ops.CreateIndexOp(
                    "ixc", op.table_name, [op.column.name]
                )
                return [op, idx_op]

            writer = writer1.chain(writer2)

        :param other: a :class:`.Rewriter` instance
        :return: a new :class:`.Rewriter` that will run the operations
         of this writer, then the "other" writer, in succession.

        )	__class____new____dict__updater   )r"   otherwrs      r#   chainzRewriter.chainB   sH    H ^^##DNN3
4==)
x	r%   c                8    | j                   j                  |      S )a  Register a function as rewriter for a given type.

        The function should receive three arguments, which are
        the :class:`.MigrationContext`, a ``revision`` tuple, and
        an op directive of the type indicated.  E.g.::

            @writer1.rewrites(ops.AddColumnOp)
            def add_column_nullable(context, revision, op):
                op.column.nullable = True
                return op

        )r!   dispatch_for)r"   operators     r#   rewriteszRewriter.rewritesk   s    , }}))(33r%   c              #  $  K   	 | j                   j                  |      }| |j                  v r| y t        j                   ||||      g       D ]'  }|j                  j	                  | g      |_        | ) y # t
        $ r	 d }| Y y w xY wwr   )r!   
_mutationsr   to_listunion
ValueError)r"   contextrevision	directive	_rewriterr_directives         r#   _rewritezRewriter._rewrite   s     	&..y9I
 y+++#'<<gx;R$ &K .9-C-C-I-I.K* &%&  	IO	s(   BA; AB;B
BBBc                `    | j                  |||       | j                  D ]  } ||||        y r   )process_revision_directivesr   )r"   r7   r8   
directivesr>   s        r#   __call__zRewriter.__call__   s8     	(((JG+/== 	G'':F	Gr%   c                l   g }|j                   D ]B  }| j                  |||      }t        |      dk7  rt        d      |j	                  |d          D ||_        g }|j                  D ]B  }| j                  |||      }t        |      dk7  rt        d      |j	                  |d          D ||_        y )N   z5Can only return single object for UpgradeOps traverser   z7Can only return single object for DowngradeOps traverse)upgrade_ops_list_traverse_forlenr6   appendupgrade_opsdowngrade_ops_listdowngrade_ops)	r"   r7   r8   r9   rC   rG   retrH   rI   s	            r#   _traverse_scriptzRewriter._traverse_script   s     .0$55 	,K$$WhDC3x1} K  ##CF+	, !1	13&99 	.M$$WhFC3x1} M  %%c!f-	. #5	r%   c                >    | j                  |||j                         y r   )_traverse_listr   r"   r7   r8   r9   s       r#   _traverse_op_containerzRewriter._traverse_op_container   s     	GXy}}=r%   c                     y r   r   rN   s       r#   _traverse_any_directivez Rewriter._traverse_any_directive   s     	r%   c                    t        | j                  |||            }|D ](  }| j                  j                  |      } || |||       * |S r   )listr<   	_traverser!   )r"   r7   r8   r9   r?   	traversers         r#   rD   zRewriter._traverse_for   sT     $--9EF
# 	:I//	:IdGXy9	: r%   c                d    g }|D ]$  }|j                  | j                  |||             & ||d d  y r   )extendrD   )r"   r7   r8   r?   destr9   s         r#   rM   zRewriter._traverse_list   s@     # 	JIKK**7HiHI	J 
1r%   c                *    | j                  |||       y r   )rM   )r"   r7   r8   r?   s       r#   r>   z$Rewriter.process_revision_directives   s     	GXz:r%   N)returnNone)r+   z+Union[ProcessRevisionDirectiveFn, Rewriter]rZ   r   )r0   zpUnion[Type[AddColumnOp], Type[MigrateOperation], Type[AlterColumnOp], Type[CreateTableOp], Type[ModifyTableOps]]rZ   zCallable[..., Any])r7   r   r8   r   r9   r   rZ   zIterator[MigrateOperation])r7   r   r8   r   r?   zList[MigrationScript]rZ   r[   )r7   r   r8   r   r9   r   rZ   r[   )r7   r   r8   r   r9   r   rZ   r[   )r7   r   r8   r   r9   r   rZ   r[   )r7   r   r8   r   r9   r   rZ   r   )r7   r   r8   r   r?   r   rZ   r[   )__name__
__module____qualname____doc__r   r    rT   r   __annotations__r$   r-   r1   r<   r@   r/   r   r   rK   r   rO   r   rQ   rD   rM   r>   r   r%   r#   r   r   !   s   2  !IHJHEJ*'
' 
'R4
4 
40&!& & $	&
 
$&.G!G G *	G
 
G C//05!5 5 #	5
 
5 156 COO,>!> > 	>
 
> -> C001!  $	
 
 2
!
 
 $	

 


!
 
 	

 

;!; ; *	;
 
;r%   r   )
__future__r   typingr   r   r   r   r   r	   r
   r    r   
operationsr   operations.opsr   r   r   r   r   r   r   r   r   runtime.migrationr   script.revisionr   ProcessRevisionDirectiveFnr   r   r%   r#   <module>ri      sv    "           ,..-10/,+4,%t,='>?E 
O; O;r%   