| 210 | # Check Db tables |
| 211 | # Return: <list> Changed table names |
| 212 | def checkTables(self): |
| 213 | s = time.time() |
| 214 | changed_tables = [] |
| 215 | |
| 216 | cur = self.getSharedCursor() |
| 217 | |
| 218 | # Check internal tables |
| 219 | # Check keyvalue table |
| 220 | changed = cur.needTable("keyvalue", [ |
| 221 | ["keyvalue_id", "INTEGER PRIMARY KEY AUTOINCREMENT"], |
| 222 | ["key", "TEXT"], |
| 223 | ["value", "INTEGER"], |
| 224 | ["json_id", "INTEGER"], |
| 225 | ], [ |
| 226 | "CREATE UNIQUE INDEX key_id ON keyvalue(json_id, key)" |
| 227 | ], version=self.schema["version"]) |
| 228 | if changed: |
| 229 | changed_tables.append("keyvalue") |
| 230 | |
| 231 | # Create json table if no custom one defined |
| 232 | if "json" not in self.schema.get("tables", {}): |
| 233 | if self.schema["version"] == 1: |
| 234 | changed = cur.needTable("json", [ |
| 235 | ["json_id", "INTEGER PRIMARY KEY AUTOINCREMENT"], |
| 236 | ["path", "VARCHAR(255)"] |
| 237 | ], [ |
| 238 | "CREATE UNIQUE INDEX path ON json(path)" |
| 239 | ], version=self.schema["version"]) |
| 240 | elif self.schema["version"] == 2: |
| 241 | changed = cur.needTable("json", [ |
| 242 | ["json_id", "INTEGER PRIMARY KEY AUTOINCREMENT"], |
| 243 | ["directory", "VARCHAR(255)"], |
| 244 | ["file_name", "VARCHAR(255)"] |
| 245 | ], [ |
| 246 | "CREATE UNIQUE INDEX path ON json(directory, file_name)" |
| 247 | ], version=self.schema["version"]) |
| 248 | elif self.schema["version"] == 3: |
| 249 | changed = cur.needTable("json", [ |
| 250 | ["json_id", "INTEGER PRIMARY KEY AUTOINCREMENT"], |
| 251 | ["site", "VARCHAR(255)"], |
| 252 | ["directory", "VARCHAR(255)"], |
| 253 | ["file_name", "VARCHAR(255)"] |
| 254 | ], [ |
| 255 | "CREATE UNIQUE INDEX path ON json(directory, site, file_name)" |
| 256 | ], version=self.schema["version"]) |
| 257 | if changed: |
| 258 | changed_tables.append("json") |
| 259 | |
| 260 | # Check schema tables |
| 261 | for table_name, table_settings in self.schema.get("tables", {}).items(): |
| 262 | try: |
| 263 | indexes = table_settings.get("indexes", []) |
| 264 | version = table_settings.get("schema_changed", 0) |
| 265 | changed = cur.needTable( |
| 266 | table_name, table_settings["cols"], |
| 267 | indexes, version=version |
| 268 | ) |
| 269 | if changed: |