�
J�g�V � � � d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z d d lm
Z
d d
lmZ d dlmZ d dl
mZ d
dlmZ d
dlmZ ddlmZ ddlmZ ddlmZ erld dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddlmZ ddl 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�Z3 G dG� dH� � Z4 G dI� 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 �returnr c �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� r c �� � t | |�� � }t j dt j g � � t j g � � �� � }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)�AutogenContextr r r r r �_populate_migration_script)r( r) �autogen_contextr/ s r0 r, 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�}|�%ddl m} ddlm}
| j |
� � �� � }t ||�� � }t
|� � |_ t j t j
| |� � � � 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? rA rF rB rJ Nr r# r )�DefaultDialect)�dialect)�opts)�runtime.migrationr$ �sqlalchemy.engine.defaultrM � configurer7 �setrD r �_indent�_render_cmd_body)r= r? rA rB rD rF rH rJ rO r$ rM r9 s 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 | � � }t j g � � }t j ||� � t j d||� � � �� � }t j |||� � dS )z6legacy, used by test_autogen_composition at the momentNr4 ) r7 r r r �_produce_net_changesr �reverser � _render_python_into_templatevars)r( rW r9 r- r/ s r0 �_render_migration_diffsr^ � s� � �
%�W�-�-�O��.��$�$�K�� ��+�>�>�>��*���!�)�)�+�+�� � �� �+��)�=�� � � � r2 c � � e Zd ZU dZdZded<