backup db pre 0.16 release
This commit is contained in:
@@ -143,6 +143,7 @@ func main() {
|
|||||||
defer dbc.Close()
|
defer dbc.Close()
|
||||||
|
|
||||||
err = dbc.Migrate(db.MigrationContext{
|
err = dbc.Migrate(db.MigrationContext{
|
||||||
|
DBPath: *confDBPath,
|
||||||
OriginalMusicPath: confMusicPaths[0].path,
|
OriginalMusicPath: confMusicPaths[0].path,
|
||||||
PlaylistsPath: *confPlaylistsPath,
|
PlaylistsPath: *confPlaylistsPath,
|
||||||
PodcastsPath: *confPodcastPath,
|
PodcastsPath: *confPodcastPath,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -18,6 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MigrationContext struct {
|
type MigrationContext struct {
|
||||||
|
DBPath string
|
||||||
OriginalMusicPath string
|
OriginalMusicPath string
|
||||||
PlaylistsPath string
|
PlaylistsPath string
|
||||||
PodcastsPath string
|
PodcastsPath string
|
||||||
@@ -56,6 +58,7 @@ func (db *DB) Migrate(ctx MigrationContext) error {
|
|||||||
construct(ctx, "202206101425", migrateUser),
|
construct(ctx, "202206101425", migrateUser),
|
||||||
construct(ctx, "202207251148", migrateStarRating),
|
construct(ctx, "202207251148", migrateStarRating),
|
||||||
construct(ctx, "202211111057", migratePlaylistsQueuesToFullID),
|
construct(ctx, "202211111057", migratePlaylistsQueuesToFullID),
|
||||||
|
constructNoTx(ctx, "202212272312", backupDBPre016),
|
||||||
construct(ctx, "202304221528", migratePlaylistsToM3U),
|
construct(ctx, "202304221528", migratePlaylistsToM3U),
|
||||||
construct(ctx, "202305301718", migratePlayCountToLength),
|
construct(ctx, "202305301718", migratePlayCountToLength),
|
||||||
construct(ctx, "202307281628", migrateAlbumArtistsMany2Many),
|
construct(ctx, "202307281628", migrateAlbumArtistsMany2Many),
|
||||||
@@ -70,12 +73,18 @@ func (db *DB) Migrate(ctx MigrationContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func construct(ctx MigrationContext, id string, f func(*gorm.DB, MigrationContext) error) *gormigrate.Migration {
|
func construct(ctx MigrationContext, id string, f func(*gorm.DB, MigrationContext) error) *gormigrate.Migration {
|
||||||
|
return constructNoTx(ctx, id, func(db *gorm.DB, ctx MigrationContext) error {
|
||||||
|
tx := db.Begin()
|
||||||
|
defer tx.Commit()
|
||||||
|
return f(tx, ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func constructNoTx(ctx MigrationContext, id string, f func(*gorm.DB, MigrationContext) error) *gormigrate.Migration {
|
||||||
return &gormigrate.Migration{
|
return &gormigrate.Migration{
|
||||||
ID: id,
|
ID: id,
|
||||||
Migrate: func(db *gorm.DB) error {
|
Migrate: func(db *gorm.DB) error {
|
||||||
tx := db.Begin()
|
if err := f(db, ctx); err != nil {
|
||||||
defer tx.Commit()
|
|
||||||
if err := f(tx, ctx); err != nil {
|
|
||||||
return fmt.Errorf("%q: %w", id, err)
|
return fmt.Errorf("%q: %w", id, err)
|
||||||
}
|
}
|
||||||
log.Printf("migration '%s' finished", id)
|
log.Printf("migration '%s' finished", id)
|
||||||
@@ -710,3 +719,7 @@ func migratePlaylistsPaths(tx *gorm.DB, ctx MigrationContext) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func backupDBPre016(tx *gorm.DB, ctx MigrationContext) error {
|
||||||
|
return Dump(context.Background(), tx, fmt.Sprintf("%s.%d.bak", ctx.DBPath, time.Now().Unix()))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user