Class: Convergence::Command::Apply

Inherits:
Convergence::Command show all
Defined in:
lib/convergence/command/apply.rb

Instance Method Summary collapse

Methods inherited from Convergence::Command

#connector, #database_adapter, #dumper, #initialize, #logger, #sql_generator

Constructor Details

This class inherits a constructor from Convergence::Command

Instance Method Details

#executeObject



10
11
12
13
14
15
16
# File 'lib/convergence/command/apply.rb', line 10

def execute
  validate!
  current_dir_path = Pathname.new(@opts[:input]).realpath.dirname
  input_tables = Convergence::DSL.parse(File.open(@opts[:input]).read, current_dir_path)
  current_tables = dumper.dump
  execute_sql(input_tables, current_tables)
end

#execute_sql(input_tables, current_tables) ⇒ Object



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
# File 'lib/convergence/command/apply.rb', line 18

def execute_sql(input_tables, current_tables)
  sql = generate_sql(input_tables, current_tables)
  unless sql.strip.empty?
    sql = "SET FOREIGN_KEY_CHECKS=0;\n    \#{sql}\nSET FOREIGN_KEY_CHECKS=1;\n    SQL\n  end\n  sql.split(';').each do |q2|\n    q = q2.strip\n    unless q.empty?\n      begin\n        q = q + ';'\n        time = Benchmark.realtime { connector.client.query(q) }\n        logger.output q\n        logger.output \"  --> \#{time}s\"\n      rescue => e\n        logger.output 'Invalid Query Exception >>>'\n        logger.output q\n        logger.output '<<<'\n        throw e\n      end\n    end\n  end\nend\n"

#generate_sql(input_tables, current_tables) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/convergence/command/apply.rb', line 45

def generate_sql(input_tables, current_tables)
  current_tables_with_full_option =
    Convergence::DefaultParameter.append_database_default_parameter(current_tables, database_adapter)
  input_tables_with_full_option =
    Convergence::DefaultParameter.append_database_default_parameter(input_tables, database_adapter)
  delta = Convergence::Diff.new.diff(current_tables_with_full_option, input_tables_with_full_option)
  sql_generator.generate(input_tables_with_full_option, delta, current_tables_with_full_option)
end

#validate!Object



5
6
7
8
# File 'lib/convergence/command/apply.rb', line 5

def validate!
  fail ArgumentError.new('--config required') if @config.nil?
  fail ArgumentError.new('--input required') unless @opts[:input]
end