module ActiveGraph::ModelSchema

This is here to support the removed functionality of being able to defined indexes and constraints on models This code should be removed later

Constants

MODEL_CONSTRAINTS
MODEL_INDEXES
REQUIRED_INDEXES

Public Class Methods

add_defined_constraint(model, property_name) click to toggle source
   # File lib/active_graph/model_schema.rb
12 def add_defined_constraint(model, property_name)
13   MODEL_CONSTRAINTS[model] ||= Set.new
14   MODEL_CONSTRAINTS[model] << property_name.to_sym
15 end
add_defined_index(model, property_name) click to toggle source
   # File lib/active_graph/model_schema.rb
17 def add_defined_index(model, property_name)
18   MODEL_INDEXES[model] ||= Set.new
19   MODEL_INDEXES[model] << property_name.to_sym
20 end
add_required_index(model, property_name) click to toggle source
   # File lib/active_graph/model_schema.rb
22 def add_required_index(model, property_name)
23   REQUIRED_INDEXES[model] ||= Set.new
24   REQUIRED_INDEXES[model] << property_name.to_sym
25 end
defined_constraint?(model, property_name) click to toggle source
   # File lib/active_graph/model_schema.rb
27 def defined_constraint?(model, property_name)
28   MODEL_CONSTRAINTS[model] &&
29     MODEL_CONSTRAINTS[model].include?(property_name.to_sym)
30 end
ensure_model_data_state!() click to toggle source
   # File lib/active_graph/model_schema.rb
67 def ensure_model_data_state!
68   # If we load a new model, reset everything
69   if @previously_loaded_models_count != ActiveGraph::Node.loaded_classes.size
70     # Make sure we've finalized id_property details and have called
71     # add_ constraint/index methods above
72     ActiveGraph::Node.loaded_classes.each(&:ensure_id_property_info!)
73     reload_models_data!
74   end
75 end
force_add_message(index_or_constraint, label, property_name) click to toggle source
    # File lib/active_graph/model_schema.rb
120 def force_add_message(index_or_constraint, label, property_name)
121   "rake neo4j:generate_schema_migration[#{index_or_constraint},#{label},#{property_name}]"
122 end
legacy_model_schema_informations() click to toggle source
   # File lib/active_graph/model_schema.rb
82 def legacy_model_schema_informations
83   ensure_model_data_state!
84   data = {index: [], constraint: []}
85   each_schema_element do |type, model, label, property_name|
86     data[type] << {label: label, property_name: property_name, model: model}
87   end
88   data
89 end
log_warning!(index_or_constraint, model, property_name) click to toggle source
    # File lib/active_graph/model_schema.rb
124 def log_warning!(index_or_constraint, model, property_name)
125   ActiveGraph::Base.logger.warn "WARNING: The #{index_or_constraint} option is no longer supported (Defined on #{model.name} for #{property_name})"
126 end
model_constraints() click to toggle source
   # File lib/active_graph/model_schema.rb
32 def model_constraints
33   return @model_constraints if @model_constraints
34 
35   constraints = ActiveGraph::Base.constraints.each_with_object({}) do |row, result|
36     result[row[:label]] ||= []
37     result[row[:label]] << row[:properties]
38   end
39 
40   @model_constraints = schema_elements_list(MODEL_CONSTRAINTS, constraints)
41 end
model_indexes() click to toggle source
   # File lib/active_graph/model_schema.rb
43 def model_indexes
44   return @model_indexes if @model_indexes
45 
46   indexes = ActiveGraph::Base.indexes.each_with_object({}) do |row, result|
47     result[row[:label]] ||= []
48     result[row[:label]] << row[:properties]
49   end
50 
51   @model_indexes = schema_elements_list(MODEL_INDEXES, indexes) +
52                    schema_elements_list(REQUIRED_INDEXES, indexes).reject(&:last)
53   # reject required indexes which are already in the DB
54 end
reload_models_data!() click to toggle source
   # File lib/active_graph/model_schema.rb
77 def reload_models_data!
78   @previously_loaded_models_count = ActiveGraph::Node.loaded_classes.size
79   @model_indexes = @model_constraints = nil
80 end
schema_elements_list(by_model, db_results) click to toggle source

should be private

   # File lib/active_graph/model_schema.rb
57 def schema_elements_list(by_model, db_results)
58   by_model.flat_map do |model, property_names|
59     label = model.mapped_label_name.to_sym
60     property_names.map do |property_name|
61       exists = db_results[label] && db_results[label].include?([property_name])
62       [model, label, property_name, exists]
63     end
64   end
65 end
validate_model_schema!() click to toggle source
    # File lib/active_graph/model_schema.rb
 91 def validate_model_schema!
 92   ensure_model_data_state!
 93   messages = {index: [], constraint: []}
 94   each_schema_element do |type, model, label, property_name, exists|
 95     if exists
 96       log_warning!(type, model, property_name) if model.id_property_name.to_sym != property_name
 97     else
 98       messages[type] << force_add_message(type, label, property_name)
 99     end
100   end
101 
102   return if messages.values.all?(&:empty?)
103 
104   fail ::ActiveGraph::DeprecatedSchemaDefinitionError, validation_error_message(messages)
105 end
validation_error_message(messages) click to toggle source
    # File lib/active_graph/model_schema.rb
107       def validation_error_message(messages)
108         <<MSG
109           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 if you haven't already:
110 
111 #{messages[:constraint].join("\n")}
112 #{messages[:index].join("\n")}
113 
114 And then run `rake neo4j:migrate`
115 
116 (zshell users may need to escape the brackets)
117 MSG
118       end

Private Class Methods

each_schema_element() { |type, *args| ... } click to toggle source
    # File lib/active_graph/model_schema.rb
130 def each_schema_element
131   [[:constraint, model_constraints], [:index, model_indexes]].each do |type, schema_elements|
132     schema_elements.each do |args|
133       yield(type, *args)
134     end
135   end
136 end