Skip to content

Commit a81a489

Browse files
committed
Added FK option
1 parent 7f15e42 commit a81a489

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

Sources/PureSQL/ConnectionPool.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public actor ConnectionPool: Sendable {
2222
private var count: Int = 1
2323
/// The maximum number of connections we can create
2424
private let limit: Int
25+
/// Whether to enforce foreign key constraints on each connection.
26+
private let foreignKeys: Bool
2527
/// Any connections available for use
2628
private var availableConnections: [RawConnection]
2729
/// Any caller waiting for a connection
@@ -37,21 +39,27 @@ public actor ConnectionPool: Sendable {
3739
path: String,
3840
limit: Int,
3941
migrations: [String],
40-
runMigrations: Bool = true
42+
runMigrations: Bool = true,
43+
foreignKeys: Bool = true
4144
) throws {
4245
guard limit > 0 else {
4346
throw SQLError.poolCannotHaveZeroConnections
4447
}
4548

4649
self.path = path
4750
self.limit = limit
51+
self.foreignKeys = foreignKeys
4852

4953
let connection = try SQLiteConnection(path: path)
5054
self.observer.installHooks(into: connection)
5155

5256
// Turn on WAL mode
5357
try connection.execute(sql: "PRAGMA journal_mode=WAL;")
5458

59+
if foreignKeys {
60+
try connection.execute(sql: "PRAGMA foreign_keys=ON;")
61+
}
62+
5563
if runMigrations {
5664
try MigrationRunner.execute(migrations: migrations, connection: connection)
5765
}
@@ -115,6 +123,12 @@ public actor ConnectionPool: Sendable {
115123
count += 1
116124
let connection = try SQLiteConnection(path: path)
117125
observer.installHooks(into: connection)
126+
127+
// Foreign key enforcement is per connection
128+
if foreignKeys {
129+
try connection.execute(sql: "PRAGMA foreign_keys=ON;")
130+
}
131+
118132
return connection
119133
}
120134

Sources/PureSQL/Database.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ public extension Database {
4949
path: path,
5050
limit: config.maxConnectionCount,
5151
migrations: Self.sanitizedMigrations,
52-
runMigrations: config.autoMigrate
52+
runMigrations: config.autoMigrate,
53+
foreignKeys: config.foreignKeys
5354
)
5455
} else {
5556
try ConnectionPool(
5657
path: ":memory:",
5758
limit: 1,
5859
migrations: Self.sanitizedMigrations,
59-
runMigrations: config.autoMigrate
60+
runMigrations: config.autoMigrate,
61+
foreignKeys: config.foreignKeys
6062
)
6163
}
6264

Sources/PureSQL/DatabaseConfig.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ public struct DatabaseConfig {
1818
/// If `true` the migrations will run when the connection is opened.
1919
/// Default is `true`.
2020
public var autoMigrate: Bool
21+
/// If `true`, `PRAGMA foreign_keys` is enabled on every connection.
22+
public var foreignKeys: Bool
2123

2224
public init(
2325
path: String?,
2426
maxConnectionCount: Int = 5,
25-
autoMigrate: Bool = true
27+
autoMigrate: Bool = true,
28+
foreignKeys: Bool = true
2629
) {
2730
self.path = path
2831
self.maxConnectionCount = maxConnectionCount
2932
self.autoMigrate = autoMigrate
33+
self.foreignKeys = foreignKeys
3034
}
3135
}

0 commit comments

Comments
 (0)