o
    õï‡hØ  ã                   @  s  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	rzd
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 G dd„ dƒZdS )é    )Úannotations)ÚAny)ÚCallable)ÚIterator)ÚList)ÚOptional)ÚType)ÚTYPE_CHECKING)ÚUnioné   )Úutil)Úops)ÚAddColumnOp)ÚAlterColumnOp)ÚCreateTableOp)ÚMigrateOperation)ÚMigrationScript)ÚModifyTableOps)ÚOpContainer)Ú
_GetRevArg)ÚMigrationContextc                   @  s¸   e Zd ZU dZe ¡ ZdZded< d-dd„Z	d.d
d„Z
d/dd„Zd0dd„Zd1dd„Ze ej¡d2dd „ƒZe ej¡d3d"d#„ƒZe ej¡d4d$d%„ƒZd5d'd(„Zd6d)d*„Zd1d+d,„ZdS )7Ú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

    NzOptional[Rewriter]Ú_chainedÚreturnÚNonec                 C  s   t  ¡ | _d S ©N)r   Ú
DispatcherÚdispatch)Úself© r   ú`/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/alembic/autogenerate/rewriter.pyÚ__init__9   s   zRewriter.__init__Úotherc                 C  s&   | j  | j ¡}|j | j¡ ||_|S )a’  Produce a "chain" of this :class:`.Rewriter` to another.

        This allows two 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   r"   Úwrr   r   r    Úchain<   s   zRewriter.chainÚoperatorúpUnion[Type[AddColumnOp], Type[MigrateOperation], Type[AlterColumnOp], Type[CreateTableOp], Type[ModifyTableOps]]r   c                 C  s   | 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   r)   r   r   r    Úrewrites_   s   zRewriter.rewritesÚcontextr   Úrevisionr   Ú	directiver   úIterator[MigrateOperation]c                 c  sz    z| j   |¡}W n ty   d }|V  Y d S w | |jv r#|V  d S t ||||ƒg ¡D ]}|j | g¡|_|V  q-d S r   )r   Ú
ValueErrorÚ
_mutationsr   Úto_listÚunion)r   r-   r.   r/   Ú	_rewriterÚr_directiver   r   r    Ú_rewritew   s"   €þ

ÿÿúzRewriter._rewriteÚ
directivesúList[MigrationScript]c                 C  s*   |   |||¡ | jr|  |||¡ d S d S r   )Úprocess_revision_directivesr   ©r   r-   r.   r8   r   r   r    Ú__call__Ž   s   ÿzRewriter.__call__r   c           	      C  s   g }|j D ]}|  |||¡}t|ƒdkrtdƒ‚| |d ¡ q||_g }|jD ]}|  |||¡}t|ƒdkr;tdƒ‚| |d ¡ q(||_d S )Né   z5Can only return single object for UpgradeOps traverser   z7Can only return single object for DowngradeOps traverse)Úupgrade_ops_listÚ_traverse_forÚlenr1   ÚappendÚupgrade_opsÚdowngrade_ops_listÚdowngrade_ops)	r   r-   r.   r/   r>   rB   ÚretrC   rD   r   r   r    Ú_traverse_script˜   s$   
ÿ
ÿ
zRewriter._traverse_scriptr   c                 C  s   |   |||j¡ d S r   )Ú_traverse_listr   ©r   r-   r.   r/   r   r   r    Ú_traverse_op_container³   s   zRewriter._traverse_op_containerc                 C  s   d S r   r   rH   r   r   r    Ú_traverse_any_directive¼   s   z Rewriter._traverse_any_directiver   c                 C  s:   t |  |||¡ƒ}|D ]}| j |¡}|| |||ƒ q|S r   )Úlistr7   Ú	_traverser   )r   r-   r.   r/   r8   Ú	traverserr   r   r    r?   Å   s
   zRewriter._traverse_forc                 C  s2   g }|D ]}|  |  |||¡¡ q||d d …< d S r   )Úextendr?   )r   r-   r.   r8   Údestr/   r   r   r    rG   Ñ   s   zRewriter._traverse_listc                 C  s   |   |||¡ d S r   )rG   r;   r   r   r    r:   Ý   s   z$Rewriter.process_revision_directives)r   r   )r"   r   r   r   )r)   r*   r   r   )r-   r   r.   r   r/   r   r   r0   )r-   r   r.   r   r8   r9   r   r   )r-   r   r.   r   r/   r   r   r   )r-   r   r.   r   r/   r   r   r   )r-   r   r.   r   r/   r   r   r   )r-   r   r.   r   r/   r   r   r   )r-   r   r.   r   r8   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rL   r   Ú__annotations__r!   r(   r,   r7   r<   r+   r   r   rF   r   rI   r   rJ   r?   rG   r:   r   r   r   r    r      s$   
 


#







r   N)Ú
__future__r   Útypingr   r   r   r   r   r   r	   r
   Ú r   Ú
operationsr   Úoperations.opsr   r   r   r   r   r   r   Úruntime.environmentr   Úruntime.migrationr   r   r   r   r   r    Ú<module>   s,    