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 comment).freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Diff

Returns a new instance of Diff.



4
5
6
7
# File 'lib/ridgepole/diff.rb', line 4

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

Instance Method Details

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



9
10
11
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
# File 'lib/ridgepole/diff.rb', line 9

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 (attrs_delta = diff_inspect(from_attrs, to_attrs)).empty?
        @logger.verbose_info(attrs_delta)
      end

      scan_change(table_name, from_attrs, to_attrs, delta)
    else
      delta[:add] ||= {}
      delta[:add][table_name] = to_attrs
    end
  end

  scan_relation_info(relation_info)

  unless @options[:merge] or @options[:skip_drop_table]
    from.each do |table_name, from_attrs|
      next unless target?(table_name)

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

  delta[:execute] = options[:execute]

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