Ejemplo n.º 1
0
def upgrade_db(options):
    """
    Upgrades database to latest (head) revision.
    """
    init_model(config, check_version=False)
    cfg = migrationsutil.create_config()
    command.upgrade(cfg, revision="head")
Ejemplo n.º 2
0
def init_model(config, drop=False, check_version=True):
    """
    Initializes the tables and classes of the model using specified engine.

    You must call this method before using any of the tables or classes in
    the model!

    :param config: The application configurat    ion object, where SA config is prefixed with "sqlalchemy."
    :type config: dict
    
    :param drop: Whether to drop the tables first.
    :type drop: bool
    
    :param check_version: Whether to ensure that the database version is up-to-date.
    :type check_version: bool
    """
    engine = engine_from_config(config)
    sm = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
    meta.engine = engine
    meta.Session = orm.scoped_session(sm)
    
    alembic_cfg = migrationsutil.create_config()

    # Check to see whether the database has already been created or not.
    # Based on this, we know whether we need to upgrade the database or mark the database
    # as the latest version.
    
    inspector = Inspector.from_engine(engine)
    # We choose an arbitrary table name here to see if database objects have been created
    db_objects_created = (groups_table.name in inspector.get_table_names())
    fresh_db = False
    if not db_objects_created:
        log.info("Database apears uninitialized, creating database tables")
        meta.metadata.create_all(engine, checkfirst=True)
        fresh_db = True
    elif drop:
        log.info("Dropping database tables and re-creating.")
        meta.metadata.drop_all(engine, checkfirst=True)
        meta.metadata.create_all(engine)
        fresh_db = True
        
    if fresh_db:
        command.stamp(alembic_cfg, "head")
    else:
        # Existing model may need upgrade.
        if check_version:
            latest = migrationsutil.get_head_version()
            installed = migrationsutil.get_database_version()
            if latest != installed:
                raise DatabaseVersionError("Installed database ({0}) does not match latest available ({1}). (Use the `paver upgrade_db` command.)".format(installed, latest))
        else:
            log.info("Skipping database upgrade.")
    
    if check_version:
        # We only want to run this code if this is a normal version-checking model initialization.
        # Basically we do *not* want to run this when we are upgrading the database.
        # (This may need to get refactored to be clearer.)
        #
        # Special check for acl data (this is kinda kludgy, but app won't work if this row isn't here.)
        s = meta.Session()
        a = s.query(Access).get(1)
        if not a:
            # Can't import this at top-level due to circular
            from ensconce import acl
            a = Access()
            a.description = 'Administrator'
            a.level = acl.ALL_ACCESS
            s.add(a)
            s.commit() # We really do want to commit immediately in this particular case.