Skip to content

Commit

Permalink
auto migrate plays before migration
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Jun 30, 2023
1 parent 2e879f6 commit c5cac5b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,22 @@ 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));
`)
if err := step.Error; err != nil {
return fmt.Errorf("calculate length: %w", err)
}

return nil
}


0 comments on commit c5cac5b

Please sign in to comment.