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

#create_indexes_after_execute_online(plan) ⇒ Object



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

def create_indexes_after_execute_online(plan)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    original_table_name = table.table_name.gsub("_#{migration.version}", '')
    indexes_file_name = <<~EOS.chomp
      #{Sequent.configuration.migration_sql_files_directory}/#{original_table_name}.indexes.sql
    EOS
    next unless File.exist?(indexes_file_name)

    statements = sql_file_to_statements(indexes_file_name) do |raw_sql|
      raw_sql.gsub('%SUFFIX%', "_#{migration.version}")
    end
    statements.each(&method(:exec_sql))
  end
end

#execute_offline(plan, current_version) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sequent/migrations/executor.rb', line 41

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 = <<~EOS.chomp
      #{Sequent.configuration.migration_sql_files_directory}/#{table.table_name}_#{migration.version}.sql
    EOS
    statements = sql_file_to_statements(sql_file)
    statements.each(&method(:exec_sql))
  end
end

#execute_online(plan) ⇒ Object



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

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



64
65
66
67
68
69
70
# File 'lib/sequent/migrations/executor.rb', line 64

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sequent/migrations/executor.rb', line 72

def set_table_names_to_new_version(plan)
  plan.replay_tables.each do |migration|
    table = migration.record_class
    next if table.table_name.end_with?("_#{migration.version}")

    table.table_name = "#{table.table_name}_#{migration.version}"
    table.reset_column_information
    unless table.table_exists?
      fail MigrationError,
           "Table #{table.table_name} does not exist. Did you run ViewSchema.migrate_online first?"
    end
  end
end