class OrientdbSchemaMigrator::Migrator

Public Class Methods

connect_to_db(db, user, password) click to toggle source
# 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_version() click to toggle source
# 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
disconnect() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 22
def disconnect
  OrientdbSchemaMigrator.client.disconnect
end
migrate(target_version = nil) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 26
def migrate(target_version = nil)
  run(:up, target_version)
end
migrations() click to toggle source
# 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_path() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 14
def migrations_path
  @migrations_path
end
migrations_path=(path) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 10
def migrations_path= path
  @migrations_path = path
end
new(current_version, target_version, direction) click to toggle source
# 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
rollback(options = {}) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 53
def rollback(options = {})
  new(current_version, nil, nil).rollback
end
run(command, target_version) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 49
def run(command, target_version)
  new(current_version, target_version, command).migrate
end

Public Instance Methods

migrate() click to toggle source
# 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
migrations() click to toggle source
# 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
rollback() click to toggle source
# 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

Private Instance Methods

down?() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 121
def down?
  @direction == :down
end
drop_migration(migration) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 113
def drop_migration(migration)
  OrientdbSchemaMigrator.client.command "DELETE FROM schema_versions WHERE schema_version = '#{migration[:version]}'"
end
finish() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 105
def finish
  @target_version.nil? ? migrations.size - 1 : migrations.find_index { |m| m[:version] == @target_version }
end
record_migration(migration) click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 109
def record_migration(migration)
  OrientdbSchemaMigrator.client.command "INSERT INTO schema_versions (schema_version) VALUES (#{migration[:version]})"
end
start() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 101
def start
  @current_version.nil? ? 0 : migrations.find_index { |m| m[:version] == @current_version }
end
up?() click to toggle source
# File lib/orientdb_schema_migrator/migrator.rb, line 117
def up?
  @direction == :up
end