std::vector<ObjectSchemaValidationException> ObjectStore::verify_object_schema(ObjectSchema const& table_schema, ObjectSchema& target_schema) { std::vector<ObjectSchemaValidationException> exceptions; // check to see if properties are the same for (auto& current_prop : table_schema.persisted_properties) { auto target_prop = target_schema.property_for_name(current_prop.name); if (!target_prop) { exceptions.emplace_back(MissingPropertyException(table_schema.name, current_prop)); continue; } if (property_has_changed(current_prop, *target_prop)) { exceptions.emplace_back(MismatchedPropertiesException(table_schema.name, current_prop, *target_prop)); continue; } // create new property with aligned column target_prop->table_column = current_prop.table_column; } // check for change to primary key if (table_schema.primary_key != target_schema.primary_key) { exceptions.emplace_back(ChangedPrimaryKeyException(table_schema.name, table_schema.primary_key, target_schema.primary_key)); } // check for new missing properties for (auto& target_prop : target_schema.persisted_properties) { if (!table_schema.property_for_name(target_prop.name)) { exceptions.emplace_back(ExtraPropertyException(table_schema.name, target_prop)); } } return exceptions; }
static void compare(ObjectSchema const& existing_schema, ObjectSchema const& target_schema, std::vector<SchemaChange>& changes) { for (auto& current_prop : existing_schema.persisted_properties) { auto target_prop = target_schema.property_for_name(current_prop.name); if (!target_prop) { changes.emplace_back(schema_change::RemoveProperty{&existing_schema, ¤t_prop}); continue; } if (target_schema.property_is_computed(*target_prop)) { changes.emplace_back(schema_change::RemoveProperty{&existing_schema, ¤t_prop}); continue; } if (current_prop.type != target_prop->type || current_prop.object_type != target_prop->object_type || is_array(current_prop.type) != is_array(target_prop->type)) { changes.emplace_back(schema_change::ChangePropertyType{&existing_schema, ¤t_prop, target_prop}); continue; } if (is_nullable(current_prop.type) != is_nullable(target_prop->type)) { if (is_nullable(current_prop.type)) changes.emplace_back(schema_change::MakePropertyRequired{&existing_schema, ¤t_prop}); else changes.emplace_back(schema_change::MakePropertyNullable{&existing_schema, ¤t_prop}); } if (target_prop->requires_index()) { if (!current_prop.is_indexed) changes.emplace_back(schema_change::AddIndex{&existing_schema, ¤t_prop}); } else if (current_prop.requires_index()) { changes.emplace_back(schema_change::RemoveIndex{&existing_schema, ¤t_prop}); } } if (existing_schema.primary_key != target_schema.primary_key) { changes.emplace_back(schema_change::ChangePrimaryKey{&existing_schema, target_schema.primary_key_property()}); } for (auto& target_prop : target_schema.persisted_properties) { if (!existing_schema.property_for_name(target_prop.name)) { changes.emplace_back(schema_change::AddProperty{&existing_schema, &target_prop}); } } // Move all RemovePropertys to the end and sort in descending order of // column index, as removing a column will shift all columns after that one auto it = std::partition(begin(changes), end(changes), IsNotRemoveProperty{}); std::sort(it, end(changes), [](auto a, auto b) { return GetRemovedColumn()(a) > GetRemovedColumn()(b); }); }
std::vector<std::string> ObjectStore::validate_schema(Group *group, ObjectSchema &target_schema) { vector<string> validation_errors; ObjectSchema table_schema(group, target_schema.name); // check to see if properties are the same for (auto& current_prop : table_schema.properties) { auto target_prop = target_schema.property_for_name(current_prop.name); if (!target_prop) { validation_errors.push_back("Property '" + current_prop.name + "' is missing from latest object model."); continue; } if (current_prop.type != target_prop->type) { validation_errors.push_back("Property types for '" + target_prop->name + "' property do not match. " + "Old type '" + string_for_property_type(current_prop.type) + "', new type '" + string_for_property_type(target_prop->type) + "'"); continue; } if (current_prop.type == PropertyTypeObject || target_prop->type == PropertyTypeArray) { if (current_prop.object_type != target_prop->object_type) { validation_errors.push_back("Target object type for property '" + current_prop.name + "' does not match. " + "Old type '" + current_prop.object_type + "', new type '" + target_prop->object_type + "'."); } } if (current_prop.is_primary != target_prop->is_primary) { if (current_prop.is_primary) { validation_errors.push_back("Property '" + current_prop.name + "' is no longer a primary key."); } else { validation_errors.push_back("Property '" + current_prop.name + "' has been made a primary key."); } } if (current_prop.is_nullable != target_prop->is_nullable) { if (current_prop.is_nullable) { validation_errors.push_back("Property '" + current_prop.name + "' is no longer optional."); } else { validation_errors.push_back("Property '" + current_prop.name + "' has been made optional."); } } // create new property with aligned column target_prop->table_column = current_prop.table_column; } // check for new missing properties for (auto& target_prop : target_schema.properties) { if (!table_schema.property_for_name(target_prop.name)) { validation_errors.push_back("Property '" + target_prop.name + "' has been added to latest object model."); } } return validation_errors; }