chore: update postgres profile

This commit is contained in:
Steven 2023-12-19 08:59:07 +08:00
parent 47346182f0
commit d4c7de3916
2 changed files with 13 additions and 25 deletions

View File

@ -34,7 +34,7 @@ func (p *Profile) IsDev() bool {
return p.Mode != "prod" return p.Mode != "prod"
} }
func checkDSN(dataDir string) (string, error) { func checkDataDir(dataDir string) (string, error) {
// Convert to absolute path if relative path is supplied. // Convert to absolute path if relative path is supplied.
if !filepath.IsAbs(dataDir) { if !filepath.IsAbs(dataDir) {
relativeDir := filepath.Join(filepath.Dir(os.Args[0]), dataDir) relativeDir := filepath.Join(filepath.Dir(os.Args[0]), dataDir)
@ -82,15 +82,19 @@ func GetProfile() (*Profile, error) {
} }
} }
dataDir, err := checkDSN(profile.Data) if profile.Driver == "sqlite" {
if err != nil { dataDir, err := checkDataDir(profile.Data)
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err) if err != nil {
return nil, err fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
} return nil, err
}
profile.Data = dataDir profile.Data = dataDir
dbFile := fmt.Sprintf("slash_%s.db", profile.Mode) if profile.DSN == "" {
profile.DSN = filepath.Join(dataDir, dbFile) dbFile := fmt.Sprintf("slash_%s.db", profile.Mode)
profile.DSN = filepath.Join(dataDir, dbFile)
}
}
profile.Version = version.GetCurrentVersion(profile.Mode) profile.Version = version.GetCurrentVersion(profile.Mode)
return &profile, nil return &profile, nil

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"regexp" "regexp"
"sort" "sort"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -72,16 +71,6 @@ func (d *DB) Migrate(ctx context.Context) error {
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) { if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
minorVersionList := getMinorVersionList() minorVersionList := getMinorVersionList()
// backup the raw database file before migration
rawBytes, err := os.ReadFile(d.profile.DSN)
if err != nil {
return errors.Wrap(err, "failed to read raw database file")
}
backupDBFilePath := fmt.Sprintf("%s/memos_%s_%d_backup.db", d.profile.Data, d.profile.Version, time.Now().Unix())
if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil {
return errors.Wrap(err, "failed to write raw database file")
}
println("succeed to copy a backup database file")
println("start migrate") println("start migrate")
for _, minorVersion := range minorVersionList { for _, minorVersion := range minorVersionList {
normalizedVersion := minorVersion + ".0" normalizedVersion := minorVersion + ".0"
@ -93,11 +82,6 @@ func (d *DB) Migrate(ctx context.Context) error {
} }
} }
println("end migrate") println("end migrate")
// remove the created backup db file after migrate succeed
if err := os.Remove(backupDBFilePath); err != nil {
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
}
} }
} }
} else { } else {