Skip to content

Commit 9fa228a

Browse files
committed
Support file selectors in pipelines run
1 parent 50f8198 commit 9fa228a

13 files changed

Lines changed: 500 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Support source files in `pipelines run --refresh` and `--full-refresh` selectors.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
bundle:
22
name: test-pipeline-run-flags
33

4+
sync:
5+
exclude:
6+
- excluded.sql
7+
48
resources:
59
pipelines:
610
my_pipeline:
711
name: test-pipeline-flags
812
libraries:
913
- file:
1014
path: pipeline_file.py
15+
- notebook:
16+
path: notebook.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Databricks notebook source
2+
print("notebook")

‎acceptance/pipelines/run/refresh-flags/output.txt‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
>>> [CLI] pipelines deploy
33
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-pipeline-run-flags/default/files...
44
Created pipelines.my_pipeline
5-
Files: 2 uploaded, 0 deleted
5+
Files: 3 uploaded, 0 deleted
66
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
77
View your pipeline my_pipeline here: [DATABRICKS_URL]/pipelines/[UUID]?w=[WORKSPACE_ID]
88

@@ -114,3 +114,121 @@ Pipeline configurations for this update:
114114
]
115115
}
116116
}
117+
118+
=== Running pipeline with a file selector
119+
>>> [CLI] pipelines run --refresh pipeline_file.py --no-wait
120+
Recommendation: This command runs the last deployed version of the code
121+
122+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
123+
124+
Update URL: [DATABRICKS_URL]/#joblist/pipelines/[UUID]/updates/[UUID]
125+
126+
Update ID: [UUID]
127+
128+
Update for pipeline completed successfully.
129+
130+
Pipeline configurations for this update:
131+
• All tables are refreshed
132+
133+
>>> print_requests.py //pipelines
134+
{
135+
"method": "POST",
136+
"path": "/api/2.0/pipelines/[UUID]/updates",
137+
"body": {
138+
"refresh_selection_by_path": [
139+
"/Workspace/Users/[USERNAME]/.bundle/test-pipeline-run-flags/default/files/pipeline_file.py"
140+
]
141+
}
142+
}
143+
144+
=== Running pipeline with a full-refresh notebook selector
145+
>>> [CLI] pipelines run --full-refresh notebook.py --no-wait
146+
Recommendation: This command runs the last deployed version of the code
147+
148+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
149+
150+
Update URL: [DATABRICKS_URL]/#joblist/pipelines/[UUID]/updates/[UUID]
151+
152+
Update ID: [UUID]
153+
154+
Update for pipeline completed successfully.
155+
156+
Pipeline configurations for this update:
157+
• All tables are refreshed
158+
159+
>>> print_requests.py //pipelines
160+
{
161+
"method": "POST",
162+
"path": "/api/2.0/pipelines/[UUID]/updates",
163+
"body": {
164+
"full_refresh_selection_by_path": [
165+
"/Workspace/Users/[USERNAME]/.bundle/test-pipeline-run-flags/default/files/notebook"
166+
]
167+
}
168+
}
169+
170+
=== Running pipeline with mixed table and source selectors
171+
>>> [CLI] pipelines run --refresh table1,pipeline_file.py --full-refresh table2,notebook.py --no-wait
172+
Recommendation: This command runs the last deployed version of the code
173+
174+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
175+
176+
Update URL: [DATABRICKS_URL]/#joblist/pipelines/[UUID]/updates/[UUID]
177+
178+
Update ID: [UUID]
179+
180+
Update for pipeline completed successfully.
181+
182+
Pipeline configurations for this update:
183+
• All tables are refreshed
184+
185+
>>> print_requests.py //pipelines
186+
{
187+
"method": "POST",
188+
"path": "/api/2.0/pipelines/[UUID]/updates",
189+
"body": {
190+
"full_refresh_selection": [
191+
"table2"
192+
],
193+
"full_refresh_selection_by_path": [
194+
"/Workspace/Users/[USERNAME]/.bundle/test-pipeline-run-flags/default/files/notebook"
195+
],
196+
"refresh_selection": [
197+
"table1"
198+
],
199+
"refresh_selection_by_path": [
200+
"/Workspace/Users/[USERNAME]/.bundle/test-pipeline-run-flags/default/files/pipeline_file.py"
201+
]
202+
}
203+
}
204+
205+
=== Rejecting missing and excluded sources
206+
>>> musterr [CLI] pipelines run --refresh ./missing.sql --no-wait
207+
Recommendation: This command runs the last deployed version of the code
208+
209+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
210+
211+
Error: invalid --refresh: source file "./missing.sql" not found; use a table name or an existing source file
212+
213+
>>> musterr [CLI] pipelines run --refresh ./excluded.sql --no-wait
214+
Recommendation: This command runs the last deployed version of the code
215+
216+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
217+
218+
Error: invalid --refresh: source file "./excluded.sql" is excluded from bundle sync; include it and run 'databricks pipelines deploy' first
219+
220+
>>> musterr [CLI] pipelines run --refresh pipeline_file.py --full-refresh-all --no-wait
221+
Recommendation: This command runs the last deployed version of the code
222+
223+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
224+
225+
Error: cannot use --refresh or --full-refresh together with --full-refresh-all, these flags are mutually exclusive
226+
227+
>>> musterr [CLI] pipelines run --full-refresh ./missing.sql --restart --no-wait
228+
Recommendation: This command runs the last deployed version of the code
229+
230+
If you've made local changes, run 'databricks pipelines deploy' first to ensure they are included.
231+
232+
Error: invalid --full-refresh: source file "./missing.sql" not found; use a table name or an existing source file
233+
234+
>>> test ! -e [OUTPUT_DIR]/out.requests.txt

