diff --git a/internal/cmd/import.go b/internal/cmd/import.go index 8ce71fb9..c1109f04 100644 --- a/internal/cmd/import.go +++ b/internal/cmd/import.go @@ -57,10 +57,7 @@ func registerImportCmd(rootCmd *cobra.Command) { if err != nil { return err } - prefix, err := determinePrefixForSchema(cmd.Context(), cobrautil.MustGetString(cmd, "schema-definition-prefix"), client, nil) - if err != nil { - return err - } + prefix := cobrautil.MustGetString(cmd, "schema-definition-prefix") log.Trace().Msgf("using prefix: %s", prefix) return importCmdFunc(cmd, client, client, prefix, args[0]) }, diff --git a/internal/cmd/schema.go b/internal/cmd/schema.go index 031117fd..e92369ee 100644 --- a/internal/cmd/schema.go +++ b/internal/cmd/schema.go @@ -230,7 +230,7 @@ func schemaCopyInner(ctx context.Context, srcClient, destClient v1.SchemaService } log.Trace().Interface("response", readResp).Msg("read schema") - prefix, err := determinePrefixForSchema(ctx, definitionPrefix, nil, &readResp.SchemaText) + prefix, err := determinePrefixForSchema(definitionPrefix, readResp.SchemaText) if err != nil { return nil, err } @@ -284,12 +284,7 @@ func schemaWriteCmdImpl(cmd *cobra.Command, args []string, client v1.SchemaServi return errors.New("attempted to write empty schema") } - prefix, err := determinePrefixForSchema(cmd.Context(), cobrautil.MustGetString(cmd, "schema-definition-prefix"), client, nil) - if err != nil { - return err - } - - schemaText, err := rewriteSchema(cmd.Context(), string(schemaBytes), prefix) + schemaText, err := rewriteSchema(cmd.Context(), string(schemaBytes), cobrautil.MustGetString(cmd, "schema-definition-prefix")) if err != nil { return err } @@ -337,32 +332,20 @@ func rewriteSchema(ctx context.Context, existingSchemaText string, definitionPre // determinePrefixForSchema determines the prefix to be applied to a schema that will be written. // // If specifiedPrefix is non-empty, it is returned immediately. -// If existingSchema is non-nil, it is parsed for the prefix. -// Otherwise, the client is used to retrieve the existing schema (if any), and the prefix is retrieved from there. -func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, client v1.SchemaServiceClient, existingSchema *string) (string, error) { +// Otherwise, the existing schema is parsed for the prefix. +func determinePrefixForSchema(specifiedPrefix string, existingSchema string) (string, error) { if specifiedPrefix != "" { return specifiedPrefix, nil } - var schemaText string - if existingSchema != nil { - schemaText = *existingSchema - } else { - readSchemaText, err := commands.ReadSchema(ctx, client) - if err != nil { - return "", nil - } - schemaText = readSchemaText - } - // If there is no schema found, return the empty string. - if schemaText == "" { + if existingSchema == "" { return "", nil } // Otherwise, compile the schema and grab the prefixes of the namespaces defined. found, err := compiler.Compile( - compiler.InputSchema{Source: input.Source("schema"), SchemaString: schemaText}, + compiler.InputSchema{Source: input.Source("schema"), SchemaString: existingSchema}, compiler.AllowUnprefixedObjectType(), compiler.SkipValidation(), ) diff --git a/internal/cmd/schema_test.go b/internal/cmd/schema_test.go index 80d4b884..b26b470c 100644 --- a/internal/cmd/schema_test.go +++ b/internal/cmd/schema_test.go @@ -74,7 +74,7 @@ func TestDeterminePrefixForSchema(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - found, err := determinePrefixForSchema(t.Context(), test.specifiedPrefix, nil, &test.existingSchema) + found, err := determinePrefixForSchema(test.specifiedPrefix, test.existingSchema) require.NoError(t, err) require.Equal(t, test.expectedPrefix, found) }) @@ -450,11 +450,11 @@ definition resource { defer ctrl.Finish() mockClient := NewMockSchemaServiceClient(ctrl) - // ReadSchema is always called at least once + // ReadSchema must never be called: the schema is written as given, + // without inferring a definition prefix from the existing schema. mockClient.EXPECT(). ReadSchema(gomock.Any(), gomock.Any()). - Return(&v1.ReadSchemaResponse{SchemaText: ""}, nil). - MaxTimes(2) // sometimes we read for prefix determination + Times(0) // Set up WriteSchema expectations based on test case var receivedSchema string diff --git a/internal/commands/schema.go b/internal/commands/schema.go index e810ef46..13d19275 100644 --- a/internal/commands/schema.go +++ b/internal/commands/schema.go @@ -1,14 +1,10 @@ package commands import ( - "context" - "github.com/jzelinskie/cobrautil/v2" "github.com/jzelinskie/stringz" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" @@ -63,22 +59,3 @@ func schemaReadCmdFunc(cmd *cobra.Command, _ []string) error { console.Println(stringz.Join("\n\n", resp.SchemaText)) return nil } - -// ReadSchema calls read schema for the client and returns the schema found. -func ReadSchema(ctx context.Context, client v1.SchemaServiceClient) (string, error) { - request := &v1.ReadSchemaRequest{} - log.Trace().Interface("request", request).Msg("requesting schema read") - - resp, err := client.ReadSchema(ctx, request) - if err != nil { - errStatus, ok := status.FromError(err) - if !ok || errStatus.Code() != codes.NotFound { - return "", err - } - - log.Debug().Msg("no schema defined") - return "", nil - } - - return resp.SchemaText, nil -}