Skip to content

Commit e1a83a6

Browse files
committed
migrate for yfszzx#71
1 parent b85a2b9 commit e1a83a6

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

scripts/wib/wib_db.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import datetime
12
import json
23
import os
34
import sqlite3
45
from modules import scripts
5-
import datetime
66

7-
version = 3
7+
version = 4
88

99
path_recorder_file = os.path.join(scripts.basedir(), "path_recorder.txt")
1010
aes_cache_file = os.path.join(scripts.basedir(), "aes_scores.json")
@@ -341,30 +341,53 @@ def migrate_ranking_dirs(cursor, db_version):
341341

342342
return
343343

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+
344364
def check():
345365
if not os.path.exists(db_file):
346-
print("Image Browser: Creating database")
347366
conn, cursor = transaction_begin()
367+
print("Image Browser: Creating database")
348368
create_db(cursor)
349369
update_db_data(cursor, "version", version)
350370
migrate_path_recorder(cursor)
351371
migrate_exif_data(cursor)
352372
migrate_ranking(cursor)
373+
migrate_ranking_c_time(cursor)
353374
transaction_end(conn, cursor)
354375
print("Image Browser: Database created")
355376
db_version = get_version()
377+
conn, cursor = transaction_begin()
356378
if db_version[0] <= "2":
357379
# version 1 database had mixed path notations, changed them all to abspath
358380
# version 2 database still had mixed path notations, because of windows short name, changed them all to realpath
359381
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)
362382
migrate_path_recorder_dirs(cursor)
363383
migrate_exif_data_dirs(cursor)
364384
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)
366388
print(f"Image Browser: Database upgraded from version {db_version[0]} to version {version}")
367-
389+
transaction_end(conn, cursor)
390+
368391
return version
369392

370393
def load_path_recorder():
@@ -378,19 +401,19 @@ def load_path_recorder():
378401

379402
return path_recorder
380403

381-
def select_ranking(filename):
404+
def select_ranking(file):
382405
conn, cursor = transaction_begin()
383406
cursor.execute('''
384407
SELECT ranking
385408
FROM ranking
386409
WHERE file = ?
387-
''', (filename,))
410+
''', (file,))
388411
ranking_value = cursor.fetchone()
389412

390413
# if ranking not found search again, without path (moved?)
391414
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)
394417
cursor.execute('''
395418
SELECT ranking
396419
FROM ranking
@@ -411,7 +434,7 @@ def select_ranking(filename):
411434
cursor.execute('''
412435
INSERT INTO ranking (file, name, ranking, created)
413436
VALUES (?, ?, ?, ?)
414-
''', (filename, name, insert_ranking, c_time))
437+
''', (file, name, insert_ranking, c_time))
415438

416439
if ranking_value is None:
417440
return_ranking = "None"
@@ -425,7 +448,7 @@ def update_ranking(file, ranking):
425448
name = os.path.basename(file)
426449
with sqlite3.connect(db_file, timeout=timeout) as conn:
427450
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)
429452
if ranking == "None":
430453
cursor.execute('''
431454
DELETE FROM ranking

0 commit comments

Comments
 (0)