Class: Ridgepole::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/ridgepole/diff.rb

Constant Summary collapse

PRIMARY_KEY_OPTIONS =
%i[id limit default null precision scale collation unsigned].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Diff

Returns a new instance of Diff.



7
8
9
10
# File 'lib/ridgepole/diff.rb', line 7

def initialize(options = {})
  @options = options
  @logger = Ridgepole::Logger.instance
end

Instance Method Details

#diff(from, to, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ridgepole/diff.rb', line 12

def diff(from, to, options = {})
  from = (from || {}).deep_dup
  to = (to || {}).deep_dup

  check_table_existence(to)

  delta = {}
  relation_info = {}

  scan_table_rename(from, to, delta)

  to.each do |table_name, to_attrs|
    collect_relation_info!(table_name, to_attrs, relation_info)

    next unless target?(table_name)

    if (from_attrs = from.delete(table_name))
      @logger.verbose_info("#   #{table_name}")

      unless @options[:drop_table_only]
        unless (attrs_delta = diff_inspect(from_attrs, to_attrs)).empty?
          @logger.verbose_info(attrs_delta)
        end

        scan_change(table_name, from_attrs, to_attrs, delta)
      end
    elsif !@options[:drop_table_only]
      delta[:add] ||= {}
      delta[:add][table_name] = to_attrs
    end
  end

  scan_relation_info(relation_info)

  if !@options[:merge] && (@options[:force_drop_table] || @options[:drop_table_only])
    from.each do |table_name, from_attrs|
      next unless target?(table_name)

      delta[:delete] ||= {}
      delta[:delete][table_name] = from_attrs

      foreign_keys = from_attrs.fetch(:foreign_keys, {})
      next if foreign_keys.empty?

      table_delta = {}
      scan_foreign_keys_change(foreign_keys, {}, table_delta, @options)
      delta[:change] ||= {}
      delta[:change][table_name] = table_delta
    end
  end

  delta[:execute] = options[:execute]

  Ridgepole::Delta.new(delta, @options)
end