Skip to content
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

fix: validate deploying module rather than entire schema #1217

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func (s *Service) CreateDeployment(ctx context.Context, req *connect.Request[ftl
logger.Errorf(err, "Invalid module schema")
return nil, fmt.Errorf("%s: %w", "invalid module schema", err)
}
module, err = s.validateWholeSchema(ctx, module)
module, err = s.validateModuleSchema(ctx, module)
if err != nil {
logger.Errorf(err, "Invalid module schema")
return nil, fmt.Errorf("%s: %w", "invalid module schema", err)
Expand All @@ -732,16 +732,17 @@ func (s *Service) CreateDeployment(ctx context.Context, req *connect.Request[ftl
return connect.NewResponse(&ftlv1.CreateDeploymentResponse{DeploymentKey: dname.String()}), nil
}

// Load schemas for existing modules, combine with our new one, and validate as a whole.
func (s *Service) validateWholeSchema(ctx context.Context, module *schema.Module) (*schema.Module, error) {
// Load schemas for existing modules, combine with our new one, and validate the new module in the context
// of the whole schema.
func (s *Service) validateModuleSchema(ctx context.Context, module *schema.Module) (*schema.Module, error) {
existingModules, err := s.dal.GetActiveDeploymentSchemas(ctx)
if err != nil {
return nil, fmt.Errorf("%s: %w", "could not get existing schemas", err)
}
schemaMap := ftlmaps.FromSlice(existingModules, func(el *schema.Module) (string, *schema.Module) { return el.Name, el })
schemaMap[module.Name] = module
fullSchema := &schema.Schema{Modules: maps.Values(schemaMap)}
schema, err := schema.Validate(fullSchema)
schema, err := schema.ValidateModuleInSchema(fullSchema, optional.Some[*schema.Module](module))
if err != nil {
return nil, fmt.Errorf("%s: %w", "invalid schema", err)
}
Expand Down Expand Up @@ -1133,7 +1134,7 @@ func (s *Service) getActiveSchema(ctx context.Context) (*schema.Schema, error) {
if err != nil {
return nil, err
}
return schema.Validate(&schema.Schema{
return schema.ValidateSchema(&schema.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *schema.Module {
return d.Schema
}),
Expand Down
4 changes: 2 additions & 2 deletions backend/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func ParseString(filename, input string) (*Schema, error) {
if err != nil {
return nil, err
}
return Validate(mod)
return ValidateSchema(mod)
}

func ParseModuleString(filename, input string) (*Module, error) {
Expand All @@ -184,7 +184,7 @@ func Parse(filename string, r io.Reader) (*Schema, error) {
if err != nil {
return nil, err
}
return Validate(mod)
return ValidateSchema(mod)
}

func ParseModule(filename string, r io.Reader) (*Module, error) {
Expand Down
2 changes: 1 addition & 1 deletion backend/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@ func FromProto(s *schemapb.Schema) (*Schema, error) {
schema := &Schema{
Modules: modules,
}
return Validate(schema)
return ValidateSchema(schema)
}
2 changes: 1 addition & 1 deletion backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Module
func TestParserRoundTrip(t *testing.T) {
actual, err := ParseString("", testSchema.String())
assert.NoError(t, err, "%s", testSchema.String())
actual, err = Validate(actual)
actual, err = ValidateSchema(actual)
assert.NoError(t, err)
assert.Equal(t, Normalise(testSchema), Normalise(actual))
}
Expand Down
16 changes: 13 additions & 3 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ var (
//
// This is useful for testing.
func MustValidate(schema *Schema) *Schema {
clone, err := Validate(schema)
clone, err := ValidateSchema(schema)
if err != nil {
panic(err)
}
return clone
}

// Validate clones, normalises and semantically valies a schema.
func Validate(schema *Schema) (*Schema, error) {
// ValidateSchema clones, normalises and semantically validates a schema.
func ValidateSchema(schema *Schema) (*Schema, error) {
return ValidateModuleInSchema(schema, optional.None[*Module]())
}

// ValidateModuleInSchema clones and normalises a schema and semantically validates a single module within it.
// If no module is provided, all modules in the schema are validated.
func ValidateModuleInSchema(schema *Schema, m optional.Option[*Module]) (*Schema, error) {
schema = dc.DeepCopy(schema)
modules := map[string]bool{}
merr := []error{}
Expand Down Expand Up @@ -81,6 +87,10 @@ func Validate(schema *Schema) (*Schema, error) {
if module.Name == "builtin" {
continue
}
if v, ok := m.Get(); ok && v.Name != module.Name {
// Don't validate other modules when validating a single module.
continue
}

if _, seen := modules[module.Name]; seen {
merr = append(merr, errorf(module, "duplicate module %q", module.Name))
Expand Down
Loading