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
57 lines
1.1 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|