Drop values must be at the top for drop+add use case.

This commit is contained in:
Kishore Nallan 2022-07-14 12:18:12 +05:30
parent 4242383db7
commit c2856b02b5
2 changed files with 48 additions and 0 deletions

View File

@ -2843,6 +2843,12 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
// we will first do a pass at basic validations and pick out fields to be deleted
std::set<std::string> delete_field_names;
// ensure that drop values are at the top: required for drop+add use case
std::sort(schema_changes["fields"].begin(), schema_changes["fields"].end(),
[](nlohmann::json& a, nlohmann::json& b) {
return a.contains("drop") > b.contains("drop");
});
for(const auto& kv: schema_changes["fields"].items()) {
if (!kv.value().is_object()) {
return Option<bool>(400, err_msg);

View File

@ -983,3 +983,45 @@ TEST_F(CollectionSchemaChangeTest, ChangeFromStringStarToAutoField) {
ASSERT_EQ(2, coll1->get_fields().size());
ASSERT_EQ(1, coll1->get_dynamic_fields().size());
}
TEST_F(CollectionSchemaChangeTest, ChangeFromGeoToIntField) {
nlohmann::json req_json = R"({
"name": "coll1",
"fields": [
{"name": "loc", "type": "geopoint"}
]
})"_json;
auto coll1_op = collectionManager.create_collection(req_json);
ASSERT_TRUE(coll1_op.ok());
auto coll1 = coll1_op.get();
nlohmann::json doc;
doc["id"] = "0";
doc["loc"] = {1, 2};
ASSERT_TRUE(coll1->add(doc.dump()).ok());
// try to alter to a bad type (int32)
auto schema_changes = R"({
"fields": [
{"name": "loc", "type": "int32"},
{"name": "loc", "drop": true}
]
})"_json;
auto alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
schema_changes = R"({
"fields": [
{"name": "loc", "drop": true},
{"name": "loc", "type": "int32"}
]
})"_json;
alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
}