Class: Sequent::Migrations::Executor

Inherits:
Object
  • Object
show all
Includes:
Sql
Defined in:
lib/sequent/migrations/executor.rb

Overview

The executor is the implementation of the 3-phase deploy in Sequent. is responsible for executing the ‘Planner::Plan`.

Instance Method Summary collapse

Methods included from Sql

#exec_sql, #sql_file_to_statements

Instance Method Details

#execute_offline(plan, current_version) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sequent/migrations/executor.rb', line 24

def execute_offline(plan, current_version)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    current_table_name = table.table_name.gsub("_#{migration.version}", "")
    # 2 Rename old table
    exec_sql("ALTER TABLE IF EXISTS #{current_table_name} RENAME TO #{current_table_name}_#{current_version}")
    # 3 Rename new table
    exec_sql("ALTER TABLE #{table.table_name} RENAME TO #{current_table_name}")
    # Use new table from now on
    table.table_name = current_table_name
    table.reset_column_information
  end

  plan.alter_tables.each do |migration|
    table = migration.record_class
    sql_file = "#{Sequent.configuration.migration_sql_files_directory}/#{table.table_name}_#{migration.version}.sql"
    statements = sql_file_to_statements(sql_file)
    statements.each(&method(:exec_sql))
  end
end

#execute_online(plan) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/sequent/migrations/executor.rb', line 13

def execute_online(plan)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    sql_file = "#{Sequent.configuration.migration_sql_files_directory}/#{table.table_name}.sql"
    statements = sql_file_to_statements(sql_file) { |raw_sql| raw_sql.gsub('%SUFFIX%', "_#{migration.version}") }
    statements.each(&method(:exec_sql))
    table.table_name = "#{table.table_name}_#{migration.version}"
    table.reset_column_information
  end
end

#reset_table_names(plan) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/sequent/migrations/executor.rb', line 45

def reset_table_names(plan)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    table.table_name = table.table_name.gsub("_#{migration.version}", "")
    table.reset_column_information
  end
end

#set_table_names_to_new_version(plan) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/sequent/migrations/executor.rb', line 53

def set_table_names_to_new_version(plan)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    unless table.table_name.end_with?("_#{migration.version}")
      table.table_name = "#{table.table_name}_#{migration.version}"
      table.reset_column_information
      fail MigrationError.new("Table #{table.table_name} does not exist. Did you run ViewSchema.migrate_online first?") unless table.table_exists?
    end
  end
end