Exemplo n.º 1
0
def get_installed_version(db_uri, schema_name):
    """
    Return the currently installed version number of the given schema
    in this database, or None if it is not currently installed.
    """
    db = connect_uri(db_uri)
    try:
        c = db.cursor()
        # First: Check to be sure the sa_meta schema and versions tables exist
        c.execute("""
            select exists
             (select true from pg_tables
                where schemaname = 'sa_meta' and tablename = 'versions')
        """)
        if not c.fetchall()[0][0]:
            # The meta-schema is not installed, so surely nothing else is.
            return None
        c.execute("""
            select version from sa_meta.versions
              where schema_name = %(schema_name)s
            order by schema_name
        """, {'schema_name': schema_name})
        r = c.fetchone()
        if r is None:
            return None
        return r[0]
    finally:
        db.close()
Exemplo n.º 2
0
def update_or_install_schema(db_uri, paths, schema_name):
    """
    Connect to this database and Update the named schema to the latest
    version available in paths (a list of directories), or install it
    if it is not currently installed.
    """
    analyze_paths(paths)
    installed_ver = get_installed_version(db_uri, schema_name)
    latest_ver = schema_latest.get(schema_name, None)
    if installed_ver == latest_ver:
        # Already done--do nothing.
        return None
    if latest_ver is None:
        raise Exception(
            "No install script for schema %s is available" % schema_name)
    # Find an upgrade script
    try:
        upgrade_file = \
            schema_upgrades[schema_name][installed_ver][latest_ver]
    except KeyError:
        raise Exception(
            "No upgrade script from %s to %s for schema %s is available" %
            (installed_ver, latest_ver, schema_name))
    sql = open(upgrade_file, 'r').read()
    db = connect_uri(db_uri)
    try:
        try:
            c = db.cursor()
            c.execute(sql)
            c.execute("""
                delete from sa_meta.versions where schema_name = %(schema_name)s
            """, {'schema_name': schema_name})
            c.execute("""
                insert into sa_meta.versions ( schema_name, version, load_time )
                  values ( %(schema_name)s, %(version)s, current_timestamp );
            """, {'schema_name': schema_name, 'version': latest_ver})
            db.commit()
            return latest_ver
        except:
            db.rollback()
            raise
    finally:
#       for notice in db.notices:
#           sys.stdout.write(notice)
        db.close()
Exemplo n.º 3
0
def get_installed_schemas(db_uri):
    """
    Return a list of (name, verson, load_time) triples describing what
    schemas are currently installed in this database, and when they
    were last installed or updated.
    """
    # Check that sa_meta is installed:
    sa_meta_ver = get_installed_version(db_uri, "sa_meta")
    if not sa_meta_ver:
        # Nothing is installed
        return []
    db = connect_uri(db_uri)
    try:
        c = db.cursor()
        c.execute("""
            select schema_name, version, load_time from sa_meta.versions
        """)
        result = []
        for r in c:
            result.append((r[0], r[1], r[2]))
        return result
    finally:
        db.close()