From 946906221d4e8b643804d7bee38e039f1aa4910b Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Tue, 11 Apr 2023 16:57:01 +0200 Subject: [PATCH] Delete sync snapshots file when destroying a bundle (#323) ## Changes This PR changes the files.Delete() mutator to delete the sync snapshots file on destroy. This ensures that files will be uploaded when the bundle is uploaded again. ## Tests - [x] Manual test: Ran `bricks bundle destroy`, observed that the sync snapshots file was deleted. --- bundle/deploy/files/delete.go | 10 ++++++++++ bundle/deploy/files/sync.go | 26 ++++++++++++++++++++++++++ bundle/deploy/files/upload.go | 18 +----------------- libs/sync/snapshot.go | 8 ++++++++ libs/sync/sync.go | 4 ++++ 5 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 bundle/deploy/files/sync.go diff --git a/bundle/deploy/files/delete.go b/bundle/deploy/files/delete.go index 91bedc2a..673adbb4 100644 --- a/bundle/deploy/files/delete.go +++ b/bundle/deploy/files/delete.go @@ -49,6 +49,16 @@ func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, return nil, err } + // Clean up sync snapshot file + sync, err := getSync(ctx, b) + if err != nil { + return nil, err + } + err = sync.DestroySnapshot(ctx) + if err != nil { + return nil, err + } + fmt.Println("Successfully deleted files!") return nil, nil } diff --git a/bundle/deploy/files/sync.go b/bundle/deploy/files/sync.go new file mode 100644 index 00000000..262a205f --- /dev/null +++ b/bundle/deploy/files/sync.go @@ -0,0 +1,26 @@ +package files + +import ( + "context" + "fmt" + + "github.com/databricks/bricks/bundle" + "github.com/databricks/bricks/libs/sync" +) + +func getSync(ctx context.Context, b *bundle.Bundle) (*sync.Sync, error) { + cacheDir, err := b.CacheDir() + if err != nil { + return nil, fmt.Errorf("cannot get bundle cache directory: %w", err) + } + + opts := sync.SyncOptions{ + LocalPath: b.Config.Path, + RemotePath: b.Config.Workspace.FilePath.Workspace, + Full: false, + + SnapshotBasePath: cacheDir, + WorkspaceClient: b.WorkspaceClient(), + } + return sync.New(ctx, opts) +} diff --git a/bundle/deploy/files/upload.go b/bundle/deploy/files/upload.go index a8382630..b9a6dacb 100644 --- a/bundle/deploy/files/upload.go +++ b/bundle/deploy/files/upload.go @@ -2,10 +2,8 @@ package files import ( "context" - "fmt" "github.com/databricks/bricks/bundle" - sync "github.com/databricks/bricks/libs/sync" ) type upload struct{} @@ -15,21 +13,7 @@ func (m *upload) Name() string { } func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) { - cacheDir, err := b.CacheDir() - if err != nil { - return nil, fmt.Errorf("cannot get bundle cache directory: %w", err) - } - - opts := sync.SyncOptions{ - LocalPath: b.Config.Path, - RemotePath: b.Config.Workspace.FilePath.Workspace, - Full: false, - - SnapshotBasePath: cacheDir, - WorkspaceClient: b.WorkspaceClient(), - } - - sync, err := sync.New(ctx, opts) + sync, err := getSync(ctx, b) if err != nil { return nil, err } diff --git a/libs/sync/snapshot.go b/libs/sync/snapshot.go index cf5e60cf..0640dcac 100644 --- a/libs/sync/snapshot.go +++ b/libs/sync/snapshot.go @@ -126,6 +126,14 @@ func (s *Snapshot) Save(ctx context.Context) error { return nil } +func (s *Snapshot) Destroy(ctx context.Context) error { + err := os.Remove(s.SnapshotPath) + if err != nil { + return fmt.Errorf("failed to destroy sync snapshot file: %s", err) + } + return nil +} + func loadOrNewSnapshot(ctx context.Context, opts *SyncOptions) (*Snapshot, error) { snapshot, err := newSnapshot(ctx, opts) if err != nil { diff --git a/libs/sync/sync.go b/libs/sync/sync.go index 7d91e028..4bcbc728 100644 --- a/libs/sync/sync.go +++ b/libs/sync/sync.go @@ -160,6 +160,10 @@ func (s *Sync) RunOnce(ctx context.Context) error { return nil } +func (s *Sync) DestroySnapshot(ctx context.Context) error { + return s.snapshot.Destroy(ctx) +} + func (s *Sync) RunContinuous(ctx context.Context) error { ticker := time.NewTicker(s.PollInterval) defer ticker.Stop()