Files
migrate/database/sqlite3/sqlite3_test.go
T
zeekay daee3030ce State a version once
A version written into a manifest is written into every manifest that wants it,
which is how eslint came to exist in a hundred and four forms across this estate
and how a package came to resolve twice at run time. The catalog moves the number
out of the manifest: a manifest names the package, the workspace names the
version.

Four are held below their latest because the newer release cannot be used, and
the reason is recorded rather than the version alone: @noble/hashes 2 renamed
every subpath the web3 stack imports, @splinetool/runtime 2 depends on a package
that was never published, @dynamic-labs 5.4.1 the same, and react-resizable-panels
3 points its types condition at the bundle.
2026-08-29 13:46:58 -07:00

134 lines
3.0 KiB
Go

package sqlite3
import (
"database/sql"
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/golang-migrate/migrate/v4"
dt "github.com/golang-migrate/migrate/v4/database/testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/hanzoai/csqlite"
)
func Test(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);"))
}
func TestMigrate(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
driver, err := WithInstance(db, &Config{})
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
dt.TestMigrate(t, m)
}
func TestMigrationTable(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
config := &Config{
MigrationsTable: "my_migration_table",
}
driver, err := WithInstance(db, config)
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
t.Log("UP")
err = m.Up()
if err != nil {
t.Fatal(err)
}
_, err = db.Query(fmt.Sprintf("SELECT * FROM %s", config.MigrationsTable))
if err != nil {
t.Fatal(err)
}
}
func TestNoTxWrap(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
// An explicit BEGIN statement would ordinarily fail without x-no-tx-wrap.
// (Transactions in sqlite may not be nested.)
dt.Test(t, d, []byte("BEGIN; CREATE TABLE t (Qty int, Name string); COMMIT;"))
}
func TestNoTxWrapInvalidValue(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
_, err := p.Open(addr)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "x-no-tx-wrap")
assert.Contains(t, err.Error(), "invalid syntax")
}
}
func TestMigrateWithDirectoryNameContainsWhitespaces(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "sqlite3.db")
t.Logf("DB path : %s\n", dbPath)
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://file:%s", dbPath)
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);"))
}