Skip to content

Commit

Permalink
Allow using DatabaseSchema.deleteField(_:)` with SQLite (#92)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gwynne authored May 16, 2024
1 parent be7051a commit 9e924b3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let package = Package(
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.3"),
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.1"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
],
targets: [
Expand Down
4 changes: 2 additions & 2 deletions Package@swift-5.9.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let package = Package(
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.3"),
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.5.1"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
],
targets: [
Expand Down
4 changes: 2 additions & 2 deletions Sources/FluentSQLiteDriver/FluentSQLiteConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ extension DatabaseConfigurationFactory {
/// - Parameters:
/// - configuration: The underlying `SQLiteConfiguration`.
/// - maxConnnectionsPerEventLoop: Ignored. The value is always treated as 1.
/// - dataEncoder: An ``SQLiteDataEncoder`` used to translate bound query parameters into `SQLiteData` values.
/// - dataDecoder: An ``SQLiteDataDecoder`` used to translate `SQLiteData` values into output values.
/// - dataEncoder: An `SQLiteDataEncoder` used to translate bound query parameters into `SQLiteData` values.
/// - dataDecoder: An `SQLiteDataDecoder` used to translate `SQLiteData` values into output values.
/// - queryLogLevel: The level at which SQL queries issued through the Fluent or SQLKit interfaces will be logged.
/// - Returns: A configuration factory,
public static func sqlite(
Expand Down
6 changes: 2 additions & 4 deletions Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
case .dataType(_, .enum(_)): return false
default: return true
} }
guard schema.createConstraints.isEmpty, schema.updateFields.isEmpty,
schema.deleteFields.isEmpty, schema.deleteConstraints.isEmpty
else {
guard schema.createConstraints.isEmpty, schema.updateFields.isEmpty, schema.deleteConstraints.isEmpty else {
return self.eventLoop.makeFailedFuture(FluentSQLiteUnsupportedAlter())
}
if schema.createFields.isEmpty { // If there were only enum updates, bail out.
if schema.createFields.isEmpty, schema.deleteFields.isEmpty { // If there were only enum updates, bail out.
return self.eventLoop.makeSucceededFuture(())
}
}
Expand Down
27 changes: 26 additions & 1 deletion Tests/FluentSQLiteDriverTests/FluentSQLiteDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class FluentSQLiteDriverTests: XCTestCase {

func revert(on database: any Database) async throws {
try await database.schema("users")
.deleteField("apple_id")
.deleteUnique(on: "apple_id")
.update()
}
}
Expand All @@ -128,6 +128,31 @@ final class FluentSQLiteDriverTests: XCTestCase {
}
await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().revert(on: self.database))
}

// https://github.com/vapor/fluent-sqlite-driver/issues/91
func testDeleteFieldMigration() async throws {
struct UserMigration_v1_0_0: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema("users").id().field("email", .string, .required).field("password", .string, .required).create()
}
func revert(on database: any Database) async throws {
try await database.schema("users").delete()
}
}
struct UserMigration_v1_1_0: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema("users").deleteField("password").update()
}
func revert(on database: any Database) async throws {
try await database.schema("users").field("password", .string, .required).update()
}
}

await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().prepare(on: self.database))
await XCTAssertNoThrowAsync(try await UserMigration_v1_1_0().prepare(on: self.database))
await XCTAssertNoThrowAsync(try await UserMigration_v1_1_0().revert(on: self.database))
await XCTAssertNoThrowAsync(try await UserMigration_v1_0_0().revert(on: self.database))
}

func testCustomJSON() async throws {
struct Metadata: Codable { let createdAt: Date }
Expand Down

0 comments on commit 9e924b3

Please sign in to comment.