Class: OrientdbSchemaMigrator::Migration

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

Class Method Summary collapse

Class Method Details

.add_index(class_name, property_name, index_name, type) ⇒ Object



56
57
58
59
60
61
# File 'lib/orientdb_schema_migrator/migration.rb', line 56

def self.add_index class_name, property_name, index_name, type
  assert_class_exists!(class_name)
  assert_property_exists!(class_name, property_name)
  assert_index_not_exists!(class_name, index_name)
  OrientdbSchemaMigrator.client.command "create index #{index_name} on #{class_name} (#{property_name}) #{type}"
end

.add_property(class_name, property_name, type, property_options = {}) ⇒ Object



38
39
40
41
42
# File 'lib/orientdb_schema_migrator/migration.rb', line 38

def self.add_property class_name, property_name, type, property_options={}
  assert_class_exists!(class_name)
  assert_property_not_exists!(class_name, property_name)
  OrientdbSchemaMigrator.client.create_property class_name,property_name,type, property_options
end

.alter_property(class_name, property_name, attribute_name, new_value) ⇒ Object



50
51
52
53
54
# File 'lib/orientdb_schema_migrator/migration.rb', line 50

def self.alter_property class_name, property_name, attribute_name, new_value
  assert_class_exists!(class_name)
  assert_property_exists!(class_name, property_name)
  OrientdbSchemaMigrator.client.command "alter property #{class_name}.#{property_name} #{attribute_name} #{new_value}"
end

.assert_class_exists!(class_name) ⇒ Object



91
92
93
# File 'lib/orientdb_schema_migrator/migration.rb', line 91

def self.assert_class_exists!(class_name)
  fail MigrationError.new("Class #{class_name} does not exist") unless class_exists?(class_name)
end

.assert_class_not_exists!(class_name) ⇒ Object



95
96
97
# File 'lib/orientdb_schema_migrator/migration.rb', line 95

def self.assert_class_not_exists!(class_name)
  fail MigrationError.new("Class #{class_name} already exists") unless !class_exists?(class_name)
end

.assert_index_not_exists!(class_name, index) ⇒ Object



107
108
109
# File 'lib/orientdb_schema_migrator/migration.rb', line 107

def self.assert_index_not_exists!(class_name, index)
  fail MigrationError.new("#{class_name},#{index} already exists") unless !index_exists?(class_name, index)
end

.assert_property_exists!(class_name, property) ⇒ Object



99
100
101
# File 'lib/orientdb_schema_migrator/migration.rb', line 99

def self.assert_property_exists!(class_name, property)
  fail MigrationError.new("#{class_name}.#{property} does not exist") unless property_exists?(class_name, property)
end

.assert_property_not_exists!(class_name, property) ⇒ Object



103
104
105
# File 'lib/orientdb_schema_migrator/migration.rb', line 103

def self.assert_property_not_exists!(class_name, property)
  fail MigrationError.new("#{class_name}.#{property} already exists") unless !property_exists?(class_name, property)
end

.class_exists?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/orientdb_schema_migrator/migration.rb', line 67

def self.class_exists? class_name
  OrientdbSchemaMigrator.client.class_exists? class_name
end

.create_class(class_name, class_options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/orientdb_schema_migrator/migration.rb', line 5

def self.create_class class_name, class_options={}
  # check if class exists first
  assert_class_not_exists!(class_name)
  OrientdbSchemaMigrator.client.create_class class_name, class_options
  if block_given?
    proxy = Proxy.new(self, class_name)
    yield proxy
  end
end

.drop_class(class_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/orientdb_schema_migrator/migration.rb', line 15

def self.drop_class class_name
  # check if class exists first
  if class_exists?(class_name)
    # delete vertices/edges first
    super_class = OrientdbSchemaMigrator.client.get_class(class_name)["superClass"]
    if super_class == "V"
      OrientdbSchemaMigrator.client.command "delete vertex #{class_name}"
    elsif super_class == "E"
      OrientdbSchemaMigrator.client.command "delete edge #{class_name}"
    end
    # drop class
    OrientdbSchemaMigrator.client.command "drop class #{class_name}"
    return true
  else
    return false
  end
end

.drop_index(index_name) ⇒ Object



63
64
65
# File 'lib/orientdb_schema_migrator/migration.rb', line 63

def self.drop_index index_name
  OrientdbSchemaMigrator.client.command "drop index #{index_name}"
end

.drop_property(class_name, property_name) ⇒ Object



44
45
46
47
48
# File 'lib/orientdb_schema_migrator/migration.rb', line 44

def self.drop_property class_name, property_name
  assert_class_exists!(class_name)
  assert_property_exists!(class_name, property_name)
  OrientdbSchemaMigrator.client.command "drop property #{class_name}.#{property_name}"
end

.index_exists?(class_name, index_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/orientdb_schema_migrator/migration.rb', line 71

def self.index_exists?(class_name, index_name)
  return false unless class_exists?(class_name)
  indexes = OrientdbSchemaMigrator.client.get_class(class_name)["indexes"]
  return false unless indexes
  return indexes.any? { |idx| idx['name'] == index_name }
end

.property_exists?(class_name, property_name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orientdb_schema_migrator/migration.rb', line 78

def self.property_exists? class_name, property_name
  if class_exists? class_name
    properties = OrientdbSchemaMigrator.client.get_class(class_name)["properties"]
    if properties
      return properties.collect{|i| i["name"]}.include? property_name
    else
      return false
    end
  else
    return false
  end
end

.rename_class(old_name, new_name) ⇒ Object



33
34
35
36
# File 'lib/orientdb_schema_migrator/migration.rb', line 33

def self.rename_class old_name, new_name
  assert_class_exists!(old_name)
  OrientdbSchemaMigrator.client.command "alter class #{old_name} name #{new_name}"
end