File: //opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/api.pyc
�
�M!Vc           @   s�   d  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	 Z	 d
 �  Z
 d �  Z d d
 e d d	 d � Z d �  Z d e f d �  �  YZ d e f d �  �  YZ d	 S(   sX   Provide the 'autogenerate' feature which can produce migration operations
automatically.i   (   t   opsi   (   t   render(   t   compare(   t   utili����(   t	   InspectorNc         C   s   t  |  | � } | j j �  S(   s�
  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.schema import SchemaItem
        from sqlalchemy.types import TypeEngine
        from sqlalchemy import (create_engine, MetaData, Column,
                Integer, String, Table)
        import pprint
        engine = create_engine("sqlite://")
        engine.execute('''
            create table foo (
                id integer not null primary key,
                old_data varchar,
                x integer
            )''')
        engine.execute('''
            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(bind=None),
                Column('info', String(), table=<bat>), schema=None)),
          ( 'remove_table',
            Table(u'bar', MetaData(bind=None),
                Column(u'data', VARCHAR(), table=<bar>), schema=None)),
          ( 'add_column',
            None,
            'foo',
            Column('data', Integer(), table=<foo>)),
          ( 'remove_column',
            None,
            'foo',
            Column(u'old_data', VARCHAR(), table=None)),
          [ ( 'modify_nullable',
              None,
              'foo',
              u'x',
              { 'existing_server_default': None,
                'existing_type': INTEGER()},
              True,
              False)]]
    :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.
    (   t   produce_migrationst   upgrade_opst   as_diffs(   t   contextt   metadatat   migration_script(    (    sI   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/api.pyt   compare_metadata   s    Zc         C   sV   t  |  d | �} t j d d d t j g  � d t j g  � � } t j | | � | S(   s�  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`.
    .. versionadded:: 0.8.0
    .. seealso::
        :func:`.compare_metadata` - returns more fundamental "diff"
        data from comparing a schema.
    R	   t   rev_idR   t
   downgrade_opsN(   t   AutogenContextR    t   MigrationScriptt   Nonet
   UpgradeOpst   DowngradeOpsR   t   _populate_migration_script(   R   R	   t   autogen_contextR
   (    (    sI   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/api.pyR   j   s    	s   sa.s   op.c         C   s\   i | d 6| d 6| d 6| d 6} t  d d | �} t | � | _ t j t j |  | � � S(   s�   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.
    t   sqlalchemy_module_prefixt   alembic_module_prefixt   render_itemt   render_as_batcht   optsN(   R   R   t   sett   importsR   t   _indentt   _render_cmd_body(   t
   up_or_down_opR   R   R   R   R   R   R   (    (    sI   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/api.pyt   render_python_code�   s    
c         C   sf   t  |  � } t j g  � } t j | | � t j d d d | d | j �  � } t j	 | | | � d S(   s6   legacy, used by test_autogen_composition at the momentR   R   R
   N(
   R   R    R   R   t   _produce_net_changesR   R   t   reverseR   t    _render_python_into_templatevars(   R   t
   template_argsR   R   R
   (    (    sI   /opt/alt/python27/lib/python2.7/site-packages/alembic/autogenerate/api.pyt   _render_migration_diffs�   s    	R   c           B   sk   e  Z d  Z d Z d Z d Z d Z d Z d d e	 d � Z
 e j d �  � Z
 e j d �  � Z d �  Z RS(   sS   Maintains configuration and state that's specific to an
    autogenerate operation.c            s|  | r- | d  k	 r- | j r- t j d � � n  | d  k rE | j } n  | d  k rc | j d d  � n | |  _ } | d  k r� | d  k	 r� | j d  k	 r� t j d | j j � � n  | j d d  � �  | j d d  � } g  } �  r�  f d �  } | j	 | � n  | r| j	 | � n  | |  _
 | |  _ |  j d  k	 rZ|  j j |  _
 |  j j |  _ n  t �  |  _ | |  _ t |  _ d  S(   Ns^   autogenerate can't use as_sql=True as it prevents querying the database for schema informationt   target_metadatasr   Can't proceed with --autogenerate option; environment script %s does not provide a MetaData object to the context.t   include_symbolt   include_objectc            s$   | d k r �  | |  j  � St Sd  S(   Nt   table(   t   schemat   True(   t   objectt   namet   type_t	   reflectedt
   compare_to(   R&