|
|
@@ -35,10 +35,10 @@ import bisect |
|
|
|
# seems that 'PRAGMA synchronous=NORMAL' and 'PRAGMA journal_mode=WAL' |
|
|
|
# give an equivalent speedup more safely. That is what is used here. |
|
|
|
_sql_schema_updates = { |
|
|
|
0: """ |
|
|
|
0: { "next": 1, "sql": """ |
|
|
|
-- All streams |
|
|
|
CREATE TABLE streams( |
|
|
|
id INTEGER PRIMARY KEY, -- stream ID |
|
|
|
id INTEGER PRIMARY KEY, -- stream ID |
|
|
|
path TEXT UNIQUE NOT NULL, -- path, e.g. '/newton/prep' |
|
|
|
layout TEXT NOT NULL -- layout name, e.g. float32_8 |
|
|
|
); |
|
|
@@ -59,16 +59,21 @@ _sql_schema_updates = { |
|
|
|
end_pos INTEGER NOT NULL |
|
|
|
); |
|
|
|
CREATE INDEX _ranges_index ON ranges (stream_id, start_time, end_time); |
|
|
|
""", |
|
|
|
""" }, |
|
|
|
|
|
|
|
1: """ |
|
|
|
1: { "next": 3, "sql": """ |
|
|
|
-- Generic dictionary-type metadata that can be associated with a stream |
|
|
|
CREATE TABLE metadata( |
|
|
|
stream_id INTEGER NOT NULL, |
|
|
|
key TEXT NOT NULL, |
|
|
|
value TEXT |
|
|
|
); |
|
|
|
""", |
|
|
|
""" }, |
|
|
|
|
|
|
|
2: { "error": "old format with floating-point timestamps requires " |
|
|
|
"nilmdb 1.3.1 or older" }, |
|
|
|
|
|
|
|
3: { "next": None }, |
|
|
|
} |
|
|
|
|
|
|
|
@nilmdb.utils.must_close() |
|
|
@@ -96,7 +101,10 @@ class NilmDB(object): |
|
|
|
# SQLite database too |
|
|
|
sqlfilename = os.path.join(self.basepath, "data.sql") |
|
|
|
self.con = sqlite3.connect(sqlfilename, check_same_thread = True) |
|
|
|
self._sql_schema_update() |
|
|
|
try: |
|
|
|
self._sql_schema_update() |
|
|
|
finally: # pragma: no cover |
|
|
|
self.data.close() |
|
|
|
|
|
|
|
# See big comment at top about the performance implications of this |
|
|
|
self.con.execute("PRAGMA synchronous=NORMAL") |
|
|
@@ -123,11 +131,20 @@ class NilmDB(object): |
|
|
|
version = cur.execute("PRAGMA user_version").fetchone()[0] |
|
|
|
oldversion = version |
|
|
|
|
|
|
|
while version in _sql_schema_updates: |
|
|
|
cur.executescript(_sql_schema_updates[version]) |
|
|
|
version = version + 1 |
|
|
|
while True: |
|
|
|
if version not in _sql_schema_updates: # pragma: no cover |
|
|
|
raise Exception(self.basepath + ": unknown database version " |
|
|
|
+ str(version)) |
|
|
|
update = _sql_schema_updates[version] |
|
|
|
if "error" in update: # pragma: no cover |
|
|
|
raise Exception(self.basepath + ": can't use database version " |
|
|
|
+ str(version) + ": " + update["error"]) |
|
|
|
if update["next"] is None: |
|
|
|
break |
|
|
|
cur.executescript(update["sql"]) |
|
|
|
version = update["next"] |
|
|
|
if self.verbose: # pragma: no cover |
|
|
|
printf("Schema updated to %d\n", version) |
|
|
|
printf("Database schema updated to %d\n", version) |
|
|
|
|
|
|
|
if version != oldversion: |
|
|
|
with self.con: |
|
|
|