Skip to content
Merged
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 @@ -106,16 +106,13 @@ type Generic struct {
}

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

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

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

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

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

// createBloatedDB creates a temporary SQLite database in WAL mode with the kine
Expand Down Expand Up @@ -148,3 +154,25 @@ func TestSetupVacuumDisabledPreservesFileSize(t *testing.T) {

t.Logf("No VACUUM: freelist pages before=%d, after=%d", freelistBefore, freelistAfter)
}

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") + "?" + DefaultParams,
// 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