관리-도구
편집 파일: environment.cpython-37.pyc
B �M!V!� � @ s: d dl mZ ddlmZ d dlmZ G dd� dej�ZdS )� )� Operations� )�MigrationContext)�utilc @ s� e Zd ZdZdZdZdZdd� Zdd� Zdd� Z d d � Z dd� Zd d� Zdd� Z dd� Zdd� Zdd� Zdd� Zd0dd�Zd1d d!�Zd"d#� Zd2d$d%�Zd&d'� Zd(d)� Zd*d+� Zd,d-� Zd.d/� ZdS )3�EnvironmentContextau 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. Nc K s || _ || _|| _dS )a^ 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)�config�script�context_opts)�selfr r �kw� r �L/opt/alt/python37/lib/python3.7/site-packages/alembic/runtime/environment.py�__init__S s zEnvironmentContext.__init__c C s | � � | S )z�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``. )Z_install_proxy)r r r r � __enter__a s zEnvironmentContext.__enter__c O s | � � d S )N)Z _remove_proxy)r �argr r r r �__exit__m s zEnvironmentContext.__exit__c C s | j �dd�S )a% 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. Zas_sqlF)r �get)r r r r �is_offline_modep s z"EnvironmentContext.is_offline_modec C s | � � jjS )a� 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`. )�get_context�impl�transactional_ddl)r r r r �is_transactional_ddl} s z'EnvironmentContext.is_transactional_ddlc C s | � � S )N)r )r r r r �requires_connection� s z&EnvironmentContext.requires_connectionc C s | j �d�S )a� 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` �head)r �as_revision_number)r r r r �get_head_revision� s z$EnvironmentContext.get_head_revisionc C s | j �d�S )a: 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 Zheads)r r )r r r r �get_head_revisions� s z%EnvironmentContext.get_head_revisionsc C sF | j dk r| j�| �� j�S d| jkr8| j�| jd �S t�d��dS )aT 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. N�starting_revz+No starting revision argument is available.)�_migration_contextr r r Z_start_from_revr r ZCommandError)r r r r �get_starting_revision_argument� s z1EnvironmentContext.get_starting_revision_argumentc C s | j �| jd �S )a� 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. Zdestination_rev)r r r )r r r r �get_revision_argument� s z(EnvironmentContext.get_revision_argumentc C s | j �dd�S )ag 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. �tagN)r r )r r r r �get_tag_argument� s z#EnvironmentContext.get_tag_argumentFc C s: | j jdk r| j jjpg }ng }|r6tdd� |D ��}|S )a� 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` Nc s s | ]}|� d d�V qdS )�=r N)�split)�.0r r r r � <genexpr> s z4EnvironmentContext.get_x_argument.<locals>.<genexpr>)r Zcmd_opts�x�dict)r Z as_dictionary�valuer r r �get_x_argument� s )z!EnvironmentContext.get_x_argument�upgrades� downgrades�op.�sa.c K s: | j }|dk r||d<