From 47378c68828861751b9d1a05d3fc9b29ce8dd9f0 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 29 Jun 2024 11:45:41 -0400 Subject: [PATCH] Remove unnecessary annotation table primary key --- .../20240629152843_remove_annotation_id.go | 66 +++++++++++++++++++ persistence/sql_annotations.go | 3 - 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 db/migrations/20240629152843_remove_annotation_id.go diff --git a/db/migrations/20240629152843_remove_annotation_id.go b/db/migrations/20240629152843_remove_annotation_id.go new file mode 100644 index 000000000..b450b26d4 --- /dev/null +++ b/db/migrations/20240629152843_remove_annotation_id.go @@ -0,0 +1,66 @@ +package migrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationContext(upRemoveAnnotationId, downRemoveAnnotationId) +} + +func upRemoveAnnotationId(ctx context.Context, tx *sql.Tx) error { + _, err := tx.ExecContext(ctx, ` +create table annotation_dg_tmp +( + user_id varchar(255) default '' not null, + item_id varchar(255) default '' not null, + item_type varchar(255) default '' not null, + play_count integer default 0, + play_date datetime, + rating integer default 0, + starred bool default FALSE not null, + starred_at datetime, + unique (user_id, item_id, item_type) +); + +insert into annotation_dg_tmp(user_id, item_id, item_type, play_count, play_date, rating, starred, starred_at) +select user_id, + item_id, + item_type, + play_count, + play_date, + rating, + starred, + starred_at +from annotation; + +drop table annotation; + +alter table annotation_dg_tmp + rename to annotation; + +create index annotation_play_count + on annotation (play_count); + +create index annotation_play_date + on annotation (play_date); + +create index annotation_rating + on annotation (rating); + +create index annotation_starred + on annotation (starred); + +create index annotation_starred_at + on annotation (starred_at); + +`) + return err +} + +func downRemoveAnnotationId(ctx context.Context, tx *sql.Tx) error { + return nil +} diff --git a/persistence/sql_annotations.go b/persistence/sql_annotations.go index 77dc816c2..8ce1bdd69 100644 --- a/persistence/sql_annotations.go +++ b/persistence/sql_annotations.go @@ -6,7 +6,6 @@ import ( "time" . "github.com/Masterminds/squirrel" - "github.com/google/uuid" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/log" @@ -52,7 +51,6 @@ func (r sqlRepository) annUpsert(values map[string]interface{}, itemIDs ...strin c, err := r.executeSQL(upd) if c == 0 || errors.Is(err, sql.ErrNoRows) { for _, itemID := range itemIDs { - values["ann_id"] = uuid.NewString() values["user_id"] = userId(r.ctx) values["item_type"] = r.tableName values["item_id"] = itemID @@ -83,7 +81,6 @@ func (r sqlRepository) IncPlayCount(itemID string, ts time.Time) error { if c == 0 || errors.Is(err, sql.ErrNoRows) { values := map[string]interface{}{} - values["ann_id"] = uuid.NewString() values["user_id"] = userId(r.ctx) values["item_type"] = r.tableName values["item_id"] = itemID