Skip to content

Commit c52fcff

Browse files
author
Hein
committed
Preload fixes
1 parent ce106fa commit c52fcff

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

.vscode/tasks.json

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,63 @@
2424
"type": "go",
2525
"label": "go: test workspace",
2626
"command": "test",
27-
2827
"options": {
29-
"env": {
30-
"CGO_ENABLED": "0"
31-
},
32-
"cwd": "${workspaceFolder}/bin",
28+
"cwd": "${workspaceFolder}"
3329
},
3430
"args": [
35-
"../..."
31+
"-v",
32+
"-race",
33+
"-coverprofile=coverage.out",
34+
"-covermode=atomic",
35+
"./..."
3636
],
3737
"problemMatcher": [
3838
"$go"
3939
],
40-
"group": "build",
41-
40+
"group": {
41+
"kind": "test",
42+
"isDefault": true
43+
},
44+
"presentation": {
45+
"reveal": "always",
46+
"panel": "new"
47+
}
48+
},
49+
{
50+
"type": "shell",
51+
"label": "go: vet workspace",
52+
"command": "go vet ./...",
53+
"options": {
54+
"cwd": "${workspaceFolder}"
55+
},
56+
"problemMatcher": [
57+
"$go"
58+
],
59+
"group": "test"
4260
},
61+
{
62+
"type": "shell",
63+
"label": "go: lint workspace",
64+
"command": "golangci-lint run --timeout=5m",
65+
"options": {
66+
"cwd": "${workspaceFolder}"
67+
},
68+
"problemMatcher": [],
69+
"group": "test"
70+
},
71+
{
72+
"type": "shell",
73+
"label": "go: full test suite",
74+
"dependsOrder": "sequence",
75+
"dependsOn": [
76+
"go: vet workspace",
77+
"go: test workspace"
78+
],
79+
"problemMatcher": [],
80+
"group": {
81+
"kind": "test",
82+
"isDefault": false
83+
}
84+
}
4385
]
4486
}

pkg/common/validation.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ func (v *ColumnValidator) ValidateRequestOptions(options RequestOptions) error {
183183
}
184184

