__init__.py 文件源码

python
阅读 82 收藏 0 点赞 0 评论 0

项目:piwheels 作者: bennuttall 项目源码 文件源码
def detect_version(conn):
    """
    Detect the version of the database. This is typically done by reading the
    contents of the ``configuration`` table, but before that was added we can
    guess a couple of versions based on what tables exist (or don't). Returns
    ``None`` if the database appears uninitialized, and raises
    :exc:`RuntimeError` is the version is so ancient we can't do anything with
    it.
    """
    try:
        with conn.begin():
            db_version = conn.scalar(text(
                "SELECT version FROM configuration"))
    except exc.ProgrammingError:
        with conn.begin():
            packages_exists = bool(conn.scalar(text(
                "SELECT 1 FROM pg_catalog.pg_tables "
                "WHERE schemaname = 'public' AND tablename = 'packages'")))
        with conn.begin():
            statistics_exists = bool(conn.scalar(text(
                "SELECT 1 FROM pg_catalog.pg_views "
                "WHERE schemaname = 'public' AND viewname = 'statistics'")))
        with conn.begin():
            files_exists = bool(conn.scalar(text(
                "SELECT 1 FROM pg_catalog.pg_tables "
                "WHERE schemaname = 'public' AND tablename = 'files'")))
        if not packages_exists:
            # Database is uninitialized
            return None
        elif not files_exists:
            # Database is too ancient to upgrade
            raise RuntimeError("Database version older than 0.4; cannot upgrade")
        elif not statistics_exists:
            return "0.4"
        else:
            return "0.5"
    else:
        return db_version
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号