Module: ActiveGraph::ModelSchema

Defined in:
lib/active_graph/model_schema.rb

Overview

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

Constant Summary collapse

MODEL_INDEXES =
{}
MODEL_CONSTRAINTS =
{}
REQUIRED_INDEXES =
{}

Class Method Summary collapse

Class Method Details

.add_defined_constraint(model, property_name) ⇒ Object



11
12
13
14
# File 'lib/active_graph/model_schema.rb', line 11

def add_defined_constraint(model, property_name)
  MODEL_CONSTRAINTS[model] ||= Set.new
  MODEL_CONSTRAINTS[model] << property_name.to_sym
end

.add_defined_index(model, property_name) ⇒ Object



16
17
18
19
# File 'lib/active_graph/model_schema.rb', line 16

def add_defined_index(model, property_name)
  MODEL_INDEXES[model] ||= Set.new
  MODEL_INDEXES[model] << property_name.to_sym
end

.add_required_index(model, property_name) ⇒ Object



21
22
23
24
# File 'lib/active_graph/model_schema.rb', line 21

def add_required_index(model, property_name)
  REQUIRED_INDEXES[model] ||= Set.new
  REQUIRED_INDEXES[model] << property_name.to_sym
end

.defined_constraint?(model, property_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/active_graph/model_schema.rb', line 26

def defined_constraint?(model, property_name)
  MODEL_CONSTRAINTS[model] &&
    MODEL_CONSTRAINTS[model].include?(property_name.to_sym)
end

.ensure_model_data_state!Object



66
67
68
69
70
71
72
73
74
# File 'lib/active_graph/model_schema.rb', line 66

def ensure_model_data_state!
  # If we load a new model, reset everything
  if @previously_loaded_models_count != ActiveGraph::Node.loaded_classes.size
    # Make sure we've finalized id_property details and have called
    # add_ constraint/index methods above
    ActiveGraph::Node.loaded_classes.each(&:ensure_id_property_info!)
    reload_models_data!
  end
end

.force_add_message(index_or_constraint, label, property_name) ⇒ Object



119
120
121
# File 'lib/active_graph/model_schema.rb', line 119

def force_add_message(index_or_constraint, label, property_name)
  "rake neo4j:generate_schema_migration[#{index_or_constraint},#{label},#{property_name}]"
end

.legacy_model_schema_informationsObject



81
82
83
84
85
86
87
88
# File 'lib/active_graph/model_schema.rb', line 81

def legacy_model_schema_informations
  ensure_model_data_state!
  data = {index: [], constraint: []}
  each_schema_element do |type, model, label, property_name|
    data[type] << {label: label, property_name: property_name, model: model}
  end
  data
end

.log_warning!(index_or_constraint, model, property_name) ⇒ Object



123
124
125
# File 'lib/active_graph/model_schema.rb', line 123

def log_warning!(index_or_constraint, model, property_name)
  ActiveGraph::Base.logger.warn "WARNING: The #{index_or_constraint} option is no longer supported (Defined on #{model.name} for #{property_name})"
end

.model_constraintsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/active_graph/model_schema.rb', line 31

def model_constraints
  return @model_constraints if @model_constraints

  constraints = ActiveGraph::Base.constraints.each_with_object({}) do |row, result|
    result[row[:label]] ||= []
    result[row[:label]] << row[:properties]
  end

  @model_constraints = schema_elements_list(MODEL_CONSTRAINTS, constraints)
end

.model_indexesObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_graph/model_schema.rb', line 42

def model_indexes
  return @model_indexes if @model_indexes

  indexes = ActiveGraph::Base.indexes.each_with_object({}) do |row, result|
    result[row[:label]] ||= []
    result[row[:label]] << row[:properties]
  end

  @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

.reload_models_data!Object



76
77
78
79
# File 'lib/active_graph/model_schema.rb', line 76

def reload_models_data!
  @previously_loaded_models_count = ActiveGraph::Node.loaded_classes.size
  @model_indexes = @model_constraints = nil
end

.schema_elements_list(by_model, db_results) ⇒ Object

should be private



56
57
58
59
60
61
62
63
64
# File 'lib/active_graph/model_schema.rb', line 56

def schema_elements_list(by_model, db_results)
  by_model.flat_map do |model, property_names|
    label = model.mapped_element_name.to_sym
    property_names.map do |property_name|
      exists = db_results[label] && db_results[label].include?([property_name])
      [model, label, property_name, exists]
    end
  end
end

.validate_model_schema!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/active_graph/model_schema.rb', line 90

def validate_model_schema!
  ensure_model_data_state!
  messages = {index: [], constraint: []}
  each_schema_element do |type, model, label, property_name, exists|
    if exists
      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

  return if messages.values.all?(&:empty?)

  fail ::ActiveGraph::DeprecatedSchemaDefinitionError, validation_error_message(messages)
end

.validation_error_message(messages) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_graph/model_schema.rb', line 106

def validation_error_message(messages)
  <<MSG
    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:

#{messages[:constraint].join("\n")}
#{messages[:index].join("\n")}

And then run `rake neo4j:migrate`

(zshell users may need to escape the brackets)
MSG
end