Skip to content

Commit 5f8a197

Browse files
authored
fix: Fix SQLite migration logic (#6372)
There was a bug in the query that checks whether a table exists - it always found the table to not exist because `?` was being quoted. We also now select only the columns defined in the table schema, rather than all columns. This will allow https://github.com/cloudquery/plugin-sdk/pull/574/files to pass.
1 parent 491941a commit 5f8a197

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

plugins/destination/sqlite/client/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
const (
13-
isTableExistSQL = "SELECT count(name) FROM sqlite_master WHERE type='table' AND name='?';"
13+
isTableExistSQL = "SELECT count(name) FROM sqlite_master WHERE type='table' AND name=?;"
1414

1515
// https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
1616
sqlTableInfo = "PRAGMA table_info('%s');"

plugins/destination/sqlite/client/read.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"strings"
78

89
"github.com/cloudquery/plugin-sdk/schema"
910
)
1011

1112
const (
12-
readSQL = `SELECT * FROM "%s" WHERE _cq_source_name = $1 order by _cq_sync_time asc`
13+
readSQL = `SELECT %s FROM "%s" WHERE _cq_source_name = $1 order by _cq_sync_time asc`
1314
)
1415

1516
func (*Client) createResultsArray(table *schema.Table) []any {
@@ -73,7 +74,12 @@ func (*Client) createResultsArray(table *schema.Table) []any {
7374
}
7475

7576
func (c *Client) Read(ctx context.Context, table *schema.Table, sourceName string, res chan<- []any) error {
76-
rows, err := c.db.Query(fmt.Sprintf(readSQL, table.Name), sourceName)
77+
colNames := make([]string, 0, len(table.Columns))
78+
for _, col := range table.Columns {
79+
colNames = append(colNames, `"`+col.Name+`"`)
80+
}
81+
cols := strings.Join(colNames, ", ")
82+
rows, err := c.db.Query(fmt.Sprintf(readSQL, cols, table.Name), sourceName)
7783
if err != nil {
7884
return err
7985
}

0 commit comments

Comments
 (0)