Module: Neo4j::ModelSchema

Defined in:
lib/neo4j/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


12
13
14
15
# File 'lib/neo4j/model_schema.rb', line 12

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


17
18
19
20
# File 'lib/neo4j/model_schema.rb', line 17

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


22
23
24
25
# File 'lib/neo4j/model_schema.rb', line 22

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)

27
28
29
30
# File 'lib/neo4j/model_schema.rb', line 27

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

.ensure_model_data_state!Object


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

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

.force_add_message(index_or_constraint, label, property_name) ⇒ Object


120
121
122
# File 'lib/neo4j/model_schema.rb', line 120

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


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

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


124
125
126
# File 'lib/neo4j/model_schema.rb', line 124

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

.model_constraintsObject


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

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

  @model_constraints = schema_elements_list(MODEL_CONSTRAINTS, constraints)
end

.model_indexesObject


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

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

  @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


77
78
79
80
# File 'lib/neo4j/model_schema.rb', line 77

def reload_models_data!
  @previously_loaded_models_count = Neo4j::ActiveNode.loaded_classes.size
  @model_indexes = @model_constraints = nil
end

.schema_elements_list(by_model, db_results) ⇒ Object

should be private


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

def schema_elements_list(by_model, db_results)
  by_model.flat_map do |model, property_names|
    label = model.mapped_label_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


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

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 ::Neo4j::DeprecatedSchemaDefinitionError, validation_error_message(messages)
end

.validation_error_message(messages) ⇒ Object


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

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