github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/tests/wal_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/ncruces/go-sqlite3"
     9  	_ "github.com/ncruces/go-sqlite3/embed"
    10  	_ "github.com/ncruces/go-sqlite3/tests/testcfg"
    11  	"github.com/ncruces/go-sqlite3/vfs"
    12  )
    13  
    14  func TestWAL_enter_exit(t *testing.T) {
    15  	if !vfs.SupportsFileLocking {
    16  		t.Skip("skipping without locks")
    17  	}
    18  	t.Parallel()
    19  
    20  	file := filepath.Join(t.TempDir(), "test.db")
    21  
    22  	db, err := sqlite3.Open(file)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	defer db.Close()
    27  
    28  	if !vfs.SupportsSharedMemory {
    29  		err = db.Exec(`PRAGMA locking_mode=exclusive`)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  	}
    34  
    35  	err = db.Exec(`
    36  		CREATE TABLE test (col);
    37  		PRAGMA journal_mode=wal;
    38  		SELECT * FROM test;
    39  		PRAGMA journal_mode=delete;
    40  		SELECT * FROM test;
    41  		PRAGMA journal_mode=wal;
    42  		SELECT * FROM test;
    43  	`)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  }
    48  
    49  func TestWAL_readonly(t *testing.T) {
    50  	if !vfs.SupportsSharedMemory {
    51  		t.Skip("skipping without shared memory")
    52  	}
    53  	t.Parallel()
    54  
    55  	tmp := filepath.Join(t.TempDir(), "test.db")
    56  	err := os.WriteFile(tmp, walDB, 0666)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	db, err := sqlite3.OpenFlags(tmp, sqlite3.OPEN_READONLY)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	defer db.Close()
    66  
    67  	stmt, _, err := db.Prepare(`SELECT * FROM sqlite_master`)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	defer stmt.Close()
    72  
    73  	if stmt.Step() {
    74  		t.Error("want no rows")
    75  	}
    76  }
    77  
    78  func TestConn_WalCheckpoint(t *testing.T) {
    79  	if !vfs.SupportsFileLocking {
    80  		t.Skip("skipping without locks")
    81  	}
    82  	t.Parallel()
    83  
    84  	file := filepath.Join(t.TempDir(), "test.db")
    85  
    86  	db, err := sqlite3.Open(file)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	defer db.Close()
    91  
    92  	err = db.WalAutoCheckpoint(1000)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	db.WalHook(func(db *sqlite3.Conn, schema string, pages int) error {
    98  		log, ckpt, err := db.WalCheckpoint(schema, sqlite3.CHECKPOINT_FULL)
    99  		t.Log(log, ckpt, err)
   100  		return err
   101  	})
   102  
   103  	err = db.Exec(`
   104  		PRAGMA locking_mode=exlusive;
   105  		PRAGMA journal_mode=wal;
   106  		CREATE TABLE test (col);
   107  	`)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  }