-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modernize SQLite feature support (#100)
* Drop support for Swift <5.5 * Same heavy CI update SQLiteNIO got * Do a mostly visual update to SQLiteDialect, removing quite a lot of confusing whitespace and simplifying most of the content. Adds a `customDataType(for:)` override to handling requests for bigint types and switches from plain `?` for placeholders to SQLite's numbered `?NNNN` representation per SQLite docs recommendation. * Tiny bit of tests cleanup * Provide a type implementing SQLDatabaseReportedVersion, vend it from SQLiteDatabase, and use it to enable version-dependent RETURNING and UPSERT support for SQLite. * Require updated SQLiteNIO that has the new APIs
- Loading branch information
Showing
5 changed files
with
211 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,118 @@ | ||
name: test | ||
on: | ||
pull_request: | ||
pull_request: { branches: ['*'] } | ||
push: { branches: [ main ] } | ||
defaults: | ||
run: | ||
shell: bash | ||
|
||
env: | ||
LOG_LEVEL: debug | ||
SWIFT_DETERMINISTIC_HASHING: 1 | ||
|
||
jobs: | ||
dependents: | ||
|
||
codecov: | ||
runs-on: ubuntu-latest | ||
container: swift:5.5-focal | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
dependent: | ||
- fluent-sqlite-driver | ||
container: swift:5.7-jammy | ||
steps: | ||
# N.B.: When we switch to embedded SQLite, these first two steps should be removed, | ||
# and the version saved to the environment should come from the checked-out package. | ||
- name: Install libsqlite3 dependency | ||
run: apt-get -q update && apt-get -q install -y libsqlite3-dev | ||
- name: Save SQLite version to env | ||
run: | | ||
echo SQLITE_VERSION="$(pkg-config --modversion sqlite3)" >> $GITHUB_ENV | ||
- name: Check out package | ||
uses: actions/checkout@v3 | ||
- name: Run local tests with coverage | ||
run: swift test --enable-code-coverage | ||
- name: Submit coverage report to Codecov.io | ||
uses: vapor/[email protected] | ||
with: | ||
cc_flags: 'unittests' | ||
cc_env_vars: 'SWIFT_VERSION,SWIFT_PLATFORM,RUNNER_OS,RUNNER_ARCH,SQLITE_VERSION' | ||
cc_fail_ci_if_error: true | ||
cc_verbose: true | ||
cc_dry_run: false | ||
|
||
# Check for API breakage versus main | ||
api-breakage: | ||
if: github.event_name == 'pull_request' | ||
runs-on: ubuntu-latest | ||
container: swift:5.7-jammy | ||
steps: | ||
- name: Install dependencies | ||
- name: Install libsqlite3 dependency | ||
run: apt-get -q update && apt-get -q install -y libsqlite3-dev | ||
- name: Check out package | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
with: | ||
path: package | ||
- name: Check out dependent | ||
uses: actions/checkout@v2 | ||
fetch-depth: 0 | ||
# https://github.com/actions/checkout/issues/766 | ||
- name: Mark the workspace as safe | ||
run: git config --global --add safe.directory ${GITHUB_WORKSPACE} | ||
- name: Check for API breaking changes | ||
run: swift package diagnose-api-breaking-changes origin/main | ||
|
||
# Make sure downstream dependents still work | ||
dependents-check: | ||
if: github.event_name == 'pull_request' | ||
runs-on: ubuntu-latest | ||
container: swift:5.7-jammy | ||
steps: | ||
- name: Install libsqlite3 dependency | ||
run: apt-get -q update && apt-get -q install -y libsqlite3-dev | ||
- name: Check out package | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: vapor/${{ matrix.dependent }} | ||
path: dependent | ||
ref: main | ||
- name: Use local package | ||
run: swift package edit sqlite-kit --path ../package | ||
working-directory: dependent | ||
- name: Run tests with Thread Sanitizer | ||
run: swift test --enable-test-discovery --sanitize=thread | ||
working-directory: dependent | ||
linux: | ||
path: sqlite-kit | ||
- name: Check out FluentKit driver | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: vapor/fluent-sqlite-driver | ||
path: fluent-sqlite-driver | ||
- name: Tell dependents to use local checkout | ||
run: 'swift package --package-path fluent-sqlite-driver edit sqlite-kit --path sqlite-kit' | ||
- name: Run FluentSQLiteDriver tests with Thread Sanitizer | ||
run: swift test --package-path fluent-sqlite-driver --sanitize=thread | ||
|
||
# Run unit tests (Linux) | ||
linux-unit: | ||
if: github.event_name == 'pull_request' | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
runner: | ||
- swift:5.2-focal | ||
- swift:5.5-focal | ||
- swiftlang/swift:nightly-main-focal | ||
- swift:5.5-bionic | ||
- swift:5.6-focal | ||
- swift:5.7-jammy | ||
- swiftlang/swift:nightly-main-jammy | ||
container: ${{ matrix.runner }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install dependencies | ||
- name: Install libsqlite3 dependency | ||
run: apt-get -q update && apt-get -q install -y libsqlite3-dev | ||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
- name: Run tests with Thread Sanitizer | ||
run: swift test --enable-test-discovery --sanitize=thread | ||
macOS: | ||
run: swift test --sanitize=thread | ||
|
||
|
||
# Run unit tests (macOS). | ||
macos-unit: | ||
if: github.event_name == 'pull_request' | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
version: | ||
- latest | ||
macos: | ||
- macos-11 | ||
- macos-12 | ||
xcode: | ||
- latest-stable | ||
runs-on: macos-11 | ||
runs-on: ${{ matrix.macos }} | ||
steps: | ||
- name: Select latest available Xcode | ||
uses: maxim-lobanov/setup-xcode@v1 | ||
with: | ||
xcode-version: ${{ matrix.version }} | ||
xcode-version: ${{ matrix.xcode }} | ||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
- name: Run tests with Thread Sanitizer | ||
run: swift test --enable-test-discovery --sanitize=thread | ||
run: swift test --sanitize=thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,42 @@ | ||
import SQLKit | ||
|
||
/// The ``SQLDialect`` defintions for SQLite. | ||
/// | ||
/// - Note: There is only ever one SQLite library in use by SQLiteNIO in any given process (even if there are | ||
/// other versions of the library being used by other things). As such, there is no need for the dialect to | ||
/// concern itself with what version the connection using it "connected" to - it can always just look up the | ||
/// global "runtime version". | ||
public struct SQLiteDialect: SQLDialect { | ||
public var name: String { | ||
"sqlite" | ||
} | ||
public var name: String { "sqlite" } | ||
|
||
public var identifierQuote: SQLExpression { | ||
return SQLRaw("\"") | ||
} | ||
|
||
public var literalStringQuote: SQLExpression { | ||
return SQLRaw("'") | ||
} | ||
|
||
public var autoIncrementClause: SQLExpression { | ||
return SQLRaw("AUTOINCREMENT") | ||
} | ||
|
||
public func bindPlaceholder(at position: Int) -> SQLExpression { | ||
return SQLRaw("?") | ||
} | ||
|
||
public func literalBoolean(_ value: Bool) -> SQLExpression { | ||
switch value { | ||
case true: return SQLRaw("TRUE") | ||
case false: return SQLRaw("FALSE") | ||
public var identifierQuote: SQLExpression { SQLRaw("\"") } | ||
public var literalStringQuote: SQLExpression { SQLRaw("'") } | ||
public func bindPlaceholder(at position: Int) -> SQLExpression { SQLRaw("?\(position)") } | ||
public func literalBoolean(_ value: Bool) -> SQLExpression { SQLRaw(value ? "TRUE" : "FALSE") } | ||
public var literalDefault: SQLExpression { SQLLiteral.null } | ||
|
||
public var supportsAutoIncrement: Bool { false } | ||
public var autoIncrementClause: SQLExpression { SQLRaw("AUTOINCREMENT") } | ||
|
||
public var enumSyntax: SQLEnumSyntax { .unsupported } | ||
public var triggerSyntax: SQLTriggerSyntax { .init(create: [.supportsBody, .supportsCondition]) } | ||
public var alterTableSyntax: SQLAlterTableSyntax { .init(allowsBatch: false) } | ||
public var upsertSyntax: SQLUpsertSyntax { self.isAtLeastVersion(3, 24, 0) ? .standard : .unsupported } // `UPSERT` was added to SQLite in 3.24.0. | ||
public var supportsReturning: Bool { self.isAtLeastVersion(3, 35, 0) } // `RETURNING` was added to SQLite in 3.35.0. | ||
public var unionFeatures: SQLUnionFeatures { [.union, .unionAll, .intersect, .except] } | ||
|
||
public func customDataType(for dataType: SQLDataType) -> SQLExpression? { | ||
if case .bigint = dataType { | ||
// Translate requests for bigint to requests for SQLite's plain integer type. This yields the autoincrement | ||
// primary key behavior when a 64-bit integer is requested from a higher layer. | ||
return SQLDataType.int | ||
} | ||
return nil | ||
} | ||
|
||
public var literalDefault: SQLExpression { | ||
return SQLLiteral.null | ||
} | ||
|
||
public var enumSyntax: SQLEnumSyntax { | ||
.unsupported | ||
} | ||
|
||
public var supportsAutoIncrement: Bool { | ||
false | ||
} | ||
|
||
public var alterTableSyntax: SQLAlterTableSyntax { | ||
.init( | ||
alterColumnDefinitionClause: nil, | ||
alterColumnDefinitionTypeKeyword: nil, | ||
allowsBatch: false | ||
) | ||
} | ||
|
||
public var triggerSyntax: SQLTriggerSyntax { | ||
return .init(create: [.supportsBody, .supportsCondition]) | ||
} | ||
public init() { } | ||
|
||
public var unionFeatures: SQLUnionFeatures { | ||
[.union, .unionAll, .intersect, .except] | ||
private func isAtLeastVersion(_ major: Int, _ minor: Int, _ patch: Int) -> Bool { | ||
_SQLiteDatabaseVersion.runtimeVersion.isNotOlder(than: _SQLiteDatabaseVersion(major: major, minor: minor, patch: patch)) | ||
} | ||
|
||
public init() { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters