Skip to content

Commit 58930f2

Browse files
authored
Clarify ALTER TABLE restrictions (#66)
* clarify sqlite ALTER TABLE restrictions * cleanup tests * bump
1 parent e931b6f commit 58930f2

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

.github/workflows/api-docs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ on:
33
push:
44
branches:
55
- master
6-
76
jobs:
87
deploy:
98
name: api.vapor.codes

.github/workflows/test.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
name: Test Matrix
2-
1+
name: test
32
on:
43
pull_request:
54
push:
65
branches:
76
- master
8-
97
defaults:
108
run:
119
shell: bash
12-
1310
jobs:
14-
15-
Linux:
11+
linux:
1612
runs-on: ubuntu-latest
1713
strategy:
1814
fail-fast: false
@@ -34,11 +30,11 @@ jobs:
3430
- swiftlang/swift:nightly-master-centos8
3531
- swiftlang/swift:nightly-master-amazonlinux2
3632
include:
37-
- depscmd: 'apt-get -q update && apt-get -q install -y libsqlite3-dev'
33+
- depscmd: apt-get -q update && apt-get -q install -y libsqlite3-dev
3834
- image: swiftlang/swift:nightly-master-centos8
39-
depscmd: 'dnf install -y sqlite-devel'
35+
depscmd: dnf install -y sqlite-devel
4036
- image: swiftlang/swift:nightly-master-amazonlinux2
41-
depscmd: 'yum install -y sqlite-devel'
37+
depscmd: yum install -y sqlite-devel
4238
container: ${{ matrix.image }}
4339
steps:
4440
- name: Install dependencies
@@ -57,14 +53,15 @@ jobs:
5753
uses: actions/checkout@v2
5854
- name: Run tests with Thread Sanitizer
5955
run: swift test --enable-test-discovery --sanitize=thread
60-
6156
macOS:
6257
runs-on: macos-latest
6358
steps:
6459
- name: Select latest available Xcode
6560
uses: maxim-lobanov/setup-xcode@1.0
66-
with: { 'xcode-version': 'latest' }
61+
with:
62+
xcode-version: latest
6763
- name: Checkout code
6864
uses: actions/checkout@v2
6965
- name: Run tests with Thread Sanitizer
7066
run: swift test --enable-test-discovery --sanitize=thread
67+

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ database.db
88
Package.resolved
99
DerivedData
1010
.swiftpm
11-
11+
Tests/LinuxMain.swift

Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,34 @@ extension _FluentSQLiteDatabase: Database {
6868
}
6969

7070
func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
71+
var schema = schema
7172
switch schema.action {
7273
case .update:
74+
// Remove enum updates as they are unnecessary.
75+
schema.updateFields = schema.updateFields.filter({
76+
switch $0 {
77+
case .custom:
78+
return true
79+
case .dataType(_, let dataType):
80+
switch dataType {
81+
case .enum:
82+
return false
83+
default:
84+
return true
85+
}
86+
}
87+
})
88+
guard
89+
schema.createConstraints.isEmpty,
90+
schema.updateFields.isEmpty,
91+
schema.deleteFields.isEmpty,
92+
schema.deleteConstraints.isEmpty
93+
else {
94+
return self.eventLoop.makeFailedFuture(FluentSQLiteError.unsupportedAlter)
95+
}
96+
97+
// If only enum updates, then skip.
7398
if schema.createFields.isEmpty {
74-
self.logger.warning("Ignoring schema update. SQLite only supports adding columns to existing tables")
7599
return self.eventLoop.makeSucceededFuture(())
76100
}
77101
default:
@@ -103,6 +127,17 @@ extension _FluentSQLiteDatabase: Database {
103127
}
104128
}
105129

130+
private enum FluentSQLiteError: Error, CustomStringConvertible {
131+
case unsupportedAlter
132+
133+
var description: String {
134+
switch self {
135+
case .unsupportedAlter:
136+
return "SQLite only supports adding columns in ALTER TABLE statements."
137+
}
138+
}
139+
}
140+
106141
extension _FluentSQLiteDatabase: SQLDatabase {
107142
var dialect: SQLDialect {
108143
SQLiteDialect()

Tests/FluentSQLiteDriverTests/FluentSQLiteDriverTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ final class FluentSQLiteDriverTests: XCTestCase {
6666
XCTFail("\(error)")
6767
}
6868
}
69+
70+
// https://github.com/vapor/fluent-sqlite-driver/issues/62
71+
func testUnsupportedUpdateMigration() throws {
72+
struct UserMigration_v1_0_0: Migration {
73+
func prepare(on database: Database) -> EventLoopFuture<Void> {
74+
database.schema("users")
75+
.id()
76+
.field("email", .string, .required)
77+
.field("password", .string, .required)
78+
.unique(on: "email")
79+
.create()
80+
}
81+
82+
func revert(on database: Database) -> EventLoopFuture<Void> {
83+
database.schema("users").delete()
84+
}
85+
}
86+
struct UserMigration_v1_2_0: Migration {
87+
func prepare(on database: Database) -> EventLoopFuture<Void> {
88+
database.schema("users")
89+
.field("apple_id", .string)
90+
.unique(on: "apple_id")
91+
.update()
92+
}
93+
94+
func revert(on database: Database) -> EventLoopFuture<Void> {
95+
database.schema("users")
96+
.deleteField("apple_id")
97+
.update()
98+
}
99+
}
100+
try UserMigration_v1_0_0().prepare(on: self.database).wait()
101+
do {
102+
try UserMigration_v1_2_0().prepare(on: self.database).wait()
103+
try UserMigration_v1_2_0().revert(on: self.database).wait()
104+
} catch {
105+
print(error)
106+
XCTAssertTrue("\(error)".contains("adding columns"))
107+
}
108+
try UserMigration_v1_0_0().revert(on: self.database).wait()
109+
}
69110

70111
var benchmarker: FluentBenchmarker {
71112
return .init(databases: self.dbs)

0 commit comments

Comments
 (0)