Skip to content

Commit a538fc6

Browse files
authored
Update for the nested subpath changes in FluentKit (#215)
* Remove no longer needed (well, never needed, really) nested subpath support in SQL converter delegate. General tidying and CI. * Work around weird bug where Swift 5.7 thinks this particular test model isn't Equatable even though it is
1 parent 8cd805f commit a538fc6

File tree

6 files changed

+73
-66
lines changed

6 files changed

+73
-66
lines changed

.github/CODEOWNERS

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* @gwynne
2+
/.github/CONTRIBUTING.md @gwynne @0xTim
3+
/.github/workflows/*.yml @gwynne @0xTim
4+
/.github/workflows/test.yml @gwynne
5+
/.spi.yml @gwynne @0xTim
6+
/.gitignore @gwynne @0xTim
7+
/LICENSE @gwynne @0xTim
8+
/README.md @gwynne @0xTim

.github/workflows/test.yml

+21-48
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ concurrency:
33
group: ${{ github.workflow }}-${{ github.ref }}
44
cancel-in-progress: true
55
on:
6-
pull_request: { branches: ['*'] }
7-
push: { branches: ['main'] }
6+
pull_request: { types: [opened, reopened, synchronize, ready_for_review] }
7+
push: { branches: [ main ] }
88

99
env:
1010
LOG_LEVEL: info
@@ -23,50 +23,8 @@ env:
2323
POSTGRES_PASSWORD_B: 'test_password'
2424

2525
jobs:
26-
# Baseline test run for code coverage stats
27-
codecov:
28-
strategy:
29-
matrix:
30-
include:
31-
- dbimage: postgres:15
32-
dbauth: scram-sha-256
33-
runs-on: ubuntu-latest
34-
container: swift:5.8-jammy
35-
services:
36-
psql-a:
37-
image: ${{ matrix.dbimage }}
38-
env:
39-
POSTGRES_USER: 'test_username'
40-
POSTGRES_DB: 'test_database'
41-
POSTGRES_PASSWORD: 'test_password'
42-
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
43-
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
44-
psql-b:
45-
image: ${{ matrix.dbimage }}
46-
env:
47-
POSTGRES_USER: 'test_username'
48-
POSTGRES_DB: 'test_database'
49-
POSTGRES_PASSWORD: 'test_password'
50-
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
51-
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
52-
steps:
53-
- name: Save Postgres version and method to env
54-
run: |
55-
echo POSTGRES_VERSION='${{ matrix.dbimage }}' >> $GITHUB_ENV
56-
echo POSTGRES_AUTH_METHOD='${{ matrix.dbauth }}' >> $GITHUB_ENV
57-
- name: Check out package
58-
uses: actions/checkout@v3
59-
- name: Run local tests with coverage
60-
run: swift test --enable-code-coverage
61-
- name: Submit coverage report to Codecov.io
62-
uses: vapor/[email protected]
63-
with:
64-
cc_env_vars: 'SWIFT_VERSION,SWIFT_PLATFORM,RUNNER_OS,RUNNER_ARCH,POSTGRES_VERSION,POSTGRES_AUTH_METHOD'
65-
cc_fail_ci_if_error: false
66-
67-
# Check for API breakage versus main
6826
api-breakage:
69-
if: github.event_name == 'pull_request'
27+
if: ${{ !(github.event.pull_request.draft || false) }}
7028
runs-on: ubuntu-latest
7129
container: swift:5.8-jammy
7230
steps:
@@ -77,7 +35,7 @@ jobs:
7735
uses: vapor/ci/.github/actions/ci-swift-check-api-breakage@reusable-workflows
7836

7937
linux-all:
80-
if: github.event_name == 'pull_request'
38+
if: ${{ !(github.event.pull_request.draft || false) }}
8139
strategy:
8240
fail-fast: false
8341
matrix:
@@ -107,13 +65,28 @@ jobs:
10765
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
10866
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
10967
steps:
68+
- name: Display versions
69+
shell: bash
70+
run: |
71+
echo POSTGRES_VERSION='${{ matrix.dbimage }}' >> $GITHUB_ENV
72+
echo POSTGRES_AUTH_METHOD='${{ matrix.dbauth }}' >> $GITHUB_ENV
73+
if [[ '${{ contains(matrix.container, 'nightly') }}' == 'true' ]]; then
74+
SWIFT_PLATFORM="$(source /etc/os-release && echo "${ID}${VERSION_ID}")" SWIFT_VERSION="$(cat /.swift_tag)"
75+
printf 'SWIFT_PLATFORM=%s\nSWIFT_VERSION=%s\n' "${SWIFT_PLATFORM}" "${SWIFT_VERSION}" >>"${GITHUB_ENV}"
76+
fi
77+
printf 'OS: %s\nTag: %s\nVersion:\n' "${SWIFT_PLATFORM}-${RUNNER_ARCH}" "${SWIFT_VERSION}" && swift --version
11078
- name: Check out package
11179
uses: actions/checkout@v3
11280
- name: Run all tests
113-
run: swift test --sanitize=thread
81+
run: swift test --sanitize=thread --enable-code-coverage
82+
- name: Submit coverage report to Codecov.io
83+
uses: vapor/[email protected]
84+
with:
85+
cc_env_vars: 'SWIFT_VERSION,SWIFT_PLATFORM,RUNNER_OS,RUNNER_ARCH,POSTGRES_VERSION,POSTGRES_AUTH_METHOD'
86+
cc_fail_ci_if_error: false
11487

11588
macos-all:
116-
if: github.event_name == 'pull_request'
89+
if: ${{ !(github.event.pull_request.draft || false) }}
11790
strategy:
11891
fail-fast: false
11992
matrix:

Package.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ let package = Package(
1313
.library(name: "FluentPostgresDriver", targets: ["FluentPostgresDriver"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/vapor/async-kit.git", from: "1.14.0"),
17-
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.36.0"),
18-
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.11.0"),
16+
.package(url: "https://github.com/vapor/async-kit.git", from: "1.17.0"),
17+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.43.0"),
18+
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.11.4"),
1919
],
2020
targets: [
2121
.target(name: "FluentPostgresDriver", dependencies: [

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
<a href="LICENSE">
1616
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
1717
</a>
18-
<a href="https://github.com/vapor/fluent-postgres-driver/actions">
19-
<img src="https://github.com/vapor/fluent-postgres-driver/workflows/test/badge.svg" alt="Continuous Integration">
18+
<a href="https://github.com/vapor/fluent-postgres-driver/actions/workflows/test.yml">
19+
<img src="https://github.com/vapor/fluent-postgres-driver/actions/workflows/test.yml/badge.svg?event=push" alt="Continuous Integration">
20+
</a>
21+
<a href="https://codecov.io/gh/vapor/fluent-postgres-driver">
22+
<img src="https://codecov.io/gh/vapor/fluent-postgres-driver/branch/main/graph/badge.svg?token=PizqqlcRSJ" alt="Test Coverage">
2023
</a>
2124
<a href="https://swift.org">
22-
<img src="http://img.shields.io/badge/swift-5.2-brightgreen.svg" alt="Swift 5.2">
25+
<img src="http://img.shields.io/badge/swift-5.7-brightgreen.svg" alt="Swift 5.7">
2326
</a>
2427
</p>

Sources/FluentPostgresDriver/PostgresConverterDelegate.swift

-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@ struct PostgresConverterDelegate: SQLConverterDelegate {
4545
return nil
4646
}
4747
}
48-
49-
func nestedFieldExpression(_ column: String, _ path: [String]) -> any SQLExpression {
50-
switch path.count {
51-
case 1:
52-
return SQLRaw("\(column)->>'\(path[0])'")
53-
case 2...:
54-
let inner = path[0..<path.count - 1].map { "'\($0)'" }.joined(separator: "->")
55-
return SQLRaw("\(column)->\(inner)->>'\(path.last!)'")
56-
default:
57-
fatalError()
58-
}
59-
}
6048
}
6149

6250
private struct SQLArrayDataType: SQLExpression {

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import FluentBenchmark
44
import FluentPostgresDriver
55
import XCTest
66
import PostgresKit
7+
import SQLKit
78

89
final class FluentPostgresDriverTests: XCTestCase {
910
//func testAll() throws { try self.benchmarker.testAll() }
@@ -162,6 +163,40 @@ final class FluentPostgresDriverTests: XCTestCase {
162163
try! EventWithFooMigration().revert(on: self.db).wait()
163164
try! EnumMigration().revert(on: self.db).wait()
164165
}
166+
167+
func testEncodingArrayOfModels() throws {
168+
final class Elem: Model, ExpressibleByIntegerLiteral {
169+
static let schema = ""
170+
@ID(custom: .id) var id: Int?
171+
init() {}; init(integerLiteral l: Int) { self.id = l }
172+
}
173+
final class Seq: Model, ExpressibleByNilLiteral, ExpressibleByArrayLiteral {
174+
static let schema = "seqs"
175+
@ID(custom: .id) var id: Int?; @OptionalField(key: "list") var list: [Elem]?
176+
init() {}; init(nilLiteral: ()) { self.list = nil }; init(arrayLiteral el: Elem...) { self.list = el }
177+
}
178+
do {
179+
try self.db.schema(Seq.schema).field(.id, .int, .identifier(auto: true)).field("list", .sql(embed: "JSONB[]")).create().wait()
180+
defer { try! db.schema(Seq.schema).delete().wait() }
181+
182+
let s1: Seq = [1, 2], s2: Seq = nil; try [s1, s2].forEach { try $0.create(on: self.db).wait() }
183+
184+
// Make sure it went into the DB as "array of jsonb" rather than as "array of one jsonb containing array" or such.
185+
let raws = try (self.db as! SQLDatabase).raw("SELECT array_to_json(list)::text t FROM seqs").all().wait().map { try $0.decode(column: "t", as: String?.self) }
186+
XCTAssertEqual(raws, [#"[{"id": 1},{"id": 2}]"#, nil])
187+
188+
// Make sure it round-trips through Fluent.
189+
let seqs = try Seq.query(on: self.db).all().wait()
190+
191+
XCTAssertEqual(seqs.count, 2)
192+
XCTAssertEqual(seqs.dropFirst(0).first?.id, s1.id)
193+
XCTAssertEqual(seqs.dropFirst(0).first?.list?.map(\.id), s1.list?.map(\.id))
194+
XCTAssertEqual(seqs.dropFirst(1).first?.id, s2.id)
195+
XCTAssertEqual(seqs.dropFirst(1).first?.list?.map(\.id), s2.list?.map(\.id))
196+
} catch let error {
197+
XCTFail("caught error: \(String(reflecting: error))")
198+
}
199+
}
165200

166201

167202
var benchmarker: FluentBenchmarker {

0 commit comments

Comments
 (0)