Files
sqlite3/tests/wal_test.go

141 lines
2.7 KiB
Go
Raw Normal View History

package tests
import (
"path/filepath"
"testing"
"github.com/ncruces/go-sqlite3"
2024-06-10 00:24:15 +01:00
"github.com/ncruces/go-sqlite3/driver"
2024-04-16 17:33:48 +01:00
_ "github.com/ncruces/go-sqlite3/embed"
2024-06-02 10:33:20 +01:00
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
2024-04-13 13:43:24 +01:00
"github.com/ncruces/go-sqlite3/vfs"
)
func TestWAL_enter_exit(t *testing.T) {
2024-05-02 23:22:43 +01:00
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
t.Parallel()
file := filepath.Join(t.TempDir(), "test.db")
db, err := sqlite3.Open(file)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if !vfs.SupportsSharedMemory {
2024-05-07 16:34:51 +01:00
err = db.Exec(`PRAGMA locking_mode=exclusive`)
if err != nil {
t.Fatal(err)
}
}
err = db.Exec(`
CREATE TABLE test (col);
2024-05-07 16:34:51 +01:00
PRAGMA journal_mode=wal;
SELECT * FROM test;
2024-05-07 16:34:51 +01:00
PRAGMA journal_mode=delete;
SELECT * FROM test;
2024-05-07 16:34:51 +01:00
PRAGMA journal_mode=wal;
SELECT * FROM test;
`)
if err != nil {
t.Fatal(err)
}
}
2024-04-12 14:57:13 +01:00
2024-04-13 13:43:24 +01:00
func TestWAL_readonly(t *testing.T) {
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}
t.Parallel()
2024-06-10 00:24:15 +01:00
tmp := filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))
db1, err := driver.Open("file:" + tmp + "?_pragma=journal_mode(wal)&_txlock=immediate")
2024-04-13 13:43:24 +01:00
if err != nil {
t.Fatal(err)
}
2024-06-10 00:24:15 +01:00
defer db1.Close()
2024-04-13 13:43:24 +01:00
db2, err := driver.Open("file:" + tmp + "?_pragma=journal_mode(wal)&mode=ro")
2024-04-13 13:43:24 +01:00
if err != nil {
t.Fatal(err)
}
2024-06-10 00:24:15 +01:00
defer db2.Close()
// Create the table using the first (writable) connection.
_, err = db1.Exec(`
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.QueryRow("SELECT name FROM t").Scan(&name)
if err != nil {
t.Fatal(err)
}
if name != "alice" {
t.Errorf("got %q want alice", name)
}
2024-04-13 13:43:24 +01:00
2024-06-10 00:24:15 +01:00
// Update table.
_, err = db1.Exec(`
DELETE FROM t;
INSERT INTO t(name) VALUES('bob');
`)
2024-04-13 13:43:24 +01:00
if err != nil {
t.Fatal(err)
}
2024-06-10 00:24:15 +01:00
// Select the data using the second (readonly) connection.
err = db2.QueryRow("SELECT name FROM t").Scan(&name)
if err != nil {
t.Fatal(err)
}
if name != "bob" {
t.Errorf("got %q want bob", name)
2024-04-13 13:43:24 +01:00
}
}
2024-10-24 00:22:20 +01:00
func TestConn_WALCheckpoint(t *testing.T) {
2024-05-02 23:22:43 +01:00
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
2024-04-12 14:57:13 +01:00
t.Parallel()
file := filepath.Join(t.TempDir(), "test.db")
db, err := sqlite3.Open(file)
if err != nil {
t.Fatal(err)
}
defer db.Close()
2024-10-24 00:22:20 +01:00
err = db.WALAutoCheckpoint(1000)
2024-04-12 14:57:13 +01:00
if err != nil {
t.Fatal(err)
}
2024-10-24 00:22:20 +01:00
db.WALHook(func(db *sqlite3.Conn, schema string, pages int) error {
log, ckpt, err := db.WALCheckpoint(schema, sqlite3.CHECKPOINT_FULL)
2024-04-12 14:57:13 +01:00
t.Log(log, ckpt, err)
return err
})
err = db.Exec(`
2024-05-07 16:34:51 +01:00
PRAGMA locking_mode=exlusive;
PRAGMA journal_mode=wal;
2024-04-12 14:57:13 +01:00
CREATE TABLE test (col);
`)
if err != nil {
t.Fatal(err)
}
}