Return the version value stored in the db

def get_db_version(db_filename: Path) -> int:
    """Return the version value stored in the db"""

    assert os.path.exists(db_filename)

    # Perform a query directly through SQL rather than using
    # storage.get_version()
    # as get_version will return the latest version if it doesn't
    # find a record in the database.
    conn = sqlite3.connect(
        str(db_filename),
        detect_types=sqlite3.PARSE_DECLTYPES,
    )
    cursor = conn.cursor()

    try:
        cursor.execute('SELECT value FROM settings WHERE name="version";')
        result = cursor.fetchone()
    except sqlite3.OperationalError:
        raise RuntimeError(
            'Corrupted database. Database does not the settings table.',
        )

    if not result:
        raise RuntimeError(
            'Corrupted database. Settings table does not contain an entry the db version.',
        )

    return int(result[0])