Skip to content
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
12 changes: 6 additions & 6 deletions mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (c client) process(ctx context.Context,
return fmt.Errorf("failed to get current schema: %w", err)
}

// Get the type name from the object
schemaObjVal := reflect.ValueOf(schemaObj)
if schemaObjVal.Kind() == reflect.Ptr {
schemaObjVal = schemaObjVal.Elem()
}
typeName := schemaObjVal.Type().Name()
// Resolve the Dgraph type name the same way mutations and schema
// generation do (the DType tag, falling back to the Go struct name).
// Using the raw Go struct name here would reject types whose Dgraph
// name differs from the struct name, e.g. a `migrationLock` struct
// declared as `dgraph:"MigrationLock"`.
typeName := getNodeType(schemaObj)

// When AutoSchema is disabled, validate that required schema exists
// Fail if user schema for the type doesn't exist, even if only system schema exists
Expand Down
43 changes: 43 additions & 0 deletions mutate_dtype_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package modusgraph_test

import (
"context"
"testing"

mg "github.com/matthewmcneely/modusgraph"
"github.com/stretchr/testify/require"
)

// lockLikeNode mirrors modusGraph-migrate's migrationLock: its Dgraph type
// name (declared via the DType tag, "DTypeMismatchType") differs from its Go
// struct name ("lockLikeNode").
type lockLikeNode struct {
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty" dgraph:"DTypeMismatchType"`
Name string `json:"dtypeMismatchName,omitempty" dgraph:"predicate=dtype_mismatch_name index=exact"`
}

// TestMutateValidation_ResolvesDTypeName guards the AutoSchema-disabled mutation
// path: it must resolve the type name from the DType tag (the same way schema
// generation and getNodeType do), not from the raw Go struct name. Before the
// fix, Insert rejected the row with "schema validation failed: database schema
// does not contain type lockLikeNode" even though the schema held the (correctly
// named) "type DTypeMismatchType".
func TestMutateValidation_ResolvesDTypeName(t *testing.T) {
client, err := mg.NewClient("file://" + GetTempDir(t)) // autoSchema disabled by default
require.NoError(t, err)
defer client.Close()

ctx := context.Background()
// Register the type under its Dgraph name (derived from the DType tag).
require.NoError(t, client.UpdateSchema(ctx, &lockLikeNode{}))

// Insert must pass validation now that the type name resolves via DType.
err = client.Insert(ctx, &lockLikeNode{DType: []string{"DTypeMismatchType"}, Name: "x"})
require.NoError(t, err)
}
Loading