곡 합치기: 멜론 id가 없는 곡으로 합칠 때 UNIQUE 오류로 실패하던 것 수정(복사본에서 빼고 남는 곡으로 옮김). fix-text 중복 합치기가 운영에서 이걸로 막혔음(트랜잭션이라 바뀐 것 없음)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-19 22:42:23 +09:00
parent 8e6fd229da
commit 86bd2d8166
3 changed files with 12 additions and 3 deletions

View File

@@ -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]);

View File

@@ -40,6 +40,7 @@ test('fix-text repairs saved songs, chart rows and artists, retries 원곡 찾
);
const broken = add('Somebody&#x27;s Watching Me', 'Rockwell', 'none');
const matched = add('I&#x27;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 &amp; 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');

View File

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