2015-04-10 16:15:27 +00:00
|
|
|
import os, sys
|
|
|
|
import sqlite3
|
|
|
|
from pkg_resources import resource_string
|
|
|
|
|
|
|
|
class DBError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_schema(version):
|
2016-04-21 02:04:01 +00:00
|
|
|
schema_bytes = resource_string("wormhole.server",
|
2016-04-15 23:53:11 +00:00
|
|
|
"db-schemas/v%d.sql" % version)
|
2015-09-27 00:51:21 +00:00
|
|
|
return schema_bytes.decode("utf-8")
|
2015-04-10 16:15:27 +00:00
|
|
|
|
|
|
|
def get_db(dbfile, stderr=sys.stderr):
|
|
|
|
"""Open or create the given db file. The parent directory must exist.
|
|
|
|
Returns the db connection object, or raises DBError.
|
|
|
|
"""
|
|
|
|
|
|
|
|
must_create = not os.path.exists(dbfile)
|
|
|
|
try:
|
|
|
|
db = sqlite3.connect(dbfile)
|
2015-09-27 00:47:13 +00:00
|
|
|
except (EnvironmentError, sqlite3.OperationalError) as e:
|
2015-04-10 16:15:27 +00:00
|
|
|
raise DBError("Unable to create/open db file %s: %s" % (dbfile, e))
|
|
|
|
db.row_factory = sqlite3.Row
|
|
|
|
|
2016-05-17 05:04:25 +00:00
|
|
|
VERSION = 2
|
2015-04-10 16:15:27 +00:00
|
|
|
if must_create:
|
|
|
|
schema = get_schema(VERSION)
|
|
|
|
db.executescript(schema)
|
|
|
|
db.execute("INSERT INTO version (version) VALUES (?)", (VERSION,))
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
try:
|
|
|
|
version = db.execute("SELECT version FROM version").fetchone()[0]
|
2015-09-27 00:47:13 +00:00
|
|
|
except sqlite3.DatabaseError as e:
|
2015-04-10 16:15:27 +00:00
|
|
|
# this indicates that the file is not a compatible database format.
|
|
|
|
# Perhaps it was created with an old version, or it might be junk.
|
|
|
|
raise DBError("db file is unusable: %s" % e)
|
|
|
|
|
|
|
|
if version != VERSION:
|
|
|
|
raise DBError("Unable to handle db version %s" % version)
|
|
|
|
|
|
|
|
return db
|