1
+ import datetime
1
2
import json
2
3
import os
3
4
import sqlite3
4
5
from modules import scripts
5
- import datetime
6
6
7
- version = 3
7
+ version = 4
8
8
9
9
path_recorder_file = os .path .join (scripts .basedir (), "path_recorder.txt" )
10
10
aes_cache_file = os .path .join (scripts .basedir (), "aes_scores.json" )
@@ -341,30 +341,53 @@ def migrate_ranking_dirs(cursor, db_version):
341
341
342
342
return
343
343
344
+ def get_c_time (file ):
345
+ c_time = datetime .datetime .fromtimestamp (os .path .getctime (file )).strftime ('%Y-%m-%d %H:%M:%S' )
346
+ return c_time
347
+
348
+ def migrate_ranking_c_time (cursor ):
349
+ cursor .execute ('''
350
+ SELECT file
351
+ FROM ranking
352
+ ''' )
353
+ for (file ,) in cursor .fetchall ():
354
+ if os .path .exists (file ):
355
+ c_time = get_c_time (file )
356
+ cursor .execute ('''
357
+ UPDATE ranking
358
+ SET created = ?
359
+ WHERE file = ?
360
+ ''' , (c_time , file ))
361
+
362
+ return
363
+
344
364
def check ():
345
365
if not os .path .exists (db_file ):
346
- print ("Image Browser: Creating database" )
347
366
conn , cursor = transaction_begin ()
367
+ print ("Image Browser: Creating database" )
348
368
create_db (cursor )
349
369
update_db_data (cursor , "version" , version )
350
370
migrate_path_recorder (cursor )
351
371
migrate_exif_data (cursor )
352
372
migrate_ranking (cursor )
373
+ migrate_ranking_c_time (cursor )
353
374
transaction_end (conn , cursor )
354
375
print ("Image Browser: Database created" )
355
376
db_version = get_version ()
377
+ conn , cursor = transaction_begin ()
356
378
if db_version [0 ] <= "2" :
357
379
# version 1 database had mixed path notations, changed them all to abspath
358
380
# version 2 database still had mixed path notations, because of windows short name, changed them all to realpath
359
381
print (f"Image Browser: Upgrading database from version { db_version [0 ]} to version { version } " )
360
- conn , cursor = transaction_begin ()
361
- update_db_data (cursor , "version" , version )
362
382
migrate_path_recorder_dirs (cursor )
363
383
migrate_exif_data_dirs (cursor )
364
384
migrate_ranking_dirs (cursor , db_version [0 ])
365
- transaction_end (conn , cursor )
385
+ if db_version [0 ] <= "3" :
386
+ migrate_ranking_c_time (cursor )
387
+ update_db_data (cursor , "version" , version )
366
388
print (f"Image Browser: Database upgraded from version { db_version [0 ]} to version { version } " )
367
-
389
+ transaction_end (conn , cursor )
390
+
368
391
return version
369
392
370
393
def load_path_recorder ():
@@ -378,19 +401,19 @@ def load_path_recorder():
378
401
379
402
return path_recorder
380
403
381
- def select_ranking (filename ):
404
+ def select_ranking (file ):
382
405
conn , cursor = transaction_begin ()
383
406
cursor .execute ('''
384
407
SELECT ranking
385
408
FROM ranking
386
409
WHERE file = ?
387
- ''' , (filename ,))
410
+ ''' , (file ,))
388
411
ranking_value = cursor .fetchone ()
389
412
390
413
# if ranking not found search again, without path (moved?)
391
414
if ranking_value is None :
392
- c_time = datetime . datetime . fromtimestamp ( os . path . getctime ( filename )). strftime ( '%Y-%m-%d %H:%M:%S' )
393
- name = os .path .basename (filename )
415
+ c_time = get_c_time ( file )
416
+ name = os .path .basename (file )
394
417
cursor .execute ('''
395
418
SELECT ranking
396
419
FROM ranking
@@ -411,7 +434,7 @@ def select_ranking(filename):
411
434
cursor .execute ('''
412
435
INSERT INTO ranking (file, name, ranking, created)
413
436
VALUES (?, ?, ?, ?)
414
- ''' , (filename , name , insert_ranking , c_time ))
437
+ ''' , (file , name , insert_ranking , c_time ))
415
438
416
439
if ranking_value is None :
417
440
return_ranking = "None"
@@ -425,7 +448,7 @@ def update_ranking(file, ranking):
425
448
name = os .path .basename (file )
426
449
with sqlite3 .connect (db_file , timeout = timeout ) as conn :
427
450
cursor = conn .cursor ()
428
- c_time = datetime . datetime . fromtimestamp ( os . path . getctime ( file )). strftime ( '%Y-%m-%d %H:%M:%S' )
451
+ c_time = get_c_time ( file )
429
452
if ranking == "None" :
430
453
cursor .execute ('''
431
454
DELETE FROM ranking
0 commit comments