@@ -2131,6 +2131,92 @@ func TestSetFileControlInt(t *testing.T) {
2131
2131
})
2132
2132
}
2133
2133
2134
+ func TestDBConfigNoCkptOnClose (t * testing.T ) {
2135
+ fname := TempFilename (t )
2136
+ defer os .Remove (fname )
2137
+ db , err := sql .Open ("sqlite3" , fname )
2138
+ if err != nil {
2139
+ t .Fatal (err )
2140
+ }
2141
+ defer db .Close ()
2142
+
2143
+ // Enable WAL mode.
2144
+ if _ , err := db .Exec (`PRAGMA journal_mode = wal` ); err != nil {
2145
+ t .Fatal (err )
2146
+ }
2147
+
2148
+ // Write some data.
2149
+ _ , err = db .Exec ("create table foo (department integer, profits integer)" )
2150
+ if err != nil {
2151
+ t .Fatal ("Failed to create table:" , err )
2152
+ }
2153
+
2154
+ // Confirm WAL file exists.
2155
+ if _ , err := os .Stat (fname + "-wal" ); err != nil {
2156
+ t .Fatal ("Expected WAL file to exist" , err )
2157
+ }
2158
+
2159
+ // Close the database, and confirm WAL file is removed.
2160
+ if err := db .Close (); err != nil {
2161
+ t .Fatal ("Failed to close database" , err )
2162
+ }
2163
+ if _ , err := os .Stat (fname + "-wal" ); err == nil {
2164
+ t .Fatal ("Expected WAL file to be removed after close" )
2165
+ }
2166
+
2167
+ // Now do it again, but with the DBConfig option set.
2168
+ db , err = sql .Open ("sqlite3" , fname )
2169
+ if err != nil {
2170
+ t .Fatal (err )
2171
+ }
2172
+ defer db .Close ()
2173
+
2174
+ // Insert a record, confirm a WAL file appears.
2175
+ if _ , err := db .Exec (`insert into foo values (1, 2)` ); err != nil {
2176
+ t .Fatal (err )
2177
+ }
2178
+ if _ , err := os .Stat (fname + "-wal" ); err != nil {
2179
+ t .Fatal ("Expected WAL file to exist" , err )
2180
+ }
2181
+
2182
+ // Disable checkpoint-on-close.
2183
+ f := func (driverConn interface {}) error {
2184
+ c := driverConn .(* SQLiteConn )
2185
+ return c .DBConfigNoCkptOnClose ()
2186
+ }
2187
+ conn , err := db .Conn (context .Background ())
2188
+ if err != nil {
2189
+ t .Fatal (err )
2190
+ }
2191
+ if err := conn .Raw (f ); err != nil {
2192
+ t .Fatal (err )
2193
+ }
2194
+
2195
+ // Read the SQLite file into a byte slice for comparison later.
2196
+ bufPre , err := os .ReadFile (fname )
2197
+ if err != nil {
2198
+ t .Fatal (err )
2199
+ }
2200
+
2201
+ // Close the database, and confirm WAL file is still present.
2202
+ if err := db .Close (); err != nil {
2203
+ t .Fatal ("Failed to close database" , err )
2204
+ }
2205
+ if _ , err := os .Stat (fname + "-wal" ); err != nil {
2206
+ t .Fatal ("Expected WAL file to be present after close" , err )
2207
+ }
2208
+
2209
+ // Confirm the SQLite file is the same as before since no checkpoint
2210
+ // was performed.
2211
+ bufPost , err := os .ReadFile (fname )
2212
+ if err != nil {
2213
+ t .Fatal (err )
2214
+ }
2215
+ if ! bytes .Equal (bufPre , bufPost ) {
2216
+ t .Fatal ("Expected SQLite file to be unchanged after close" )
2217
+ }
2218
+ }
2219
+
2134
2220
func TestNonColumnString (t * testing.T ) {
2135
2221
db , err := sql .Open ("sqlite3" , ":memory:" )
2136
2222
if err != nil {
0 commit comments