Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
)
}

/// A collection drop is a shell statement, not a SQL one: `db.getCollection("<name>").drop()`.
/// The app-level fallback would emit `DROP TABLE <name>`, which the Mongo shell parser rejects.
/// Mongo has no schemas or cascade, so both are ignored.
func dropObjectStatement(name: String, objectType: String, schema: String?, cascade: Bool) -> String? {
"db.getCollection(\"\(escapeJsonString(name))\").drop()"
}

func dropDatabase(name: String) async throws {
guard let conn = mongoConnection else {
throw MongoDBPluginError.notConnected
Expand Down
23 changes: 23 additions & 0 deletions TableProTests/Core/MongoDB/MongoShellParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ struct MongoShellParserTests {
}
}

@Test("getCollection with drop")
func testGetCollectionDrop() throws {
let op = try MongoShellParser.parse("db.getCollection(\"audit-logs.2026\").drop()")
if case .drop(let collection) = op {
#expect(collection == "audit-logs.2026")
} else {
Issue.record("Expected .drop operation")
}
}

@Test("SQL DROP TABLE fallback is rejected for MongoDB collections")
func testDropTableFallbackIsRejected() {
// The sidebar's drop used to fall through to the SQL fallback and emit
// `DROP TABLE "<collection>"`, which the Mongo shell parser rejects. Dropping a
// collection must go through `db.getCollection("<collection>").drop()` instead.
do {
_ = try MongoShellParser.parse("DROP TABLE \"users\"")
Issue.record("Expected the SQL DROP TABLE fallback to be rejected")
} catch {
// Any parse failure is the point: a Mongo connection must never send SQL.
}
}

@Test("runCommand")
func testRunCommand() throws {
let op = try MongoShellParser.parse("db.runCommand({\"ping\": 1})")
Expand Down
Loading