Module: ActiveGraph::Migrations::Helpers::Schema

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_graph/migrations/helpers/schema.rb

Constant Summary collapse

MISSING_CONSTRAINT_OR_INDEX =
'No such %{type} for %{label}#%{property}'.freeze
DUPLICATE_CONSTRAINT_OR_INDEX =
'Duplicate %{type} for %{label}#%{property}'.freeze

Instance Method Summary collapse

Instance Method Details

#add_constraint(label, property, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_graph/migrations/helpers/schema.rb', line 9

def add_constraint(label, property, options = {})
  force = options[:force] || false
  type = options[:type] || :uniqueness
  label_object = ActiveGraph::Base.label_object(label)
  if label_object.constraint?(property)
    if force
      label_object.drop_constraint(property, type: type)
    else
      fail_duplicate_constraint_or_index!(:constraint, label, property)
    end
  end
  label_object.create_constraint(property, type: type)
end

#add_index(label, property, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_graph/migrations/helpers/schema.rb', line 23

def add_index(label, property, options = {})
  force = options[:force] || false
  label_object = ActiveGraph::Base.label_object(label)
  if label_object.index?(property)
    if force
      label_object.drop_index(property)
    else
      fail_duplicate_constraint_or_index!(:index, label, property)
    end
  end
  label_object.create_index(property)
end

#drop_constraint(label, property, options = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/active_graph/migrations/helpers/schema.rb', line 36

def drop_constraint(label, property, options = {})
  type = options[:type] || :uniqueness
  label_object = ActiveGraph::Base.label_object(label)
  fail_missing_constraint_or_index!(:constraint, label, property) if !options[:force] && !label_object.constraint?(property)
  label_object.drop_constraint(property, type: type)
end

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



43
44
45
46
47
# File 'lib/active_graph/migrations/helpers/schema.rb', line 43

def drop_index(label, property, options = {})
  label_object = ActiveGraph::Base.label_object(label)
  fail_missing_constraint_or_index!(:index, label, property) if !options[:force] && !label_object.index?(property)
  label_object.drop_index(property)
end