� J�g�V���ddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd l mZd dlmZd dlmZddlmZddlmZddlmZ e rlddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"ddl m#Z#ddl$m%Z%ddl$m&Z&dd l$m'Z'dd!l$m(Z(dd"l)m*Z*dd#l+m,Z,dd$l+m-Z-dd%l.m/Z/dKd,�Z0dLd.�Z1 dMdNdB�Z2dOdF�Z3GdG�dH��Z4GdI�dJ��Z5dS)P�)� annotationsN)�Any)�Dict)�Iterator)�List)�Optional)�Sequence)�Set)� TYPE_CHECKING)�Union)�inspect�)�compare)�render�)�util)�ops)� sqla_compat)� Connection)�Dialect)� Inspector)�MetaData)� SchemaItem)�Table)�Config)� DowngradeOps)�MigrationScript)� UpgradeOps)�NameFilterParentNames)�NameFilterType)�ProcessRevisionDirectiveFn)� RenderItemFn��MigrationContext)�Script)�ScriptDirectory)� _GetRevArg�contextr$�metadatar�returnrc�f�t||��}|j�J�|j���S)aB Compare a database schema to that given in a :class:`~sqlalchemy.schema.MetaData` instance. The database connection is presented in the context of a :class:`.MigrationContext` object, which provides database connectivity as well as optional comparison functions to use for datatypes and server defaults - see the "autogenerate" arguments at :meth:`.EnvironmentContext.configure` for details on these. The return format is a list of "diff" directives, each representing individual differences:: from alembic.migration import MigrationContext from alembic.autogenerate import compare_metadata from sqlalchemy import ( create_engine, MetaData, Column, Integer, String, Table, text, ) import pprint engine = create_engine("sqlite://") with engine.begin() as conn: conn.execute( text( ''' create table foo ( id integer not null primary key, old_data varchar, x integer ) ''' ) ) conn.execute(text("create table bar (data varchar)")) metadata = MetaData() Table( "foo", metadata, Column("id", Integer, primary_key=True), Column("data", Integer), Column("x", Integer, nullable=False), ) Table("bat", metadata, Column("info", String)) mc = MigrationContext.configure(engine.connect()) diff = compare_metadata(mc, metadata) pprint.pprint(diff, indent=2, width=20) Output:: [ ( "add_table", Table( "bat", MetaData(), Column("info", String(), table=<bat>), schema=None, ), ), ( "remove_table", Table( "bar", MetaData(), Column("data", VARCHAR(), table=<bar>), schema=None, ), ), ( "add_column", None, "foo", Column("data", Integer(), table=<foo>), ), [ ( "modify_nullable", None, "foo", "x", { "existing_comment": None, "existing_server_default": False, "existing_type": INTEGER(), }, True, False, ) ], ( "remove_column", None, "foo", Column("old_data", VARCHAR(), table=<foo>), ), ] :param context: a :class:`.MigrationContext` instance. :param metadata: a :class:`~sqlalchemy.schema.MetaData` instance. .. seealso:: :func:`.produce_migrations` - produces a :class:`.MigrationScript` structure based on metadata comparison. )�produce_migrations� upgrade_ops�as_diffs)r(r)�migration_scripts �h/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/alembic/autogenerate/api.py�compare_metadatar1/s9��r*�'�8�<�<�� � '� 3� 3� 3� � '� 0� 0� 2� 2�2�rc���t||���}tjdtjg��tjg�����}t j||��|S)a�Produce a :class:`.MigrationScript` structure based on schema comparison. This function does essentially what :func:`.compare_metadata` does, but then runs the resulting list of diffs to produce the full :class:`.MigrationScript` object. For an example of what this looks like, see the example in :ref:`customizing_revision`. .. seealso:: :func:`.compare_metadata` - returns more fundamental "diff" data from comparing a schema. )r)N��rev_idr-� downgrade_ops)�AutogenContextrrrrr�_populate_migration_script)r(r)�autogen_contextr/s r0r,r,�sh��$%�W�x�@�@�@�O��*���N�2�&�&��&�r�*�*�����  �&��8H�I�I�I� �r2�sa.�op.F�� up_or_down_op�Union[UpgradeOps, DowngradeOps]�sqlalchemy_module_prefix�str�alembic_module_prefix�render_as_batch�bool�imports� Sequence[str]� render_item�Optional[RenderItemFn]�migration_context�Optional[MigrationContext]�user_module_prefix� Optional[str]c���|||||d�}|�%ddlm} ddlm} | j| �����}t ||���} t |��| _tj tj || ����S) a*Render Python code given an :class:`.UpgradeOps` or :class:`.DowngradeOps` object. This is a convenience function that can be used to test the autogenerate output of a user-defined :class:`.MigrationScript` structure. :param up_or_down_op: :class:`.UpgradeOps` or :class:`.DowngradeOps` object :param sqlalchemy_module_prefix: module prefix for SQLAlchemy objects :param alembic_module_prefix: module prefix for Alembic constructs :param render_as_batch: use "batch operations" style for rendering :param imports: sequence of import symbols to add :param render_item: callable to render items :param migration_context: optional :class:`.MigrationContext` :param user_module_prefix: optional string prefix for user-defined types .. versionadded:: 1.11.0 )r?rArFrBrJNrr#r)�DefaultDialect)�dialect)�opts) �runtime.migrationr$�sqlalchemy.engine.defaultrM� configurer7�setrDr�_indent�_render_cmd_body) r=r?rArBrDrFrHrJrOr$rMr9s r0�render_python_coderV�s���:%=�!6�"�*�0�  � �D�� �8�8�8�8�8�8�<�<�<�<�<�<�6�,�6�"�N�$�$� � � ��%�%6�T�B�B�B�O�!�'�l�l�O�� �>��� ��?�?� � �r2� template_args�Dict[Any, Any]�Nonec���t|��}tjg��}tj||��tjd||������}tj|||��dS)z6legacy, used by test_autogen_composition at the momentNr4) r7rrr�_produce_net_changesr�reverser� _render_python_into_templatevars)r(rWr9r-r/s r0�_render_migration_diffsr^�s��� %�W�-�-�O��.��$�$�K� � ��+�>�>�>��*���!�)�)�+�+�����  �+��)�=�����r2c��eZdZUdZdZded< dZded< dZded< dZd ed < dZ d ed < d+d,d�Z e j d-d���Z ejd.d���Zd/d�Zd0d&�ZeZe j d1d(���Ze j d2d*���ZdS)3r7zSMaintains configuration and state that's specific to an autogenerate operation.N�Optional[MetaData]r)zOptional[Connection]� connectionzOptional[Dialect]rNzSet[str]rDr$rHTrO�Optional[Dict[str, Any]]� autogeneraterCr*rYc�n�|r|�|jrtjd���|�|j}|�|�dd��n|x|_}|r,|�*|�(|j�!tjd|jjz���|�dd��}|�dd��}g}g}|r|�|��|r|�|��||_ ||_ ||_ |j �"|j j |_ |j j|_t��|_||_d|_dS)Nz^autogenerate can't use as_sql=True as it prevents querying the database for schema information�target_metadataz�Can't proceed with --autogenerate option; environment script %s does not provide a MetaData object or sequence of objects to the context.�include_object� include_nameF)�as_sqlr� CommandErrorrO�getr)�script�env_py_location�append�_object_filters� _name_filtersrH�bindrarNrSrD� _has_batch) �selfrHr)rOrcrfrg�object_filters� name_filterss r0�__init__zAutogenContext.__init__Ls~�� � �!�-�!�(�.��#�6��� � �<�$�)�D�2:�1A�D�H�H�&�� -� -� -�x� �� �� � �� �!�-�!�(�4��#�K�%�+�;�=��� ����"2�D�9�9���x�x���5�5� ���� � � 2� � !� !�.� 1� 1� 1� � .� � � � � -� -� -�-���)���!2��� � !� -�"�4�9�D�O��1�9�D�L��u�u�� �$(�� � %����r2rc�V�|j�td���t|j��S)NzHcan't return inspector as this AutogenContext has no database connection)ra� TypeErrorr �rrs r0� inspectorzAutogenContext.inspector�s3�� �?� "��<��� ��t��'�'�'r2�Iterator[None]c#�.K�d|_dV�d|_dS)NTF)rqrxs r0� _within_batchzAutogenContext._within_batch�s!������� ��������r2�namerK�type_r � parent_namesrc��d|vr;|dkr|}n|�dd��}|r|d}|r |�d|��|d<n||d<|jD]}||||��sdS�dS) a�Run the context's name filters and return True if the targets should be part of the autogenerate operation. This method should be run for every kind of name encountered within the reflection side of an autogenerate operation, giving the environment the chance to filter what names should be reflected as database objects. The filters here are produced directly via the :paramref:`.EnvironmentContext.configure.include_name` parameter. � schema_name�table� table_nameN�.�schema_qualified_table_nameFT)rjro)rrr}r~rr�r��fns r0�run_name_filterszAutogenContext.run_name_filters�s��� �L� (� (�����!� � �)�-�-�l�D�A�A� �� M�*�=�9� ��M�#� � �"� �C�L�!>�?�?� CM�L�!>�?��$� � �B��2�d�E�<�0�0� ��u�u� ��4r2�object_r�sqla_compat._ConstraintName� reflected� compare_to�Optional[SchemaItem]c�>�|jD]}||||||��sdS�dS)a�Run the context's object filters and return True if the targets should be part of the autogenerate operation. This method should be run for every kind of object encountered within an autogenerate operation, giving the environment the chance to filter what objects should be included in the comparison. The filters here are produced directly via the :paramref:`.EnvironmentContext.configure.include_object` parameter. FT)rn)rrr�r}r~r�r�r�s r0�run_object_filtersz!AutogenContext.run_object_filters�sC��$�&� � �B��2�g�t�U�I�z�B�B� ��u�u� ��4r2� List[Table]c�v�g}tj|j��D]}|�|j���|S)aiReturn an aggregate of the :attr:`.MetaData.sorted_tables` collection(s). For a sequence of :class:`.MetaData` objects, this concatenates the :attr:`.MetaData.sorted_tables` collection for each individual :class:`.MetaData` in the order of the sequence. It does **not** collate the sorted tables collections. )r�to_listr)�extend� sorted_tables)rr�result�ms r0r�zAutogenContext.sorted_tables�s@������d�m�,�,� +� +�A� �M�M�!�/� *� *� *� *�� r2�Dict[str, Table]c �Z�i}tj|j��D]�}t|���t|j����}|r<t dd�d�t|��D����z���|� |j����|S)a�Return an aggregate of the :attr:`.MetaData.tables` dictionaries. The :attr:`.MetaData.tables` collection is a dictionary of table key to :class:`.Table`; this method aggregates the dictionary across multiple :class:`.MetaData` objects into one dictionary. Duplicate table keys are **not** supported; if two :class:`.MetaData` objects contain the same table key, an exception is raised. z9Duplicate table keys across multiple MetaData objects: %sz, c3� K�|] }d|zV�� dS)z"%s"Nr<)�.0�keys r0� <genexpr>z4AutogenContext.table_key_to_table.<locals>.<genexpr>�s&���� K� K�#��#�� K� K� K� K� K� Kr2) rr�r)rS� intersection�tables� ValueError�join�sorted�update)rrr�r�� intersects r0�table_key_to_tablez!AutogenContext.table_key_to_table�s���$&����d�m�,�,� $� $�A��F� � �0�0��Q�X���?�?�I�� � �+��y�y� K� K�� �9J�9J� K� K� K�K�K�M���� �M�M�!�(� #� #� #� #�� r2)NNT) rHr$r)r`rOrbrcrCr*rY)r*r)r*rz)r}rKr~r rrr*rC) r�rr}r�r~r r�rCr�r�r*rC)r*r�)r*r�)�__name__� __module__� __qualname__�__doc__r)�__annotations__rarNrDrHrur�memoized_propertyry� contextlib�contextmanagerr|r�r�� run_filtersr�r�r<r2r0r7r7sz���������$(�H�'�'�'�'��"(,�J�+�+�+�+��"&�G�%�%�%�%���G����� �+/��.�.�.�.�N� (,�)-�!� 9&�9&�9&�9&�9&�v ��(�(�(���(��� � � ��� � #�#�#�#�J����0%�K� �� � � ��� � ���������r2r7c�d�eZdZUdZded<ded< d d!d�Zd"d�Zd#d�Zd#d�Zd$d�Z d%d�Z d&d�Z dS)'�RevisionContextz^Maintains configuration and state that's specific to a revision file generation operation.zList[MigrationScript]�generated_revisions�$Optional[ProcessRevisionDirectiveFn]�process_revision_directivesN�configr�script_directoryr&� command_args�Dict[str, Any]r*rYc��||_||_||_||_d|i|_|���g|_dS)Nr�)r�r�r�r�rW�_default_revisionr�)rrr�r�r�r�s r0ruzRevisionContext.__init__sQ���� � 0���(���+F��(� �f� ���%)�$:�$:�$<�$<�#=�� � � r2r/r�Optional[Script]c ��|j���}t|dd��rV|j}t ��|_|jr|j�|j��tj|||��|j �J�|j j |j |j fd|j |j|j|j|jd�|��S)N� _needs_renderFT)�refresh�head�splice� branch_labels� version_path� depends_on)rW�copy�getattr�_last_autogen_contextrSrDr�rr]r5r��generate_revision�messager�r�� branch_labelr�r�)rrr/rWr9s r0� _to_scriptzRevisionContext._to_scripts���)-�(:�(?�(?�(A�(A� � �#�_�e� <� <� �"�8�O�'*�e�e�O� #��'� I��'�.�.�/?�/G�H�H�H� � 3��!1�=� � � � �&�2�2�2�6�t�$�6� � #� � $�  ��!�&�#�*�*�7�)�6�'�2�  �  ��  �  � r2�revr'rHr$c�4�|�||d��dS)NT��_run_environment�rrr�rHs r0�run_autogeneratez RevisionContext.run_autogenerate7s#�� ���c�#4�d�;�;�;�;�;r2c�4�|�||d��dS)NFr�r�s r0�run_no_autogeneratez#RevisionContext.run_no_autogenerate<s#�� ���c�#4�e�<�<�<�<�<r2rcrCc��|r�|jdrtjd���t|j�|����t|j�d����krtjd���|jd}|jd}|jd}t|dd ��s,||j d_ ||j d_ d |_ n\|j�t!jg|� ����|j�t!jg|� ����t)||� ��}||_|rt-j||��|jr|�|||j��|jd}|r||||j��|jD] }d |_ � dS)N�sqlz7Using --sql with --autogenerate does not make any sense�headsz"Target database is not up to date.� upgrade_token�downgrade_token�����r�FT)r�)r�)rcr�)r�rrirSr�� get_revisionsrOr�r��upgrade_ops_listr��downgrade_ops_listr�r�� _upgrade_opsrmrr�_downgrade_opsrr7r�rr8r�) rrr�rHrcr�r�r/r9�hooks r0r�z RevisionContext._run_environmentAs2�� � N�� ��'� ��'�M�����4�(�6�6�s�;�;�<�<���%�3�3�G�<�<�A�A����'�(L�M�M�M�)�.��?� �+�0�1B�C���3�B�7���'��%�@�@� �BO� � -�b� 1� ?�� � /�� 3� C�.2� � *� *� � )� 0� 0���r��?�?�?� � � � � +� 2� 2�� ��_�E�E�E� � � �)� �L� � � ��6E��"� � � � .��!1� � � � � +� � � ,� ,�!�3��(@� � � �!�%�&C�D�� � C� �D�"�C��)A� B� B� B� $� 8� 2� 2� �-1� � *� *� 2� 2r2c ��|j}tj|dptj��|dtjg��tjg��|d|d|d|d|d�� � }|S) Nr5r�r�r�r�r�r�) r5r�r-r6r�r�r�r�r�)r�rrrr5rr)rrr��ops r0r�z!RevisionContext._default_revisionys���'+�'8� � � ���)�:�T�[�]�]� ��+���r�*�*��*�2�.�.��f�%���)�%�n�5�%�n�5�#�L�1�  �  �  ��� r2�Iterator[Optional[Script]]c#�LK�|jD]}|�|��V��dS�N)r�r�)rr�generated_revisions r0�generate_scriptsz RevisionContext.generate_scripts�s?����"&�":� 6� 6� ��/�/�"4�5�5� 5� 5� 5� 5� 6� 6r2r�) r�rr�r&r�r�r�r�r*rY)r/rr*r�)r�r'rHr$r*rY)r�r'rHr$rcrCr*rY)r*r)r*r�) r�r�r�r�r�rur�r�r�r�r�r�r<r2r0r�r��s��������"�"�/�.�.�.�E�E�E�E� �>�>�>�>�>�& � � � �<<�<�<�<� =�=�=�=� 62�62�62�62�p � � � �6�6�6�6�6�6r2r�)r(r$r)rr*r)r(r$r)rr*r)r:r;Fr<NNN)r=r>r?r@rAr@rBrCrDrErFrGrHrIrJrKr*r@)r(r$rWrXr*rY)6� __future__rr��typingrrrrrr r r r � sqlalchemyr �rrr� operationsrr�sqlalchemy.enginerrr�sqlalchemy.sql.schemarrrr�r�operations.opsrrr�runtime.environmentrr r!r"rPr$� script.baser%r&�script.revisionr'r1r,rVr^r7r�r<r2r0�<module>r�sd��"�"�"�"�"�"����������������������������������������������� � � � � � ���������������������������������������������-�,�,�,�,�,�,�)�)�)�)�)�)�+�+�+�+�+�+�.�.�.�.�.�.�0�0�0�0�0�0�+�+�+�+�+�+�������-�-�-�-�-�-�0�0�0�0�0�0�+�+�+�+�+�+�;�;�;�;�;�;�4�4�4�4�4�4�@�@�@�@�@�@�2�2�2�2�2�2�4�4�4�4�4�4�$�$�$�$�$�$�-�-�-�-�-�-�,�,�,�,�,�,�{3�{3�{3�{3�|����B%*�!&�!��*.�48�(,�0�0�0�0�0�f����*h�h�h�h�h�h�h�h�VK6�K6�K6�K6�K6�K6�K6�K6�K6�K6r2
Memory