2024-04-10 13:15:36 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-13 13:43:24 +01:00
|
|
|
"os"
|
2024-04-10 13:15:36 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/ncruces/go-sqlite3"
|
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"
|
2024-04-10 13:15:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestWAL_enter_exit(t *testing.T) {
|
2024-05-02 23:22:43 +01:00
|
|
|
if !vfs.SupportsFileLocking {
|
|
|
|
|
t.Skip("skipping without locks")
|
|
|
|
|
}
|
2024-04-10 13:15:36 +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-04-24 15:49:45 +01:00
|
|
|
if !vfs.SupportsSharedMemory {
|
2024-05-07 16:34:51 +01:00
|
|
|
err = db.Exec(`PRAGMA locking_mode=exclusive`)
|
2024-04-24 15:49:45 +01:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 13:15:36 +01:00
|
|
|
err = db.Exec(`
|
|
|
|
|
CREATE TABLE test (col);
|
2024-05-07 16:34:51 +01:00
|
|
|
PRAGMA journal_mode=wal;
|
2024-04-10 13:15:36 +01:00
|
|
|
SELECT * FROM test;
|
2024-05-07 16:34:51 +01:00
|
|
|
PRAGMA journal_mode=delete;
|
2024-04-10 13:15:36 +01:00
|
|
|
SELECT * FROM test;
|
2024-05-07 16:34:51 +01:00
|
|
|
PRAGMA journal_mode=wal;
|
2024-04-10 13:15:36 +01:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
tmp := filepath.Join(t.TempDir(), "test.db")
|
2024-05-07 16:34:51 +01:00
|
|
|
err := os.WriteFile(tmp, walDB, 0666)
|
2024-04-13 13:43:24 +01:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := sqlite3.OpenFlags(tmp, sqlite3.OPEN_READONLY)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
|
|
stmt, _, err := db.Prepare(`SELECT * FROM sqlite_master`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
|
|
if stmt.Step() {
|
|
|
|
|
t.Error("want no rows")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-12 14:57:13 +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()
|
|
|
|
|
|
|
|
|
|
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(`
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|