lib/neo4j/model_schema.rb in neo4j-8.0.0.alpha.1 vs lib/neo4j/model_schema.rb in neo4j-8.0.0.alpha.2
- old
+ new
@@ -28,26 +28,30 @@
MODEL_CONSTRAINTS[model] &&
MODEL_CONSTRAINTS[model].include?(property_name.to_sym)
end
def model_constraints
+ return @model_constraints if @model_constraints
+
constraints = Neo4j::ActiveBase.current_session.constraints.each_with_object({}) do |row, result|
result[row[:label]] ||= []
result[row[:label]] << row[:properties]
end
- schema_elements_list(MODEL_CONSTRAINTS, constraints)
+ @model_constraints = schema_elements_list(MODEL_CONSTRAINTS, constraints)
end
def model_indexes
+ return @model_indexes if @model_indexes
+
indexes = Neo4j::ActiveBase.current_session.indexes.each_with_object({}) do |row, result|
result[row[:label]] ||= []
result[row[:label]] << row[:properties]
end
- schema_elements_list(MODEL_INDEXES, indexes) +
- schema_elements_list(REQUIRED_INDEXES, indexes).reject(&:last)
+ @model_indexes = schema_elements_list(MODEL_INDEXES, indexes) +
+ schema_elements_list(REQUIRED_INDEXES, indexes).reject(&:last)
# reject required indexes which are already in the DB
end
# should be private
def schema_elements_list(by_model, db_results)
@@ -58,16 +62,32 @@
[model, label, property_name, exists]
end
end
end
+ def ensure_model_data_state!
+ # If we load a new model, reset everything
+ if @previously_loaded_models_count != Neo4j::ActiveNode.loaded_classes.size
+ # Make sure we've finalized id_property details and have called
+ # add_ constraint/index methods above
+ Neo4j::ActiveNode.loaded_classes.each(&:ensure_id_property_info!)
+ reload_models_data!
+ end
+ end
+
+ def reload_models_data!
+ @previously_loaded_models_count = Neo4j::ActiveNode.loaded_classes.size
+ @model_indexes = @model_constraints = nil
+ end
+
def validate_model_schema!
+ ensure_model_data_state!
messages = {index: [], constraint: []}
[[:constraint, model_constraints], [:index, model_indexes]].each do |type, schema_elements|
schema_elements.map do |model, label, property_name, exists|
if exists
- log_warning!(type, model, property_name)
+ log_warning!(type, model, property_name) if model.id_property_name.to_sym != property_name
else
messages[type] << force_add_message(type, label, property_name)
end
end
end
@@ -77,13 +97,15 @@
fail validation_error_message(messages)
end
def validation_error_message(messages)
<<MSG
- Some schema elements were defined by the model (which is no longer support), but they do not exist in the database. Run the following to create them:
+ Some schema elements were defined by the model (which is no longer supported), but they do not exist in the database. Run the following to create them:
#{messages[:constraint].join("\n")}
#{messages[:index].join("\n")}
+
+And then run `rake neo4j:migrate`
(zshell users may need to escape the brackets)
MSG
end