mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
Fix readonly shared memory (see #94).
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
"github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
|
||||
"github.com/ncruces/go-sqlite3/vfs"
|
||||
@@ -52,26 +52,55 @@ func TestWAL_readonly(t *testing.T) {
|
||||
}
|
||||
t.Parallel()
|
||||
|
||||
tmp := filepath.Join(t.TempDir(), "test.db")
|
||||
err := os.WriteFile(tmp, walDB, 0666)
|
||||
tmp := filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))
|
||||
|
||||
db1, err := driver.Open("file:"+tmp+"?_pragma=journal_mode(wal)&_txlock=immediate", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db1.Close()
|
||||
|
||||
db2, err := driver.Open("file:"+tmp+"?_pragma=journal_mode(wal)&mode=ro", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
db, err := sqlite3.OpenFlags(tmp, sqlite3.OPEN_READONLY)
|
||||
// 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)
|
||||
}
|
||||
defer db.Close()
|
||||
if name != "alice" {
|
||||
t.Errorf("got %q want alice", name)
|
||||
}
|
||||
|
||||
stmt, _, err := db.Prepare(`SELECT * FROM sqlite_master`)
|
||||
// Update table.
|
||||
_, err = db1.Exec(`
|
||||
DELETE FROM t;
|
||||
INSERT INTO t(name) VALUES('bob');
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
if stmt.Step() {
|
||||
t.Error("want no rows")
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user