Module: Whiteprint::Migrator

Defined in:
lib/whiteprint/migrator.rb

Class Method Summary collapse

Class Method Details

.eager_load!Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/whiteprint/migrator.rb', line 4

def eager_load!
  return unless Whiteprint.config.eager_load

  Rails.application.eager_load! if defined?(Rails)

  [*Whiteprint.config.eager_load_paths.uniq].each do |path|
    Gem.find_files(path).each do |file|
      load file
    end
  end
end

.explanationsObject



16
17
18
19
20
# File 'lib/whiteprint/migrator.rb', line 16

def explanations
  Whiteprint.changed_whiteprints.map.with_index do |whiteprint, index|
    whiteprint.explanation(index + 1)
  end
end

.interactive(input: $stdin, output: $stdout, migrate_input: $stdin, migrate_output: $stdout) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/whiteprint/migrator.rb', line 22

def interactive(input: $stdin, output: $stdout, migrate_input: $stdin, migrate_output: $stdout)
  # TODO: Clean up

  eager_load!
  cli = HighLine.new input, output

  if number_of_changes == 0
    cli.say('Whiteprint detected no changes')
    return
  end

  cli.say "Whiteprint has detected <%= color('#{number_of_changes}', :bold, :white) %> changes to your models."
  explanations.each do |explanation|
    cli.say explanation
  end

  cli.choose do |menu|
    menu.header = 'Migrations'
    menu.prompt = 'How would you like to process these changes?'
    menu.choice('In one migration')       { migrate_at_once(input: migrate_input, output: migrate_output) }
    menu.choice('In separate migrations') { cli.say 'Bar' }
  end
end

.migrate_at_once(input: $stdin, output: $stdout) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/whiteprint/migrator.rb', line 46

def migrate_at_once(input: $stdin, output: $stdout)
  # TODO: Clean up

  cli = HighLine.new input, output
  name = cli.ask 'How would you like to name this migration?'
  Whiteprint.changed_whiteprints
           .group_by(&:transformer)
           .map do |adapter, whiteprints|
             adapter.generate_migration(name, whiteprints.map(&:changes_tree))
           end

  ActiveRecord::Migration.verbose = true
  ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths)
end

.number_of_changesObject



61
62
63
# File 'lib/whiteprint/migrator.rb', line 61

def number_of_changes
  Whiteprint.changed_whiteprints.size
end