Spaces:
Running
Running
Download scripts/migrate_to_fsrs.py from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/scripts/migrate_to_fsrs.py
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/scripts/migrate_to_fsrs.py
-
curl -L -o migrate_to_fsrs.py https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/scripts/migrate_to_fsrs.py
4.03 kB
| 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() | |