Skip to content

Commit 8ed1d94

Browse files
authored
fix(sqlite-migrate): Use int when getting pk column information (#6848)
#### Summary SQLite migration always fails on tables that have multiple PKs. The reason is that `pk` in `table_info` is either `0` for non PKs or the 1 based index of the column within the PKs (usually 1 for a single PK, but can be 2, 3, etc.) See https://www.sqlite.org/pragma.html#pragma_table_info. If you remove the fix you'll get the following error: ``` failed to get table table_1 columns types: sql: Scan error on column index 5, name "pk": sql/driver: couldn't convert 2 into type bool ``` <!--
1 parent 8c301f8 commit 8ed1d94

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

plugins/destination/sqlite/client/client_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package client
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/cloudquery/plugin-sdk/plugins/destination"
8+
"github.com/cloudquery/plugin-sdk/schema"
9+
"github.com/cloudquery/plugin-sdk/specs"
10+
"github.com/rs/zerolog"
711
)
812

913
func TestPlugin(t *testing.T) {
@@ -15,3 +19,33 @@ func TestPlugin(t *testing.T) {
1519
},
1620
destination.PluginTestSuiteTests{})
1721
}
22+
23+
func TestPluginMigrateMultiplePKs(t *testing.T) {
24+
table := schema.Table{
25+
Name: "table_1",
26+
Columns: []schema.Column{
27+
{Name: "id", Type: schema.TypeUUID, CreationOptions: schema.ColumnCreationOptions{PrimaryKey: true}},
28+
{Name: "name", Type: schema.TypeString, CreationOptions: schema.ColumnCreationOptions{PrimaryKey: true}},
29+
{Name: "age", Type: schema.TypeInt},
30+
},
31+
}
32+
p := destination.NewPlugin("sqlite", "development", New)
33+
ctx := context.Background()
34+
35+
spec := Spec{
36+
ConnectionString: ":memory:",
37+
}
38+
39+
// Init the plugin so we can call migrate
40+
if err := p.Init(ctx, zerolog.Logger{}, specs.Destination{Name: "cq_test_migrate_multiple_pks", Spec: spec}); err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
if err := p.Migrate(ctx, []*schema.Table{&table}); err != nil {
45+
t.Fatal(err)
46+
}
47+
48+
if err := p.Migrate(ctx, []*schema.Table{&table}); err != nil {
49+
t.Fatal(err)
50+
}
51+
}

plugins/destination/sqlite/client/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type columnInfo struct {
2020
typ string
2121
notNull bool
2222
defaultValue any
23-
pk bool
23+
pk int
2424
}
2525

2626
type tableInfo struct {

0 commit comments

Comments
 (0)