Skip to content

Commit eff40e7

Browse files
authored
fix: PK Addition Order (#607)
#### Summary This will only add the default PKs once the `Transformers` have run and added any and all PKs <!-- Explain what problem this PR addresses --> ---
1 parent f548a54 commit eff40e7

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

plugins/source/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ func NewPlugin(name string, version string, tables []*schema.Table, newExecution
112112
metrics: &Metrics{TableClient: make(map[string]map[string]*TableClientMetrics)},
113113
caser: caser.New(),
114114
}
115-
addInternalColumns(p.tables)
116115
setParents(p.tables, nil)
117116
if err := transformTables(p.tables); err != nil {
118117
panic(err)
119118
}
119+
addInternalColumns(p.tables)
120120
if err := p.validate(); err != nil {
121121
panic(err)
122122
}

plugins/source/plugin_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66

77
"github.com/cloudquery/plugin-sdk/schema"
88
"github.com/cloudquery/plugin-sdk/specs"
9+
"github.com/cloudquery/plugin-sdk/transformers"
910
"github.com/google/go-cmp/cmp"
1011
"github.com/google/uuid"
1112
"github.com/rs/zerolog"
13+
"github.com/stretchr/testify/assert"
1214
"golang.org/x/sync/errgroup"
1315
)
1416

@@ -316,3 +318,39 @@ func TestIgnoredColumns(t *testing.T) {
316318
},
317319
}})
318320
}
321+
322+
var testTable struct {
323+
PrimaryKey string
324+
SecondaryKey string
325+
TertiaryKey string
326+
Quaternary string
327+
}
328+
329+
func TestNewPluginPrimaryKeys(t *testing.T) {
330+
testTransforms := []struct {
331+
transformerOptions []transformers.StructTransformerOption
332+
resultKeys []string
333+
}{
334+
{
335+
transformerOptions: []transformers.StructTransformerOption{transformers.WithPrimaryKeys("PrimaryKey")},
336+
resultKeys: []string{"primary_key"},
337+
},
338+
{
339+
transformerOptions: []transformers.StructTransformerOption{},
340+
resultKeys: []string{"_cq_id"},
341+
},
342+
}
343+
for _, tc := range testTransforms {
344+
tables := []*schema.Table{
345+
{
346+
Name: "test_table",
347+
Transform: transformers.TransformWithStruct(
348+
&testTable, tc.transformerOptions...,
349+
),
350+
},
351+
}
352+
353+
plugin := NewPlugin("testSourcePlugin", "1.0.0", tables, newTestExecutionClient)
354+
assert.Equal(t, tc.resultKeys, plugin.tables[0].PrimaryKeys())
355+
}
356+
}

0 commit comments

Comments
 (0)