Skip to content

Commit 08b28a5

Browse files
CDRIVER-4321: Support a "rename" operation for collections (#1011)
CDRIVER-4321: Support a "rename" operation for collections This adds support for the "rename" operation in the unified test format this will rename the collection in the server. This changeset does not include test updates, but was manually validated locally against the updated changestreams tests, which cannot be enabled without additional changes that are beyond the scope of this commit.
1 parent 3c5d98c commit 08b28a5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/libmongoc/tests/unified/operation.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,50 @@ operation_assert_number_connections_checked_out (test_t *test,
27612761
return true;
27622762
}
27632763

2764+
static bool
2765+
operation_rename (test_t *test,
2766+
operation_t *op,
2767+
result_t *result,
2768+
bson_error_t *error)
2769+
{
2770+
// First validate the arguments
2771+
const char *object = op->object;
2772+
bson_parser_t *bp = bson_parser_new ();
2773+
bool ret = false;
2774+
char *new_name = NULL;
2775+
bson_parser_utf8 (bp, "to", &new_name);
2776+
bool parse_ok = bson_parser_parse (bp, op->arguments, error);
2777+
bson_parser_destroy (bp);
2778+
if (!parse_ok) {
2779+
goto done;
2780+
}
2781+
2782+
// Now get the entity
2783+
entity_t *ent = entity_map_get (test->entity_map, object, error);
2784+
if (!ent) {
2785+
goto done;
2786+
}
2787+
// We only support collections so far
2788+
if (0 != strcmp (ent->type, "collection")) {
2789+
test_set_error (
2790+
error,
2791+
"'rename' is only supported for collection objects '%s' has type '%s'",
2792+
object,
2793+
ent->type);
2794+
goto done;
2795+
}
2796+
// Rename the collection in the server,
2797+
mongoc_collection_t *coll = ent->value;
2798+
if (!mongoc_collection_rename (coll, coll->db, new_name, false, error)) {
2799+
goto done;
2800+
}
2801+
result_from_ok (result);
2802+
ret = true;
2803+
done:
2804+
bson_free (new_name);
2805+
return ret;
2806+
}
2807+
27642808
typedef struct {
27652809
const char *op;
27662810
bool (*fn) (test_t *, operation_t *, result_t *, bson_error_t *);
@@ -2809,6 +2853,7 @@ operation_run (test_t *test, bson_t *op_bson, bson_error_t *error)
28092853
{"replaceOne", operation_replace_one},
28102854
{"updateOne", operation_update_one},
28112855
{"updateMany", operation_update_many},
2856+
{"rename", operation_rename},
28122857

28132858
/* Change stream and cursor operations */
28142859
{"iterateUntilDocumentOrError",

0 commit comments

Comments
 (0)