Skip to content

Remove use of legacy codepath when establishing Postgres connections #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/projectboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ on:
jobs:
update_project_boards:
name: Update project boards
uses: vapor/ci/.github/workflows/update-project-boards-for-issue.yml@reusable-workflows
uses: vapor/ci/.github/workflows/update-project-boards-for-issue.yml@main
secrets: inherit
182 changes: 95 additions & 87 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,100 +27,120 @@ jobs:
api-breakage:
if: ${{ !(github.event.pull_request.draft || false) }}
runs-on: ubuntu-latest
container: swift:5.8-jammy
container: swift:jammy
steps:
- name: Check out code
uses: actions/checkout@v3
with: { 'fetch-depth': 0 }
- name: Run API breakage check action
uses: vapor/ci/.github/actions/ci-swift-check-api-breakage@reusable-workflows
uses: vapor/ci/.github/actions/ci-swift-check-api-breakage@main

code-coverage:
if: ${{ !(github.event.pull_request.draft || false) }}
runs-on: ubuntu-latest
container: swift:jammy
services:
psql-a:
image: postgres:15
env:
POSTGRES_USER: test_username
POSTGRES_DB: test_database
POSTGRES_PASSWORD: test_password
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Run unit tests for coverage data
run: swift test --enable-code-coverage
- name: Upload coverage data
uses: vapor/[email protected]

gh-codeql:
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
runner_os:
- ubuntu-latest
- macos-13
runs-on: ${{ matrix.runner_os }}
permissions:
security-events: write
steps:
- name: Select appropriate Xcode version
if: ${{ startsWith(matrix.runner_os, 'macos') }}
uses: maxim-lobanov/setup-xcode@v1
with: { xcode-version: '~14.3' }
- name: Check out code
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with: { languages: swift }
- name: Perform build
run: swift build
- name: Run CodeQL analyze
uses: github/codeql-action/analyze@v2

# Run Linux unit tests against various configurations
linux-unit:
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
dbimage: ['postgres:15', 'postgres:13', 'postgres:11']
swiftver: [
'swift:5.7-jammy', 'swift:5.8-jammy',
'swiftlang/swift:nightly-5.9-jammy',
'swiftlang/swift:nightly-main-jammy'
]
include: [
{dbimage: 'postgres:15', dbauth: 'scram-sha-256'},
{dbimage: 'postgres:13', dbauth: 'md5'},
{dbimage: 'postgres:11', dbauth: 'trust'}
]
postgres-image:
- postgres:15
- postgres:13
- postgres:11
swift-image:
- swift:5.7-jammy
- swift:5.8-jammy
- swiftlang/swift:nightly-5.9-jammy
- swiftlang/swift:nightly-main-jammy
include:
- postgres-image: postgres:15
postgres-auth: scram-sha-256
- postgres-image: postgres:13
postgres-auth: md5
- postgres-image: postgres:11
postgres-auth: trust
runs-on: ubuntu-latest
container: ${{ matrix.swiftver }}
container: ${{ matrix.swift-image }}
services:
psql-a:
image: ${{ matrix.dbimage }}
image: ${{ matrix.postgres-image }}
env:
POSTGRES_USER: 'test_username'
POSTGRES_DB: 'test_database'
POSTGRES_PASSWORD: 'test_password'
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
psql-b:
image: ${{ matrix.dbimage }}
env:
POSTGRES_USER: 'test_username'
POSTGRES_DB: 'test_database'
POSTGRES_PASSWORD: 'test_password'
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
POSTGRES_USER: test_username
POSTGRES_DB: test_database
POSTGRES_PASSWORD: test_password
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.postgres-auth }}
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.postgres-auth }}
steps:
- name: Check out package
uses: actions/checkout@v3
- name: Run local tests
run: swift test --enable-code-coverage
- name: Note Swift version
if: ${{ contains(matrix.swiftver, 'nightly') }}
run: |
echo "SWIFT_PLATFORM=$(. /etc/os-release && echo "${ID}${VERSION_ID}")" >>"${GITHUB_ENV}"
echo "SWIFT_VERSION=$(cat /.swift_tag)" >>"${GITHUB_ENV}"
- name: Upload code coverage
uses: vapor/[email protected]
env:
POSTGRES_VERSION: ${{ matrix.dbimage }}
POSTGRES_AUTH_METHOD: ${{ matrix.dbauth }}
with:
cc_env_vars: 'SWIFT_VERSION,SWIFT_PLATFORM,RUNNER_OS,RUNNER_ARCH,POSTGRES_VERSION,POSTGRES_AUTH_METHOD'
run: swift test

