class Nandi::CompiledMigration

Attributes

class_name[R]
file_name[R]
source_file_path[R]

Public Class Methods

build(source_file_path) click to toggle source
# File lib/nandi/compiled_migration.rb, line 11
def self.build(source_file_path)
  new(source_file_path)
end
new(source_file_path) click to toggle source
# File lib/nandi/compiled_migration.rb, line 15
def initialize(source_file_path)
  @source_file_path = source_file_path
  require source_file_path

  @file_name, @class_name = /\d+_([a-z0-9_]+)\.rb\z/.match(source_file_path)[0..1]
end

Public Instance Methods

body() click to toggle source
# File lib/nandi/compiled_migration.rb, line 33
def body
  @body ||= if migration_unchanged?
              File.read(output_path)
            else
              validate!
              compiled_body
            end
end
compiled_digest() click to toggle source
# File lib/nandi/compiled_migration.rb, line 50
def compiled_digest
  Digest::SHA256.hexdigest(body)
end
migration() click to toggle source
# File lib/nandi/compiled_migration.rb, line 46
def migration
  @migration ||= class_name.camelize.constantize.new(Nandi.validator)
end
migration_unchanged?() click to toggle source
# File lib/nandi/compiled_migration.rb, line 58
def migration_unchanged?
  return false unless File.exist?(output_path)

  source_migration_diff = Nandi::FileDiff.new(
    file_path: source_file_path,
    known_digest: Nandi::Lockfile.get(file_name).fetch(:source_digest),
  )

  compiled_migration_diff = Nandi::FileDiff.new(
    file_path: output_path,
    known_digest: Nandi::Lockfile.get(file_name).fetch(:compiled_digest),
  )

  source_migration_diff.unchanged? && compiled_migration_diff.unchanged?
end
output_path() click to toggle source
# File lib/nandi/compiled_migration.rb, line 42
def output_path
  "#{Nandi.compiled_output_directory}/#{file_name}"
end
source_digest() click to toggle source
# File lib/nandi/compiled_migration.rb, line 54
def source_digest
  Digest::SHA256.hexdigest(File.read(source_file_path))
end
validate!() click to toggle source
# File lib/nandi/compiled_migration.rb, line 22
def validate!
  validation = migration.validate

  unless validation.valid?
    raise InvalidMigrationError, "Migration #{source_file_path} " \
      "is not valid:\n#{validation.error_list}"
  end

  self
end

Private Instance Methods

compiled_body() click to toggle source
# File lib/nandi/compiled_migration.rb, line 76
def compiled_body
  output = Nandi.config.renderer.generate(migration)

  if Nandi.config.post_processor
    Nandi.config.post_processor.call(output)
  else
    output
  end
end