Skip to content

Commit

Permalink
feature: Add function to set journal mode on sqlite databases (apollo…
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Jul 30, 2024
1 parent 5f52953 commit 70c3447
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Sources/ApolloSQLite/JournalMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

public enum JournalMode: String {
/// The rollback journal is deleted at the conclusion of each transaction. This is the default behaviour.
case delete = "DELETE"
/// Commits transactions by truncating the rollback journal to zero-length instead of deleting it.
case truncate = "TRUNCATE"
/// Prevents the rollback journal from being deleted at the end of each transaction. Instead, the header
/// of the journal is overwritten with zeros.
case persist = "PERSIST"
/// Stores the rollback journal in volatile RAM. This saves disk I/O but at the expense of database
/// safety and integrity.
case memory = "MEMORY"
/// Uses a write-ahead log instead of a rollback journal to implement transactions. The WAL journaling
/// mode is persistent; after being set it stays in effect across multiple database connections and after
/// closing and reopening the database.
case wal = "WAL"
/// Disables the rollback journal completely
case off = "OFF"
}
4 changes: 2 additions & 2 deletions Sources/ApolloSQLite/SQLiteDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct DatabaseRow {
}

public protocol SQLiteDatabase {

init(fileURL: URL) throws

func createRecordsTableIfNeeded() throws
Expand All @@ -23,7 +23,7 @@ public protocol SQLiteDatabase {
func deleteRecords(matching pattern: CacheKey) throws

func clearDatabase(shouldVacuumOnClear: Bool) throws

}

public extension SQLiteDatabase {
Expand Down
9 changes: 8 additions & 1 deletion Sources/ApolloSQLite/SQLiteDotSwiftDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase {
private let records: Table
private let keyColumn: SQLite.Expression<CacheKey>
private let recordColumn: SQLite.Expression<String>

public init(fileURL: URL) throws {
self.records = Table(Self.tableName)
self.keyColumn = Expression<CacheKey>(Self.keyColumnName)
Expand Down Expand Up @@ -66,4 +66,11 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase {
try self.db.prepare("VACUUM;").run()
}
}

/// Sets the journal mode for the current database.
///
/// - Parameter mode: The journal mode controls how the journal file is stored and processed.
public func setJournalMode(mode: JournalMode) throws {
try self.db.run("PRAGMA journal_mode = \(mode.rawValue)")
}
}

0 comments on commit 70c3447

Please sign in to comment.