Class: Migflow::Services::SchemaPatchBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_tables:, to_tables:, changed_tables:, include_unchanged:) ⇒ SchemaPatchBuilder

Returns a new instance of SchemaPatchBuilder.



15
16
17
18
19
20
# File 'lib/migflow/services/schema_patch_builder.rb', line 15

def initialize(from_tables:, to_tables:, changed_tables:, include_unchanged:)
  @from_tables = from_tables
  @to_tables = to_tables
  @changed_tables = changed_tables&.to_set
  @include_unchanged = include_unchanged
end

Class Method Details

.call(from_tables:, to_tables:, changed_tables: nil, include_unchanged: false) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/migflow/services/schema_patch_builder.rb', line 6

def self.call(from_tables:, to_tables:, changed_tables: nil, include_unchanged: false)
  new(
    from_tables: from_tables || {},
    to_tables: to_tables || {},
    changed_tables: changed_tables,
    include_unchanged: include_unchanged
  ).build
end

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/migflow/services/schema_patch_builder.rb', line 22

def build
  document = build_document
  lines = document[:lines]
  has_diff = lines.any? { |line| ["+", "-"].include?(line[:prefix]) }
  return "" unless has_diff

  return build_full_patch(lines) if @include_unchanged

  build_collapsed_patch(lines, document[:sections])
end