Class: Nandi::CompiledMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/nandi/compiled_migration.rb

Defined Under Namespace

Classes: InvalidMigrationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file_path) ⇒ CompiledMigration

Returns a new instance of CompiledMigration.



15
16
17
18
19
20
# 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

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



9
10
11
# File 'lib/nandi/compiled_migration.rb', line 9

def class_name
  @class_name
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



9
10
11
# File 'lib/nandi/compiled_migration.rb', line 9

def file_name
  @file_name
end

#source_file_pathObject (readonly)

Returns the value of attribute source_file_path.



9
10
11
# File 'lib/nandi/compiled_migration.rb', line 9

def source_file_path
  @source_file_path
end

Class Method Details

.build(source_file_path) ⇒ Object



11
12
13
# File 'lib/nandi/compiled_migration.rb', line 11

def self.build(source_file_path)
  new(source_file_path)
end

Instance Method Details

#bodyObject



33
34
35
36
37
38
39
40
# 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_digestObject



50
51
52
# File 'lib/nandi/compiled_migration.rb', line 50

def compiled_digest
  Digest::SHA256.hexdigest(body)
end

#migrationObject



46
47
48
# File 'lib/nandi/compiled_migration.rb', line 46

def migration
  @migration ||= class_name.camelize.constantize.new(Nandi.validator)
end

#migration_unchanged?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_pathObject



42
43
44
# File 'lib/nandi/compiled_migration.rb', line 42

def output_path
  "#{Nandi.compiled_output_directory}/#{file_name}"
end

#source_digestObject



54
55
56
# File 'lib/nandi/compiled_migration.rb', line 54

def source_digest
  Digest::SHA256.hexdigest(File.read(source_file_path))
end

#validate!Object



22
23
24
25
26
27
28
29
30
31
# 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