Sha256: 8f81e3cbed6eceb6dbc67fb6a09d49c15089ea53b38c1e51f5438ca1854d8d8a

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

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

require 'lhm/command'
require 'lhm/migration'
require 'lhm/sql_retry'

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

    attr_reader :connection

    LOG_PREFIX = "AtomicSwitcher"

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

    def atomic_switch
      "RENAME TABLE `#{ @origin.name }` TO `#{ @migration.archive_name }`, " \
      "`#{ @destination.name }` TO `#{ @origin.name }`"
    end

    def validate
      unless @connection.data_source_exists?(@origin.name) &&
        @connection.data_source_exists?(@destination.name)
        error "`#{ @origin.name }` AND `#{ @destination.name }` MUST EXIST"
      end
    end

    private

    def execute
      @connection.execute(atomic_switch, should_retry: true, log_prefix: LOG_PREFIX)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
lhm-shopify-4.5.0 lib/lhm/atomic_switcher.rb
lhm-shopify-4.4.2 lib/lhm/atomic_switcher.rb
lhm-shopify-4.4.1 lib/lhm/atomic_switcher.rb
lhm-shopify-4.4.0 lib/lhm/atomic_switcher.rb
lhm-shopify-4.3.0 lib/lhm/atomic_switcher.rb
lhm-shopify-4.2.3 lib/lhm/atomic_switcher.rb
lhm-shopify-4.2.2 lib/lhm/atomic_switcher.rb
lhm-shopify-4.2.1 lib/lhm/atomic_switcher.rb
lhm-shopify-4.2.0 lib/lhm/atomic_switcher.rb
lhm-shopify-4.1.1 lib/lhm/atomic_switcher.rb
lhm-shopify-4.1.0 lib/lhm/atomic_switcher.rb