Module: Lhm

Extended by:
Lhm, Throttler
Included in:
Lhm
Defined in:
lib/lhm.rb,
lib/lhm/table.rb,
lib/lhm/chunker.rb,
lib/lhm/invoker.rb,
lib/lhm/printer.rb,
lib/lhm/version.rb,
lib/lhm/command.rb,
lib/lhm/migrator.rb,
lib/lhm/entangler.rb,
lib/lhm/migration.rb,
lib/lhm/throttler.rb,
lib/lhm/sql_helper.rb,
lib/lhm/intersection.rb,
lib/lhm/throttler/time.rb,
lib/lhm/locked_switcher.rb,
lib/lhm/atomic_switcher.rb,
lib/lhm/throttler/slave_lag.rb

Overview

Copyright © 2011 - 2013, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias Schmidt

Defined Under Namespace

Modules: Command, Printer, SqlHelper, Throttler Classes: AtomicSwitcher, Chunker, Entangler, Error, Intersection, Invoker, LockedSwitcher, Migration, Migrator, Table

Constant Summary collapse

DEFAULT_LOGGER_OPTIONS =
{ level: Logger::INFO, file: STDOUT }
VERSION =
'3.0.0.alpha'

Constants included from Throttler

Throttler::CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Throttler

setup_throttler, throttler

Class Method Details

.loggerObject


107
108
109
110
111
112
113
114
115
# File 'lib/lhm.rb', line 107

def self.logger
  @@logger ||=
    begin
      logger = Logger.new(DEFAULT_LOGGER_OPTIONS[:file])
      logger.level = DEFAULT_LOGGER_OPTIONS[:level]
      logger.formatter = nil
      logger
    end
end

.logger=(new_logger) ⇒ Object


103
104
105
# File 'lib/lhm.rb', line 103

def self.logger=(new_logger)
  @@logger = new_logger
end

Instance Method Details

#change_table(table_name, options = {}) {|Migrator| ... } ⇒ Boolean

Alters a table with the changes described in the block

Options Hash (options):

  • :stride (Fixnum)

    Size of a chunk (defaults to: 40,000)

  • :throttle (Fixnum)

    Time to wait between chunks in milliseconds (defaults to: 100)

  • :start (Fixnum)

    Primary Key position at which to start copying chunks

  • :limit (Fixnum)

    Primary Key position at which to stop copying chunks

  • :atomic_switch (Boolean)

    Use atomic switch to rename tables (defaults to: true) If using a version of mysql affected by atomic switch bug, LHM forces user to set this option (see SqlHelper#supports_atomic_switch?)

Yields:

  • (Migrator)

    Yielded Migrator object records the changes

Raises:

  • (Error)

    Raises Lhm::Error in case of a error and aborts the migration


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

def change_table(table_name, options = {}, &block)
  origin = Table.parse(table_name, connection)
  invoker = Invoker.new(origin, connection)
  block.call(invoker.migrator)
  invoker.run(options)
  true
end

#cleanup(run = false, options = {}) ⇒ Object

Cleanup tables and triggers

Options Hash (options):

  • :until (Time)

    Filter to only remove tables up to specified time (defaults to: nil)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lhm.rb', line 59

def cleanup(run = false, options = {})
  lhm_tables = connection.select_values('show tables').select { |name| name =~ /^lhm(a|n)_/ }
  if options[:until]
    lhm_tables.select! do |table|
      table_date_time = Time.strptime(table, 'lhma_%Y_%m_%d_%H_%M_%S')
      table_date_time <= options[:until]
    end
  end

  lhm_triggers = connection.select_values('show triggers').collect do |trigger|
    trigger.respond_to?(:trigger) ? trigger.trigger : trigger
  end.select { |name| name =~ /^lhmt/ }

  if run
    lhm_triggers.each do |trigger|
      connection.execute("drop trigger if exists #{trigger}")
    end
    lhm_tables.each do |table|
      connection.execute("drop table if exists #{table}")
    end
    true
  elsif lhm_tables.empty? && lhm_triggers.empty?
    puts 'Everything is clean. Nothing to do.'
    true
  else
    puts "Existing LHM backup tables: #{lhm_tables.join(', ')}."
    puts "Existing LHM triggers: #{lhm_triggers.join(', ')}."
    puts 'Run Lhm.cleanup(:run) to drop them all.'
    false
  end
end

#connectionObject


95
96
97
98
99
100
101
# File 'lib/lhm.rb', line 95

def connection
  @@connection ||=
    begin
      raise 'Please call Lhm.setup' unless defined?(ActiveRecord)
      ActiveRecord::Base.connection
    end
end

#setup(connection) ⇒ Object


91
92
93
# File 'lib/lhm.rb', line 91

def setup(connection)
  @@connection = connection
end