Skip to content

Commit 51b8191

Browse files
committed
Closing bug fixes
Closing a file-backed database no longer deletes the underlying file. In WAL journal mode, closing the database causes a WAL checkpoint.
1 parent 8c00e98 commit 51b8191

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 2.1.2 2023-09-07
9+
### Fixed
10+
- Closing a file-backed database no longer deletes the underlying file.
11+
- In WAL journal mode, closing the database causes a WAL checkpoint.
12+
813
## 2.1.1 2023-09-07
914
### Fixed
1015
- Storing a `Statement` no longer causes a crash when cleaning up.

Sources/Restructure/Restructure.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@ public class Restructure {
139139
return
140140
}
141141

142-
preparedStatements.forEach {
143-
sqlite3_finalize($0)
142+
// Finalize all known statements
143+
for statement in preparedStatements {
144+
sqlite3_finalize(statement)
144145
}
145146

146147
preparedStatements.removeAll()
147148

148-
sqlite3_close_v2(db)
149-
150-
if let filePath = path {
151-
try? FileManager.default.removeItem(atPath: filePath)
149+
// In WAL mode, force a flush
150+
if journalMode == .wal {
151+
sqlite3_wal_checkpoint_v2(db, nil, SQLITE_CHECKPOINT_TRUNCATE, nil, nil)
152152
}
153153

154+
// Final close
155+
sqlite3_close_v2(db)
154156
isOpen = false
155157
}
156158

Tests/RestructureTests/RestructureInitializationTests.swift

-18
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,6 @@ class RestructureInitializationTests: XCTestCase {
5454
XCTAssertNotNil(restructure)
5555
}
5656

57-
func testClosingTemporaryDeletedFile() {
58-
// Ensure the file doesn't exist
59-
XCTAssertFalse(FileManager.default.fileExists(atPath: tempPath))
60-
61-
// Build the structure
62-
restructure = try! Restructure(path: tempPath)
63-
restructure!.isTemporary = true
64-
65-
// Ensure the file does exist
66-
XCTAssertTrue(FileManager.default.fileExists(atPath: tempPath))
67-
68-
// Close and clean up
69-
restructure!.close()
70-
71-
// Ensure the file doesn't exist
72-
XCTAssertFalse(FileManager.default.fileExists(atPath: tempPath))
73-
}
74-
7557
func testJournalModeDefault() {
7658
restructure = try! Restructure(path: tempPath)
7759
XCTAssertEqual(restructure!.journalMode, .wal)

0 commit comments

Comments
 (0)