Class: Mxrb::Frontend::MigrationPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/frontend/migrator.rb

Overview

Immutable, fail-closed preview of frontend model migrations. rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, version:, changes:, issues:) ⇒ MigrationPlan

Returns a new instance of MigrationPlan.



18
19
20
21
22
23
24
# File 'lib/mxrb/frontend/migrator.rb', line 18

def initialize(path:, version:, changes:, issues:)
  @path = path
  @version = version
  @changes = changes.freeze
  @issues = issues.freeze
  @applied = false
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



16
17
18
# File 'lib/mxrb/frontend/migrator.rb', line 16

def changes
  @changes
end

#issuesObject (readonly)

Returns the value of attribute issues.



16
17
18
# File 'lib/mxrb/frontend/migrator.rb', line 16

def issues
  @issues
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/mxrb/frontend/migrator.rb', line 16

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/mxrb/frontend/migrator.rb', line 16

def version
  @version
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


27
# File 'lib/mxrb/frontend/migrator.rb', line 27

def applied? = @applied

#apply!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mxrb/frontend/migrator.rb', line 32

def apply!
  raise SerializationError, blocked_message unless safe?
  raise SerializationError, 'frontend migration plan was already applied' if applied?

  mpr = IO::MprFile.open(path)
  mpr.transaction do
    changes.each do |change|
      current = mpr.unit(change.unit_id)
      raise SerializationError, "frontend unit disappeared: #{change.unit_id}" unless current
      raise SerializationError, "frontend unit changed after preview: #{change.unit_id}" unless
        current['ContentsHash'] == change.before_hash

      mpr.update_unit(change.unit_id, change.after)
    end
  end
  @applied = true
  self
ensure
  mpr&.close
end

#design_propertiesObject



30
# File 'lib/mxrb/frontend/migrator.rb', line 30

def design_properties = changes.sum(&:design_properties)

#layout_rowsObject



29
# File 'lib/mxrb/frontend/migrator.rb', line 29

def layout_rows = changes.sum(&:layout_rows)

#safe?Boolean

Returns:

  • (Boolean)


26
# File 'lib/mxrb/frontend/migrator.rb', line 26

def safe? = issues.empty?

#widgetsObject



28
# File 'lib/mxrb/frontend/migrator.rb', line 28

def widgets = changes.sum(&:widgets)