add primary key field, update index, remove rowid references

This commit is contained in:
Kendall Garner 2026-07-12 14:33:15 -07:00
parent d1f16e0bb0
commit c8fc6e6e0a
No known key found for this signature in database
GPG Key ID: 9355F387FE765C94
4 changed files with 49 additions and 16 deletions

View File

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

View File

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

View File

@ -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 {

View File

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