Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/drivers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,13 @@ func q(sql, param string, numbered bool) string {
}

func (d *Generic) Migrate(ctx context.Context) {
var (
count = 0
countKV = d.queryRow(ctx, "SELECT COUNT(*) FROM key_value")
countKine = d.queryRow(ctx, "SELECT COUNT(*) FROM kine")
)
var count int

countKV := d.queryRow(ctx, "SELECT COUNT(*) FROM key_value")
if err := countKV.Scan(&count); err != nil || count == 0 {
return
}

countKine := d.queryRow(ctx, "SELECT COUNT(*) FROM kine")
if err := countKine.Scan(&count); err != nil || count != 0 {
return
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/drivers/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build cgo
// +build cgo

package sqlite

import (
"context"
"path/filepath"
"sync"
"testing"
"time"

"github.com/k3s-io/kine/pkg/drivers"
"github.com/k3s-io/kine/pkg/drivers/generic"
)

func TestMigrateReleasesItsConnections(t *testing.T) {
var wg sync.WaitGroup
_, dialect, err := NewVariant(t.Context(), &wg, "sqlite3", &drivers.Config{
DataSourceName: filepath.Join(t.TempDir(), "test.db") + "?_journal=WAL&cache=shared&_busy_timeout=30000&_txlock=immediate",
// Use a single connection to catch any connection leaks in Migrate
ConnectionPoolConfig: generic.ConnectionPoolConfig{
MaxOpen: 1,
MaxIdle: 1,
},
})
if err != nil {
t.Fatalf("failed to create dialect: %v", err)
}
t.Cleanup(wg.Wait)

ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
if _, err := dialect.DB.ExecContext(ctx, `SELECT 1`); err != nil {
t.Fatalf("database unusable after Migrate: %v", err)
}
}
Loading