Skip to content

Commit

Permalink
chore: relationship fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Feb 20, 2025
1 parent 4f0bc9c commit 969cee4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
4 changes: 4 additions & 0 deletions migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestMigrations(t *testing.T) {
}()

for _, testCase := range testCases {
if testCase.Name() != "indexes_added_inputs.txt" {
continue
}

t.Run(strings.TrimSuffix(testCase.Name(), ".txt"), func(t *testing.T) {
// Make a database name for this test
re := regexp.MustCompile(`[^\w]`)
Expand Down
38 changes: 22 additions & 16 deletions migrations/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,28 @@ func createIndexStmts(schema *proto.Schema, existingIndexes []*IndexRow) []strin
indexedFields := []*proto.Field{}
for _, model := range schema.Models {
for _, action := range model.Actions {
if action.Type != proto.ActionType_ACTION_TYPE_LIST {
continue
}

message := proto.FindWhereInputMessage(schema, action.Name)
if message == nil {
continue
}

// Find fields used as required inputs
indexedFields = append(indexedFields, findIndexableInputFields(schema, model, message)...)
fieldsToIndex := findIndexableInputFields(schema, model, message)
for _, field := range fieldsToIndex {
// They could have been added already if used as another action's input
if !lo.Contains(indexedFields, field) {
indexedFields = append(indexedFields, field)
}
}

// Find fields used as facets
for _, facet := range action.Facets {
field := model.FindField(facet)

// They could have been added already as an input
if !lo.Contains(indexedFields, field) {
indexedFields = append(indexedFields, field)
}
Expand Down Expand Up @@ -636,35 +646,31 @@ func indexName(modelName string, fieldName string) string {
func findIndexableInputFields(schema *proto.Schema, model *proto.Model, message *proto.Message) []*proto.Field {
indexedFields := []*proto.Field{}

for _, msgField := range message.Fields {
m := model
for _, input := range message.Fields {
field := proto.FindField(schema.Models, model.Name, input.Name)

// Skip optional inputs
if msgField.Optional {
if input.Optional {
continue
}

if msgField.Type.Type == proto.Type_TYPE_MESSAGE {
nestedMsg := schema.FindMessage(msgField.Type.MessageName.Value)
indexedFields = append(indexedFields, findIndexableInputFields(schema, model, nestedMsg)...)
if !input.IsModelField() && input.Type.Type == proto.Type_TYPE_MESSAGE {
messageModel := schema.FindModel(field.Type.ModelName.Value)
nestedMsg := schema.FindMessage(input.Type.MessageName.Value)
indexedFields = append(indexedFields, findIndexableInputFields(schema, messageModel, nestedMsg)...)
}

// Skip indexing on optional fields
if msgField.Optional {
if input.Optional {
continue
}

// Skip inputs which don't correlate to a model field
if len(msgField.Target) == 0 {
if len(input.Target) == 0 {
continue
}

// If this is a nested relationship query input, then we need to find the model in targets
if len(msgField.Target) > 1 {
m = schema.FindModel(strcase.ToCamel(msgField.Target[len(msgField.Target)-2]))
}

f := m.FindField(msgField.Target[len(msgField.Target)-1])
f := model.FindField(input.Target[len(input.Target)-1])

indexedFields = append(indexedFields, f)
}
Expand Down
18 changes: 17 additions & 1 deletion migrations/testdata/indexes_added_inputs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ enum Category {
Books
}

model Product {
fields {
order Order
name Text
}
}

===

model Order {
Expand All @@ -29,10 +36,11 @@ model Order {
customer Customer
category Category?
tag Text
products Product[]
}

actions {
list listOrders(customer.id, customer.name, category, tag?) {
list listOrders(customer.id, customer.name, category, tag?, products.name) {
@facet(price, quantity, category)
}
}
Expand All @@ -44,6 +52,13 @@ model Customer {
}
}

model Product {
fields {
order Order
name Text
}
}

enum Category {
Food
Electronics
Expand All @@ -54,6 +69,7 @@ enum Category {

CREATE INDEX "customer__name__idx" ON "customer" ("name");
CREATE INDEX "order__category__idx" ON "order" ("category");
CREATE INDEX "product__name__idx" ON "product" ("name");
CREATE INDEX "order__price__idx" ON "order" ("price");
CREATE INDEX "order__quantity__idx" ON "order" ("quantity");

Expand Down

0 comments on commit 969cee4

Please sign in to comment.