Skip to content

Commit

Permalink
beta 1 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Oct 25, 2019
1 parent 851a36d commit d682d7f
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [tanner0101] # loganwright, joscdk
open_collective: vapor
52 changes: 52 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: test
on:
- pull_request
jobs:
xenial:
container:
image: vapor/swift:5.1-xenial
services:
psql:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- run: swift test
bionic:
container:
image: vapor/swift:5.1-bionic
services:
psql:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- run: swift test
thread:
container:
image: vapor/swift:5.1-bionic
services:
psql:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- run: swift test --sanitize=thread
19 changes: 0 additions & 19 deletions Sources/FluentPostgresDriver/ConnectionPool+Database.swift

This file was deleted.

20 changes: 12 additions & 8 deletions Sources/FluentPostgresDriver/Databases+Postgres.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
extension Databases {
public mutating func postgres(
config: PostgresConfiguration,
poolConfig: ConnectionPoolConfig = .init(),
public func postgres(
configuration: PostgresConfiguration,
poolConfiguration: ConnectionPoolConfiguration = .init(),
as id: DatabaseID = .psql,
isDefault: Bool = true
isDefault: Bool = true,
on eventLoopGroup: EventLoopGroup
) {
let db = PostgresConnectionSource(
configuration: config,
on: self.eventLoop
configuration: configuration
)
let pool = ConnectionPool(config: poolConfig, source: db)
self.add(pool, as: id, isDefault: isDefault)
let pool = ConnectionPool(
configuration: poolConfiguration,
source: db,
on: eventLoopGroup
)
self.add(PostgresDatabaseDriver(pool: pool), as: id, isDefault: isDefault)
}
}

Expand Down
38 changes: 29 additions & 9 deletions Sources/FluentPostgresDriver/PostgresConnection+Database.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
import FluentSQL

extension PostgresConnection: Database {
public func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
return closure(self)
final class PostgresDatabaseDriver: DatabaseDriver {
let pool: ConnectionPool<PostgresConnectionSource>

var eventLoopGroup: EventLoopGroup {
return self.pool.eventLoopGroup
}

public func execute(_ query: DatabaseQuery, _ onOutput: @escaping (DatabaseOutput) throws -> ()) -> EventLoopFuture<Void> {

init(pool: ConnectionPool<PostgresConnectionSource>) {
self.pool = pool
}

func execute(query: DatabaseQuery, database: Database, onRow: @escaping (DatabaseRow) -> ()) -> EventLoopFuture<Void> {
var sql = SQLQueryConverter(delegate: PostgresConverterDelegate())
.convert(query)
switch query.action {
case .create:
sql = PostgresReturning(sql)
default: break
}
return self.execute(sql: sql) { row in
try onOutput(row as! PostgresRow)
return self.pool.execute(sql: sql) { row in
onRow(row as! PostgresRow)
}
}

public func execute(_ schema: DatabaseSchema) -> EventLoopFuture<Void> {
func execute(schema: DatabaseSchema, database: Database) -> EventLoopFuture<Void> {
let sql = SQLSchemaConverter(delegate: PostgresConverterDelegate())
.convert(schema)
return self.execute(sql: sql) { row in
return self.pool.execute(sql: sql) { row in
fatalError("unexpected output")
}
}

func shutdown() {
self.pool.shutdown()
}
}

extension PostgresDatabaseDriver: PostgresClient {
var eventLoop: EventLoop {
return self.eventLoopGroup.next()
}

func send(_ request: PostgresRequest) -> EventLoopFuture<Void> {
return self.pool.withConnection { $0.send(request) }
}
}

private struct PostgresReturning: SQLExpression {
Expand Down
8 changes: 6 additions & 2 deletions Sources/FluentPostgresDriver/PostgresRow+Database.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
extension PostgresRow: DatabaseOutput {
extension PostgresRow: DatabaseRow {
public func contains(field: String) -> Bool {
return self.column(field) != nil
}

public func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
public func decode<T>(
field: String,
as type: T.Type,
for database: Database
) throws -> T where T : Decodable {
return try self.decode(column: field, as: T.self)
}
}
32 changes: 17 additions & 15 deletions Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ final class FluentPostgresDriverTests: XCTestCase {
}
}

try CreateFoo().prepare(on: self.connectionPool).wait()
try CreateFoo().revert(on: self.connectionPool).wait()
try CreateFoo().prepare(on: self.db).wait()
try CreateFoo().revert(on: self.db).wait()
}

func testSaveModelWithBool() throws {
Expand Down Expand Up @@ -192,24 +192,29 @@ final class FluentPostgresDriverTests: XCTestCase {
}
}

try CreateOrganization().prepare(on: self.connectionPool).wait()
try CreateOrganization().prepare(on: self.db).wait()
defer {
try! CreateOrganization().revert(on: self.connectionPool).wait()
try! CreateOrganization().revert(on: self.db).wait()
}

let new = Organization()
new.disabled = false
try new.save(on: self.connectionPool).wait()
try new.save(on: self.db).wait()
}

var benchmarker: FluentBenchmarker!
var connectionPool: ConnectionPool<PostgresConnectionSource>!

var benchmarker: FluentBenchmarker {
return .init(database: self.db)
}
var eventLoopGroup: EventLoopGroup!
var dbs: Databases!
var db: Database {
return self.dbs.default()
}

override func setUp() {
XCTAssert(isLoggingConfigured)
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = eventLoopGroup.next()
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let hostname: String
#if os(Linux)
hostname = "psql"
Expand All @@ -224,15 +229,12 @@ final class FluentPostgresDriverTests: XCTestCase {
database: "vapor_database",
tlsConfiguration: nil
)
let db = PostgresConnectionSource(configuration: configuration, on: eventLoop)
let pool = ConnectionPool(config: .init(maxConnections: 1), source: db)
self.benchmarker = FluentBenchmarker(database: pool)
self.connectionPool = pool
self.eventLoopGroup = eventLoopGroup
self.dbs = Databases()
self.dbs.postgres(configuration: configuration, poolConfiguration: .init(maxConnections: 1), on: self.eventLoopGroup)
}

override func tearDown() {
try! self.connectionPool.close().wait()
self.dbs.shutdown()
try! self.eventLoopGroup.syncShutdownGracefully()
}
}
Expand Down
68 changes: 0 additions & 68 deletions circle.yml

This file was deleted.

0 comments on commit d682d7f

Please sign in to comment.