module Delfos::Neo4j::Schema

Public Instance Methods

ensure_constraints!(required) click to toggle source
# File lib/delfos/neo4j/schema.rb, line 7
def ensure_constraints!(required)
  log "-" * 80
  log "checking constraints"
  log Time.now
  log "-" * 80

  if satisfies_constraints?(required)
    log "Neo4j schema constraints satisfied"
  else
    error "-" * 80
    error "Neo4j schema constraints not satisfied - adding"
    error Time.now

    required.each do |label, attribute|
      create_constraint(label, attribute)
    end

    log "-" * 80
    log "Constraints added"
    log Time.now
  end
end

Private Instance Methods

constraints_uri() click to toggle source
# File lib/delfos/neo4j/schema.rb, line 68
def constraints_uri
  Delfos.neo4j.uri_for("/db/data/schema/constraint")
end
create_constraint(label, attribute) click to toggle source
# File lib/delfos/neo4j/schema.rb, line 40
      def create_constraint(label, attribute)
        Neo4j.execute_sync <<-QUERY
          CREATE CONSTRAINT ON (c:#{label}) ASSERT c.#{attribute} IS UNIQUE
        QUERY
      end
error(s) click to toggle source
# File lib/delfos/neo4j/schema.rb, line 32
def error(s)
  log(s, :error)
end
fetch_existing_constraints() click to toggle source
# File lib/delfos/neo4j/schema.rb, line 56
def fetch_existing_constraints
  response = QueryExecution::Http.new(constraints_uri).get

  if response.code == "200"
    JSON.parse response.body
  else
    raise IOError.new uri, response
  end
end
log(s, type = :debug) click to toggle source
# File lib/delfos/neo4j/schema.rb, line 36
def log(s, type = :debug)
  Delfos.logger.send(type, s)
end
satisfies_constraints?(required) click to toggle source
# File lib/delfos/neo4j/schema.rb, line 46
def satisfies_constraints?(required)
  existing_constraints = fetch_existing_constraints

  required.inject(true) do |_result, (label, attribute)|
    constraint = existing_constraints.find { |c| c["label"] == label }

    constraint && constraint["property_keys"].include?(attribute)
  end
end