From b191d87dfd8b3db28ab299cc8452a1f0a70dca4d Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sat, 31 May 2025 20:54:37 +0100 Subject: [PATCH] Fix scrobble tracks schema Remove the primary key, and just set a unique index on scrobbled time and title, which allows skipping duplicates. --- lib/last_fm/import.ex | 3 ++- .../migrations/20250530203701_create_scrobbled_tracks.exs | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/last_fm/import.ex b/lib/last_fm/import.ex index 37eda8d4..2da2db15 100644 --- a/lib/last_fm/import.ex +++ b/lib/last_fm/import.ex @@ -20,7 +20,8 @@ defmodule LastFm.Import do # HACK: if two tracks happen to have the exact same scrobbled_at_uts, # we move it by a sec. MusicLibrary.Repo.insert_all(LastFm.Track, track_params, - on_conflict: [inc: [scrobbled_at_uts: -1]] + on_conflict: :nothing, + conflict_target: [:scrobbled_at_uts, :title] ) end end diff --git a/priv/repo/migrations/20250530203701_create_scrobbled_tracks.exs b/priv/repo/migrations/20250530203701_create_scrobbled_tracks.exs index 9ac4d5b5..85c5a314 100644 --- a/priv/repo/migrations/20250530203701_create_scrobbled_tracks.exs +++ b/priv/repo/migrations/20250530203701_create_scrobbled_tracks.exs @@ -3,7 +3,7 @@ defmodule MusicLibrary.Repo.Migrations.CreateScrobbledTracks do def change do create table(:scrobbled_tracks, primary_key: false) do - add :scrobbled_at_uts, :integer, primary_key: true + add :scrobbled_at_uts, :integer add :musicbrainz_id, :string add :title, :string add :cover_url, :string @@ -12,5 +12,7 @@ defmodule MusicLibrary.Repo.Migrations.CreateScrobbledTracks do add :album, :map add :last_fm_data, :map end + + create index(:scrobbled_tracks, [:scrobbled_at_uts, :title], unique: true) end end