Hacked By AnonymousFox
�
�܋f�T � �� � 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 erZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlm Z ddlm!Z! ddl"m#Z# ddl"m$Z$ ddl"m%Z% ddl&m'Z' dd l(m)Z) dd!l(m*Z* dGd(�Z+dHd*�Z, dIdJd>�Z-dKdB�Z. G dC� dD� � Z/ G dE� dF� � Z0dS )L� )�annotationsN)�Any)�Callable)�Dict)�Iterator)�Optional)�Sequence)�Set)�
TYPE_CHECKING)�Union)�inspect� )�compare)�render� )�util)�ops)�
Connection)�Dialect)� Inspector)�MetaData)�
SchemaItem)�Config)�DowngradeOps)�MigrationScript)�
UpgradeOps)�NameFilterParentNames)�NameFilterType)�RenderItemFn��MigrationContext)�Script)�ScriptDirectory�contextr! �metadatar �returnr c �T � t | |� � }|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 �O/opt/cloudlinux/venv/lib64/python3.11/site-packages/alembic/autogenerate/api.py�compare_metadatar- + s* � �r *�'�8�<�<���'�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 r, r( r( � sh � �$ %�W�x�@�@�@�O��*���N�2�&�&��&�r�*�*�� � �� �&��8H�I�I�I��r. �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; r= rB r>