Skip to content

Commit

Permalink
CXX-727 Add a name setter for renaming collections
Browse files Browse the repository at this point in the history
Until now we had support for getting a collection's name by
using the getter collection::name(...); this overloads this
method with an additional name setter.
  • Loading branch information
koraa committed Nov 4, 2015
1 parent 51c2f15 commit a1aa45b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/mongocxx/collection.cpp
Expand Up @@ -65,6 +65,20 @@ stdx::string_view collection::name() const noexcept {
return _impl->name;
}

void collection::rename(stdx::string_view new_name, bool drop_target_before_rename) {
bson_error_t error;

auto result = libmongoc::collection_rename(_impl->collection_t, _impl->database_name.c_str(),
new_name.data(), drop_target_before_rename, &error);

if (!result) {
throw exception::operation(std::make_tuple(error.message, error.code));
}

_impl->name = (std::string)new_name;
}


collection::collection(const database& database, stdx::string_view collection_name)
: _impl(stdx::make_unique<impl>(
libmongoc::database_get_collection(database._impl->database_t, collection_name.data()),
Expand Down
12 changes: 12 additions & 0 deletions src/mongocxx/collection.hpp
Expand Up @@ -476,6 +476,18 @@ class MONGOCXX_API collection {
///
stdx::string_view name() const noexcept;

///
/// Rename this collection.
///
/// @param new_name The new name to assign to the collection.
/// @param drop_target_before_rename Whether to overwrite any
/// existing collections called new_name. The default is false.
///
/// @throws exception::operation if the operation fails.
///
void rename(stdx::string_view new_name, bool drop_target_before_rename = false);


///
/// Sets the read_preference for this collection. Changes will not have any effect on existing
/// cursors or other read operations which use the read preference.
Expand Down
1 change: 1 addition & 0 deletions src/mongocxx/private/libmongoc_symbols.hpp
Expand Up @@ -44,6 +44,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(client_pool_try_pop)
MONGOCXX_LIBMONGOC_SYMBOL(client_set_read_prefs)
MONGOCXX_LIBMONGOC_SYMBOL(client_set_ssl_opts)
MONGOCXX_LIBMONGOC_SYMBOL(client_set_write_concern)
MONGOCXX_LIBMONGOC_SYMBOL(collection_rename)
MONGOCXX_LIBMONGOC_SYMBOL(collection_aggregate)
MONGOCXX_LIBMONGOC_SYMBOL(collection_count)
MONGOCXX_LIBMONGOC_SYMBOL(collection_count_with_opts)
Expand Down
20 changes: 20 additions & 0 deletions src/mongocxx/test/collection_mocked.cpp
Expand Up @@ -466,4 +466,24 @@ TEST_CASE("Collection", "[collection]") {
REQUIRE(bulk_operation_execute_called);
REQUIRE(bulk_operation_destroy_called);
}

SECTION("Rename Fails, name stays the same", "collection::rename") {
bool collection_rename_called = false, collection_rename_error = false;
std::string old_name = (std::string)mongo_coll.name();

collection_rename->interpose([&](mongoc_collection_t *, const char*, const char*, bool, bson_error_t*) {
collection_rename_called = true;
return false;
});

try {
mongo_coll.rename("bad_name");
} catch (exception::operation &) {
collection_rename_error = true;
}

REQUIRE(collection_rename_called);
REQUIRE(collection_rename_error);
REQUIRE(mongocxx::stdx::string_view{old_name} == mongo_coll.name());
}
}
3 changes: 2 additions & 1 deletion src/third_party/catch/include/helpers.hpp
Expand Up @@ -103,7 +103,8 @@
auto collection_count_with_opts = libmongoc::collection_count_with_opts.create_instance(); \
auto collection_create_index = libmongoc::collection_create_index.create_instance(); \
auto collection_find = libmongoc::collection_find.create_instance(); \
auto collection_aggregate = libmongoc::collection_aggregate.create_instance();
auto collection_aggregate = libmongoc::collection_aggregate.create_instance(); \
auto collection_rename = libmongoc::collection_rename.create_instance();

#define MOCK_CURSOR \
auto cursor_destroy = libmongoc::cursor_destroy.create_instance(); \
Expand Down

0 comments on commit a1aa45b

Please sign in to comment.