Class: Ridgepole::Delta

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

Constant Summary collapse

SCRIPT_NAME =
'<Schema>'

Instance Method Summary collapse

Constructor Details

#initialize(delta, options = {}) ⇒ Delta

Returns a new instance of Delta.



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

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

Instance Method Details

#differ?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ridgepole/delta.rb', line 51

def differ?
  not script.empty? or not delta_execute.empty?
end

#migrate(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ridgepole/delta.rb', line 10

def migrate(options = {})
  if log_file = @options[:log_file]
    result = ActiveRecord::Migration.record_time do
      migrate0(options)
    end

    open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
    result
  else
    migrate0(options)
  end
end

#scriptObject



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
# File 'lib/ridgepole/delta.rb', line 23

def script
  buf = StringIO.new
  pre_buf_for_fk = StringIO.new
  post_buf_for_fk = StringIO.new

  (@delta[:add] || {}).each do |table_name, attrs|
    append_create_table(table_name, attrs, buf, post_buf_for_fk)
  end

  (@delta[:rename] || {}).each do |table_name, attrs|
    append_rename_table(table_name, attrs, buf)
  end

  (@delta[:change] || {}).each do |table_name, attrs|
    append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
  end

  (@delta[:delete] || {}).each do |table_name, attrs|
    append_drop_table(table_name, attrs, buf)
  end

  [
    pre_buf_for_fk,
    buf,
    post_buf_for_fk,
  ].map {|b| b.string.strip }.join("\n\n").strip
end