From 9bc9c4afd56d1695b9e3e8935cb234c860609f56 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Wed, 4 Nov 2026 20:50:18 +0900 Subject: [PATCH] =?UTF-8?q?redo-readings:=20=EC=95=84=EC=A7=81=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EC=95=88=20=EB=90=9C=20=EC=A0=9C=EB=AA=A9=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC=20=ED=96=89=EC=9D=98=20=EC=A0=95=EB=8B=B5?= =?UTF-8?q?=EB=8F=84=20=EC=83=88=20=ED=91=9C=EA=B8=B0=EB=A1=9C(=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=A9=B4=20=EC=98=9B=20=EB=B0=9C=EC=9D=8C?= =?UTF-8?q?=EC=9D=B4=20=EB=8F=8C=EC=95=84=EC=98=A4=EB=8D=98=20=EA=B2=83),?= =?UTF-8?q?=20=ED=80=B4=EC=A6=88=20=EC=A0=95=EB=8B=B5=20=EC=A4=91=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0=EB=90=9C=20DB=20=EA=B3=A1=EC=97=90=20?= =?UTF-8?q?=EC=82=AC=EB=9E=8C=EC=9D=B4=20=EB=84=A3=EC=9D=80=20=EB=B3=84?= =?UTF-8?q?=EC=B9=AD(quiz/admin)=EC=9C=BC=EB=A1=9C=EB=8F=84=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EA=B0=92=EC=9D=80=20=EB=B9=BC=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EC=9D=8C.=20=ED=9A=8C=EA=B7=80=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=202=EA=B0=9C(=EA=B3=A0=EC=B9=98=EA=B8=B0=20=EC=A0=84=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=20=ED=99=95=EC=9D=B8),=20docs=2013?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/collector/cli.ts | 2 +- apps/server/src/readings-redo.test.ts | 26 ++++++++++++++++ apps/server/src/songs/readings-redo.ts | 41 ++++++++++++++++++++++---- docs/13-title-answers.md | 1 + 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/apps/server/src/collector/cli.ts b/apps/server/src/collector/cli.ts index 0d778ad..d580019 100644 --- a/apps/server/src/collector/cli.ts +++ b/apps/server/src/collector/cli.ts @@ -145,7 +145,7 @@ switch (cmd) { const oldTables = i >= 0 ? [(await Bun.file(rest[i + 1]!).json()) as LexiconTable] : []; const r = redoReadings(db, { dryRun: rest.includes('--dry-run'), oldTables }); for (const e of r.examples) console.log(`${e.where}\t${e.name}\t- ${e.drop.join(', ')}\t+ ${e.add.join(', ')}`); - console.log(JSON.stringify({ songs: r.songs, quizItems: r.quizItems, unknownSongs: r.unknownSongs, unknownWords: r.unknownWords.size })); + console.log(JSON.stringify({ songs: r.songs, quizItems: r.quizItems, pendingReviews: r.pendingReviews, unknownSongs: r.unknownSongs, unknownWords: r.unknownWords.size })); break; } case 'lexicon-missing': { diff --git a/apps/server/src/readings-redo.test.ts b/apps/server/src/readings-redo.test.ts index a799b19..be0fc32 100644 --- a/apps/server/src/readings-redo.test.ts +++ b/apps/server/src/readings-redo.test.ts @@ -69,3 +69,29 @@ test('saved quizzes: generated readings replaced, typed answers kept, 이지선 expect(JSON.parse(got(a).a)).toEqual(['정꾹이', '정국']); expect(got(b)).toEqual({ t: '[]', a: '[]' }); }); + +test('제목 정리 rows still waiting get the new spellings too: applying one later does not bring the old reading back', async () => { + const { applyTitleReviews } = await import('./songs/title-review'); + const db = openDb(tempDbPath()); + const [id] = new Catalog(db).upsert({ title: 'Heaven (feat. X)', artists: [{ name: 'Rod Stewart' }], source: 't', ref: '1' }); + db.query( + "INSERT INTO title_reviews (song_id, kind, status, old_title, old_artist, new_title, new_artist, title_answers, artist_answers, created_at) VALUES (?, 'title', 'pending', 'Heaven (feat. X)', 'Rod Stewart', 'Heaven', 'Rod Stewart', ?, ?, 0)", + ).run(id!, JSON.stringify(['헤벤']), JSON.stringify(['로드 스투어트'])); + expect(redoReadings(db).pendingReviews).toBe(1); + expect(applyTitleReviews(db, { ids: [id!] }, 'admin').applied).toBe(1); + expect(aliases(db, id!, 'title')).toEqual(['Heaven (feat. X)', '헤븐']); + expect(aliases(db, id!, 'artist')).toEqual(['로드 스튜어트', '로드스튜어트']); +}); + +test('saved quizzes: an answer the song DB has from a person (quiz save / admin) stays even when it equals an old generated reading', () => { + const db = openDb(tempDbPath()); + db.query("INSERT INTO users (id, kind, nickname, created_at, last_seen_at) VALUES ('u', 'member', 'u', 0, 0)").run(); + const quiz = (db.query("INSERT INTO custom_quizzes (owner_id, name, description, visibility, settings_json, kind, created_at, updated_at) VALUES ('u', 'q', '', 'private', '{}', 'normal', 0, 0) RETURNING id").get() as { id: number }).id; + const [song] = new Catalog(db).upsert({ title: 'Heaven', artists: [{ name: '에일리' }], source: 't', ref: '1' }); + db.query("INSERT INTO song_aliases (song_id, part, alias, source) VALUES (?, 'title', '헤벤', 'quiz')").run(song!); + const it = (db.query("INSERT INTO custom_quiz_items (quiz_id, pos, song_id, video_id, title, artist, title_answers, artist_answers) VALUES (?, 0, ?, 'aaaaaaaaaaa', 'Heaven', '에일리', ?, '[]') RETURNING id").get(quiz, song!, JSON.stringify(['헤벤'])) as { id: number }).id; + redoReadings(db); + expect(JSON.parse(db.query<{ t: string }, [number]>('SELECT title_answers t FROM custom_quiz_items WHERE id = ?').get(it)!.t)).toEqual(['헤벤', '헤븐']); + // the song's own typed alias is never touched either + expect(aliases(db, song!, 'title')).toEqual(['헤벤', '헤븐']); +}); diff --git a/apps/server/src/songs/readings-redo.ts b/apps/server/src/songs/readings-redo.ts index ce381b0..982e020 100644 --- a/apps/server/src/songs/readings-redo.ts +++ b/apps/server/src/songs/readings-redo.ts @@ -98,6 +98,8 @@ export function planReadings( export interface RedoResult { songs: { changed: number; dropped: number; added: number }; quizItems: { changed: number; dropped: number; added: number }; + /** 제목 정리 rows still waiting: their answers are applied later, so they get the same change. */ + pendingReviews: number; unknownSongs: number; unknownWords: Map; examples: { where: string; name: string; drop: string[]; add: string[] }[]; @@ -108,7 +110,7 @@ export interface RedoResult { * as it was before (`oldTables`, from git) so the spellings it made go too. */ export function redoReadings(db: Database, opts: { dryRun?: boolean; oldTables?: LexiconTable[] } = {}): RedoResult { - const out: RedoResult = { songs: { changed: 0, dropped: 0, added: 0 }, quizItems: { changed: 0, dropped: 0, added: 0 }, unknownSongs: 0, unknownWords: new Map(), examples: [] }; + const out: RedoResult = { songs: { changed: 0, dropped: 0, added: 0 }, quizItems: { changed: 0, dropped: 0, added: 0 }, pendingReviews: 0, unknownSongs: 0, unknownWords: new Map(), examples: [] }; const songs = db .query<{ id: number; title: string; artist: string; old_title: string | null }, []>( 'SELECT s.id, s.title, s.artist, r.old_title FROM songs s LEFT JOIN title_reviews r ON r.song_id = s.id WHERE s.deleted_at IS NULL ORDER BY s.id', @@ -146,17 +148,46 @@ export function redoReadings(db: Database, opts: { dryRun?: boolean; oldTables?: for (const a of plan.title.add) add.run(s.id, 'title', a.slice(0, 200)); for (const a of plan.artist.add) add.run(s.id, 'artist', a.slice(0, 200)); } - // 이지선다 quizzes have no answers - const items = db - .query<{ id: number; title: string; artist: string; title_answers: string; artist_answers: string }, []>( - "SELECT i.id, i.title, i.artist, i.title_answers, i.artist_answers FROM custom_quiz_items i JOIN custom_quizzes q ON q.id = i.quiz_id WHERE q.kind <> 'duel'", + // 제목 정리 rows not applied yet: whatever they would add is all generated (made by buildTitleReviews) + const pending = db + .query<{ song_id: number; old_title: string; new_title: string; new_artist: string; title_answers: string; artist_answers: string }, []>( + "SELECT song_id, old_title, new_title, new_artist, title_answers, artist_answers FROM title_reviews WHERE status = 'pending'", ) .all(); + const saveReview = db.query('UPDATE title_reviews SET title_answers = ?, artist_answers = ? WHERE song_id = ?'); + for (const r of pending) { + const ta = JSON.parse(r.title_answers) as string[]; + const aa = JSON.parse(r.artist_answers) as string[]; + const plan = planReadings({ title: r.new_title, artist: r.new_artist, source: r.old_title, titleAnswers: ta, artistAnswers: aa }, opts.oldTables); + if (!plan.title.drop.length && !plan.artist.drop.length && !plan.title.add.length && !plan.artist.add.length) continue; + out.pendingReviews++; + const dT = new Set(plan.title.drop); + const dA = new Set(plan.artist.drop); + if (!opts.dryRun) + saveReview.run( + JSON.stringify([...ta.filter((a) => !dT.has(a)), ...plan.title.add].slice(0, 30)), + JSON.stringify([...aa.filter((a) => !dA.has(a)), ...plan.artist.add].slice(0, 30)), + r.song_id, + ); + } + // 이지선다 quizzes have no answers. A quiz answer has no record of who wrote it: a generated one is recognised by being + // what the old reader made from that item's own names; one the song's DB also has from an admin or a quiz save + // (song_aliases source other than 'auto') is someone's typing and stays. + const items = db + .query<{ id: number; song_id: number | null; title: string; artist: string; title_answers: string; artist_answers: string }, []>( + "SELECT i.id, i.song_id, i.title, i.artist, i.title_answers, i.artist_answers FROM custom_quiz_items i JOIN custom_quizzes q ON q.id = i.quiz_id WHERE q.kind <> 'duel'", + ) + .all(); + const typed = db.query<{ part: string; alias: string }, [number]>("SELECT part, alias FROM song_aliases WHERE song_id = ? AND source <> 'auto'"); const save = db.query('UPDATE custom_quiz_items SET title_answers = ?, artist_answers = ? WHERE id = ?'); for (const it of items) { const ta = JSON.parse(it.title_answers) as string[]; const aa = JSON.parse(it.artist_answers) as string[]; const plan = planReadings({ title: it.title, artist: it.artist, titleAnswers: ta, artistAnswers: aa }, opts.oldTables); + const own = it.song_id ? typed.all(it.song_id) : []; + const mine = (part: string, a: string) => own.some((x) => x.part === part && low(x.alias) === low(a)); + plan.title.drop = plan.title.drop.filter((a) => !mine('title', a)); + plan.artist.drop = plan.artist.drop.filter((a) => !mine('artist', a)); if (!plan.title.drop.length && !plan.artist.drop.length && !plan.title.add.length && !plan.artist.add.length) continue; const dT = new Set(plan.title.drop); const dA = new Set(plan.artist.drop); diff --git a/docs/13-title-answers.md b/docs/13-title-answers.md index b687d19..2bdeb00 100644 --- a/docs/13-title-answers.md +++ b/docs/13-title-answers.md @@ -55,6 +55,7 @@ - `decodeEntities`: `é`·`°` 등 라틴 문자 이름 추가(Céline Dion) → `cli.ts fix-text`로 DB 고침 - 퀴즈 파일 가수 칸의 따옴표 안('Miki Matsubara Official "stay with me"')을 with로 자르지 않음(가수 정답 '미' 같은 조각) - 배포 (2026-11-04 20:44~): 백업 `~/backups/music_quiz/app-before-readings-20261104-2044.db` → eac4d97 배포 → `fix-text`(16곡, Céline Dion 등) → `fix-melon-titles` 18곡(Rod Stewart → Your Song 등, Chuckie는 같은 곡 10449에 합침, 16곡 바로 반영) → `redo-readings`(DB 3,707곡: 옛 발음 2,328개 빼고 10,533개 넣음 / 일반 퀴즈 30곡) → 테스트용1 真夜中のドア 가수 칸 'Miki Matsubara'로 → `redo-readings` 한 번 더(그 곡 가수 발음 6개). 두 번째부터는 바뀌는 것 없음. 표기표에 없는 단어가 남은 곡 56곡(대부분 누군지 확인 안 된 가수 이름) + - redo-readings는 아직 적용 안 된 제목 정리 행(pending)의 정답도 같이 바꿈(나중에 적용해도 옛 발음이 안 돌아오게). 퀴즈 정답에는 누가 썼는지 기록이 없어서 '그 곡 이름으로 옛 규칙이 만든 것과 같은 값'을 자동으로 봄 — 연결된 DB 곡에 사람이 넣은 별칭(source quiz/admin)으로도 있는 값은 남김. 반영 때 빠진 퀴즈 정답 18개는 백업과 대조해 모두 옛 규칙 결과(융 쿠크, 어 하, 헤벤, 미 …)임을 확인 - 'Sheep (Alan Walker Relift)'(10550)는 리믹스라 휴지통 기준에 해당 — 사용자 확인 대기 ## 운영 DB에서 확인한 숫자 (2026-11-04 사본)