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(file_name:, db_name:) ⇒ CompiledMigration

Returns a new instance of CompiledMigration.



19
20
21
22
23
24
25
26
# File 'lib/nandi/compiled_migration.rb', line 19

def initialize(file_name:, db_name:)
  @file_name = file_name
  @db_config = Nandi.config.database(db_name)
  @source_file_path = File.join(@db_config.migration_directory, file_name)
  require File.expand_path(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

#db_configObject (readonly)

Returns the value of attribute db_config.



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

def db_config
  @db_config
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(file_name:, db_name:) ⇒ Object



15
16
17
# File 'lib/nandi/compiled_migration.rb', line 15

def self.build(file_name:, db_name:)
  new(file_name:, db_name:)
end

Instance Method Details

#bodyObject



39
40
41
42
43
44
45
46
# File 'lib/nandi/compiled_migration.rb', line 39

def body
  @body ||= if migration_unchanged?
              File.read(output_path)
            else
              validate!
              compiled_body
            end
end

#compiled_digestObject



56
57
58
# File 'lib/nandi/compiled_migration.rb', line 56

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

#db_nameObject



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

def db_name
  @db_config.name
end

#migrationObject



52
53
54
# File 'lib/nandi/compiled_migration.rb', line 52

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

#migration_unchanged?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nandi/compiled_migration.rb', line 64

def migration_unchanged?
  return false unless File.exist?(output_path)

  lockfile = Nandi::Lockfile.for(db_config.name)

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

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

  source_migration_diff.unchanged? && compiled_migration_diff.unchanged?
end

#output_pathObject



48
49
50
# File 'lib/nandi/compiled_migration.rb', line 48

def output_path
  "#{db_config.output_directory}/#{file_name}"
end

#source_digestObject



60
61
62
# File 'lib/nandi/compiled_migration.rb', line 60

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

#validate!Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/nandi/compiled_migration.rb', line 28

def validate!
  validation = migration.validate

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

  self
end