mirror of
https://github.com/typesense/typesense.git
synced 2025-05-19 05:08:43 +08:00
Drop values must be at the top for drop+add use case.
This commit is contained in:
parent
4242383db7
commit
c2856b02b5
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user