| 61 |
|
sha256 TEXT, |
| 62 |
|
status TEXT, |
| 63 |
|
no_intro_system_id INTEGER NOT NULL, |
| 64 |
+ |
app_system_id INTEGER, |
| 65 |
|
FOREIGN KEY(no_intro_system_id) REFERENCES no_intro_system(id) |
| 66 |
|
) |
| 67 |
|
""", |
| 73 |
|
crc TEXT, |
| 74 |
|
md5 TEXT, |
| 75 |
|
sha1 TEXT, |
| 76 |
< |
system_id INTEGER NOT NULL, |
| 76 |
> |
system_id INTEGER, |
| 77 |
> |
app_system_id INTEGER, |
| 78 |
|
FOREIGN KEY(system_id) REFERENCES no_intro_system(id) |
| 79 |
|
) |
| 80 |
|
""", |
| 81 |
+ |
"dat_import_history": """ |
| 82 |
+ |
CREATE TABLE dat_import_history( |
| 83 |
+ |
id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 84 |
+ |
app_system_id INTEGER NOT NULL, |
| 85 |
+ |
source TEXT NOT NULL, |
| 86 |
+ |
dat_name TEXT, |
| 87 |
+ |
dat_description TEXT, |
| 88 |
+ |
dat_version TEXT, |
| 89 |
+ |
imported_at TEXT NOT NULL, |
| 90 |
+ |
entry_count INTEGER NOT NULL DEFAULT 0, |
| 91 |
+ |
UNIQUE(app_system_id, source), |
| 92 |
+ |
FOREIGN KEY(app_system_id) REFERENCES main_app_system(id) |
| 93 |
+ |
) |
| 94 |
+ |
""", |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
def create_database(self): |
| 144 |
|
"no_intro_main", |
| 145 |
|
[ |
| 146 |
|
"game_name", "no_intro_id", "clone_of_id", "description", "rom_name", |
| 147 |
< |
"crc", "md5", "sha1", "sha256", "status", "no_intro_system_id", |
| 147 |
> |
"crc", "md5", "sha1", "sha256", "status", "no_intro_system_id", "app_system_id", |
| 148 |
|
], |
| 149 |
|
) |
| 150 |
|
self._ensure_columns( |
| 151 |
|
cursor, |
| 152 |
|
"tosec_main", |
| 153 |
< |
["game_name", "description", "rom_name", "crc", "md5", "sha1", "system_id"], |
| 153 |
> |
["game_name", "description", "rom_name", "crc", "md5", "sha1", "system_id", "app_system_id"], |
| 154 |
> |
) |
| 155 |
> |
self._ensure_columns( |
| 156 |
> |
cursor, |
| 157 |
> |
"dat_import_history", |
| 158 |
> |
["app_system_id", "source", "dat_name", "dat_description", "dat_version", "imported_at", "entry_count"], |
| 159 |
|
) |
| 160 |
|
|
| 161 |
|
self._backfill_aliases(cursor) |
| 239 |
|
"UPDATE no_intro_main SET no_intro_system_id=system_id " |
| 240 |
|
"WHERE (no_intro_system_id IS NULL OR no_intro_system_id='') AND system_id IS NOT NULL" |
| 241 |
|
) |
| 242 |
+ |
if self._column_exists(cursor, "no_intro_main", "app_system_id"): |
| 243 |
+ |
cursor.execute( |
| 244 |
+ |
""" |
| 245 |
+ |
UPDATE no_intro_main |
| 246 |
+ |
SET app_system_id = ( |
| 247 |
+ |
SELECT ma.system_console |
| 248 |
+ |
FROM main_app_file_hash fh |
| 249 |
+ |
JOIN main_app ma ON ma.id = fh.container_file_id |
| 250 |
+ |
WHERE lower(fh.file_md5_hash) = lower(no_intro_main.md5) |
| 251 |
+ |
GROUP BY ma.system_console |
| 252 |
+ |
ORDER BY COUNT(*) DESC |
| 253 |
+ |
LIMIT 1 |
| 254 |
+ |
) |
| 255 |
+ |
WHERE app_system_id IS NULL |
| 256 |
+ |
""" |
| 257 |
+ |
) |
| 258 |
|
|
| 259 |
|
# tosec_main alias mapping |
| 260 |
|
if self._column_exists(cursor, "tosec_main", "rom_hash"): |
| 267 |
|
"UPDATE tosec_main SET rom_name=rom_filename " |
| 268 |
|
"WHERE (rom_name IS NULL OR rom_name='') AND rom_filename IS NOT NULL" |
| 269 |
|
) |
| 270 |
+ |
if self._column_exists(cursor, "tosec_main", "app_system_id"): |
| 271 |
+ |
cursor.execute( |
| 272 |
+ |
""" |
| 273 |
+ |
UPDATE tosec_main |
| 274 |
+ |
SET app_system_id = ( |
| 275 |
+ |
SELECT ma.system_console |
| 276 |
+ |
FROM main_app_file_hash fh |
| 277 |
+ |
JOIN main_app ma ON ma.id = fh.container_file_id |
| 278 |
+ |
WHERE lower(fh.file_md5_hash) = lower(tosec_main.md5) |
| 279 |
+ |
GROUP BY ma.system_console |
| 280 |
+ |
ORDER BY COUNT(*) DESC |
| 281 |
+ |
LIMIT 1 |
| 282 |
+ |
) |
| 283 |
+ |
WHERE app_system_id IS NULL |
| 284 |
+ |
""" |
| 285 |
+ |
) |
| 286 |
|
|
| 287 |
|
def _create_indexes(self, cursor): |
| 288 |
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_main_app_md5 ON main_app(container_md5_hash)") |
| 289 |
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_main_app_system ON main_app(system_console)") |
| 290 |
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_no_intro_md5 ON no_intro_main(md5)") |
| 291 |
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_tosec_md5 ON tosec_main(md5)") |
| 292 |
+ |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_no_intro_system_md5 ON no_intro_main(app_system_id, md5)") |
| 293 |
+ |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_tosec_system_md5 ON tosec_main(app_system_id, md5)") |
| 294 |
+ |
cursor.execute("CREATE INDEX IF NOT EXISTS idx_dat_import_system_source ON dat_import_history(app_system_id, source)") |
| 295 |
|
|
| 296 |
|
def backup_database(self, backup_path): |
| 297 |
|
"""Copies the current database to a new location.""" |
| 309 |
|
for table in [ |
| 310 |
|
"main_app_system", "main_app", |
| 311 |
|
"no_intro_system", "no_intro_main", |
| 312 |
< |
"tosec_main" |
| 312 |
> |
"tosec_main", "dat_import_history" |
| 313 |
|
]: |
| 314 |
|
cursor.execute( |
| 315 |
|
"SELECT name FROM sqlite_master WHERE type='table' AND name=?", |