Class: ActiveCypher::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cypher/migration.rb

Overview

Base class for GraphDB migrations. Provides a small DSL for defining index and constraint operations.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = ActiveCypher::Base.connection) ⇒ Migration

Returns a new instance of Migration.



18
19
20
21
# File 'lib/active_cypher/migration.rb', line 18

def initialize(connection = ActiveCypher::Base.connection)
  @connection = connection
  @operations = []
end

Class Attribute Details

.up_blockObject (readonly)

Returns the value of attribute up_block.



8
9
10
# File 'lib/active_cypher/migration.rb', line 8

def up_block
  @up_block
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



16
17
18
# File 'lib/active_cypher/migration.rb', line 16

def connection
  @connection
end

#operationsObject (readonly)

Returns the value of attribute operations.



16
17
18
# File 'lib/active_cypher/migration.rb', line 16

def operations
  @operations
end

Class Method Details

.up(&block) ⇒ Object

Define the migration steps.



11
12
13
# File 'lib/active_cypher/migration.rb', line 11

def up(&block)
  @up_block = block if block_given?
end

Instance Method Details

#create_node_index(label, *props, unique: false, if_not_exists: true, name: nil) ⇒ Object

DSL —————————————————————



31
32
33
34
35
36
37
38
39
40
# File 'lib/active_cypher/migration.rb', line 31

def create_node_index(label, *props, unique: false, if_not_exists: true, name: nil)
  props_clause = props.map { |p| "n.#{p}" }.join(', ')
  cypher = +'CREATE '
  cypher << 'UNIQUE ' if unique
  cypher << 'INDEX'
  cypher << " #{name}" if name
  cypher << ' IF NOT EXISTS' if if_not_exists
  cypher << " FOR (n:#{label}) ON (#{props_clause})"
  operations << cypher
end

#create_rel_index(rel_type, *props, if_not_exists: true, name: nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/active_cypher/migration.rb', line 42

def create_rel_index(rel_type, *props, if_not_exists: true, name: nil)
  props_clause = props.map { |p| "r.#{p}" }.join(', ')
  cypher = +'CREATE INDEX'
  cypher << " #{name}" if name
  cypher << ' IF NOT EXISTS' if if_not_exists
  cypher << " FOR ()-[r:#{rel_type}]-() ON (#{props_clause})"
  operations << cypher
end

#create_uniqueness_constraint(label, *props, if_not_exists: true, name: nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/active_cypher/migration.rb', line 51

def create_uniqueness_constraint(label, *props, if_not_exists: true, name: nil)
  props_clause = props.map { |p| "n.#{p}" }.join(', ')
  cypher = +'CREATE CONSTRAINT'
  cypher << " #{name}" if name
  cypher << ' IF NOT EXISTS' if if_not_exists
  cypher << " FOR (n:#{label}) REQUIRE (#{props_clause}) IS UNIQUE"
  operations << cypher
end

#execute(cypher_string) ⇒ Object



60
61
62
# File 'lib/active_cypher/migration.rb', line 60

def execute(cypher_string)
  operations << cypher_string.strip
end

#runObject

Execute the migration.



24
25
26
27
# File 'lib/active_cypher/migration.rb', line 24

def run
  instance_eval(&self.class.up_block) if self.class.up_block
  execute_operations
end