Module: ActiveGraph::Core::Schema

Included in:
Base
Defined in:
lib/active_graph/core/schema.rb

Constant Summary collapse

FILTER =
{
  3 => [:type, 'node_unique_property'],
  4 => [:uniqueness, 'UNIQUE'],
}

Instance Method Summary collapse

Instance Method Details

#constraint_owned?(record) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_graph/core/schema.rb', line 53

def constraint_owned?(record)
  FILTER[major]&.then { |(key, value)| record[key] == value } || record[:owningConstraint]
end

#constraintsObject



32
33
34
35
36
37
38
# File 'lib/active_graph/core/schema.rb', line 32

def constraints
  if version?('<4.3')
    raw_indexes.select(&method(:constraint_owned?))
  else
    raw_constraints.select(&method(:constraint_filter))
  end.map { |row| definition(row, :constraint_cypher).merge(type: :uniqueness) }
end

#indexesObject



21
22
23
# File 'lib/active_graph/core/schema.rb', line 21

def indexes
  normalize(raw_indexes, *%i[type state])
end

#normalize(result, *extra) ⇒ Object



25
26
27
28
29
30
# File 'lib/active_graph/core/schema.rb', line 25

def normalize(result, *extra)
  result.map do |row|
    definition(row, version?('<4') ? :index_cypher_v3 : :index_cypher)
      .merge(extra.to_h { |key| [key, row[key].to_sym] })
  end
end

#raw_indexesObject



46
47
48
49
50
51
# File 'lib/active_graph/core/schema.rb', line 46

def raw_indexes
  read_transaction do
    query(version?('<4.3') ? 'CALL db.indexes()' : 'SHOW INDEXES YIELD *', {}, skip_instrumentation: true)
      .reject { |row| row[:type] == 'LOOKUP' }
  end
end

#versionObject



9
10
11
12
13
14
15
# File 'lib/active_graph/core/schema.rb', line 9

def version
  @version ||= read_transaction do
    # BTW: community / enterprise could be retrieved via `result.first.edition`
    query('CALL dbms.components()', {}, skip_instrumentation: true).first[:versions][0]
      .then(&Gem::Version.method(:new))
  end
end

#version?(requirement) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/active_graph/core/schema.rb', line 17

def version?(requirement)
  Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.new(version))
end