‎acceptance/pipelines/run/refresh-flags/script‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ trace print_requests.py //pipelines
1616
title "Running pipeline with --full-refresh flag and --refresh flag"
1717
trace $CLI pipelines run --full-refresh table1,table2 --refresh table3,table4
1818
trace print_requests.py //pipelines
19+
20+
title "Running pipeline with a file selector"
21+
trace $CLI pipelines run --refresh pipeline_file.py --no-wait
22+
trace print_requests.py //pipelines
23+
24+
title "Running pipeline with a full-refresh notebook selector"
25+
trace $CLI pipelines run --full-refresh notebook.py --no-wait
26+
trace print_requests.py //pipelines
27+
28+
title "Running pipeline with mixed table and source selectors"
29+
trace $CLI pipelines run --refresh table1,pipeline_file.py --full-refresh table2,notebook.py --no-wait
30+
trace print_requests.py //pipelines
31+
32+
title "Rejecting missing and excluded sources"
33+
trace musterr $CLI pipelines run --refresh ./missing.sql --no-wait
34+
trace musterr $CLI pipelines run --refresh ./excluded.sql --no-wait
35+
trace musterr $CLI pipelines run --refresh pipeline_file.py --full-refresh-all --no-wait
36+
trace musterr $CLI pipelines run --full-refresh ./missing.sql --restart --no-wait
37+
trace test ! -e "$OUT_REQUESTS"

