Skip to content

Commit

Permalink
copy database from previous version
Browse files Browse the repository at this point in the history
  • Loading branch information
samluiz committed May 28, 2024
1 parent 1ce5e2f commit 453f79e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
35 changes: 19 additions & 16 deletions backend/db/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"dolphin/backend/shared/utils"
"os"
"path/filepath"

Expand Down Expand Up @@ -57,6 +58,12 @@ func createAppDir(homeDir string) error {
}

func createCurrentVersionDir(homeDir string, version string) error {
previousVersionPath, err := getPreviousVersionPath()

if err != nil {
return err
}

if _, err := os.Stat(filepath.Join(homeDir, ".dolphin", version)); os.IsNotExist(err) {
err = os.Mkdir(filepath.Join(homeDir, ".dolphin", version), 0755)

Expand All @@ -65,6 +72,16 @@ func createCurrentVersionDir(homeDir string, version string) error {
}
}

if previousVersionPath != "" {
err = copyPreviousDatabase(homeDir, version, previousVersionPath)

if err != nil {
return err
}

return nil
}

return nil
}

Expand All @@ -83,8 +100,6 @@ func getDbPath() (string, error) {

if Version == "" {
Version = "Build"
} else {
renamePreviousVersionPath()
}

err = createCurrentVersionDir(homeDir, Version)
Expand Down Expand Up @@ -128,20 +143,8 @@ func getPreviousVersionPath() (string, error) {
return filepath.Join(homeDir, ".dolphin", versions[len(versions)-1], "database.db"), nil
}

func renamePreviousVersionPath() error {
previousVersionPath, err := getPreviousVersionPath()

if err != nil {
return err
}

if previousVersionPath == "" {
return nil
}

newPath := previousVersionPath + ".old"

err = os.Rename(previousVersionPath, newPath)
func copyPreviousDatabase(homeDir string, version string, previousVersionPath string) error {
_, err := utils.CopyFile(previousVersionPath, filepath.Join(homeDir, ".dolphin", version, "database.db"))

if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions backend/shared/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import "os"

func CopyFile(in, out string) (int64, error) {
i, e := os.Open(in)
if e != nil {
return 0, e
}
defer i.Close()
o, e := os.Create(out)
if e != nil {
return 0, e
}
defer o.Close()
return o.ReadFrom(i)
}

0 comments on commit 453f79e

Please sign in to comment.