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
评论列表
文章目录