Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vapor/fluent-postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Jun 21, 2018
2 parents fe87307 + 28a6ea2 commit 2a5b8d1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
24 changes: 14 additions & 10 deletions Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ public final class FluentPostgreSQLProvider: Provider {

/// See `Provider`.
public func willBoot(_ worker: Container) throws -> Future<Void> {

return worker.withPooledConnection(to: .psql) { conn in
return FluentPostgreSQLProvider._setup(on: conn)
}
}

public static func _setup(on conn: PostgreSQLConnection) -> Future<Void> {
struct Setting: Codable {
var version: String
}

return worker.withPooledConnection(to: .psql) { conn in
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
_serverVersion = rows[0].version
if let versionString = _serverVersion {
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
let majorVersion = versionString[..<pointIndex]
if let ver = Int(majorVersion) {
_globalEnableIdentityColumns = ver < 10 ? false: _globalEnableIdentityColumns
}
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
_serverVersion = rows[0].version
if let versionString = _serverVersion {
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
let majorVersion = versionString[..<pointIndex]
if let ver = Int(majorVersion) {
_globalEnableIdentityColumns = ver < 10 ? false: _globalEnableIdentityColumns
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,32 @@ extension PostgreSQLDatabase: SchemaSupporting {
}

if isIdentifier {
let pkDefault: PostgreSQLPrimaryKeyDefault?
// create a unique name for the primary key since it will be added
// as a separate index.
let unique: String
if let table = column.table {
unique = table.identifier.string + "." + column.identifier.string
} else {
unique = column.identifier.string
}
if _globalEnableIdentityColumns {
let pkDefault: PostgreSQLPrimaryKeyDefault?
// create a unique name for the primary key since it will be added
// as a separate index.
let unique: String
if let table = column.table {
unique = table.identifier.string + "." + column.identifier.string
} else {
unique = column.identifier.string
}
switch dataType {
case .smallint, .integer, .bigint:
pkDefault = .generated(.byDefault)
default:
pkDefault = nil
}
constraints.append(.primaryKey(default: pkDefault, identifier: .identifier("pk:\(unique)")))
} else {
pkDefault = nil
switch dataType {
case .smallint: dataType = .smallserial
case .integer: dataType = .serial
case .bigint: dataType = .bigserial
default: break
}
}
constraints.append(.primaryKey(default: pkDefault, identifier: .identifier("pk:\(unique)")))
}

if isArray {
Expand Down
9 changes: 6 additions & 3 deletions Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class FluentPostgreSQLTests: XCTestCase {
#if os(macOS)
let hostname = "localhost"
#else
let hostname = "psql-cleartext"
let hostname = "psql"
#endif

let config: PostgreSQLDatabaseConfig = .init(
hostname: hostname,
port: 5432,
Expand All @@ -27,6 +27,9 @@ class FluentPostgreSQLTests: XCTestCase {
database = PostgreSQLDatabase(config: config)
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1)
benchmarker = try! Benchmarker(database, on: eventLoop, onFail: XCTFail)
let conn = try! benchmarker.pool.requestConnection().wait()
defer { benchmarker.pool.releaseConnection(conn) }
try! FluentPostgreSQLProvider._setup(on: conn).wait()
}

func testBenchmark() throws {
Expand Down Expand Up @@ -131,7 +134,7 @@ class FluentPostgreSQLTests: XCTestCase {

static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
return PostgreSQLDatabase.create(Pet.self, on: conn) { builder in
builder.field(for: \.id, type: .bigint, .notNull, .primaryKey(default: .generated(.byDefault)))
builder.field(for: \.id)
builder.field(for: \.type, type: .bigint)
builder.field(for: \.name, type: .text)
}
Expand Down
24 changes: 21 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ jobs:
- checkout
- run: swift build
- run: swift test
linux:
linux-10:
docker:
- image: codevapor/swift:4.1
- image: circleci/postgres:latest
name: psql-cleartext
name: psql
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
steps:
- checkout
- run:
name: Compile code
command: swift build
- run:
name: Run unit tests
command: swift test
linux-9:
docker:
- image: codevapor/swift:4.1
- image: circleci/postgres:9
name: psql
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
Expand All @@ -37,7 +54,8 @@ workflows:
version: 2
tests:
jobs:
- linux
- linux-10
- linux-9
- linux-release
# - macos
nightly:
Expand Down
15 changes: 12 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ services:
context: .
dockerfile: test.Dockerfile
depends_on:
- psql-cleartext
psql-cleartext:
image: postgres:latest
- psql-10
psql-10:
image: postgres:10
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
ports:
- 5432:5432
psql-9:
image: postgres:9
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
ports:
- 5432:5432

0 comments on commit 2a5b8d1

Please sign in to comment.