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); })();