File: //proc/self/root/opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyc
�
�M!Vc @ sM d d l m Z d d l m Z d d l m Z d e j f d � � YZ d S( i ( t
Operationsi ( t MigrationContext( t utilt EnvironmentContextc B s" e Z d Z e Z e Z e Z d � Z d � Z d � Z d � Z
d � Z d � Z d � Z
d � Z d � Z d
� Z d � Z e d � Z e e e e e e e e e e e e e e e e e e e d
d d d e d � Z d � Z e d � Z d � Z d � Z d � Z d � Z d � Z RS( su A configurational facade made available in an ``env.py`` script.
The :class:`.EnvironmentContext` acts as a *facade* to the more
nuts-and-bolts objects of :class:`.MigrationContext` as well as certain
aspects of :class:`.Config`,
within the context of the ``env.py`` script that is invoked by
most Alembic commands.
:class:`.EnvironmentContext` is normally instantiated
when a command in :mod:`alembic.command` is run. It then makes
itself available in the ``alembic.context`` module for the scope
of the command. From within an ``env.py`` script, the current
:class:`.EnvironmentContext` is available by importing this module.
:class:`.EnvironmentContext` also supports programmatic usage.
At this level, it acts as a Python context manager, that is, is
intended to be used using the
``with:`` statement. A typical use of :class:`.EnvironmentContext`::
from alembic.config import Config
from alembic.script import ScriptDirectory
config = Config()
config.set_main_option("script_location", "myapp:migrations")
script = ScriptDirectory.from_config(config)
def my_function(rev, context):
'''do something with revision "rev", which
will be the current database revision,
and "context", which is the MigrationContext
that the env.py will create'''
with EnvironmentContext(
config,
script,
fn = my_function,
as_sql = False,
starting_rev = 'base',
destination_rev = 'head',
tag = "sometag"
):
script.run_env()
The above script will invoke the ``env.py`` script
within the migration environment. If and when ``env.py``
calls :meth:`.MigrationContext.run_migrations`, the
``my_function()`` function above will be called
by the :class:`.MigrationContext`, given the context
itself as well as the current revision in the database.
.. note::
For most API usages other than full blown
invocation of migration scripts, the :class:`.MigrationContext`
and :class:`.ScriptDirectory` objects can be created and
used directly. The :class:`.EnvironmentContext` object
is *only* needed when you need to actually invoke the
``env.py`` module present in the migration environment.
c K s | | _ | | _ | | _ d S( s^ Construct a new :class:`.EnvironmentContext`.
:param config: a :class:`.Config` instance.
:param script: a :class:`.ScriptDirectory` instance.
:param \**kw: keyword options that will be ultimately
passed along to the :class:`.MigrationContext` when
:meth:`.EnvironmentContext.configure` is called.
N( t configt scriptt context_opts( t selfR R t kw( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt __init__S s
c C s | j � | S( s� Establish a context which provides a
:class:`.EnvironmentContext` object to
env.py scripts.
The :class:`.EnvironmentContext` will
be made available as ``from alembic import context``.
( t _install_proxy( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt __enter__a s
c O s | j � d S( N( t
_remove_proxy( R t argR ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt __exit__m s c C s | j j d t � S( s% Return True if the current migrations environment
is running in "offline mode".
This is ``True`` or ``False`` depending
on the the ``--sql`` flag passed.
This function does not require that the :class:`.MigrationContext`
has been configured.
t as_sql( R t gett False( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt is_offline_modep s c C s | j � j j S( s� Return True if the context is configured to expect a
transactional DDL capable backend.
This defaults to the type of database in use, and
can be overridden by the ``transactional_ddl`` argument
to :meth:`.configure`
This function requires that a :class:`.MigrationContext`
has first been made available via :meth:`.configure`.
( t get_contextt implt transactional_ddl( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt is_transactional_ddl} s c C s | j � S( N( R ( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt requires_connection� s c C s | j j d � S( s� Return the hex identifier of the 'head' script revision.
If the script directory has multiple heads, this
method raises a :class:`.CommandError`;
:meth:`.EnvironmentContext.get_head_revisions` should be preferred.
This function does not require that the :class:`.MigrationContext`
has been configured.
.. seealso:: :meth:`.EnvironmentContext.get_head_revisions`
t head( R t as_revision_number( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_head_revision� s
c C s | j j d � S( s: Return the hex identifier of the 'heads' script revision(s).
This returns a tuple containing the version number of all
heads in the script directory.
This function does not require that the :class:`.MigrationContext`
has been configured.
.. versionadded:: 0.7.0
t heads( R R ( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_head_revisions� s c C sa | j d k r( | j j | j � j � Sd | j k rN | j j | j d � St j d � � d S( sT Return the 'starting revision' argument,
if the revision was passed using ``start:end``.
This is only meaningful in "offline" mode.
Returns ``None`` if no value is available
or was configured.
This function does not require that the :class:`.MigrationContext`
has been configured.
t starting_revs+ No starting revision argument is available.N( t _migration_contextt NoneR R R t _start_from_revR R t CommandError( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_starting_revision_argument� s c C s | j j | j d � S( s� Get the 'destination' revision argument.
This is typically the argument passed to the
``upgrade`` or ``downgrade`` command.
If it was specified as ``head``, the actual
version number is returned; if specified
as ``base``, ``None`` is returned.
This function does not require that the :class:`.MigrationContext`
has been configured.
t destination_rev( R R R ( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_revision_argument� s c C s | j j d d � S( sg Return the value passed for the ``--tag`` argument, if any.
The ``--tag`` argument is not used directly by Alembic,
but is available for custom ``env.py`` configurations that
wish to use it; particularly for offline generation scripts
that wish to generate tagged filenames.
This function does not require that the :class:`.MigrationContext`
has been configured.
.. seealso::
:meth:`.EnvironmentContext.get_x_argument` - a newer and more
open ended system of extending ``env.py`` scripts via the command
line.
t tagN( R R R ( R ( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_tag_argument� s c C sS | j j d k r* | j j j p$ g } n g } | rO t d � | D� � } n | S( s� Return the value(s) passed for the ``-x`` argument, if any.
The ``-x`` argument is an open ended flag that allows any user-defined
value or values to be passed on the command line, then available
here for consumption by a custom ``env.py`` script.
The return value is a list, returned directly from the ``argparse``
structure. If ``as_dictionary=True`` is passed, the ``x`` arguments
are parsed using ``key=value`` format into a dictionary that is
then returned.
For example, to support passing a database URL on the command line,
the standard ``env.py`` script can be modified like this::
cmd_line_url = context.get_x_argument(
as_dictionary=True).get('dbname')
if cmd_line_url:
engine = create_engine(cmd_line_url)
else:
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
This then takes effect by running the ``alembic`` script as::
alembic -x dbname=postgresql://user:pass@host/dbname upgrade head
This function does not require that the :class:`.MigrationContext`
has been configured.
.. versionadded:: 0.6.0
.. seealso::
:meth:`.EnvironmentContext.get_tag_argument`
:attr:`.Config.cmd_opts`
c s s! | ] } | j d d � Vq d S( t =i N( t split( t .0R
( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pys <genexpr> s N( R t cmd_optsR t xt dict( R t
as_dictionaryt value( ( sL /opt/alt/python27/lib/python2.7/site-packages/alembic/runtime/environment.pyt get_x_argument� s )t upgradest
downgradess op.s sa.c K s� | j } | t k r"