Skip to content

Commit d2cff6b

Browse files
authored
fix: Validate missing PK components (#2037)
#### Summary Fixes cloudquery/cloudquery-issues#3004 --- Use the following steps to ensure your PR is ready to be reviewed - [ ] Read the [contribution guidelines](../blob/main/CONTRIBUTING.md) 🧑‍🎓 - [ ] Run `go fmt` to format your code 🖊 - [ ] Lint your changes via `golangci-lint run` 🚨 (install golangci-lint [here](https://golangci-lint.run/usage/install/#local-installation)) - [ ] Update or add tests 🧪 - [ ] Ensure the status checks below are successful ✅
1 parent c025987 commit d2cff6b

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

schema/resource.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,21 @@ func (r *Resource) storeCQID(value uuid.UUID) error {
126126
// Validates that all primary keys have values.
127127
func (r *Resource) Validate() error {
128128
var missingPks []string
129+
var missingPKComponents []string
129130
for i, c := range r.Table.Columns {
130-
if c.PrimaryKey {
131-
if !r.data[i].IsValid() {
132-
missingPks = append(missingPks, c.Name)
133-
}
131+
switch {
132+
case c.PrimaryKey && !r.data[i].IsValid():
133+
missingPks = append(missingPks, c.Name)
134+
case c.PrimaryKeyComponent && !r.data[i].IsValid():
135+
missingPKComponents = append(missingPKComponents, c.Name)
134136
}
135137
}
136138
if len(missingPks) > 0 {
137139
return fmt.Errorf("missing primary key on columns: %v", missingPks)
138140
}
141+
if len(missingPKComponents) > 0 {
142+
return fmt.Errorf("missing primary key component on columns: %v", missingPKComponents)
143+
}
139144
return nil
140145
}
141146

schema/resource_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package schema
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/apache/arrow-go/v18/arrow"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestResource_Validate(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
resource *Resource
15+
valueSetter func(resource *Resource) error
16+
err error
17+
}{
18+
{
19+
name: "valid resource without primary keys or primary key components",
20+
resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String}}}, nil, nil),
21+
err: nil,
22+
},
23+
{
24+
name: "valid resource with primary keys",
25+
resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKey: true}}}, nil, nil),
26+
err: nil,
27+
valueSetter: func(resource *Resource) error {
28+
return resource.Set("col1", "test")
29+
},
30+
},
31+
{
32+
name: "valid resource with primary key components",
33+
resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKeyComponent: true}}}, nil, nil),
34+
err: nil,
35+
valueSetter: func(resource *Resource) error {
36+
return resource.Set("col1", "test")
37+
},
38+
},
39+
{
40+
name: "invalid resource with primary keys",
41+
resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKey: true}}}, nil, nil),
42+
err: errors.New(`missing primary key on columns: [col1]`),
43+
},
44+
{
45+
name: "invalid resource with primary key components",
46+
resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKeyComponent: true}}}, nil, nil),
47+
err: errors.New(`missing primary key component on columns: [col1]`),
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
if tt.valueSetter != nil {
53+
require.NoError(t, tt.valueSetter(tt.resource))
54+
}
55+
validationError := tt.resource.Validate()
56+
if tt.err == nil {
57+
require.NoError(t, validationError)
58+
} else {
59+
require.ErrorContains(t, validationError, tt.err.Error())
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)