|L i>ddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z d d l m Z d d l mZe rBd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZeddedgdfZGddZy)) annotations)Any)Callable)Iterator)List)Tuple)Type) TYPE_CHECKING)Union)util)ops) AddColumnOp) AlterColumnOp) CreateTableOp) DowngradeOps)MigrateOperation)MigrationScript)ModifyTableOps) OpContainer) UpgradeOps)MigrationContext) _GetRevArgrrrNceZdZUdZej ZdZded<ddZ ddZ ddZ ddZ dd Z ejej  dd Zejej$ dd Zejej( dd Z dd Z ddZ ddZy)Rewritera5A 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], ...]_chainedc6tj|_yN)r Dispatcherdispatch)selfs c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/alembic/autogenerate/rewriter.py__init__zRewriter.__init__?s) c|jj|j}|jj|j|xj|fz c_|S)aProduce 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"otherwrs r#chainzRewriter.chainBsHH^^ # #DNN 3 4==) x  r%c8|jj|S)aRegister 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#rewriteszRewriter.rewritesks,}}))(33r%c#$K |jj|}||jvr|ytj||||gD]'}|jj |g|_|)y#t $r d}|YywxYwwr)r! _mutationsr to_listunion ValueError)r"contextrevision directive _rewriter r_directives r#_rewritezRewriter._rewrites  & ..y9I y+++#'<<gx;R$&K.9-C-C-I-I.K*&% & IO s(BA;AB;B  B B  Bc`|j||||jD] }||||yr)process_revision_directivesr)r"r7r8 directivesr>s r#__call__zRewriter.__call__s8 (((JG+/== G ' ': F Gr%clg}|jD]B}|j|||}t|dk7r td|j |dD||_g}|j D]B}|j|||}t|dk7r td|j |dD||_y)Nz5Can only return single object for UpgradeOps traverserz7Can only return single object for DowngradeOps traverse)upgrade_ops_list _traverse_forlenr6append upgrade_opsdowngrade_ops_list downgrade_ops) r"r7r8r9rCrGretrHrIs r#_traverse_scriptzRewriter._traverse_scripts.0$55 ,K$$Wh DC3x1} K  # #CF +  ,!1 13&99 .M$$Wh FC3x1} M  % %c!f -  .#5 r%c>|j|||jyr)_traverse_listrr"r7r8r9s r#_traverse_op_containerzRewriter._traverse_op_containers GXy}}=r%cyrrrNs r#_traverse_any_directivez Rewriter._traverse_any_directives r%ct|j|||}|D](}|jj|}|||||*|Sr)listr< _traverser!)r"r7r8r9r? traversers r#rDzRewriter._traverse_forsT $--9EF # :I// :I dGXy 9 :r%cdg}|D]$}|j|j|||&||ddyr)extendrD)r"r7r8r?destr9s r#rMzRewriter._traverse_lists@ # JI KK**7HiH I J 1 r%c*|j|||yr)rM)r"r7r8r?s r#r>z$Rewriter.process_revision_directivess GXz:r%N)returnNone)r+z+Union[ProcessRevisionDirectiveFn, Rewriter]rZr)r0zpUnion[Type[AddColumnOp], Type[MigrateOperation], Type[AlterColumnOp], Type[CreateTableOp], Type[ModifyTableOps]]rZzCallable[..., Any])r7rr8rr9rrZzIterator[MigrateOperation])r7rr8rr?zList[MigrationScript]rZr[)r7rr8rr9rrZr[)r7rr8rr9rrZr[)r7rr8rr9rrZr[)r7rr8rr9rrZr)r7rr8rr?rrZr[)__name__ __module__ __qualname____doc__r r rTr__annotations__r$r-r1r<r@r/rrrKrrOrrQrDrMr>rr%r#rr!s2 !IHJHEJ*' '  'R4 4 40&!&&$ & $ &.G!GG* G  GC//05!55# 5  5156COO,>!>> >  >->C001 !  $    2  !  $   !     ;!;;* ;  ;r%r) __future__rtypingrrrrrr r r r operationsroperations.opsrrrrrrrrrruntime.migrationrscript.revisionrProcessRevisionDirectiveFnrrr%r#risv" ,..-10/,+4,%t,='>?E O;O;r%