Sha256: 1add72547af730a6a3be02498f22dfa6ac882c2f0172ebfb26b8065d54edf5ca

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
# Schmidt

require 'lhm/command'
require 'lhm/migration'
require 'lhm/sql_helper'

module Lhm
  # Switches origin with destination table using an atomic rename.
  #
  # It should only be used if the MySQL server version is not affected by the
  # bin log affecting bug #39675. This can be verified using
  # Lhm::SqlHelper.supports_atomic_switch?.
  class AtomicSwitcher
    include Command
    include SqlHelper

    attr_reader :connection

    def initialize(migration, connection = nil)
      @migration = migration
      @connection = connection
      @origin = migration.origin
      @destination = migration.destination
    end

    def statements
      atomic_switch
    end

    def atomic_switch
      [
        "rename table `#{ @origin.name }` to `#{ @migration.archive_name }`, " +
        "`#{ @destination.name }` to `#{ @origin.name }`"
      ]
    end

    def validate
      unless table?(@origin.name) && table?(@destination.name)
        error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
      end
    end

  private
    def execute
      sql statements
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
sbader-lhm-1.1.0 lib/lhm/atomic_switcher.rb
lhm-1.1.0 lib/lhm/atomic_switcher.rb