@@ -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
0 commit comments