Class: OrientdbSchemaMigrator::Migrator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_version, target_version, direction) ⇒ Migrator

Returns a new instance of Migrator.



64
65
66
67
68
# File 'lib/orientdb_schema_migrator/migrator.rb', line 64

def initialize(current_version, target_version, direction)
  @current_version = current_version
  @target_version = target_version
  @direction = direction
end

Class Method Details

.connect_to_db(db, user, password) ⇒ Object



18
19
20
# File 'lib/orientdb_schema_migrator/migrator.rb', line 18

def connect_to_db db, user, password
  OrientdbSchemaMigrator.client.connect :database => db, :user => user, :password => password
end

.current_versionObject



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

def current_version
  response = OrientdbSchemaMigrator.client.command "SELECT schema_version FROM schema_versions ORDER BY @rid DESC LIMIT 1"
  results = response['result']
  results.any? ? results.first['schema_version'] : nil
end

.disconnectObject



22
23
24
# File 'lib/orientdb_schema_migrator/migrator.rb', line 22

def disconnect
  OrientdbSchemaMigrator.client.disconnect
end

.migrate(target_version = nil) ⇒ Object



26
27
28
# File 'lib/orientdb_schema_migrator/migrator.rb', line 26

def migrate(target_version = nil)
  run(:up, target_version)
end

.migrationsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/orientdb_schema_migrator/migrator.rb', line 30

def migrations
  files = Dir[@migrations_path + '/[0-9]*_*.rb']
  seen = Hash.new false
  migrations = files.map do |f|
    version, name = f.scan(/(\d+)_([^\.]+)/).first
    if seen[version] || seen[name]
      fail MigratorConflictError.new("Duplicate migration name/version: #{name}, #{version}")
    else
      seen[version] = seen[name] = true
    end
    {
      name: name,
      version: version,
      path: f
    }
  end
  migrations.sort_by { |m| m[:version] }
end

.migrations_pathObject



14
15
16
# File 'lib/orientdb_schema_migrator/migrator.rb', line 14

def migrations_path
  @migrations_path
end

.migrations_path=(path) ⇒ Object



10
11
12
# File 'lib/orientdb_schema_migrator/migrator.rb', line 10

def migrations_path= path
  @migrations_path = path
end

.rollback(options = {}) ⇒ Object



53
54
55
# File 'lib/orientdb_schema_migrator/migrator.rb', line 53

def rollback(options = {})
  new(current_version, nil, nil).rollback
end

.run(command, target_version) ⇒ Object



49
50
51
# File 'lib/orientdb_schema_migrator/migrator.rb', line 49

def run(command, target_version)
  new(current_version, target_version, command).migrate
end

Instance Method Details

#migrateObject



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

def migrate
  migrations[start..finish].each do |m|
    require m[:path]
    m[:name].camelize.constantize.public_send(@direction)
    if up?
      record_migration(m)
    else
      drop_migration(m)
    end
  end
end

#migrationsObject



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

def migrations
  return @migrations if defined?(@migrations)
  if @direction == :down
    @migrations = self.class.migrations.reverse
  else
    @migrations = self.class.migrations
  end
end

#rollbackObject



91
92
93
94
95
96
97
# File 'lib/orientdb_schema_migrator/migrator.rb', line 91

def rollback
  migrations.find { |m| m[:version] == @current_version }.tap do |m|
    require m[:path]
    m[:name].camelize.constantize.down
    drop_migration(m)
  end
end