From 86bd2d81663b824ef09d2e155020f5e68ae95d22 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Mon, 19 Oct 2026 22:42:23 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=A1=20=ED=95=A9=EC=B9=98=EA=B8=B0:=20?= =?UTF-8?q?=EB=A9=9C=EB=A1=A0=20id=EA=B0=80=20=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EA=B3=A1=EC=9C=BC=EB=A1=9C=20=ED=95=A9=EC=B9=A0=20=EB=95=8C=20?= =?UTF-8?q?UNIQUE=20=EC=98=A4=EB=A5=98=EB=A1=9C=20=EC=8B=A4=ED=8C=A8?= =?UTF-8?q?=ED=95=98=EB=8D=98=20=EA=B2=83=20=EC=88=98=EC=A0=95(=EB=B3=B5?= =?UTF-8?q?=EC=82=AC=EB=B3=B8=EC=97=90=EC=84=9C=20=EB=B9=BC=EA=B3=A0=20?= =?UTF-8?q?=EB=82=A8=EB=8A=94=20=EA=B3=A1=EC=9C=BC=EB=A1=9C=20=EC=98=AE?= =?UTF-8?q?=EA=B9=80).=20fix-text=20=EC=A4=91=EB=B3=B5=20=ED=95=A9?= =?UTF-8?q?=EC=B9=98=EA=B8=B0=EA=B0=80=20=EC=9A=B4=EC=98=81=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=9D=B4=EA=B1=B8=EB=A1=9C=20=EB=A7=89=ED=98=94?= =?UTF-8?q?=EC=9D=8C(=ED=8A=B8=EB=9E=9C=EC=9E=AD=EC=85=98=EC=9D=B4?= =?UTF-8?q?=EB=9D=BC=20=EB=B0=94=EB=80=90=20=EA=B2=83=20=EC=97=86=EC=9D=8C?= =?UTF-8?q?)?= 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/admin-catalog.test.ts | 5 +++-- apps/server/src/collector/text.test.ts | 2 ++ apps/server/src/songs/admin-ops.ts | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/server/src/admin-catalog.test.ts b/apps/server/src/admin-catalog.test.ts index 94a11bd..a52765b 100644 --- a/apps/server/src/admin-catalog.test.ts +++ b/apps/server/src/admin-catalog.test.ts @@ -96,18 +96,19 @@ describe('admin songs', () => { expect(bad.body.error).toContain('JSON'); }); - test('duplicates are grouped by the normalized key and merged into one (sources, aliases, plays move over)', async () => { + test('duplicates are grouped by the normalized key and merged into one (sources, aliases, plays, Melon id move over)', async () => { const { call } = await admin(); const cat = new Catalog(s.db); const [a] = cat.upsert({ title: '광', artists: [{ name: 'SG워너비' }], source: 'sta', ref: '1' }); // a second row with the same key (an import that missed the match) const b = Number(s.db.query("INSERT INTO songs (title, artist, match_key, created_at, updated_at, youtube_status, youtube_id, embeddable) VALUES ('광 (狂)', 'SG워너비', (SELECT match_key FROM songs WHERE id = ?), 0, 0, 'matched', 'VO88htcm09E', 1)").run(a!).lastInsertRowid); + s.db.query('UPDATE songs SET melon_id = 711627 WHERE id = ?').run(b); s.db.query("INSERT INTO song_aliases (song_id, part, alias) VALUES (?, 'title', 'Crazy')").run(b); const groups = (await call('/songs/duplicates')).body.groups; expect(groups[0].songs.map((x: any) => x.id).sort()).toEqual([a, b].sort()); expect((await call('/songs/merge', { method: 'POST', body: JSON.stringify({ target: a, others: [b] }) })).body.merged).toBe(1); const merged = (await call(`/songs/${a}`)).body; - expect(merged.song).toMatchObject({ youtube_id: 'VO88htcm09E', youtube_status: 'matched' }); + expect(merged.song).toMatchObject({ youtube_id: 'VO88htcm09E', youtube_status: 'matched', melon_id: 711627 }); expect(merged.aliases.map((x: any) => x.alias)).toContain('Crazy'); expect((await call('/songs/duplicates')).body.groups).toEqual([]); expect((await call('/songs?trash=1')).body.rows.map((r: any) => r.id)).toEqual([b]); diff --git a/apps/server/src/collector/text.test.ts b/apps/server/src/collector/text.test.ts index ee85855..9644a92 100644 --- a/apps/server/src/collector/text.test.ts +++ b/apps/server/src/collector/text.test.ts @@ -40,6 +40,7 @@ test('fix-text repairs saved songs, chart rows and artists, retries 원곡 찾 ); const broken = add('Somebody's Watching Me', 'Rockwell', 'none'); const matched = add('I'm On Fire', 'Bruce Springsteen', 'matched', 'ytmusic'); + db.query('UPDATE songs SET melon_id = 2070041 WHERE id = ?').run(matched); // the copy came from a Melon chart const admin = add('You & Me', '제니', 'candidate', 'admin'); const original = add("I'm On Fire", 'Bruce Springsteen', 'matched', 'ytmusic'); const clean = add('Hype Boy', 'NewJeans', 'none'); @@ -53,6 +54,7 @@ test('fix-text repairs saved songs, chart rows and artists, retries 원곡 찾 expect(r.duplicates).toEqual([{ id: matched, title: "I'm On Fire", artist: 'Bruce Springsteen', sameAs: [original] }]); // the copy went to the trash and its chart rows follow the song that stays expect(db.query<{ deleted: number }, [number]>('SELECT deleted_at IS NOT NULL deleted FROM songs WHERE id = ?').get(matched)!.deleted).toBe(1); + expect(db.query<{ melon_id: number | null }, [number]>('SELECT melon_id FROM songs WHERE id = ?').get(original)!.melon_id).toBe(2070041); const row = (id: number) => db.query<{ title: string; match_key: string }, [number]>('SELECT title, match_key FROM songs WHERE id = ?').get(id)!; expect(row(broken)).toEqual({ title: "Somebody's Watching Me", match_key: songMatchKey("Somebody's Watching Me", 'Rockwell') }); expect(row(admin).title).toBe('You & Me'); diff --git a/apps/server/src/songs/admin-ops.ts b/apps/server/src/songs/admin-ops.ts index ef78a3d..e05c4ff 100644 --- a/apps/server/src/songs/admin-ops.ts +++ b/apps/server/src/songs/admin-ops.ts @@ -134,7 +134,13 @@ export function mergeSongs(db: Database, target: number, others: number[], now = FROM (SELECT youtube_id, youtube_status, youtube_source, youtube_channel, youtube_title, embeddable FROM songs WHERE id = ?) o WHERE songs.id = ? AND songs.youtube_status <> 'matched' AND o.youtube_status = 'matched'`, ).run(o, target); - db.query('UPDATE songs SET year = COALESCE(year, (SELECT year FROM songs WHERE id = ?)), melon_id = COALESCE(melon_id, (SELECT melon_id FROM songs x WHERE x.id = ? AND NOT EXISTS (SELECT 1 FROM songs y WHERE y.melon_id = x.melon_id AND y.id <> x.id))) WHERE id = ?').run(o, o, target); + db.query('UPDATE songs SET year = COALESCE(year, (SELECT year FROM songs WHERE id = ?)) WHERE id = ?').run(o, target); + // The Melon id moves over (it is unique, and later imports find the song by it): off the copy first, then onto the target. + const mid = db.query<{ melon_id: number | null }, [number]>('SELECT melon_id FROM songs WHERE id = ?').get(o)?.melon_id ?? null; + if (mid !== null && db.query<{ m: number | null }, [number]>('SELECT melon_id m FROM songs WHERE id = ?').get(target)?.m === null) { + db.query('UPDATE songs SET melon_id = NULL WHERE id = ?').run(o); + db.query('UPDATE songs SET melon_id = ? WHERE id = ?').run(mid, target); + } } trashSongs(db, rest, now); })();