From 58e232009be4f66f664134c09c056d373a25e4e2 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Fri, 28 Mar 2025 17:26:31 -0400 Subject: [PATCH] Edit process_scrobbles to conform to new schema --- src/app/jobs/process_scrobbles.zig | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/app/jobs/process_scrobbles.zig b/src/app/jobs/process_scrobbles.zig index 1314465..6305696 100644 --- a/src/app/jobs/process_scrobbles.zig +++ b/src/app/jobs/process_scrobbles.zig @@ -78,31 +78,38 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig // but song_check/album is not. Also yes, the order of these // checks is weird, I didn't put a lot of thought into it var associative_table_flags: [3]bool = [3]bool{ true, true, true }; + var associative_table_ids: [3][]const u8 = [3][]const u8{ null, null, null }; if (album_check == null) { try env.repo.execute(album_insert); - try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).execute(env.repo); + associative_table_ids[0] = try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo); associative_table_flags[0] = false; - try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).execute(env.repo); + associative_table_ids[1] = try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).returning(.{.id}).execute(env.repo); associative_table_flags[1] = false; } if (artist_check == null) { try env.repo.execute(artist_insert); - if (associative_table_flags[0]) try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).execute(env.repo); - try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).execute(env.repo); + if (associative_table_flags[0]) associative_table_ids[0] = try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo); + associative_table_ids[2] = try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo); associative_table_flags[2] = false; } if (song_check == null) { try env.repo.execute(song_insert); - if (associative_table_flags[1]) try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).execute(env.repo); - if (associative_table_flags[2]) try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).execute(env.repo); + if (associative_table_flags[1]) associative_table_ids[1] = try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).returning(.{.id}).execute(env.repo); + if (associative_table_flags[2]) associative_table_ids[2] = try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo); } - const scr_id = try jetzig.database.Query(.Scrobble).insert(.{ .song_id = song_id, .album_id = album_id, .date = scrobble.date }).returning(.{.id}).execute(env.repo); - defer env.repo.free(scr_id); - try jetzig.database.Query(.Scrobbleartist).insert(.{ .scrobble_id = scr_id.?.id, .artist_id = artist_id }).execute(env.repo); + defer { + for (0..3) |i| { + if (associative_table_ids[i]) env.repo.free(associative_table_ids[i]); + } + } + + try jetzig.database.Query(.Scrobble).insert(.{ .albumartists_id = associative_table_ids[0], .albumsong_id = associative_table_ids[1], .songartists_id = associative_table_ids[2], .date = scrobble.date }).execute(env.repo); + //defer env.repo.free(scr_id); + //try jetzig.database.Query(.Scrobbleartist).insert(.{ .scrobble_id = scr_id.?.id, .artist_id = artist_id }).execute(env.repo); } }