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 = {}
version = int(self.execute("PRAGMA user_version").fetchone()[0]) schema["db_name"] = "ContentDb"
self.log.debug("Db version: %s, needed: %s" % (version, self.version)) schema["version"] = 3
if version < self.version: schema["tables"] = {}
self.createTables()
else:
self.execute("VACUUM")
self.log.debug("Check tables in %.3fs" % (time.time() - s))
def createTables(self): if not self.getTableVersion("site"):
# Delete all tables self.log.debug("Migrating from table version-less content.db")
self.execute("PRAGMA writable_schema = 1") version = int(self.execute("PRAGMA user_version").fetchone()[0])
self.execute("DELETE FROM sqlite_master WHERE type IN ('table', 'index', 'trigger')") if version > 0:
self.execute("PRAGMA writable_schema = 0") self.checkTables()
self.execute("VACUUM") self.execute("INSERT INTO keyvalue ?", {"json_id": 0, "key": "table.site.version", "value": 1})
self.execute("PRAGMA INTEGRITY_CHECK") self.execute("INSERT INTO keyvalue ?", {"json_id": 0, "key": "table.content.version", "value": 1})
# Create new tables
self.execute("""
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"]["site"] = {
CREATE TABLE content ( "cols": [
content_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, ["site_id", "INTEGER PRIMARY KEY ASC NOT NULL UNIQUE"],
site_id INTEGER REFERENCES site (site_id) ON DELETE CASCADE, ["address", "TEXT NOT NULL"]
inner_path TEXT, ],
size INTEGER, "indexes": [
size_files INTEGER, "CREATE UNIQUE INDEX site_address ON site (address)"
size_files_optional INTEGER, ],
modified INTEGER "schema_changed": 1
); }
""")
self.execute("CREATE UNIQUE INDEX content_key ON content (site_id, inner_path);")
self.execute("CREATE INDEX content_modified ON content (site_id, modified);")
self.execute("PRAGMA user_version = %s" % self.version) schema["tables"]["content"] = {
"cols": [
["content_id", "INTEGER PRIMARY KEY UNIQUE NOT NULL"],
["site_id", "INTEGER REFERENCES site (site_id) ON DELETE CASCADE"],
["inner_path", "TEXT"],
["size", "INTEGER"],
["size_files", "INTEGER"],
["size_files_optional", "INTEGER"],
["modified", "INTEGER"]
],
"indexes": [
"CREATE UNIQUE INDEX content_key ON content (site_id, inner_path)",
"CREATE INDEX content_modified ON content (site_id, modified)"
],
"schema_changed": 1
}
def needSite(self, site_address): return schema
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]