class ActiveGraph::Core::Label
Attributes
name[R]
Public Class Methods
new(name)
click to toggle source
# File lib/active_graph/core/label.rb 6 def initialize(name) 7 @name = name 8 end
Private Class Methods
drop_constraints()
click to toggle source
# File lib/active_graph/core/label.rb 116 def drop_constraints 117 ActiveGraph::Base.transaction do |tx| 118 tx.run('CALL db.constraints').each do |record| 119 tx.run("DROP #{record.keys.include?(:name) ? "CONSTRAINT #{record[:name]}" : record[:description]}") 120 end 121 end 122 end
drop_indexes()
click to toggle source
# File lib/active_graph/core/label.rb 105 def drop_indexes 106 indexes.each do |definition| 107 begin 108 ActiveGraph::Base.query("DROP INDEX ON :`#{definition[:label]}`(#{definition[:properties][0]})") 109 rescue Neo4j::Driver::Exceptions::DatabaseException 110 # This will error on each constraint. Ignore and continue. 111 next 112 end 113 end 114 end
indexes()
click to toggle source
# File lib/active_graph/core/label.rb 101 def indexes 102 ActiveGraph::Base.indexes 103 end
Public Instance Methods
constraint?(property)
click to toggle source
# File lib/active_graph/core/label.rb 90 def constraint?(property) 91 constraints.any? { |definition| definition[:properties] == [property.to_sym] } 92 end
constraints(_options = {})
click to toggle source
# File lib/active_graph/core/label.rb 78 def constraints(_options = {}) 79 ActiveGraph::Base.constraints.select do |definition| 80 definition[:label] == @name.to_sym 81 end 82 end
create_constraint(property, constraints)
click to toggle source
Creates a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html @example
label = ActiveGraph::Label.create(:person) label.create_constraint(:name, {type: :unique})
# File lib/active_graph/core/label.rb 27 def create_constraint(property, constraints) 28 cypher = case constraints[:type] 29 when :unique, :uniqueness 30 "CREATE CONSTRAINT ON (n:`#{name}`) ASSERT n.`#{property}` IS UNIQUE" 31 else 32 fail "Not supported constraint #{constraints.inspect} for property #{property} (expected :type => :unique)" 33 end 34 schema_query(cypher) 35 end
create_index(property, options = {})
click to toggle source
# File lib/active_graph/core/label.rb 10 def create_index(property, options = {}) 11 validate_index_options!(options) 12 properties = property.is_a?(Array) ? property.join(',') : property 13 schema_query("CREATE INDEX ON :`#{@name}`(#{properties})") 14 end
create_uniqueness_constraint(property, options = {})
click to toggle source
# File lib/active_graph/core/label.rb 37 def create_uniqueness_constraint(property, options = {}) 38 create_constraint(property, options.merge(type: :unique)) 39 end
drop_constraint(property, constraint)
click to toggle source
Drops a neo4j constraint on a property See docs.neo4j.org/chunked/stable/query-constraints.html @example
label = ActiveGraph::Label.create(:person) label.create_constraint(:name, {type: :unique}) label.drop_constraint(:name, {type: :unique})
# File lib/active_graph/core/label.rb 48 def drop_constraint(property, constraint) 49 cypher = case constraint[:type] 50 when :unique, :uniqueness 51 "n.`#{property}` IS UNIQUE" 52 when :exists 53 "exists(n.`#{property}`)" 54 else 55 fail "Not supported constraint #{constraint.inspect}" 56 end 57 schema_query("DROP CONSTRAINT ON (n:`#{name}`) ASSERT #{cypher}") 58 end
drop_index(property, options = {})
click to toggle source
# File lib/active_graph/core/label.rb 16 def drop_index(property, options = {}) 17 validate_index_options!(options) 18 schema_query("DROP INDEX ON :`#{@name}`(#{property})") 19 end
drop_indexes()
click to toggle source
# File lib/active_graph/core/label.rb 70 def drop_indexes 71 self.class.drop_indexes 72 end
drop_uniqueness_constraint(property, options = {})
click to toggle source
# File lib/active_graph/core/label.rb 60 def drop_uniqueness_constraint(property, options = {}) 61 drop_constraint(property, options.merge(type: :unique)) 62 end
index?(property)
click to toggle source
# File lib/active_graph/core/label.rb 74 def index?(property) 75 indexes.any? { |definition| definition[:properties] == [property.to_sym] } 76 end
indexes()
click to toggle source
# File lib/active_graph/core/label.rb 64 def indexes 65 self.class.indexes.select do |definition| 66 definition[:label] == @name.to_sym 67 end 68 end
uniqueness_constraint?(property)
click to toggle source
# File lib/active_graph/core/label.rb 94 def uniqueness_constraint?(property) 95 uniqueness_constraints.include?([property]) 96 end
uniqueness_constraints(_options = {})
click to toggle source
# File lib/active_graph/core/label.rb 84 def uniqueness_constraints(_options = {}) 85 constraints.select do |definition| 86 definition[:type] == :uniqueness 87 end 88 end
Private Instance Methods
schema_query(cypher)
click to toggle source
# File lib/active_graph/core/label.rb 125 def schema_query(cypher) 126 ActiveGraph::Base.query(cypher, {}) 127 end
validate_index_options!(options)
click to toggle source
# File lib/active_graph/core/label.rb 129 def validate_index_options!(options) 130 return unless options[:type] && options[:type] != :exact 131 fail "Type #{options[:type]} is not supported" 132 end