Skip to content

Commit 9e924b3

Browse files
authored
Allow using DatabaseSchema.deleteField(_:)` with SQLite (#92)
* Remove the artificial restriction on using ALTER TABLE DROP COLUMN (DatabaseSchema.deleteField(_:)) with SQLite; it has been supported since SQLite 3.35.0. Fixes #91. * Can't link to stuff that's not in the same module. Fixes a DocC warning. * Update SQLiteKit and FluentKit dependency requirements
1 parent be7051a commit 9e924b3

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let package = Package(
1313
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.3"),
17-
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.0"),
16+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
17+
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.1"),
1818
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
1919
],
2020
targets: [

[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let package = Package(
1313
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.3"),
17-
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.0"),
16+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
17+
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.1"),
1818
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
1919
],
2020
targets: [

Sources/FluentSQLiteDriver/FluentSQLiteConfiguration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ extension DatabaseConfigurationFactory {
5959
/// - Parameters:
6060
/// - configuration: The underlying `SQLiteConfiguration`.
6161
/// - maxConnnectionsPerEventLoop: Ignored. The value is always treated as 1.
62-
/// - dataEncoder: An ``SQLiteDataEncoder`` used to translate bound query parameters into `SQLiteData` values.
63-
/// - dataDecoder: An ``SQLiteDataDecoder`` used to translate `SQLiteData` values into output values.
62+
/// - dataEncoder: An `SQLiteDataEncoder` used to translate bound query parameters into `SQLiteData` values.
63+
/// - dataDecoder: An `SQLiteDataDecoder` used to translate `SQLiteData` values into output values.
6464
/// - queryLogLevel: The level at which SQL queries issued through the Fluent or SQLKit interfaces will be logged.
6565
/// - Returns: A configuration factory,
6666
public static func sqlite(

Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
4848
case .dataType(_, .enum(_)): return false
4949
default: return true
5050
} }
51-
guard schema.createConstraints.isEmpty, schema.updateFields.isEmpty,
52-
schema.deleteFields.isEmpty, schema.deleteConstraints.isEmpty
53-
else {
51+
guard schema.createConstraints.isEmpty, schema.updateFields.isEmpty, schema.deleteConstraints.isEmpty else {
5452
return self.eventLoop.makeFailedFuture(FluentSQLiteUnsupportedAlter())
5553
}
56-
if schema.createFields.isEmpty { // If there were only enum updates, bail out.
54+
if schema.createFields.isEmpty, schema.deleteFields.isEmpty { // If there were only enum updates, bail out.
5755
return self.eventLoop.makeSucceededFuture(())
5856
}
5957
}

Tests/FluentSQLiteDriverTests/FluentSQLiteDriverTests.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ final class FluentSQLiteDriverTests: XCTestCase {
114114

115115
func revert(on database: any Database) async throws {
116116
try await database.schema("users")
117-
.deleteField("apple_id")
117+
.deleteUnique(on: "apple_id")
118118
.update()
119119
}
120120
}
@@ -128,6 +128,31 @@ final class FluentSQLiteDriverTests: XCTestCase {
128128
}
129129
await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().revert(on: self.database))
130130
}
131+
132+
// https://github.com/vapor/fluent-sqlite-driver/issues/91
133+
func testDeleteFieldMigration() async throws {
134+
struct UserMigration_v1_0_0: AsyncMigration {
135+
func prepare(on database: any Database) async throws {
136+
try await database.schema("users").id().field("email", .string, .required).field("password", .string, .required).create()
137+
}
138+
func revert(on database: any Database) async throws {
139+
try await database.schema("users").delete()
140+
}
141+
}
142+
struct UserMigration_v1_1_0: AsyncMigration {
143+
func prepare(on database: any Database) async throws {
144+
try await database.schema("users").deleteField("password").update()
145+
}
146+
func revert(on database: any Database) async throws {
147+
try await database.schema("users").field("password", .string, .required).update()
148+
}
149+
}
150+
151+
await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().prepare(on: self.database))
152+
await XCTAssertNoThrowAsync(try await UserMigration_v1_1_0().prepare(on: self.database))
153+
await XCTAssertNoThrowAsync(try await UserMigration_v1_1_0().revert(on: self.database))
154+
await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().revert(on: self.database))
155+
}
131156

132157
func testCustomJSON() async throws {
133158
struct Metadata: Codable { let createdAt: Date }

0 commit comments

Comments
 (0)