Method: Sequent::Migrations::Executor#execute_offline

Defined in:
lib/sequent/migrations/executor.rb

#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