示例#1
0
# # inside of a "create the database" script, first create
# # tables:
# my_metadata.create_all(engine)

# then, load the Alembic configuration and generate the
# version table, "stamping" it with the most recent rev:
import alembic.util
from alembic.config import Config
from alembic import command
from alembic.script import ScriptDirectory
from core.utils.init import home_dir

alembic_cfg = Config("%s/migrations/alembic.ini" % home_dir())
script = ScriptDirectory.from_config(alembic_cfg)


def run(connection_string):
    revision = "4f80a6b3ffc2"  # Added selenium log field

    alembic_cfg.set_main_option("sqlalchemy.url", connection_string)
    try:
        command.upgrade(alembic_cfg, revision)
    except alembic.util.CommandError:
        try:
            command.downgrade(alembic_cfg, revision)
        except alembic.util.CommandError:
            raise Exception("Could not upgrade nor downgrade "
                            "database to revision %s" % revision)
示例#2
0
import logging
from datetime import datetime, timedelta

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import ArgumentError

from core.config import config, setup_config
from core.db.models import Session, User
from core.utils import change_user_vmmaster
from core.utils.init import home_dir

from shutil import rmtree
from errno import ENOENT

setup_config('%s/config.py' % home_dir())
log = logging.getLogger(__name__)

engine = create_engine(config.DATABASE)
session_factory = sessionmaker(bind=engine)


def transaction(func):
    def wrapper(*args, **kwargs):
        dbsession = session_factory()
        try:
            return func(dbsession=dbsession, *args, **kwargs)
        except:
            dbsession.rollback()
            raise
        finally: