Skip to content

Commit 4f0b312

Browse files
authored
fix: Error if both PKs and PK components are set (#2113)
#### Summary I spent too much time debugging a bug I had when I (meaning Cursor AI) auto complete `PrimaryKeyComponent: true` instead of `PrimaryKey: true` for me. Which means I had both PKs and PK components on the same table, causing `_cq_id` to only be calculated from the PK component, losing data when `pk_mode: cq-id-only` was enabled. See cloudquery/cloudquery-private@ef8762e This PR adds a validation to ensure only either ones are defined, or not defined at all. This has to happen after the `TransformTables` call since columns can be configured manually as well ---
1 parent abd2117 commit 4f0b312

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

transformers/tables.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func TransformTables(tables schema.Tables) error {
2626
if err := TransformTables(table.Relations); err != nil {
2727
return err
2828
}
29+
if len(table.PrimaryKeys()) > 0 && len(table.PrimaryKeyComponents()) > 0 {
30+
return fmt.Errorf("primary keys and primary key components cannot both be set for table %q", table.Name)
31+
}
2932
}
3033
return nil
3134
}

transformers/tables_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package transformers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/apache/arrow-go/v18/arrow"
7+
"github.com/cloudquery/plugin-sdk/v4/schema"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestTransformTablesErrorOnPKFieldsAndPKComponentFields(t *testing.T) {
12+
type testStructWithPKComponent struct {
13+
ID int `json:"id"`
14+
Name string `json:"name"`
15+
}
16+
17+
testTable := &schema.Table{
18+
Name: "test",
19+
Transform: TransformWithStruct(testStructWithPKComponent{}, WithPrimaryKeys("id")),
20+
Columns: []schema.Column{
21+
{Name: "name", Type: arrow.BinaryTypes.String, PrimaryKeyComponent: true},
22+
},
23+
}
24+
25+
err := TransformTables([]*schema.Table{testTable})
26+
require.Error(t, err, "primary keys and primary key components cannot both be set for table \"test\"")
27+
}

0 commit comments

Comments
 (0)