Class: Migflow::Services::SnapshotBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/migflow/services/snapshot_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrations:, up_to_version:) ⇒ SnapshotBuilder

Returns a new instance of SnapshotBuilder.



12
13
14
15
# File 'lib/migflow/services/snapshot_builder.rb', line 12

def initialize(migrations:, up_to_version:)
  @migrations    = migrations.sort_by { |m| m[:version] }
  @up_to_version = up_to_version
end

Class Method Details

.call(migrations:, up_to_version:) ⇒ Object



8
9
10
# File 'lib/migflow/services/snapshot_builder.rb', line 8

def self.call(migrations:, up_to_version:)
  new(migrations: migrations, up_to_version: up_to_version).build
end

Instance Method Details

#buildObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/migflow/services/snapshot_builder.rb', line 17

def build
  before = { tables: {} }
  after  = { tables: {} }

  @migrations.each do |migration|
    break if migration[:version] > @up_to_version

    before = deep_copy(after)
    after  = apply_migration(after, migration[:raw_content])
  end

  { schema_before: before, schema_after: after, diff: calculate_diff(before, after) }
end