185185
// Validate Preload columns (if specified)
186-
for _, preload := range options.Preload {
186+
for idx := range options.Preload {
187+
preload := options.Preload[idx]
187188
// Note: We don't validate the relation name itself, as it's a relationship
188189
// Only validate columns if specified for the preload
189190
if err := v.ValidateColumns(preload.Columns); err != nil {
@@ -239,7 +240,8 @@ func (v *ColumnValidator) FilterRequestOptions(options RequestOptions) RequestOp
239240

240241
// Filter Preload columns
241242
validPreloads := make([]PreloadOption, 0, len(options.Preload))
242-
for _, preload := range options.Preload {
243+
for idx := range options.Preload {
244+
preload := options.Preload[idx]
243245
filteredPreload := preload
244246
filteredPreload.Columns = v.FilterValidColumns(preload.Columns)
245247
filteredPreload.OmitColumns = v.FilterValidColumns(preload.OmitColumns)

pkg/reflection/model_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func GetPrimaryKeyName(model any) string {
1818
if reflect.TypeOf(model) == nil {
1919
return ""
2020
}
21-
//If we are given a string model name, look up the model
21+
// If we are given a string model name, look up the model
2222
if reflect.TypeOf(model).Kind() == reflect.String {
2323
name := model.(string)
2424
m, err := modelregistry.GetModelByName(name)

pkg/resolvespec/handler.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
699699
schema := GetSchema(ctx)
700700
entity := GetEntity(ctx)
701701
tableName := GetTableName(ctx)
702+
model := GetModel(ctx)
702703

703704
logger.Info("Deleting records from %s.%s", schema, entity)
704705

@@ -711,7 +712,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
711712
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
712713
for _, itemID := range v {
713714

714-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
715+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
715716
if _, err := query.Exec(ctx); err != nil {
716717
return fmt.Errorf("failed to delete record %s: %w", itemID, err)
717718
}
@@ -750,7 +751,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
750751
continue // Skip items without ID
751752
}
752753

753-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
754+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
754755
result, err := query.Exec(ctx)
755756
if err != nil {
756757
return fmt.Errorf("failed to delete record %v: %w", itemID, err)
@@ -775,7 +776,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
775776
err := h.db.RunInTransaction(ctx, func(tx common.Database) error {
776777
for _, item := range v {
777778
if itemID, ok := item["id"]; ok && itemID != nil {
778-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
779+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
779780
result, err := query.Exec(ctx)
780781
if err != nil {
781782
return fmt.Errorf("failed to delete record %v: %w", itemID, err)
@@ -809,7 +810,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
809810
return
810811
}
811812

812-
query := h.db.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), id)
813+
query := h.db.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), id)
813814

814815
result, err := query.Exec(ctx)
815816
if err != nil {
@@ -1118,7 +1119,8 @@ func (h *Handler) applyPreloads(model interface{}, query common.SelectQuery, pre
11181119
return query
11191120
}
11201121

1121-
for _, preload := range preloads {
1122+
for idx := range preloads {
1123+
preload := preloads[idx]
11221124
logger.Debug("Processing preload for relation: %s", preload.Relation)
11231125
relInfo := h.getRelationshipInfo(modelType, preload.Relation)
11241126
if relInfo == nil {

pkg/restheadspec/handler.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
260260
query = query.ColumnExpr("(?) AS "+colName, colExpr)
261261
for colIndex := range options.Columns {
262262
if options.Columns[colIndex] == colName {
263-
//Remove the computed column from the selected columns to avoid duplication
263+
// Remove the computed column from the selected columns to avoid duplication
264264
options.Columns = append(options.Columns[:colIndex], options.Columns[colIndex+1:]...)
265265
break
266266
}
@@ -274,7 +274,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
274274
query = query.ColumnExpr("(?) AS "+cu.Name, cu.Expression)
275275
for colIndex := range options.Columns {
276276
if options.Columns[colIndex] == cu.Name {
277-
//Remove the computed column from the selected columns to avoid duplication
277+
// Remove the computed column from the selected columns to avoid duplication
278278
options.Columns = append(options.Columns[:colIndex], options.Columns[colIndex+1:]...)
279279
break
280280
}
@@ -305,13 +305,13 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
305305
}
306306
// Note: Expand would require JOIN implementation
307307
// For now, we'll use Preload as a fallback
308-
//query = query.Preload(expand.Relation)
308+
// query = query.Preload(expand.Relation)
309309
if options.Preload == nil {
310310
options.Preload = make([]common.PreloadOption, 0)
311311
}
312312
skip := false
313-
for _, existing := range options.Preload {
314-
if existing.Relation == expand.Relation {
313+
for idx := range options.Preload {
314+
if options.Preload[idx].Relation == expand.Relation {
315315
skip = true
316316
continue
317317
}
@@ -327,7 +327,8 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
327327
}
328328

329329
// Apply preloading
330-
for _, preload := range options.Preload {
330+
for idx := range options.Preload {
331+
preload := options.Preload[idx]
331332
logger.Debug("Applying preload: %s", preload.Relation)
332333
query = query.PreloadRelation(preload.Relation, func(sq common.SelectQuery) common.SelectQuery {
333334
if len(preload.OmitColumns) > 0 {
@@ -972,7 +973,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
972973
continue
973974
}
974975

975-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
976+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
976977

977978
result, err := query.Exec(ctx)
978979
if err != nil {
@@ -1039,7 +1040,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
10391040
continue
10401041
}
10411042

1042-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
1043+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
10431044
result, err := query.Exec(ctx)
10441045
if err != nil {
10451046
return fmt.Errorf("failed to delete record %v: %w", itemID, err)
@@ -1090,7 +1091,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
10901091
continue
10911092
}
10921093

1093-
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), itemID)
1094+
query := tx.NewDelete().Table(tableName).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), itemID)
10941095
result, err := query.Exec(ctx)
10951096
if err != nil {
10961097
return fmt.Errorf("failed to delete record %v: %w", itemID, err)
@@ -1150,7 +1151,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
11501151
return
11511152
}
11521153

1153-
query = query.Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(tableName))), id)
1154+
query = query.Where(fmt.Sprintf("%s = ?", common.QuoteIdent(reflection.GetPrimaryKeyName(model))), id)
11541155

11551156
// Execute BeforeScan hooks - pass query chain so hooks can modify it
11561157
hookCtx.Query = query

0 commit comments

Comments
 (0)