From ae2a61f847e10e6dd771ecd4e1c55e0421cdc7f9 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Fri, 28 Jan 2022 08:37:05 -0700 Subject: Add sqlite3_file_control() support This commit adds the SQLiteConn.FileControlInt() method which calls the underlying sqlite3_file_control() function with an int argument. This can be used for low-level operations on SQLite databases such as persisting the WAL file after database close. --- sqlite3_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'sqlite3_test.go') diff --git a/sqlite3_test.go b/sqlite3_test.go index c5deb3e..c86aba4 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -3,6 +3,7 @@ // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. +//go:build cgo // +build cgo package sqlite3 @@ -1722,6 +1723,43 @@ func TestAuthorizer(t *testing.T) { } } +func TestSetFileControlInt(t *testing.T) { + t.Run("PERSIST_WAL", func(t *testing.T) { + tempFilename := TempFilename(t) + defer os.Remove(tempFilename) + + sql.Register("sqlite3_FCNTL_PERSIST_WAL", &SQLiteDriver{ + ConnectHook: func(conn *SQLiteConn) error { + if err := conn.SetFileControlInt("", SQLITE_FCNTL_PERSIST_WAL, 1); err != nil { + return fmt.Errorf("Unexpected error from SetFileControlInt(): %w", err) + } + return nil + }, + }) + + db, err := sql.Open("sqlite3_FCNTL_PERSIST_WAL", tempFilename) + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer db.Close() + + // Set to WAL mode & write a page. + if _, err := db.Exec(`PRAGMA journal_mode = wal`); err != nil { + t.Fatal("Failed to set journal mode:", err) + } else if _, err := db.Exec(`CREATE TABLE t (x)`); err != nil { + t.Fatal("Failed to create table:", err) + } + if err := db.Close(); err != nil { + t.Fatal("Failed to close database", err) + } + + // Ensure WAL file persists after close. + if _, err := os.Stat(tempFilename + "-wal"); err != nil { + t.Fatal("Expected WAL file to be persisted after close", err) + } + }) +} + func TestNonColumnString(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") if err != nil { -- cgit v1.2.3