Skip to content

Commit 28a6ea2

Browse files
authored
Merge pull request #72 from vapor/psql-9-fixes
add CI test + fixes for v9
2 parents 521d99a + 914c0ed commit 28a6ea2

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

Diff for: Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ public final class FluentPostgreSQLProvider: Provider {
1818

1919
/// See `Provider`.
2020
public func willBoot(_ worker: Container) throws -> Future<Void> {
21+
22+
return worker.withPooledConnection(to: .psql) { conn in
23+
return FluentPostgreSQLProvider._setup(on: conn)
24+
}
25+
}
26+
27+
public static func _setup(on conn: PostgreSQLConnection) -> Future<Void> {
2128
struct Setting: Codable {
2229
var version: String
2330
}
24-
25-
return worker.withPooledConnection(to: .psql) { conn in
26-
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
27-
_serverVersion = rows[0].version
28-
if let versionString = _serverVersion {
29-
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
30-
let majorVersion = versionString[..<pointIndex]
31-
if let ver = Int(majorVersion) {
32-
_globalEnableIdentityColumns = ver < 10 ? false: _globalEnableIdentityColumns
33-
}
31+
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
32+
_serverVersion = rows[0].version
33+
if let versionString = _serverVersion {
34+
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
35+
let majorVersion = versionString[..<pointIndex]
36+
if let ver = Int(majorVersion) {
37+
_globalEnableIdentityColumns = ver < 10 ? false: _globalEnableIdentityColumns
3438
}
3539
}
3640
}

Diff for: Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift

+11-10
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,32 @@ extension PostgreSQLDatabase: SchemaSupporting {
5454
}
5555

5656
if isIdentifier {
57+
let pkDefault: PostgreSQLPrimaryKeyDefault?
58+
// create a unique name for the primary key since it will be added
59+
// as a separate index.
60+
let unique: String
61+
if let table = column.table {
62+
unique = table.identifier.string + "." + column.identifier.string
63+
} else {
64+
unique = column.identifier.string
65+
}
5766
if _globalEnableIdentityColumns {
58-
let pkDefault: PostgreSQLPrimaryKeyDefault?
59-
// create a unique name for the primary key since it will be added
60-
// as a separate index.
61-
let unique: String
62-
if let table = column.table {
63-
unique = table.identifier.string + "." + column.identifier.string
64-
} else {
65-
unique = column.identifier.string
66-
}
6767
switch dataType {
6868
case .smallint, .integer, .bigint:
6969
pkDefault = .generated(.byDefault)
7070
default:
7171
pkDefault = nil
7272
}
73-
constraints.append(.primaryKey(default: pkDefault, identifier: .identifier("pk:\(unique)")))
7473
} else {
74+
pkDefault = nil
7575
switch dataType {
7676
case .smallint: dataType = .smallserial
7777
case .integer: dataType = .serial
7878
case .bigint: dataType = .bigserial
7979
default: break
8080
}
8181
}
82+
constraints.append(.primaryKey(default: pkDefault, identifier: .identifier("pk:\(unique)")))
8283
}
8384

8485
if isArray {

Diff for: Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class FluentPostgreSQLTests: XCTestCase {
1414
#if os(macOS)
1515
let hostname = "localhost"
1616
#else
17-
let hostname = "psql-cleartext"
17+
let hostname = "psql"
1818
#endif
19-
19+
2020
let config: PostgreSQLDatabaseConfig = .init(
2121
hostname: hostname,
2222
port: 5432,
@@ -27,6 +27,9 @@ class FluentPostgreSQLTests: XCTestCase {
2727
database = PostgreSQLDatabase(config: config)
2828
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1)
2929
benchmarker = try! Benchmarker(database, on: eventLoop, onFail: XCTFail)
30+
let conn = try! benchmarker.pool.requestConnection().wait()
31+
defer { benchmarker.pool.releaseConnection(conn) }
32+
try! FluentPostgreSQLProvider._setup(on: conn).wait()
3033
}
3134

3235
func testBenchmark() throws {
@@ -131,7 +134,7 @@ class FluentPostgreSQLTests: XCTestCase {
131134

132135
static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
133136
return PostgreSQLDatabase.create(Pet.self, on: conn) { builder in
134-
builder.field(for: \.id, type: .bigint, .notNull, .primaryKey(default: .generated(.byDefault)))
137+
builder.field(for: \.id)
135138
builder.field(for: \.type, type: .bigint)
136139
builder.field(for: \.name, type: .text)
137140
}

Diff for: circle.yml

+21-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@ jobs:
88
- checkout
99
- run: swift build
1010
- run: swift test
11-
linux:
11+
linux-10:
1212
docker:
1313
- image: codevapor/swift:4.1
1414
- image: circleci/postgres:latest
15-
name: psql-cleartext
15+
name: psql
16+
environment:
17+
POSTGRES_USER: vapor_username
18+
POSTGRES_DB: vapor_database
19+
POSTGRES_PASSWORD: vapor_password
20+
steps:
21+
- checkout
22+
- run:
23+
name: Compile code
24+
command: swift build
25+
- run:
26+
name: Run unit tests
27+
command: swift test
28+
linux-9:
29+
docker:
30+
- image: codevapor/swift:4.1
31+
- image: circleci/postgres:9
32+
name: psql
1633
environment:
1734
POSTGRES_USER: vapor_username
1835
POSTGRES_DB: vapor_database
@@ -37,7 +54,8 @@ workflows:
3754
version: 2
3855
tests:
3956
jobs:
40-
- linux
57+
- linux-10
58+
- linux-9
4159
- linux-release
4260
# - macos
4361
nightly:

Diff for: docker-compose.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ services:
66
context: .
77
dockerfile: test.Dockerfile
88
depends_on:
9-
- psql-cleartext
10-
psql-cleartext:
11-
image: postgres:latest
9+
- psql-10
10+
psql-10:
11+
image: postgres:10
1212
environment:
1313
POSTGRES_USER: vapor_username
1414
POSTGRES_DB: vapor_database
1515
POSTGRES_PASSWORD: vapor_password
1616
ports:
1717
- 5432:5432
18+
psql-9:
19+
image: postgres:9
20+
environment:
21+
POSTGRES_USER: vapor_username
22+
POSTGRES_DB: vapor_database
23+
POSTGRES_PASSWORD: vapor_password
24+
ports:
25+
- 5432:5432
26+

0 commit comments

Comments
 (0)