# Test integration with dependent package on Linux
linux-integration:
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
dbimage: ['postgres:15']
dbauth: ['scram-sha-256']
swiftver: [
'swift:5.7-jammy', 'swift:5.8-jammy',
'swiftlang/swift:nightly-5.9-jammy',
'swiftlang/swift:nightly-main-jammy'
]
runs-on: ubuntu-latest
container: ${{ matrix.swiftver }}
container: swift:5.8-jammy
services:
psql-a:
image: ${{ matrix.dbimage }}
image: postgres:15
env:
POSTGRES_USER: 'test_username'
POSTGRES_DB: 'test_database'
POSTGRES_PASSWORD: 'test_password'
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
POSTGRES_USER: test_username
POSTGRES_DB: test_database
POSTGRES_PASSWORD: test_password
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
psql-b:
image: ${{ matrix.dbimage }}
image: postgres:15
env:
POSTGRES_USER: 'test_username'
POSTGRES_DB: 'test_database'
POSTGRES_PASSWORD: 'test_password'
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.dbauth }}
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.dbauth }}
POSTGRES_USER: test_username
POSTGRES_DB: test_database
POSTGRES_PASSWORD: test_password
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
steps:
- name: Check out package
uses: actions/checkout@v3
Expand All @@ -133,43 +153,31 @@ jobs:
- name: Run fluent-postgres-kit tests
run: swift test --package-path fluent-postgres-driver

# Run macOS unit tests against various configurations
macos-unit:
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
dbimage: ['postgresql@14']
dbauth: ['scram-sha-256']
macos: ['macos-13']
xcode: ['latest-stable']
runs-on: ${{ matrix.macos }}
xcode-version:
- '~14.3'
- '15.0-beta'
runs-on: macos-13
env:
POSTGRES_HOSTNAME: 127.0.0.1
POSTGRES_DB: postgres
steps:
- name: Select latest available Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: ${{ matrix.xcode }}
xcode-version: ${{ matrix.xcode-version }}
- name: Install Postgres, setup DB and auth, and wait for server start
env:
POSTGRES_VERSION: ${{ matrix.dbimage }}
POSTGRES_AUTH_METHOD: ${{ matrix.dbauth }}
run: |
export PATH="$(brew --prefix)/opt/${POSTGRES_VERSION}/bin:$PATH" PGDATA=/tmp/vapor-postgres-test
(brew unlink postgresql || true) && brew install "${POSTGRES_VERSION}" && brew link --force "${POSTGRES_VERSION}"
initdb --locale=C --auth-host "${POSTGRES_AUTH_METHOD}" -U "${POSTGRES_USER}" --pwfile=<(echo "${POSTGRES_PASSWORD}")
export PATH="$(brew --prefix)/opt/postgresql@14/bin:$PATH" PGDATA=/tmp/vapor-postgres-test
(brew unlink postgresql || true) && brew install "postgresql@14" && brew link --force "postgresql@14"
initdb --locale=C --auth-host "scram-sha-256" -U "${POSTGRES_USER}" --pwfile=<(echo "${POSTGRES_PASSWORD}")
pg_ctl start --wait
timeout-minutes: 2
- name: Checkout code
uses: actions/checkout@v3
- name: Run local tests
run: swift test --enable-code-coverage
- name: Upload code coverage
uses: vapor/[email protected]
env:
POSTGRES_VERSION: ${{ matrix.dbimage }}
POSTGRES_AUTH_METHOD: ${{ matrix.dbauth }}
with:
cc_env_vars: 'MD_APPLE_SDK_ROOT,SWIFT_VERSION,SWIFT_PLATFORM,RUNNER_OS,RUNNER_ARCH,POSTGRES_VERSION,POSTGRES_AUTH_METHOD'
run: swift test
7 changes: 5 additions & 2 deletions Sources/PostgresKit/PostgresConnectionSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Atomics
import AsyncKit
import Logging
import PostgresNIO
import SQLKit
import NIOCore

public struct PostgresConnectionSource: ConnectionPoolSource {
Expand All @@ -27,8 +28,10 @@ public struct PostgresConnectionSource: ConnectionPoolSource {

if let searchPath = self.sqlConfiguration.searchPath {
return connectionFuture.flatMap { conn in
let string = searchPath.map { #""\#($0)""# }.joined(separator: ", ")
return conn.simpleQuery("SET search_path = \(string)").map { _ in conn }
conn.sql(queryLogLevel: nil)
.raw("SET search_path TO \(idents: searchPath, joinedBy: ",")")
.run()
.map { _ in conn }
}
} else {
return connectionFuture
Expand Down
6 changes: 3 additions & 3 deletions Tests/PostgresKitTests/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ extension SQLPostgresConfiguration {
.init(
hostname: env("POSTGRES_HOSTNAME") ?? "localhost",
port: env("POSTGRES_PORT").flatMap(Int.init) ?? Self.ianaPortNumber,
username: env("POSTGRES_USER") ?? "vapor_username",
password: env("POSTGRES_PASSWORD") ?? "vapor_password",
database: env("POSTGRES_DB") ?? "vapor_database",
username: env("POSTGRES_USER") ?? "test_username",
password: env("POSTGRES_PASSWORD") ?? "test_password",
database: env("POSTGRES_DB") ?? "test_database",
tls: .disable
)
}
Expand Down