generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add local schema diff #2180
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,30 @@ func testImportExport(t *testing.T, object string) { | |
}), | ||
) | ||
} | ||
|
||
func TestLocalSchemaDiff(t *testing.T) { | ||
newVerb := ` | ||
//ftl:verb | ||
func NewFunction(ctx context.Context, req TimeRequest) (TimeResponse, error) { | ||
return TimeResponse{Time: time.Now()}, nil | ||
} | ||
` | ||
Run(t, "", | ||
CopyModule("time"), | ||
Deploy("time"), | ||
ExecWithOutput("ftl", []string{"schema", "diff"}, func(output string) { | ||
assert.Equal(t, "", output) | ||
}), | ||
EditFile("time", func(bytes []byte) []byte { | ||
s := string(bytes) | ||
s += newVerb | ||
return []byte(s) | ||
}, "time.go"), | ||
Build("time"), | ||
// We exit with code 1 when there is a difference | ||
ExpectError( | ||
ExecWithOutput("ftl", []string{"schema", "diff"}, func(output string) { | ||
assert.Contains(t, output, "- verb newFunction(time.TimeRequest) time.TimeResponse") | ||
}), "exit status 1"), | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test is perfect! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,12 @@ import ( | |
"github.com/kballard/go-shellquote" | ||
"github.com/otiai10/copy" | ||
|
||
"github.com/TBD54566975/scaffolder" | ||
|
||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" | ||
ftlexec "github.com/TBD54566975/ftl/internal/exec" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
"github.com/TBD54566975/scaffolder" | ||
) | ||
|
||
// Scaffold a directory relative to the testdata directory to a directory relative to the working directory. | ||
|
@@ -280,6 +281,21 @@ func WriteFile(path string, content []byte) Action { | |
} | ||
} | ||
|
||
// EditFile edits a file in a module | ||
func EditFile(module string, editFunc func([]byte) []byte, path ...string) Action { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noice. |
||
return func(t testing.TB, ic TestContext) { | ||
parts := []string{ic.workDir, module} | ||
parts = append(parts, path...) | ||
file := filepath.Join(parts...) | ||
Infof("Editing %s", file) | ||
contents, err := os.ReadFile(file) | ||
assert.NoError(t, err) | ||
contents = editFunc(contents) | ||
err = os.WriteFile(file, contents, os.FileMode(0)) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
type Obj map[string]any | ||
|
||
// Call a verb. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we implicitly do a build when running a local diff? It does not really seem right, but not doing it also seems like it will result in a high chance of user error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was initially thinking not, mainly because it's expensive and
ftl dev
might already be running, but I think you're right. It's better to favour correctness, and we can solve the expensiveness problem with future expansions to the build system (ie. proper dependency checks).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I just call build directly in code the output gets polluted with the build output, which is not really want you want with a diff command. I could try and suppress it (assuming you can do that in go?) but that is also not ideal, or I could try and capture it and only print it on error. In terms of capturing I guess I would need to do something like: https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string
Or maybe it is not worth building as part of the diff. If we had the ability to detect if a schema is out of date that would probably be enough as we could print a warning to stderr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, let's ship it as-is.