Class: ActiveGraph::Core::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/active_graph/core/label.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Label

Returns a new instance of Label.



7
8
9
# File 'lib/active_graph/core/label.rb', line 7

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/active_graph/core/label.rb', line 4

def name
  @name
end

Class Method Details

.drop_constraintsObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/active_graph/core/label.rb', line 140

def drop_constraints
  result = ActiveGraph::Base.read_transaction do |tx|
    tx.run(ActiveGraph::Base.version?('<4.3') ? 'CALL db.constraints' : 'SHOW CONSTRAINTS YIELD *').to_a
  end
  ActiveGraph::Base.write_transaction do |tx|
    result.each do |record|
      tx.run("DROP #{record.keys.include?(:name) ? "CONSTRAINT #{record[:name]}" : record[:description]}")
    end
  end
end

.drop_indexesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_graph/core/label.rb', line 124

def drop_indexes
  indexes.each do |definition|
    begin
      ActiveGraph::Base.query(
        if definition[:name]
          "DROP INDEX #{definition[:name]}"
        else
          "DROP INDEX ON :`#{definition[:label]}`(#{definition[:properties][0]})"
        end)
    rescue Neo4j::Driver::Exceptions::DatabaseException
      # This will error on each constraint. Ignore and continue.
      next
    end
  end
end

.indexesObject



120
121
122
# File 'lib/active_graph/core/label.rb', line 120

def indexes
  ActiveGraph::Base.indexes
end

Instance Method Details

#constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/active_graph/core/label.rb', line 109

def constraint?(property)
  constraints.any? { |definition| definition[:properties] == [property.to_sym] }
end

#constraints(_options = {}) ⇒ Object



97
98
99
100
101
# File 'lib/active_graph/core/label.rb', line 97

def constraints(_options = {})
  ActiveGraph::Base.constraints.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#create_constraint(property, constraints) ⇒ Object

Creates a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})


40
41
42
43
44
45
46
47
48
49
# File 'lib/active_graph/core/label.rb', line 40

def create_constraint(property, constraints)
  cypher = case constraints[:type]
           when :unique, :uniqueness
             _for, _require = version?('>=4.4') ? %w[FOR REQUIRE] : %w[ON ASSERT]
             "CREATE CONSTRAINT #{_for} (n:`#{name}`) #{_require} n.`#{property}` IS UNIQUE"
           else
             fail "Not supported constraint #{constraints.inspect} for property #{property} (expected :type => :unique)"
           end
  schema_query(cypher)
end

#create_index(*properties, **options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/active_graph/core/label.rb', line 11

def create_index(*properties, **options)
  validate_index_options!(options)
  if version?('>=4.4')
    properties = properties.map { |p| "l.#{p}" }
    fragment = "FOR (l:`#{@name}`) ON"
  else
    fragment = "ON :`#{@name}`"
  end
  schema_query("CREATE INDEX #{fragment} (#{properties.join('.')})")
end

#create_uniqueness_constraint(property, options = {}) ⇒ Object



51
52
53
# File 'lib/active_graph/core/label.rb', line 51

def create_uniqueness_constraint(property, options = {})
  create_constraint(property, options.merge(type: :unique))
end

#drop_constraint(property, constraint) ⇒ Object

Drops a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html

Examples:

label = ActiveGraph::Label.create(:person)
label.create_constraint(:name, {type: :unique})
label.drop_constraint(:name, {type: :unique})


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_graph/core/label.rb', line 62

def drop_constraint(property, constraint)
  return drop_constraint42(property, constraint) if version?('<4.3')
  type = case constraint[:type]
         when :unique, :uniqueness
           'UNIQUENESS'
         when :exists
           'NODE_PROPERTY_EXISTENCE'
         else
           fail "Not supported constraint #{constraint.inspect}"
         end
  schema_query(
    'SHOW CONSTRAINTS YIELD * WHERE type = $type AND labelsOrTypes = $labels AND properties = $properties',
    type: type, labels: [name], properties: [property]).first[:name].tap do |constraint_name|
    schema_query("DROP CONSTRAINT #{constraint_name}")
  end
end

#drop_index(property, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_graph/core/label.rb', line 22

def drop_index(property, options = {})
  validate_index_options!(options)
  if version?('<4.3')
    schema_query("DROP INDEX ON :`#{@name}`(#{property})")
  else
    schema_query("SHOW INDEXES YIELD * WHERE labelsOrTypes = $labels AND properties = $properties",
                 labels: [@name], properties: [property]).each do |record|
      schema_query("DROP INDEX #{record[:name]}")
    end
  end
end

#drop_indexesObject



89
90
91
# File 'lib/active_graph/core/label.rb', line 89

def drop_indexes
  self.class.drop_indexes
end

#drop_uniqueness_constraint(property, options = {}) ⇒ Object



79
80
81
# File 'lib/active_graph/core/label.rb', line 79

def drop_uniqueness_constraint(property, options = {})
  drop_constraint(property, options.merge(type: :unique))
end

#index?(property) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_graph/core/label.rb', line 93

def index?(property)
  indexes.any? { |definition| definition[:properties] == [property.to_sym] }
end

#indexesObject



83
84
85
86
87
# File 'lib/active_graph/core/label.rb', line 83

def indexes
  self.class.indexes.select do |definition|
    definition[:label] == @name.to_sym
  end
end

#uniqueness_constraint?(property) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/active_graph/core/label.rb', line 113

def uniqueness_constraint?(property)
  uniqueness_constraints.include?([property])
end

#uniqueness_constraints(_options = {}) ⇒ Object



103
104
105
106
107
# File 'lib/active_graph/core/label.rb', line 103

def uniqueness_constraints(_options = {})
  constraints.select do |definition|
    definition[:type] == :uniqueness
  end
end