class TDP::Engine

Main class of the package.

Public Class Methods

new(db) click to toggle source

Creates a new Engine object.

db

must be one of:

  • instance of Sequel::Database class

  • database URL that can be passed to Sequel.connect()

# File lib/tdp.rb, line 326
def initialize(db)
  @dao = DAO.new(db)
  @patches = PatchSet.new
end

Public Instance Methods

<<(filename) click to toggle source

Registers patch files in the engine.

filename

may be either a name of .sql file or a name

of directory (which would be recursively scanned for .sql files)

# File lib/tdp.rb, line 338
def <<(filename)
  if File.directory?(filename)
    Dir.foreach(filename) do |x|
      self << File.join(filename, x) unless x.start_with?('.')
    end
  elsif TDP.patch_file?(filename)
    @patches << Patch.new(filename)
  end
end
bootstrap() click to toggle source

Initializes database tables for keeping track of applied patches.

# File lib/tdp.rb, line 352
def bootstrap
  @dao.bootstrap
end
plan() click to toggle source

Produces an ordered list of patches that need to be applied.

May raise MismatchError in case if signatures of any permanent patches that are present in the definition don't match ones of the patches applied to the database.

# File lib/tdp.rb, line 363
def plan
  ref = @dao.applied_patches
  @patches.select do |patch|
    signature = ref[patch.name]
    next false if signature == patch.signature
    next true if signature.nil? || patch.volatile?
    raise MismatchError, patch
  end
end
plan_rename() click to toggle source

Produces an { old_name => new_name } hash for mass-renaming.

# File lib/tdp.rb, line 376
def plan_rename
  ref = @dao.applied_patches_inverse
  m = {}
  @patches.each do |patch|
    old_name = ref[patch.signature]
    raise NotAppliedError, patch if old_name.nil?
    raise DuplicateError, [patch.name, m[old_name]] if m.key?(old_name)
    m[old_name] = patch.name
  end
  m.select { |old_name, new_name| old_name != new_name }
end
rename() click to toggle source

Amends the data about applied patches after they were renamed (without content changes) in the configuration.

# File lib/tdp.rb, line 446
def rename
  plan_rename.each do |old_name, new_name|
    @dao.rename(old_name, new_name)
  end
end
retrofit() click to toggle source

Erases existing data about applied patches and replaces it with configured schema.

# File lib/tdp.rb, line 435
def retrofit
  @dao.erase
  @patches.each do |patch|
    @dao.register(patch)
  end
end
upgrade() click to toggle source

Applies all changes that need to be applied.

# File lib/tdp.rb, line 391
def upgrade
  validate_upgradable
  plan.each { |patch| @dao.apply(patch) }
end
validate_compatible() click to toggle source

Validates that all patches are applied to the database.

May raise MismatchError, NotConfiguredError or NotAppliedError in case if there are any problems.

# File lib/tdp.rb, line 420
def validate_compatible
  validate_upgradable

  @patches.each do |patch|
    signature = @dao.patch_signature(patch.name)
    next if signature == patch.signature
    raise NotAppliedError, patch if signature.nil?
    raise MismatchError, patch
  end
end
validate_upgradable() click to toggle source

Validates that it is safe run upgrade the database. In particular, the following conditions must be met:

  • there is an .sql file for every patch that is marked as applied to database

  • every permanent patch has same signature as the corresponding .sql file.

May raise MismatchError or NotConfiguredError in case if those conditions aren't met.

# File lib/tdp.rb, line 407
def validate_upgradable
  @dao.applied_patches.each_key do |name|
    raise NotConfiguredError, name unless @patches[name]
  end
  plan
end