/** * One-way migration from a Study Desk V1 database (the Prisma/SQLite `study.db`). * The V1 file is opened read-only and never modified. Everything is copied into new * rows with new ids inside one transaction, so a failed import leaves V2 untouched. */ import { DatabaseSync } from 'node:sqlite'; import { isFullyCorrect, scoreQuestion, type Option } from '../../shared/content.ts'; import { badRequest, type AppContext } from '../http.ts'; import { newId, now } from '../util.ts'; import { createClass, createDeck, createFolder, createQuiz, listCards, listQuestions } from './content.ts'; export interface V1Summary { classes: number; folders: number; decks: number; cards: number; quizzes: number; questions: number; attempts: number; scheduledCards: number; activityDays: number; skipped: string[]; } type Row = Record; function toMs(v: unknown): number | null { if (v === null || v === undefined) return null; if (typeof v === 'number') return v > 1e11 ? v : v * 1000; if (typeof v === 'bigint') return Number(v); const n = Number(v); if (Number.isFinite(n) && String(v).trim() !== '') return n > 1e11 ? n : n * 1000; const t = Date.parse(String(v)); return Number.isNaN(t) ? null : t; } export function importV1(app: AppContext, file: string, opts: { dryRun?: boolean } = {}): V1Summary { let src: DatabaseSync; try { src = new DatabaseSync(file, { readOnly: true }); } catch { throw badRequest('That file could not be opened as a SQLite database.'); } try { const tables = new Set((src.prepare(`SELECT name FROM sqlite_master WHERE type = 'table'`).all() as Row[]).map((r) => String(r.name))); if (!tables.has('Class') || !tables.has('Deck') || !tables.has('QuizSet')) { throw badRequest('This does not look like a Study Desk V1 database (expected the tables Class, Deck and QuizSet).'); } const all = (table: string, order = ''): Row[] => (tables.has(table) ? (src.prepare(`SELECT * FROM "${table}" ${order}`).all() as Row[]) : []); const classes = all('Class', 'ORDER BY sortOrder, createdAt'); const groups = all('MaterialGroup', 'ORDER BY sortOrder, createdAt'); const decks = all('Deck', 'ORDER BY sortOrder, createdAt'); const cards = all('Flashcard', 'ORDER BY sortOrder'); const quizzes = all('QuizSet', 'ORDER BY sortOrder, createdAt'); const questions = all('Question', 'ORDER BY sortOrder'); const options = all('AnswerOption', 'ORDER BY sortOrder'); const attempts = all('QuizAttempt', 'ORDER BY completedAt'); const srStates = all('SpacedRepetitionCardState'); const activity = all('StudyActivity'); const summary: V1Summary = { classes: classes.length, folders: groups.length, decks: decks.length, cards: cards.length, quizzes: quizzes.length, questions: questions.length, attempts: attempts.length, scheduledCards: new Set(srStates.map((s) => s.flashcardId)).size, activityDays: new Set(activity.map((a) => new Date(toMs(a.occurredAt) ?? 0).toISOString().slice(0, 10))).size, skipped: [], }; if (opts.dryRun) return summary; const { db } = app; const group = (rows: T[], key: string) => { const m = new Map(); for (const r of rows) { const k = String(r[key]); if (!m.has(k)) m.set(k, []); m.get(k)!.push(r); } return m; }; const cardsByDeck = group(cards, 'deckId'); const questionsByQuiz = group(questions, 'quizSetId'); const optionsByQuestion = group(options, 'questionId'); const attemptsByQuiz = group(attempts, 'quizSetId'); // Keep the most-practised schedule when a card belonged to several V1 spaced-repetition sets. const bestState = new Map(); for (const s of srStates) { const cur = bestState.get(s.flashcardId); if (!cur || Number(s.reps) > Number(cur.reps)) bestState.set(s.flashcardId, s); } db.tx(() => { const classMap = new Map(); const folderMap = new Map(); for (const c of classes) { const created = createClass(db, { name: String(c.name || 'Untitled class').slice(0, 120) }); classMap.set(c.id, created.id); const t = toMs(c.createdAt); if (t) db.run('UPDATE classes SET created_at = ? WHERE id = ?', t, created.id); } for (const g of groups) { const classId = classMap.get(g.classId); if (!classId) continue; folderMap.set(g.id, createFolder(db, classId, String(g.name || 'Folder')).id); } for (const d of decks) { const classId = classMap.get(d.classId); if (!classId) { summary.skipped.push(`Deck "${d.name}" (its class is missing)`); continue; } const source = (cardsByDeck.get(d.id) ?? []).filter((c) => String(c.front ?? '').trim() && String(c.back ?? '').trim()); const deck = createDeck(db, { classId, folderId: d.groupId ? folderMap.get(d.groupId) : null, title: String(d.name || 'Untitled deck').slice(0, 200), description: String(d.description ?? '').slice(0, 4000), cards: source.map((c) => ({ front: String(c.front).slice(0, 20000), back: String(c.back).slice(0, 20000) })), }); const created = listCards(db, deck.id); source.forEach((old, i) => { const s = bestState.get(old.id); const target = created[i]; if (!s || !target) return; db.run( `UPDATE cards SET state = ?, due = ?, stability = ?, difficulty = ?, elapsed_days = ?, scheduled_days = ?, learning_steps = ?, reps = ?, lapses = ?, last_review = ?, introduced_at = ? WHERE id = ?`, Number(s.state) || 0, toMs(s.due), Number(s.stability) || 0, Number(s.difficulty) || 0, Number(s.elapsedDays) || 0, Number(s.scheduledDays) || 0, Number(s.learningSteps) || 0, Number(s.reps) || 0, Number(s.lapses) || 0, toMs(s.lastReview), toMs(s.firstReviewedAt), target.id, ); }); } for (const z of quizzes) { const classId = classMap.get(z.classId); if (!classId) { summary.skipped.push(`Quiz "${z.name}" (its class is missing)`); continue; } const source = (questionsByQuiz.get(z.id) ?? []).filter((q) => { const opts2 = optionsByQuestion.get(q.id) ?? []; return String(q.prompt ?? '').trim() && opts2.length >= 2 && opts2.some((o) => o.isCorrect); }); const quiz = createQuiz(db, { classId, folderId: z.groupId ? folderMap.get(z.groupId) : null, title: String(z.name || 'Untitled quiz').slice(0, 200), description: String(z.description ?? '').slice(0, 4000), questions: source.map((q) => { const opts2 = (optionsByQuestion.get(q.id) ?? []).filter((o) => String(o.text ?? '').trim()).slice(0, 10); const correct = opts2.filter((o) => o.isCorrect).length; return { type: String(q.type).toUpperCase() === 'SATA' || correct > 1 ? 'multi' : 'single', stem: String(q.prompt).slice(0, 20000), options: opts2.map((o) => ({ text: String(o.text).slice(0, 4000), correct: Boolean(o.isCorrect) })), rationale: String(q.rationale ?? '').slice(0, 20000), topic: String(q.category ?? '').slice(0, 80), }; }), }); const created = listQuestions(db, quiz.id); // Old question / option ids -> new, so past attempts can be replayed. const qMap = new Map(); const oMap = new Map(); source.forEach((old, i) => { const target = created[i]; if (!target) return; qMap.set(old.id, target); const oldOpts = (optionsByQuestion.get(old.id) ?? []).filter((o) => String(o.text ?? '').trim()).slice(0, 10); oldOpts.forEach((o, j) => target.options[j] && oMap.set(o.id, target.options[j].id)); }); for (const a of attemptsByQuiz.get(z.id) ?? []) { let answers: Record = {}; try { const parsed = JSON.parse(String(a.answersJson ?? '{}')); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) answers = parsed; } catch { /* an unreadable attempt is still imported as a score */ } const partial = Boolean(a.isPartialRetake); const included = created.filter((q) => { const oldId = [...qMap.entries()].find(([, v]) => v.id === q.id)?.[0]; return !partial || (oldId !== undefined && oldId in answers); }); const completedAt = toMs(a.completedAt) ?? now(); const newAnswers: Record = {}; for (const [oldQ, sel] of Object.entries(answers)) { const target = qMap.get(oldQ); if (!target || !Array.isArray(sel)) continue; newAnswers[target.id] = { selected: sel.map((s) => oMap.get(String(s))).filter((s): s is string => Boolean(s)), checked: true, at: completedAt }; } const attemptId = newId(); const snapshot = included.map((q) => ({ id: q.id, quizId: quiz.id, quizTitle: quiz.title, type: q.type, stem: q.stem, options: q.options as Option[], rationale: q.rationale, topic: q.topic, difficulty: q.difficulty })); db.run( `INSERT INTO attempts (id, quiz_id, class_id, title, mode, kind, status, settings, snapshot, answers, question_count, score, max_score, started_at, updated_at, completed_at) VALUES (?, ?, ?, ?, 'practice', ?, 'completed', ?, ?, ?, ?, ?, ?, ?, ?, ?)`, attemptId, quiz.id, classId, quiz.title, partial ? 'retake' : 'full', JSON.stringify({ shuffleQuestions: false, shuffleOptions: false, timeLimitSec: null, sataScoring: 'partial' }), JSON.stringify({ questions: snapshot }), JSON.stringify(newAnswers), snapshot.length, Number(a.score) || 0, Number(a.maxScore) || snapshot.length, completedAt, completedAt, completedAt, ); for (const q of snapshot) { const selected = newAnswers[q.id]?.selected ?? []; if (selected.length === 0 && partial) continue; const score = scoreQuestion(q, selected, 'partial'); db.run( 'INSERT OR IGNORE INTO responses (attempt_id, question_id, quiz_id, class_id, topic, score, correct, answered_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', attemptId, q.id, quiz.id, classId, q.topic, score, isFullyCorrect(score) ? 1 : 0, completedAt, ); } } } // Flashcard activity keeps the study heatmap and streak intact. Quiz activity is rebuilt from the attempts above. for (const a of activity) { if (String(a.type) !== 'FLASHCARD') continue; const t = toMs(a.occurredAt); if (t) db.run(`INSERT INTO reviews (card_id, deck_id, class_id, rating, state_before, scheduled_days, mode, prev, reviewed_at) VALUES ('v1', NULL, NULL, 3, 0, 0, 'legacy', '', ?)`, t); } }); return summary; } finally { src.close(); } }