Files
upstream-mirror 0f4df23290
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 02:17:34 +05:00

57 lines
1.1 KiB
Go

package sqltest
import (
"database/sql"
"os"
"path/filepath"
"testing"
_ "github.com/ncruces/go-sqlite3/driver"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
_ "github.com/sqlc-dev/sqlc/internal/sqlite3ext"
)
func SQLite(t *testing.T, migrations []string) (*sql.DB, func()) {
t.Helper()
// For each test, pick a new database name at random.
source, err := os.CreateTemp("", "sqltest_sqlite_")
if err != nil {
t.Fatal(err)
}
if err := source.Close(); err != nil {
t.Fatal(err)
}
return CreateSQLiteDatabase(t, source.Name(), migrations)
}
func CreateSQLiteDatabase(t *testing.T, path string, migrations []string) (*sql.DB, func()) {
t.Helper()
t.Logf("open %s\n", path)
sdb, err := sql.Open("sqlite3", path)
if err != nil {
t.Fatal(err)
}
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
if _, err := sdb.Exec(string(blob)); err != nil {
t.Fatalf("%s: %s", filepath.Base(f), err)
}
}
return sdb, func() {
if _, err := os.Stat(path); err == nil {
os.Remove(path)
}
}
}