mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Simplify space migrator and fix not removing on error
This commit is contained in:
parent
a88f934c45
commit
81497e2071
1 changed files with 5 additions and 28 deletions
|
@ -4,8 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
anystore "github.com/anyproto/any-store"
|
anystore "github.com/anyproto/any-store"
|
||||||
|
@ -30,7 +28,6 @@ var ErrAlreadyMigrated = errors.New("already migrated")
|
||||||
|
|
||||||
type SpaceMigrator interface {
|
type SpaceMigrator interface {
|
||||||
MigrateId(ctx context.Context, id string, progress Progress) error
|
MigrateId(ctx context.Context, id string, progress Progress) error
|
||||||
CheckMigrated(ctx context.Context, id string) (bool, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Progress interface {
|
type Progress interface {
|
||||||
|
@ -47,11 +44,7 @@ type spaceMigrator struct {
|
||||||
removeFunc RemoveFunc
|
removeFunc RemoveFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSpaceMigrator(oldProvider oldstorage.SpaceStorageProvider, newProvider spacestorage.SpaceStorageProvider, numParallel int, rootPath string) SpaceMigrator {
|
func NewSpaceMigrator(oldProvider oldstorage.SpaceStorageProvider, newProvider spacestorage.SpaceStorageProvider, numParallel int, rootPath string, removeFunc RemoveFunc) SpaceMigrator {
|
||||||
return NewSpaceMigratorWithRemoveFunc(oldProvider, newProvider, numParallel, rootPath, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSpaceMigratorWithRemoveFunc(oldProvider oldstorage.SpaceStorageProvider, newProvider spacestorage.SpaceStorageProvider, numParallel int, rootPath string, removeFunc RemoveFunc) SpaceMigrator {
|
|
||||||
return &spaceMigrator{
|
return &spaceMigrator{
|
||||||
oldProvider: oldProvider,
|
oldProvider: oldProvider,
|
||||||
newProvider: newProvider,
|
newProvider: newProvider,
|
||||||
|
@ -61,32 +54,16 @@ func NewSpaceMigratorWithRemoveFunc(oldProvider oldstorage.SpaceStorageProvider,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *spaceMigrator) CheckMigrated(ctx context.Context, id string) (bool, error) {
|
|
||||||
migrated, storage := s.checkMigrated(ctx, id)
|
|
||||||
if storage != nil {
|
|
||||||
return migrated, storage.Close(ctx)
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *spaceMigrator) MigrateId(ctx context.Context, id string, progress Progress) error {
|
func (s *spaceMigrator) MigrateId(ctx context.Context, id string, progress Progress) error {
|
||||||
migrated, storage := s.checkMigrated(ctx, id)
|
migrated, storage := s.checkMigrated(ctx, id)
|
||||||
if migrated {
|
if migrated {
|
||||||
storage.Close(ctx)
|
storage.Close(ctx)
|
||||||
return ErrAlreadyMigrated
|
return ErrAlreadyMigrated
|
||||||
}
|
}
|
||||||
if storage != nil {
|
if !migrated {
|
||||||
if s.removeFunc != nil {
|
err := s.removeFunc(storage, s.rootPath)
|
||||||
err := s.removeFunc(storage, s.rootPath)
|
if err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("migration: failed to remove new storage: %w", err)
|
||||||
return fmt.Errorf("migration: failed to remove new storage: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := storage.Close(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("migration: failed to close old storage: %w", err)
|
|
||||||
}
|
|
||||||
os.RemoveAll(filepath.Join(s.rootPath, id))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oldStorage, err := s.oldProvider.WaitSpaceStorage(ctx, id)
|
oldStorage, err := s.oldProvider.WaitSpaceStorage(ctx, id)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue