Skip to content

Commit cf3e62a

Browse files
committed
fix: address PR review findings for Cloudflare D1 plugin
1 parent 535185a commit cf3e62a

3 files changed

Lines changed: 32 additions & 17 deletions

File tree

Plugins/CloudflareD1DriverPlugin/CloudflareD1PluginDriver.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,7 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable
139139

140140
let startTime = Date()
141141
let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)
142-
let anyParams: [Any?] = parameters.map { param -> Any? in
143-
guard let value = param else { return nil }
144-
return value
145-
}
146-
147-
let payload = try await client.executeRaw(sql: trimmed, params: anyParams)
142+
let payload = try await client.executeRaw(sql: trimmed, params: parameters)
148143
let executionTime = Date().timeIntervalSince(startTime)
149144
return mapRawResult(payload, executionTime: executionTime)
150145
}
@@ -220,7 +215,7 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable
220215
let query = """
221216
SELECT m.name AS tbl, p.cid, p.name, p.type, p."notnull", p.dflt_value, p.pk
222217
FROM sqlite_master m, pragma_table_info(m.name) p
223-
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%' AND m.name NOT LIKE '_cf_%'
218+
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%' AND m.name NOT GLOB '_cf_*'
224219
ORDER BY m.name, p.cid
225220
"""
226221
let result = try await execute(query: query)
@@ -259,7 +254,7 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable
259254
p."from" AS column_name, p."to" AS referenced_column,
260255
p.on_update, p.on_delete
261256
FROM sqlite_master m, pragma_foreign_key_list(m.name) p
262-
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%' AND m.name NOT LIKE '_cf_%'
257+
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%' AND m.name NOT GLOB '_cf_*'
263258
ORDER BY m.name, p.id, p.seq
264259
"""
265260
let result = try await execute(query: query)
@@ -569,9 +564,17 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable
569564

570565
// MARK: - Transactions
571566

572-
func beginTransaction() async throws {}
573-
func commitTransaction() async throws {}
574-
func rollbackTransaction() async throws {}
567+
func beginTransaction() async throws {
568+
throw CloudflareD1Error(message: String(localized: "Transactions are not supported by Cloudflare D1"))
569+
}
570+
571+
func commitTransaction() async throws {
572+
throw CloudflareD1Error(message: String(localized: "Transactions are not supported by Cloudflare D1"))
573+
}
574+
575+
func rollbackTransaction() async throws {
576+
throw CloudflareD1Error(message: String(localized: "Transactions are not supported by Cloudflare D1"))
577+
}
575578

576579
// MARK: - Private Helpers
577580

Plugins/CloudflareD1DriverPlugin/D1HttpClient.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,17 @@ final class D1HttpClient: @unchecked Sendable {
363363
message: String(localized: "Authentication failed. Check your API token and Account ID.")
364364
)
365365
case 429:
366-
let retryAfter = response.value(forHTTPHeaderField: "Retry-After") ?? "unknown"
367-
Self.logger.warning("D1 rate limited. Retry-After: \(retryAfter)")
368-
throw D1HttpError(
369-
message: String(localized: "Rate limited by Cloudflare. Retry after \(retryAfter) seconds.")
370-
)
366+
let retryAfter = response.value(forHTTPHeaderField: "Retry-After")
367+
Self.logger.warning("D1 rate limited. Retry-After: \(retryAfter ?? "not specified")")
368+
if let seconds = retryAfter {
369+
throw D1HttpError(
370+
message: String(localized: "Rate limited by Cloudflare. Retry after \(seconds) seconds.")
371+
)
372+
} else {
373+
throw D1HttpError(
374+
message: String(localized: "Rate limited by Cloudflare. Please try again later.")
375+
)
376+
}
371377
default:
372378
if let errorResponse = try? JSONDecoder().decode(
373379
D1ApiResponse<D1RawResultPayload>.self, from: data

TablePro/Views/Connection/ConnectionFormView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,13 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
874874
// Host and port can be empty (will use defaults: localhost and default port)
875875
let mode = PluginManager.shared.connectionMode(for: type)
876876
let requiresDatabase = mode == .fileBased || mode == .apiOnly
877-
let basicValid = !name.isEmpty && (requiresDatabase ? !database.isEmpty : true)
877+
var basicValid = !name.isEmpty && (requiresDatabase ? !database.isEmpty : true)
878+
if mode == .apiOnly {
879+
let hasRequiredFields = authSectionFields
880+
.filter(\.isRequired)
881+
.allSatisfy { !(additionalFieldValues[$0.id] ?? "").isEmpty }
882+
basicValid = basicValid && hasRequiredFields && !password.isEmpty
883+
}
878884
if sshEnabled {
879885
let sshPortValid = sshPort.isEmpty || (Int(sshPort).map { (1...65_535).contains($0) } ?? false)
880886
let sshValid = !sshHost.isEmpty && !sshUsername.isEmpty && sshPortValid

0 commit comments

Comments
 (0)