Skip to content

Commit d682d7f

Browse files
authored
beta 1 (#124)
1 parent 851a36d commit d682d7f

File tree

8 files changed

+118
-121
lines changed

8 files changed

+118
-121
lines changed

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: [tanner0101] # loganwright, joscdk
2+
open_collective: vapor

.github/workflows/test.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: test
2+
on:
3+
- pull_request
4+
jobs:
5+
xenial:
6+
container:
7+
image: vapor/swift:5.1-xenial
8+
services:
9+
psql:
10+
image: postgres
11+
ports:
12+
- 5432:5432
13+
env:
14+
POSTGRES_USER: vapor_username
15+
POSTGRES_DB: vapor_database
16+
POSTGRES_PASSWORD: vapor_password
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@master
20+
- run: swift test
21+
bionic:
22+
container:
23+
image: vapor/swift:5.1-bionic
24+
services:
25+
psql:
26+
image: postgres
27+
ports:
28+
- 5432:5432
29+
env:
30+
POSTGRES_USER: vapor_username
31+
POSTGRES_DB: vapor_database
32+
POSTGRES_PASSWORD: vapor_password
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@master
36+
- run: swift test
37+
thread:
38+
container:
39+
image: vapor/swift:5.1-bionic
40+
services:
41+
psql:
42+
image: postgres
43+
ports:
44+
- 5432:5432
45+
env:
46+
POSTGRES_USER: vapor_username
47+
POSTGRES_DB: vapor_database
48+
POSTGRES_PASSWORD: vapor_password
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@master
52+
- run: swift test --sanitize=thread

Sources/FluentPostgresDriver/ConnectionPool+Database.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

Sources/FluentPostgresDriver/Databases+Postgres.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
extension Databases {
2-
public mutating func postgres(
3-
config: PostgresConfiguration,
4-
poolConfig: ConnectionPoolConfig = .init(),
2+
public func postgres(
3+
configuration: PostgresConfiguration,
4+
poolConfiguration: ConnectionPoolConfiguration = .init(),
55
as id: DatabaseID = .psql,
6-
isDefault: Bool = true
6+
isDefault: Bool = true,
7+
on eventLoopGroup: EventLoopGroup
78
) {
89
let db = PostgresConnectionSource(
9-
configuration: config,
10-
on: self.eventLoop
10+
configuration: configuration
1111
)
12-
let pool = ConnectionPool(config: poolConfig, source: db)
13-
self.add(pool, as: id, isDefault: isDefault)
12+
let pool = ConnectionPool(
13+
configuration: poolConfiguration,
14+
source: db,
15+
on: eventLoopGroup
16+
)
17+
self.add(PostgresDatabaseDriver(pool: pool), as: id, isDefault: isDefault)
1418
}
1519
}
1620

Sources/FluentPostgresDriver/PostgresConnection+Database.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import FluentSQL
22

3-
extension PostgresConnection: Database {
4-
public func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
5-
return closure(self)
3+
final class PostgresDatabaseDriver: DatabaseDriver {
4+
let pool: ConnectionPool<PostgresConnectionSource>
5+
6+
var eventLoopGroup: EventLoopGroup {
7+
return self.pool.eventLoopGroup
68
}
7-
8-
public func execute(_ query: DatabaseQuery, _ onOutput: @escaping (DatabaseOutput) throws -> ()) -> EventLoopFuture<Void> {
9+
10+
init(pool: ConnectionPool<PostgresConnectionSource>) {
11+
self.pool = pool
12+
}
13+
14+
func execute(query: DatabaseQuery, database: Database, onRow: @escaping (DatabaseRow) -> ()) -> EventLoopFuture<Void> {
915
var sql = SQLQueryConverter(delegate: PostgresConverterDelegate())
1016
.convert(query)
1117
switch query.action {
1218
case .create:
1319
sql = PostgresReturning(sql)
1420
default: break
1521
}
16-
return self.execute(sql: sql) { row in
17-
try onOutput(row as! PostgresRow)
22+
return self.pool.execute(sql: sql) { row in
23+
onRow(row as! PostgresRow)
1824
}
1925
}
2026

21-
public func execute(_ schema: DatabaseSchema) -> EventLoopFuture<Void> {
27+
func execute(schema: DatabaseSchema, database: Database) -> EventLoopFuture<Void> {
2228
let sql = SQLSchemaConverter(delegate: PostgresConverterDelegate())
2329
.convert(schema)
24-
return self.execute(sql: sql) { row in
30+
return self.pool.execute(sql: sql) { row in
2531
fatalError("unexpected output")
2632
}
2733
}
34+
35+
func shutdown() {
36+
self.pool.shutdown()
37+
}
38+
}
39+
40+
extension PostgresDatabaseDriver: PostgresClient {
41+
var eventLoop: EventLoop {
42+
return self.eventLoopGroup.next()
43+
}
44+
45+
func send(_ request: PostgresRequest) -> EventLoopFuture<Void> {
46+
return self.pool.withConnection { $0.send(request) }
47+
}
2848
}
2949

3050
private struct PostgresReturning: SQLExpression {
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
extension PostgresRow: DatabaseOutput {
1+
extension PostgresRow: DatabaseRow {
22
public func contains(field: String) -> Bool {
33
return self.column(field) != nil
44
}
55

6-
public func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
6+
public func decode<T>(
7+
field: String,
8+
as type: T.Type,
9+
for database: Database
10+
) throws -> T where T : Decodable {
711
return try self.decode(column: field, as: T.self)
812
}
913
}

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ final class FluentPostgresDriverTests: XCTestCase {
162162
}
163163
}
164164

165-
try CreateFoo().prepare(on: self.connectionPool).wait()
166-
try CreateFoo().revert(on: self.connectionPool).wait()
165+
try CreateFoo().prepare(on: self.db).wait()
166+
try CreateFoo().revert(on: self.db).wait()
167167
}
168168

169169
func testSaveModelWithBool() throws {
@@ -192,24 +192,29 @@ final class FluentPostgresDriverTests: XCTestCase {
192192
}
193193
}
194194

195-
try CreateOrganization().prepare(on: self.connectionPool).wait()
195+
try CreateOrganization().prepare(on: self.db).wait()
196196
defer {
197-
try! CreateOrganization().revert(on: self.connectionPool).wait()
197+
try! CreateOrganization().revert(on: self.db).wait()
198198
}
199199

200200
let new = Organization()
201201
new.disabled = false
202-
try new.save(on: self.connectionPool).wait()
202+
try new.save(on: self.db).wait()
203203
}
204204

205-
var benchmarker: FluentBenchmarker!
206-
var connectionPool: ConnectionPool<PostgresConnectionSource>!
205+
206+
var benchmarker: FluentBenchmarker {
207+
return .init(database: self.db)
208+
}
207209
var eventLoopGroup: EventLoopGroup!
210+
var dbs: Databases!
211+
var db: Database {
212+
return self.dbs.default()
213+
}
208214

209215
override func setUp() {
210216
XCTAssert(isLoggingConfigured)
211-
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
212-
let eventLoop = eventLoopGroup.next()
217+
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
213218
let hostname: String
214219
#if os(Linux)
215220
hostname = "psql"
@@ -224,15 +229,12 @@ final class FluentPostgresDriverTests: XCTestCase {
224229
database: "vapor_database",
225230
tlsConfiguration: nil
226231
)
227-
let db = PostgresConnectionSource(configuration: configuration, on: eventLoop)
228-
let pool = ConnectionPool(config: .init(maxConnections: 1), source: db)
229-
self.benchmarker = FluentBenchmarker(database: pool)
230-
self.connectionPool = pool
231-
self.eventLoopGroup = eventLoopGroup
232+
self.dbs = Databases()
233+
self.dbs.postgres(configuration: configuration, poolConfiguration: .init(maxConnections: 1), on: self.eventLoopGroup)
232234
}
233235

234236
override func tearDown() {
235-
try! self.connectionPool.close().wait()
237+
self.dbs.shutdown()
236238
try! self.eventLoopGroup.syncShutdownGracefully()
237239
}
238240
}

circle.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)