"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/linotpd/src/upgrades/env.py" (13 May 2019, 5172 Bytes) of package /linux/misc/LinOTP-release-2.10.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "env.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #    LinOTP - the open source solution for two factor authentication
    4 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    5 #
    6 #    This file is part of LinOTP server.
    7 #
    8 #    This program is free software: you can redistribute it and/or
    9 #    modify it under the terms of the GNU Affero General Public
   10 #    License, version 3, as published by the Free Software Foundation.
   11 #
   12 #    This program is distributed in the hope that it will be useful,
   13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 #    GNU Affero General Public License for more details.
   16 #
   17 #    You should have received a copy of the
   18 #               GNU Affero General Public License
   19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   20 #
   21 #
   22 #    E-mail: linotp@keyidentity.com
   23 #    Contact: www.linotp.org
   24 #    Support: www.keyidentity.com
   25 #
   26 from __future__ import with_statement
   27 from alembic import context
   28 from sqlalchemy import engine_from_config, pool
   29 from logging.config import fileConfig
   30 import logging
   31 import re
   32 
   33 USE_TWOPHASE = False
   34 
   35 # this is the Alembic Config object, which provides
   36 # access to the values within the .ini file in use.
   37 config = context.config
   38 
   39 # Interpret the config file for Python logging.
   40 # This line sets up loggers basically.
   41 fileConfig(config.config_file_name)
   42 logger = logging.getLogger('alembic.env')
   43 
   44 # gather section names referring to different
   45 # databases.  These are named "engine1", "engine2"
   46 # in the sample .ini file.
   47 db_names = config.get_main_option('databases')
   48 
   49 # add your model's MetaData objects here
   50 # for 'autogenerate' support.  These must be set
   51 # up to hold just those tables targeting a
   52 # particular database. table.tometadata() may be
   53 # helpful here in case a "copy" of
   54 # a MetaData is needed.
   55 # from myapp import mymodel
   56 # target_metadata = {
   57 #       'engine1':mymodel.metadata1,
   58 #       'engine2':mymodel.metadata2
   59 # }
   60 target_metadata = {}
   61 
   62 # other values from the config, defined by the needs of env.py,
   63 # can be acquired:
   64 # my_important_option = config.get_main_option("my_important_option")
   65 # ... etc.
   66 
   67 
   68 def run_migrations_offline():
   69     """Run migrations in 'offline' mode.
   70 
   71     This configures the context with just a URL
   72     and not an Engine, though an Engine is acceptable
   73     here as well.  By skipping the Engine creation
   74     we don't even need a DBAPI to be available.
   75 
   76     Calls to context.execute() here emit the given string to the
   77     script output.
   78 
   79     """
   80     # for the --sql use case, run migrations for each URL into
   81     # individual files.
   82 
   83     engines = {}
   84     for name in re.split(r',\s*', db_names):
   85         engines[name] = rec = {}
   86         rec['url'] = context.config.get_section_option(name,
   87                                                        "sqlalchemy.url")
   88 
   89     for name, rec in engines.items():
   90         logger.info("Migrating database %s" % name)
   91         file_ = "%s.sql" % name
   92         logger.info("Writing output to %s" % file_)
   93         with open(file_, 'w') as buffer:
   94             context.configure(url=rec['url'],
   95                               version_table='upgrade_%s' % name,
   96                               output_buffer=buffer,
   97                               target_metadata=target_metadata.get(name))
   98             with context.begin_transaction():
   99                 context.run_migrations(engine_name=name)
  100 
  101 
  102 def run_migrations_online():
  103     """Run migrations in 'online' mode.
  104 
  105     In this scenario we need to create an Engine
  106     and associate a connection with the context.
  107 
  108     """
  109 
  110     # for the direct-to-DB use case, start a transaction on all
  111     # engines, then run all migrations, then commit all transactions.
  112 
  113     engines = {}
  114     for name in re.split(r',\s*', db_names):
  115         engines[name] = rec = {}
  116         rec['engine'] = engine_from_config(
  117             context.config.get_section(name),
  118             prefix='sqlalchemy.',
  119             poolclass=pool.NullPool)
  120 
  121     for name, rec in engines.items():
  122         engine = rec['engine']
  123         rec['connection'] = conn = engine.connect()
  124 
  125         if USE_TWOPHASE:
  126             rec['transaction'] = conn.begin_twophase()
  127         else:
  128             rec['transaction'] = conn.begin()
  129 
  130     try:
  131         for name, rec in engines.items():
  132             logger.info("Migrating database %s" % name)
  133             context.configure(
  134                 version_table='upgrade_%s' % name,
  135                 connection=rec['connection'],
  136                 upgrade_token="%s_upgrades" % name,
  137                 downgrade_token="%s_downgrades" % name,
  138                 target_metadata=target_metadata.get(name)
  139             )
  140             context.run_migrations(engine_name=name)
  141 
  142         if USE_TWOPHASE:
  143             for rec in engines.values():
  144                 rec['transaction'].prepare()
  145 
  146         for rec in engines.values():
  147             rec['transaction'].commit()
  148     except:
  149         for rec in engines.values():
  150             rec['transaction'].rollback()
  151         raise
  152     finally:
  153         for rec in engines.values():
  154             rec['connection'].close()
  155 
  156 
  157 if context.is_offline_mode():
  158     run_migrations_offline()
  159 else:
  160     run_migrations_online()