From c8fc6e6e0ae16674aa34b2a879ac69ca8e3370f0 Mon Sep 17 00:00:00 2001 From: Kendall Garner <17521368+kgarner7@users.noreply.github.com> Date: Sun, 12 Jul 2026 14:33:15 -0700 Subject: [PATCH] add primary key field, update index, remove rowid references --- ...ary_key_and_update_index_for_scrobbles.sql | 39 +++++++++++++++++++ persistence/persistence_suite_test.go | 6 +-- persistence/scrobble_repository.go | 16 +++----- server/nativeapi/native_api.go | 4 +- 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 db/migrations/20260712211040_add_primary_key_and_update_index_for_scrobbles.sql diff --git a/db/migrations/20260712211040_add_primary_key_and_update_index_for_scrobbles.sql b/db/migrations/20260712211040_add_primary_key_and_update_index_for_scrobbles.sql new file mode 100644 index 000000000..220d7cf75 --- /dev/null +++ b/db/migrations/20260712211040_add_primary_key_and_update_index_for_scrobbles.sql @@ -0,0 +1,39 @@ +-- +goose Up +CREATE TABLE scrobbles_tmp( + id INTEGER PRIMARY KEY, + media_file_id VARCHAR(255) NOT NULL + REFERENCES media_file(id) + ON DELETE CASCADE + ON UPDATE CASCADE, + user_id VARCHAR(255) NOT NULL + REFERENCES user(id) + ON DELETE CASCADE + ON UPDATE CASCADE, + submission_time INTEGER NOT NULL +); +INSERT INTO scrobbles_tmp SELECT ROWID, media_file_id, user_id, submission_time FROM scrobbles; + +DROP INDEX scrobbles_date; +DROP TABLE scrobbles; +ALTER TABLE scrobbles_tmp RENAME TO scrobbles; +CREATE INDEX scrobbles_user_time ON scrobbles(user_id, submission_time); + + +-- +goose Down +CREATE TABLE scrobbles_tmp( + media_file_id VARCHAR(255) NOT NULL + REFERENCES media_file(id) + ON DELETE CASCADE + ON UPDATE CASCADE, + user_id VARCHAR(255) NOT NULL + REFERENCES user(id) + ON DELETE CASCADE + ON UPDATE CASCADE, + submission_time INTEGER NOT NULL +); +INSERT INTO scrobbles_tmp SELECT media_file_id, user_id, submission_time FROM scrobbles; + +DROP INDEX scrobbles_user_time; +DROP TABLE scrobbles; +ALTER TABLE scrobbles_tmp RENAME TO scrobbles; +CREATE INDEX scrobbles_date ON scrobbles(submission_time); \ No newline at end of file diff --git a/persistence/persistence_suite_test.go b/persistence/persistence_suite_test.go index 9b1827674..7f7fed809 100644 --- a/persistence/persistence_suite_test.go +++ b/persistence/persistence_suite_test.go @@ -159,9 +159,9 @@ var ( ) var ( - firstScrobble = model.Scrobble{MediaFileID: "1001", UserID: "userid", SubmissionTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)} - secondScrobble = model.Scrobble{MediaFileID: "1003", UserID: "2222", SubmissionTime: time.Date(1970, 2, 1, 0, 0, 0, 0, time.UTC)} - thirdScrobble = model.Scrobble{MediaFileID: "1002", UserID: "userid", SubmissionTime: time.Date(1970, 3, 1, 0, 0, 0, 0, time.UTC)} + firstScrobble = model.Scrobble{ID: 1, MediaFileID: "1001", UserID: "userid", SubmissionTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)} + secondScrobble = model.Scrobble{ID: 2, MediaFileID: "1003", UserID: "2222", SubmissionTime: time.Date(1970, 2, 1, 0, 0, 0, 0, time.UTC)} + thirdScrobble = model.Scrobble{ID: 3, MediaFileID: "1002", UserID: "userid", SubmissionTime: time.Date(1970, 3, 1, 0, 0, 0, 0, time.UTC)} scrobbles = model.Scrobbles{firstScrobble, secondScrobble, thirdScrobble} ) diff --git a/persistence/scrobble_repository.go b/persistence/scrobble_repository.go index 481defe6e..18715cdb4 100644 --- a/persistence/scrobble_repository.go +++ b/persistence/scrobble_repository.go @@ -16,15 +16,15 @@ type scrobbleRepository struct { } type dbScrobble struct { + ID int64 `db:"id"` MediaFileID string `db:"media_file_id"` - RowId int64 `db:"row_id"` SubmissionTime int64 `db:"submission_time"` } func (m dbScrobble) toScrobble() model.Scrobble { return model.Scrobble{ MediaFileID: m.MediaFileID, - ID: m.RowId, + ID: m.ID, SubmissionTime: time.Unix(m.SubmissionTime, 0), } } @@ -49,7 +49,7 @@ func (r *scrobbleRepository) baseQuery(options ...model.QueryOptions) SelectBuil user := loggedUser(r.ctx) return r.newSelect(options...). - Columns("scrobbles.ROWID row_id", "media_file_id", "submission_time"). + Columns("id", "media_file_id", "submission_time"). Where(Eq{"scrobbles.user_id": user.ID}) } @@ -81,13 +81,7 @@ func (r *scrobbleRepository) RecordScrobble(mediaFileID string, submissionTime t } func (r *scrobbleRepository) CountAll(options ...model.QueryOptions) (int64, error) { - userID := loggedUser(r.ctx).ID - count := r.newSelect().Column("COUNT(*) as count").Where(Eq{"user_id": userID}) - // We do this instead of newSelect, because we do not want to apply limit/offset/order - count = r.applyFilters(count, options...) - var res struct{ Count int64 } - err := r.queryOne(count, &res) - return res.Count, err + return r.count(r.baseQuery(), options...) } func (r *scrobbleRepository) Count(options ...rest.QueryOptions) (int64, error) { @@ -95,7 +89,7 @@ func (r *scrobbleRepository) Count(options ...rest.QueryOptions) (int64, error) } func (r *scrobbleRepository) Get(id string) (*model.Scrobble, error) { - sel := r.baseQuery().Where(Eq{"scrobbles.ROWID": id}) + sel := r.baseQuery().Where(Eq{"id": id}) var res dbScrobble err := r.queryOne(sel, &res) if err != nil { diff --git a/server/nativeapi/native_api.go b/server/nativeapi/native_api.go index f5f532c1b..5a7023eb6 100644 --- a/server/nativeapi/native_api.go +++ b/server/nativeapi/native_api.go @@ -72,8 +72,8 @@ func (api *Router) routes() http.Handler { api.R(r, "/player", model.Player{}, true) api.R(r, "/transcoding", model.Transcoding{}, conf.Server.EnableTranscodingConfig) api.addRadioRoute(r) - api.R(r, "/tag", model.Tag{}, true) - api.R(r, "/scrobble", model.Scrobble{}, true) + api.R(r, "/tag", model.Tag{}, false) + api.R(r, "/scrobble", model.Scrobble{}, false) if conf.Server.EnableSharing { api.RX(r, "/share", api.share.NewRepository, true) }