From c5cac5b987c75baba653b705630e9674c4819125 Mon Sep 17 00:00:00 2001 From: sentriz Date: Fri, 30 Jun 2023 16:12:42 +0100 Subject: [PATCH] auto migrate plays before migration --- db/migrations.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/db/migrations.go b/db/migrations.go index 0bd40a8..a7181a4 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -519,8 +519,15 @@ func migratePlaylistsToM3U(tx *gorm.DB, ctx MigrationContext) error { } func migratePlayCountToLength(tx *gorm.DB, _ MigrationContext) error { + step := tx.AutoMigrate( + Play{}, + ) + if err := step.Error; err != nil { + return fmt.Errorf("step auto migrate: %w", err) + } + // As a best guess, we set length played so far as length of album * current count / number of tracks in album - step := tx.Exec(` + step = tx.Exec(` UPDATE plays SET length= ((SELECT SUM(length) FROM tracks WHERE tracks.album_id=plays.album_id)*plays.count/ (SELECT COUNT(*) FROM tracks WHERE tracks.album_id=plays.album_id)); @@ -528,8 +535,6 @@ func migratePlayCountToLength(tx *gorm.DB, _ MigrationContext) error { if err := step.Error; err != nil { return fmt.Errorf("calculate length: %w", err) } - + return nil } - -