Browse Source

Bump database version to 3, reject old version 2 due to timestamp changes

tags/nilmdb-1.4.0^0
Jim Paris 11 years ago
parent
commit
52ae397d7d
1 changed files with 27 additions and 10 deletions
  1. +27
    -10
      nilmdb/server/nilmdb.py

+ 27
- 10
nilmdb/server/nilmdb.py View File

@@ -35,10 +35,10 @@ import bisect
# seems that 'PRAGMA synchronous=NORMAL' and 'PRAGMA journal_mode=WAL' # seems that 'PRAGMA synchronous=NORMAL' and 'PRAGMA journal_mode=WAL'
# give an equivalent speedup more safely. That is what is used here. # give an equivalent speedup more safely. That is what is used here.
_sql_schema_updates = { _sql_schema_updates = {
0: """
0: { "next": 1, "sql": """
-- All streams -- All streams
CREATE TABLE 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' path TEXT UNIQUE NOT NULL, -- path, e.g. '/newton/prep'
layout TEXT NOT NULL -- layout name, e.g. float32_8 layout TEXT NOT NULL -- layout name, e.g. float32_8
); );
@@ -59,16 +59,21 @@ _sql_schema_updates = {
end_pos INTEGER NOT NULL end_pos INTEGER NOT NULL
); );
CREATE INDEX _ranges_index ON ranges (stream_id, start_time, end_time); 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 -- Generic dictionary-type metadata that can be associated with a stream
CREATE TABLE metadata( CREATE TABLE metadata(
stream_id INTEGER NOT NULL, stream_id INTEGER NOT NULL,
key TEXT NOT NULL, key TEXT NOT NULL,
value TEXT value TEXT
); );
""",
""" },

2: { "error": "old format with floating-point timestamps requires "
"nilmdb 1.3.1 or older" },

3: { "next": None },
} }


@nilmdb.utils.must_close() @nilmdb.utils.must_close()
@@ -96,7 +101,10 @@ class NilmDB(object):
# SQLite database too # SQLite database too
sqlfilename = os.path.join(self.basepath, "data.sql") sqlfilename = os.path.join(self.basepath, "data.sql")
self.con = sqlite3.connect(sqlfilename, check_same_thread = True) 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 # See big comment at top about the performance implications of this
self.con.execute("PRAGMA synchronous=NORMAL") self.con.execute("PRAGMA synchronous=NORMAL")
@@ -123,11 +131,20 @@ class NilmDB(object):
version = cur.execute("PRAGMA user_version").fetchone()[0] version = cur.execute("PRAGMA user_version").fetchone()[0]
oldversion = version 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 if self.verbose: # pragma: no cover
printf("Schema updated to %d\n", version)
printf("Database schema updated to %d\n", version)


if version != oldversion: if version != oldversion:
with self.con: with self.con:


Loading…
Cancel
Save