Spaces:
Running
Running
File size: 4,031 Bytes
8800c81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import os
import sqlite3
def run_migration():
# 1. Migrate SQLite
sandbox_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"scratch",
"sandbox.db",
)
if os.path.exists(sandbox_path):
conn = sqlite3.connect(sandbox_path)
cursor = conn.cursor()
cursor.execute("PRAGMA foreign_keys = OFF;")
cursor.execute("""
CREATE TABLE IF NOT EXISTS user_reviews_fsrs (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
problem_id TEXT NOT NULL,
fsrs_state INTEGER DEFAULT 0,
fsrs_stability REAL DEFAULT 0.0,
fsrs_difficulty REAL DEFAULT 0.0,
fsrs_elapsed_days INTEGER DEFAULT 0,
fsrs_scheduled_days INTEGER DEFAULT 0,
fsrs_reps INTEGER DEFAULT 0,
fsrs_lapses INTEGER DEFAULT 0,
last_reviewed TEXT DEFAULT CURRENT_TIMESTAMP,
next_review TEXT DEFAULT CURRENT_TIMESTAMP,
times_correct INTEGER DEFAULT 0,
total_attempts INTEGER DEFAULT 0,
UNIQUE(user_id, problem_id),
FOREIGN KEY(problem_id) REFERENCES problems(id) ON DELETE CASCADE
);
""")
cursor.execute("PRAGMA table_info(user_reviews);")
columns = [info[1] for info in cursor.fetchall()]
if "box_level" in columns:
print("Migrating local SQLite sandbox DB from Leitner to FSRS...")
cursor.execute("SELECT * FROM user_reviews")
rows = cursor.fetchall()
for row in rows:
row_dict = dict(zip(columns, row, strict=False))
box_level = row_dict.get("box_level", 1)
state = 0
stability = 0.0
scheduled_days = 0
if box_level > 1:
state = 2 # Review
if box_level == 2:
stability = 2.0
scheduled_days = 3
elif box_level == 3:
stability = 6.0
scheduled_days = 7
elif box_level == 4:
stability = 13.0
scheduled_days = 14
else: # 5
stability = 28.0
scheduled_days = 30
insert_query = """
INSERT OR IGNORE INTO user_reviews_fsrs
(id, user_id, problem_id, fsrs_state, fsrs_stability, fsrs_difficulty,
fsrs_elapsed_days, fsrs_scheduled_days, fsrs_reps, fsrs_lapses,
last_reviewed, next_review, times_correct, total_attempts)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
cursor.execute(
insert_query,
(
row_dict["id"],
row_dict["user_id"],
row_dict["problem_id"],
state,
stability,
0.0,
0,
scheduled_days,
row_dict.get("total_attempts", 0),
0,
row_dict.get("last_reviewed"),
row_dict.get("next_review"),
row_dict.get("times_correct", 0),
row_dict.get("total_attempts", 0),
),
)
cursor.execute("DROP TABLE user_reviews")
cursor.execute("ALTER TABLE user_reviews_fsrs RENAME TO user_reviews")
conn.commit()
print("Local migration complete.")
else:
print("No box_level found in local DB. Migration might have already run.")
cursor.execute("PRAGMA foreign_keys = ON;")
conn.close()
else:
print("No sandbox DB found to migrate.")
if __name__ == "__main__":
run_migration()
|