chore: retire auto backup for sqlite

This commit is contained in:
Steven
2023-12-19 22:34:06 +08:00
parent b575064d47
commit e0cacfc6d6
29 changed files with 23 additions and 385 deletions

View File

@@ -72,7 +72,7 @@ func (d *DB) Migrate(ctx context.Context) error {
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
minorVersionList := getMinorVersionList()
// backup the raw database file before migration
// 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")
@@ -94,7 +94,7 @@ func (d *DB) Migrate(ctx context.Context) error {
}
println("end migrate")
// remove the created backup db file after migrate succeed
// 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))
}

View File

@@ -8,7 +8,9 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"modernc.org/sqlite"
// Import the SQLite driver.
_ "modernc.org/sqlite"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
@@ -103,43 +105,6 @@ func vacuumImpl(ctx context.Context, tx *sql.Tx) error {
return nil
}
func (d *DB) BackupTo(ctx context.Context, filename string) error {
conn, err := d.db.Conn(ctx)
if err != nil {
return errors.Wrap(err, "fail to open new connection")
}
defer conn.Close()
err = conn.Raw(func(driverConn any) error {
type backuper interface {
NewBackup(string) (*sqlite.Backup, error)
}
backupConn, ok := driverConn.(backuper)
if !ok {
return errors.New("db connection is not a sqlite backuper")
}
bck, err := backupConn.NewBackup(filename)
if err != nil {
return errors.Wrap(err, "fail to create sqlite backup")
}
for more := true; more; {
more, err = bck.Step(-1)
if err != nil {
return errors.Wrap(err, "fail to execute sqlite backup")
}
}
return bck.Finish()
})
if err != nil {
return errors.Wrap(err, "fail to backup")
}
return nil
}
func (d *DB) GetCurrentDBSize(context.Context) (int64, error) {
fi, err := os.Stat(d.profile.DSN)
if err != nil {