Files
hanzo-devandClaude Opus 4.8 764539773a feat: canonical Hanzo SQLite — rename module + native encrypted hanzovfs
Fork of ncruces/go-sqlite3 (MIT, retained). Renames module to
github.com/hanzoai/sqlite3 (wasm blob stays upstream ncruces/go-sqlite3-wasm)
and adds the hanzovfs package: a pure-Go, no-FUSE SQLite VFS that seals pages
with ChaCha20-Poly1305 (per-tenant DEK, HIP-0302) for per-tenant SQLite ⇒
hanzoai/vfs ⇒ S3.

Deprecates modernc.org/sqlite (no custom-VFS hook) and direct ncruces use.
Benchmarked 3.6x faster writes / ~92x faster point-reads than FUSE; encryption ~free.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 20:32:43 +00:00

141 lines
2.8 KiB
Go

package tests
import (
"path/filepath"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs"
)
func TestWAL_enter_exit(t *testing.T) {
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
t.Parallel()
file := filepath.Join(t.TempDir(), "test.db")
db, err := sqlite3.OpenContext(testcfg.Context(t), file)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if !vfs.SupportsSharedMemory {
err = db.Exec(`PRAGMA locking_mode=exclusive`)
if err != nil {
t.Fatal(err)
}
}
err = db.Exec(`
CREATE TABLE test (col);
PRAGMA journal_mode=wal;
SELECT * FROM test;
PRAGMA journal_mode=delete;
SELECT * FROM test;
PRAGMA journal_mode=wal;
SELECT * FROM test;
`)
if err != nil {
t.Fatal(err)
}
}
func TestWAL_readonly(t *testing.T) {
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}
t.Parallel()
ctx := testcfg.Context(t)
tmp := filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))
db1, err := driver.Open("file:" + tmp + "?_pragma=journal_mode(wal)&_txlock=immediate")
if err != nil {
t.Fatal(err)
}
defer db1.Close()
db2, err := driver.Open("file:" + tmp + "?_pragma=journal_mode(wal)&mode=ro")
if err != nil {
t.Fatal(err)
}
defer db2.Close()
// Create the table using the first (writable) connection.
_, err = db1.ExecContext(ctx, `
CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO t(name) VALUES('alice');
`)
if err != nil {
t.Fatal(err)
}
// Select the data using the second (readonly) connection.
var name string
err = db2.QueryRowContext(ctx, `SELECT name FROM t`).Scan(&name)
if err != nil {
t.Fatal(err)
}
if name != "alice" {
t.Errorf("got %q want alice", name)
}
// Update table.
_, err = db1.ExecContext(ctx, `
DELETE FROM t;
INSERT INTO t(name) VALUES('bob');
`)
if err != nil {
t.Fatal(err)
}
// Select the data using the second (readonly) connection.
err = db2.QueryRowContext(ctx, `SELECT name FROM t`).Scan(&name)
if err != nil {
t.Fatal(err)
}
if name != "bob" {
t.Errorf("got %q want bob", name)
}
}
func TestConn_WALCheckpoint(t *testing.T) {
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
t.Parallel()
file := filepath.Join(t.TempDir(), "test.db")
db, err := sqlite3.OpenContext(testcfg.Context(t), file)
if err != nil {
t.Fatal(err)
}
defer db.Close()
err = db.WALAutoCheckpoint(1000)
if err != nil {
t.Fatal(err)
}
db.WALHook(func(db *sqlite3.Conn, schema string, pages int) error {
log, ckpt, err := db.WALCheckpoint(schema, sqlite3.CHECKPOINT_FULL)
t.Log(log, ckpt, err)
return err
})
err = db.Exec(`
PRAGMA locking_mode=exlusive;
PRAGMA journal_mode=wal;
CREATE TABLE test (col);
`)
if err != nil {
t.Fatal(err)
}
}