Skip to content

Commit bd4baba

Browse files
authored
SWIFT-493 Support passing sessions to "drop" commands (#275)
1 parent 72ee41c commit bd4baba

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

Sources/MongoSwift/MongoCollection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public class MongoCollection<T: Codable> {
8787
/// Drops this collection from its parent database.
8888
/// - Throws:
8989
/// - `ServerError.commandError` if an error occurs that prevents the command from executing.
90-
public func drop() throws {
91-
let operation = DropCollectionOperation(collection: self)
90+
public func drop(session: ClientSession? = nil) throws {
91+
let operation = DropCollectionOperation(collection: self, session: session)
9292
try operation.execute()
9393
}
9494
}

Sources/MongoSwift/MongoDatabase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public class MongoDatabase {
132132
/// Drops this database.
133133
/// - Throws:
134134
/// - `ServerError.commandError` if an error occurs that prevents the command from executing.
135-
public func drop() throws {
136-
let operation = DropDatabaseOperation(database: self)
135+
public func drop(session: ClientSession? = nil) throws {
136+
let operation = DropDatabaseOperation(database: self, session: session)
137137
try operation.execute()
138138
}
139139

Sources/MongoSwift/Operations/DropCollectionOperation.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import mongoc
33
/// An operation corresponding to a "drop" command on a MongoCollection.
44
internal struct DropCollectionOperation<T: Codable>: Operation {
55
private let collection: MongoCollection<T>
6+
private let session: ClientSession?
67

7-
internal init(collection: MongoCollection<T>) {
8+
internal init(collection: MongoCollection<T>, session: ClientSession?) {
89
self.collection = collection
10+
self.session = session
911
}
1012

1113
internal func execute() throws {
14+
let command: Document = ["drop": self.collection.name]
15+
let opts = try encodeOptions(options: Document(), session: self.session)
16+
17+
var reply = Document()
1218
var error = bson_error_t()
13-
guard mongoc_collection_drop(self.collection._collection, &error) else {
14-
throw parseMongocError(error)
19+
let success = withMutableBSONPointer(to: &reply) { replyPtr in
20+
mongoc_collection_write_command_with_opts(
21+
self.collection._collection, command._bson, opts?._bson, replyPtr, &error)
22+
}
23+
guard success else {
24+
throw getErrorFromReply(bsonError: error, from: reply)
1525
}
1626
}
1727
}

Sources/MongoSwift/Operations/DropDatabaseOperation.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import mongoc
33
/// An operation corresponding to a "drop" command on a MongoDatabase.
44
internal struct DropDatabaseOperation: Operation {
55
private let database: MongoDatabase
6+
private let session: ClientSession?
67

7-
internal init(database: MongoDatabase) {
8+
internal init(database: MongoDatabase, session: ClientSession?) {
89
self.database = database
10+
self.session = session
911
}
1012

1113
internal func execute() throws {
14+
let command: Document = ["dropDatabase": 1]
15+
let opts = try encodeOptions(options: Document(), session: self.session)
16+
17+
var reply = Document()
1218
var error = bson_error_t()
13-
guard mongoc_database_drop(self.database._database, &error) else {
14-
throw parseMongocError(error)
19+
let success = withMutableBSONPointer(to: &reply) { replyPtr in
20+
mongoc_database_write_command_with_opts(
21+
self.database._database, command._bson, opts?._bson, replyPtr, &error)
22+
}
23+
guard success else {
24+
throw getErrorFromReply(bsonError: error, from: reply)
1525
}
1626
}
1727
}

Tests/MongoSwiftTests/ClientSessionTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ final class ClientSessionTests: MongoSwiftTestCase {
5151
(name: "listIndexes", body: { _ = try $0.listIndexes(session: $1).next() }),
5252
(name: "findOneAndDelete", body: { _ = try $0.findOneAndDelete([:], session: $1) }),
5353
(name: "findOneAndReplace", body: { _ = try $0.findOneAndReplace(filter: [:], replacement: [:], session: $1) }),
54-
(name: "findOneAndUpdate", body: { _ = try $0.findOneAndUpdate(filter: [:], update: [:], session: $1) })
54+
(name: "findOneAndUpdate", body: { _ = try $0.findOneAndUpdate(filter: [:], update: [:], session: $1) }),
55+
(name: "drop", body: { _ = try $0.drop(session: $1) })
5556
]
5657

5758
// list of operations on MongoDatabase that take in a session
5859
let databaseSessionOps: [DatabaseSessionOp] = [
5960
(name: "runCommand", { try $0.runCommand(["isMaster": 0], session: $1) }),
6061
(name: "createCollection", body: { _ = try $0.createCollection("asdf", session: $1) }),
61-
(name: "createCollection1", body: { _ = try $0.createCollection("asdf", withType: Document.self, session: $1) })
62+
(name: "createCollection1", body: { _ = try $0.createCollection("asf", withType: Document.self, session: $1) }),
63+
(name: "drop", body: { _ = try $0.drop(session: $1) })
6264
]
6365

6466
// list of operatoins on MongoClient that take in a session

0 commit comments

Comments
 (0)