Change to schema based ContentDb to easier extension

This commit is contained in:
shortcutme 2016-11-07 23:01:40 +01:00
parent 0706748d10
commit c35ffccf6c

View file

@ -8,57 +8,56 @@ from Plugin import PluginManager
@PluginManager.acceptPlugins @PluginManager.acceptPlugins
class ContentDb(Db): class ContentDb(Db):
def __init__(self, path): def __init__(self, path):
self.version = 4 Db.__init__(self, {"db_name": "ContentDb", "tables": {}}, path)
super(ContentDb, self).__init__({"db_name": "ContentDb"}, path)
self.foreign_keys = True self.foreign_keys = True
self.schema = self.getSchema()
self.checkTables() self.checkTables()
self.site_ids = {} self.site_ids = {}
self.sites = {}
def checkTables(self): def getSchema(self):
s = time.time() schema = {}
schema["db_name"] = "ContentDb"
schema["version"] = 3
schema["tables"] = {}
if not self.getTableVersion("site"):
self.log.debug("Migrating from table version-less content.db")
version = int(self.execute("PRAGMA user_version").fetchone()[0]) version = int(self.execute("PRAGMA user_version").fetchone()[0])
self.log.debug("Db version: %s, needed: %s" % (version, self.version)) if version > 0:
if version < self.version: self.checkTables()
self.createTables() self.execute("INSERT INTO keyvalue ?", {"json_id": 0, "key": "table.site.version", "value": 1})
else: self.execute("INSERT INTO keyvalue ?", {"json_id": 0, "key": "table.content.version", "value": 1})
self.execute("VACUUM")
self.log.debug("Check tables in %.3fs" % (time.time() - s))
def createTables(self): schema["tables"]["site"] = {
# Delete all tables "cols": [
self.execute("PRAGMA writable_schema = 1") ["site_id", "INTEGER PRIMARY KEY ASC NOT NULL UNIQUE"],
self.execute("DELETE FROM sqlite_master WHERE type IN ('table', 'index', 'trigger')") ["address", "TEXT NOT NULL"]
self.execute("PRAGMA writable_schema = 0") ],
self.execute("VACUUM") "indexes": [
self.execute("PRAGMA INTEGRITY_CHECK") "CREATE UNIQUE INDEX site_address ON site (address)"
# Create new tables ],
self.execute(""" "schema_changed": 1
CREATE TABLE site ( }
site_id INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL UNIQUE,
address TEXT NOT NULL
);
""")
self.execute("CREATE UNIQUE INDEX site_address ON site (address);")
self.execute(""" schema["tables"]["content"] = {
CREATE TABLE content ( "cols": [
content_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, ["content_id", "INTEGER PRIMARY KEY UNIQUE NOT NULL"],
site_id INTEGER REFERENCES site (site_id) ON DELETE CASCADE, ["site_id", "INTEGER REFERENCES site (site_id) ON DELETE CASCADE"],
inner_path TEXT, ["inner_path", "TEXT"],
size INTEGER, ["size", "INTEGER"],
size_files INTEGER, ["size_files", "INTEGER"],
size_files_optional INTEGER, ["size_files_optional", "INTEGER"],
modified INTEGER ["modified", "INTEGER"]
); ],
""") "indexes": [
self.execute("CREATE UNIQUE INDEX content_key ON content (site_id, inner_path);") "CREATE UNIQUE INDEX content_key ON content (site_id, inner_path)",
self.execute("CREATE INDEX content_modified ON content (site_id, modified);") "CREATE INDEX content_modified ON content (site_id, modified)"
],
"schema_changed": 1
}
self.execute("PRAGMA user_version = %s" % self.version) return schema
def needSite(self, site_address):
if site_address not in self.site_ids:
self.execute("INSERT OR IGNORE INTO site ?", {"address": site_address})
for row in self.execute("SELECT * FROM site"): for row in self.execute("SELECT * FROM site"):
self.site_ids[row["address"]] = row["site_id"] self.site_ids[row["address"]] = row["site_id"]
return self.site_ids[site_address] return self.site_ids[site_address]