class Vcs4sql::Sqlite::Expected

The expected database change log. Contains all changes which should be applied to target database.

Public Class Methods

new(home, testdata) click to toggle source
# File lib/vcs4sql/sqlite/expected.rb, line 32
def initialize(home, testdata)
  @home = home
  @nonprod = ->(f) { !testdata && (f.to_s.end_with? ".testdata.sql") }
end

Public Instance Methods

apply_all(conn) click to toggle source

Apply all sql files one by one @param [Object] conn the connection to the database.

# File lib/vcs4sql/sqlite/expected.rb, line 39
def apply_all(conn)
  apply(-1, conn)
end
apply_mismatch(existing, conn) click to toggle source

Apply sql files which weren't applied yet @param [Object] conn the connection to the database.

# File lib/vcs4sql/sqlite/expected.rb, line 45
def apply_mismatch(existing, conn)
  version = -1
  changelog.each_with_index do |change, i|
    break if existing.absent(i)

    unless change.matches existing.change(i)
      raise ChecksumMismatchError.new change, existing.change(i)
    end

    version = i
  end
  apply(version, conn)
end

Private Instance Methods

apply(ver, conn) click to toggle source

Apply sql files which weren't applied yet based on version @param [Integer] ver The latest applied version to the database @param [Object] conn the connection to the database.

# File lib/vcs4sql/sqlite/expected.rb, line 64
def apply(ver, conn)
  # @todo #/DEV Raise an exception in case if any of arguments are null or
  #  empty. Potentially we may add verification of the type, but this is
  #  optional.
  changelog.select { |change| change.version > ver }.each do |change|
    change.apply(conn)
  end
end
changelog() click to toggle source
# File lib/vcs4sql/sqlite/expected.rb, line 73
def changelog
  return @changelog if defined? @changelog

  @changelog = Dir.glob("#{@home}/*.sql")
                  .select { |f| File.file? f }
                  .reject(&@nonprod)
                  .sort
                  .each_with_index.map do |f, i|
    sql = File.read(f).to_s.tr "\r", " "
    md5 = Digest::MD5.hexdigest sql
    Changelog.new(f, Time.now.iso8601(6), i, md5, sql)
  end
end