Skip to content

Commit 435d4cd

Browse files
committed
update to PostgresDatabase protocol
1 parent 389e9b7 commit 435d4cd

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

Sources/FluentPostgresDriver/Postgres+Fluent.swift

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

3-
extension FluentDatabaseID {
4-
public static var psql: FluentDatabaseID {
3+
extension DatabaseID {
4+
public static var psql: DatabaseID {
55
return .init(string: "psql")
66
}
77
}
88

9-
extension FluentDatabases {
9+
extension Databases {
1010
public mutating func postgres(
11-
config: PostgresDatabase.Config,
11+
config: PostgresConfig,
1212
poolConfig: ConnectionPoolConfig = .init(),
13-
as id: FluentDatabaseID = .psql,
13+
as id: DatabaseID = .psql,
1414
isDefault: Bool = true
1515
) {
16-
let driver = PostgresDriver(connectionPool: .init(
17-
config: poolConfig,
18-
source: .init(config: config, on: self.eventLoop)
19-
))
20-
self.add(driver, as: id, isDefault: isDefault)
16+
let db = PostgresConnectionSource(
17+
config: config,
18+
on: self.eventLoop
19+
)
20+
let pool = ConnectionPool(config: poolConfig, source: db)
21+
self.add(pool, as: id, isDefault: isDefault)
2122
}
2223
}
2324

24-
public struct PostgresDriver: FluentDatabase {
25+
extension ConnectionPool: Database where Source.Connection: SQLDatabase {
2526
public var eventLoop: EventLoop {
26-
return self.connectionPool.source.eventLoop
27+
return self.source.eventLoop
2728
}
28-
29-
public let connectionPool: ConnectionPool<PostgresDatabase>
30-
31-
public init(connectionPool: ConnectionPool<PostgresDatabase>) {
32-
self.connectionPool = connectionPool
33-
}
34-
35-
public func execute(_ query: FluentQuery, _ onOutput: @escaping (FluentOutput) throws -> ()) -> EventLoopFuture<Void> {
36-
return connectionPool.withConnection { connection in
37-
var sql = SQLQueryConverter().convert(query)
38-
switch query.action {
39-
case .create:
40-
sql = PostgresReturning(sql)
41-
default: break
42-
}
43-
return connection.sqlQuery(sql) { row in
44-
try onOutput(row.fluentOutput)
45-
}
29+
}
30+
extension PostgresConnection: Database { }
31+
32+
extension Database where Self: SQLDatabase {
33+
public func execute(_ query: DatabaseQuery, _ onOutput: @escaping (DatabaseOutput) throws -> ()) -> EventLoopFuture<Void> {
34+
var sql = SQLQueryConverter().convert(query)
35+
switch query.action {
36+
case .create:
37+
sql = PostgresReturning(sql)
38+
default: break
39+
}
40+
return self.sqlQuery(sql) { row in
41+
try onOutput(row.fluentOutput)
4642
}
4743
}
4844

49-
public func execute(_ schema: FluentSchema) -> EventLoopFuture<Void> {
50-
return self.connectionPool.withConnection { connection in
51-
return connection.sqlQuery(SQLSchemaConverter().convert(schema)) { row in
52-
fatalError("unexpected output")
53-
}
45+
public func execute(_ schema: DatabaseSchema) -> EventLoopFuture<Void> {
46+
return self.sqlQuery(SQLSchemaConverter().convert(schema)) { row in
47+
fatalError("unexpected output")
5448
}
5549
}
5650

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ final class FluentPostgresDriverTests: XCTestCase {
7070
func testNestedModel() throws {
7171
try self.benchmarker.testNestedModel()
7272
}
73+
74+
func testAggregateCount() throws {
75+
try self.benchmarker.testAggregateCount()
76+
}
7377
//
7478
// func testWorkUnit() throws {
7579
// try self.benchmarker.testWorkUnit()
@@ -83,17 +87,17 @@ final class FluentPostgresDriverTests: XCTestCase {
8387
#else
8488
hostname = "localhost"
8589
#endif
86-
let config = PostgresDatabase.Config(
90+
let config = PostgresConfig(
8791
hostname: hostname,
8892
port: 5432,
8993
username: "vapor_username",
9094
password: "vapor_password",
9195
database: "vapor_database",
9296
tlsConfig: nil
9397
)
94-
let pool = PostgresDatabase(config: config, on: eventLoop).makeConnectionPool(config: .init(maxConnections: 1))
95-
let driver = PostgresDriver(connectionPool: pool)
96-
self.benchmarker = FluentBenchmarker(database: driver)
98+
let db = PostgresConnectionSource(config: config, on: eventLoop)
99+
let pool = ConnectionPool(config: .init(maxConnections: 1), source: db)
100+
self.benchmarker = FluentBenchmarker(database: pool)
97101
}
98102

99103
static let allTests = [

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ services:
77
dockerfile: test.Dockerfile
88
depends_on:
99
- psql-10
10+
psql-11:
11+
image: postgres:11
12+
environment:
13+
POSTGRES_USER: vapor_username
14+
POSTGRES_DB: vapor_database
15+
POSTGRES_PASSWORD: vapor_password
16+
ports:
17+
- 5432:5432
1018
psql-10:
1119
image: postgres:10
1220
environment:

0 commit comments

Comments
 (0)