‎bundle/run/pipeline_options.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ type PipelineOptions struct {
1717
// List of tables to update.
1818
Refresh []string
1919

20+
RefreshByPath []string
21+
2022
// Perform a full graph reset and recompute.
2123
FullRefreshAll bool
2224

2325
// List of tables to reset and recompute.
2426
FullRefresh []string
2527

28+
FullRefreshByPath []string
29+
2630
// Perform an update to validate graph correctness.
2731
ValidateOnly bool
2832
}
@@ -51,7 +55,7 @@ func (o *PipelineOptions) Validate(pipeline *resources.Pipeline) error {
5155

5256
if len(mutuallyExclusiveFlags) > 1 {
5357
return fmt.Errorf("%s pipeline run flags are mutually exclusive", strings.Join(mutuallyExclusiveFlags, ", "))
54-
} else if len(mutuallyExclusiveFlags) == 1 && (len(o.Refresh) > 0 || len(o.FullRefresh) > 0) {
58+
} else if len(mutuallyExclusiveFlags) == 1 && (len(o.Refresh) > 0 || len(o.FullRefresh) > 0 || len(o.RefreshByPath) > 0 || len(o.FullRefreshByPath) > 0) {
5559
return fmt.Errorf("cannot use --refresh or --full-refresh together with %s, these flags are mutually exclusive", strings.Join(mutuallyExclusiveFlags, ""))
5660
}
5761

@@ -66,10 +70,12 @@ func (o *PipelineOptions) toPayload(pipeline *resources.Pipeline, pipelineID str
6670
PipelineId: pipelineID,
6771

6872
// Note: `RefreshAll` is implied if the fields below are not set.
69-
RefreshSelection: o.Refresh,
70-
FullRefresh: o.FullRefreshAll,
71-
FullRefreshSelection: o.FullRefresh,
72-
ValidateOnly: o.ValidateOnly,
73+
RefreshSelection: o.Refresh,
74+
FullRefresh: o.FullRefreshAll,
75+
FullRefreshSelection: o.FullRefresh,
76+
RefreshSelectionByPath: o.RefreshByPath,
77+
FullRefreshSelectionByPath: o.FullRefreshByPath,
78+
ValidateOnly: o.ValidateOnly,
7379

7480
// Only true is sent. When false the field is omitted, so the backend keeps
7581
// using the pipeline-level development property while it is being deprecated.

‎bundle/run/pipeline_options_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
package run
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"testing"
57

68
flag "github.com/spf13/pflag"
79
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
911
)
1012

13+
func TestPipelineOptionsPathSelectionsPayload(t *testing.T) {
14+
opts := PipelineOptions{
15+
Refresh: []string{"catalog.schema.table1"},
16+
FullRefresh: []string{"catalog.schema.table2"},
17+
RefreshByPath: []string{"/Workspace/Shared/source.sql"},
18+
FullRefreshByPath: []string{"/Workspace/Shared/notebook"},
19+
}
20+
payload, err := opts.toPayload(nil, "pipeline-id", false)
21+
require.NoError(t, err)
22+
assert.Equal(t, "pipeline-id", payload.PipelineId)
23+
data, err := json.Marshal(payload)
24+
require.NoError(t, err)
25+
assert.JSONEq(t, `{
26+
"refresh_selection": ["catalog.schema.table1"],
27+
"full_refresh_selection": ["catalog.schema.table2"],
28+
"refresh_selection_by_path": ["/Workspace/Shared/source.sql"],
29+
"full_refresh_selection_by_path": ["/Workspace/Shared/notebook"]
30+
}`, string(data))
31+
}
32+
33+
func TestPipelineOptionsValidatePathSelections(t *testing.T) {
34+
for _, full := range []bool{false, true} {
35+
for _, exclusive := range []string{"refresh-all", "full-refresh-all", "validate-only"} {
36+
t.Run(fmt.Sprintf("full=%t/%s", full, exclusive), func(t *testing.T) {
37+
opts := PipelineOptions{
38+
RefreshAll: exclusive == "refresh-all",
39+
FullRefreshAll: exclusive == "full-refresh-all",
40+
ValidateOnly: exclusive == "validate-only",
41+
}
42+
if full {
43+
opts.FullRefreshByPath = []string{"/Workspace/Shared/source.sql"}
44+
} else {
45+
opts.RefreshByPath = []string{"/Workspace/Shared/source.sql"}
46+
}
47+
_, err := opts.toPayload(nil, "pipeline-id", false)
48+
assert.ErrorContains(t, err, "cannot use --refresh or --full-refresh together with --"+exclusive)
49+
})
50+
}
51+
}
52+
}
53+
1154
func setupPipelineOptions(t *testing.T) (*flag.FlagSet, *PipelineOptions) {
1255
var fs flag.FlagSet
1356
var opts PipelineOptions

‎cmd/pipelines/refresh.go‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package pipelines
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"io/fs"
8+
"os"
9+
"path"
10+
"path/filepath"
11+
"strings"
12+
13+
"github.com/databricks/cli/bundle"
14+
"github.com/databricks/cli/bundle/config"
15+
"github.com/databricks/cli/libs/sync"
16+
)
17+
18+
// resolveRefreshSelectors separates table names from source files and translates local files to workspace paths.
19+
func resolveRefreshSelectors(ctx context.Context, b *bundle.Bundle, selectors []string) (tables, paths []string, err error) {
20+
var remoteNames map[string]string
21+
for _, selector := range selectors {
22+
if strings.HasPrefix(selector, "/Workspace/") {
23+
paths = append(paths, selector)
24+
continue
25+
}
26+
27+
// Quoted identifiers remain table selectors even when they contain path separators.
28+
if strings.Contains(selector, "`") {
29+
tables = append(tables, selector)
30+
continue
31+
}
32+
33+
info, statErr := os.Stat(selector)
34+
if statErr != nil && !errors.Is(statErr, fs.ErrNotExist) {
35+
return nil, nil, fmt.Errorf("cannot read refresh selector %q: %w", selector, statErr)
36+
}
37+
// Preserve table identifiers such as schema.sql when no local file exists.
38+
looksLikeFile := strings.ContainsAny(selector, `/\`)
39+
if errors.Is(statErr, fs.ErrNotExist) && !looksLikeFile {
40+
tables = append(tables, selector)
41+
continue
42+
}
43+
if statErr != nil {
44+
return nil, nil, fmt.Errorf("source file %q not found; use a table name or an existing source file", selector)
45+
}
46+
if info.IsDir() {
47+
return nil, nil, fmt.Errorf("refresh selector %q is a directory; select a source file", selector)
48+
}
49+
50+
localPath, err := filepath.Abs(selector)
51+
if err != nil {
52+
return nil, nil, err
53+
}
54+
rel, err := filepath.Rel(b.SyncRootPath, localPath)
55+
if err != nil {
56+
return nil, nil, err
57+
}
58+
if !filepath.IsLocal(rel) {
59+
return nil, nil, fmt.Errorf("source file %q is outside the bundle sync root", selector)
60+
}
61+
if remoteNames == nil {
62+
remoteNames, err = refreshSourcePaths(ctx, b)
63+
if err != nil {
64+
return nil, nil, err
65+
}
66+
}
67+
remoteName, ok := remoteNames[filepath.ToSlash(rel)]
68+
if !ok {
69+
return nil, nil, fmt.Errorf("source file %q is excluded from bundle sync; include it and run 'databricks pipelines deploy' first", selector)
70+
}
71+
remoteRoot := b.Config.Workspace.FilePath
72+
if config.IsExplicitlyEnabled(b.Config.Presets.SourceLinkedDeployment) {
73+
remoteRoot = filepath.ToSlash(b.SyncRootPath)
74+
}
75+
if strings.Contains(remoteRoot, "${") {
76+
return nil, nil, fmt.Errorf("cannot resolve the deployed path for %q; use its absolute /Workspace/ path", selector)
77+
}
78+
paths = append(paths, path.Join(remoteRoot, remoteName))
79+
}
80+
return tables, paths, nil
81+
}
82+
83+
// refreshSourcePaths uses the sync mapping so notebook names and sync exclusions match deployment.
84+
func refreshSourcePaths(ctx context.Context, b *bundle.Bundle) (map[string]string, error) {
85+
includes, err := b.GetSyncIncludePatterns(ctx)
86+
if err != nil {
87+
return nil, err
88+
}
89+
list, err := sync.NewFileList(ctx, b.WorktreeRoot, b.SyncRoot, b.Config.Sync.Paths, includes, b.Config.Sync.Exclude)
90+
if err != nil {
91+
return nil, err
92+
}
93+
files, err := list.Files(ctx)
94+
if err != nil {
95+
return nil, err
96+
}
97+
state, err := sync.NewSnapshotState(files)
98+
if err != nil {
99+
return nil, err
100+
}
101+
return state.LocalToRemoteNames, nil
102+
}

0 commit comments

Comments
 (0)