Module: Lhm

Defined in:
lib/lhm.rb,
lib/lhm/table.rb,
lib/lhm/chunker.rb,
lib/lhm/command.rb,
lib/lhm/invoker.rb,
lib/lhm/version.rb,
lib/lhm/migrator.rb,
lib/lhm/entangler.rb,
lib/lhm/migration.rb,
lib/lhm/connection.rb,
lib/lhm/sql_helper.rb,
lib/lhm/intersection.rb,
lib/lhm/atomic_switcher.rb,
lib/lhm/locked_switcher.rb

Overview

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.adapterObject



51
52
53
54
55
56
57
# File 'lib/lhm.rb', line 51

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

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

Alters a table with the changes described in the block

Parameters:

  • table_name (String, Symbol)

    Name of the table

  • options (Hash) (defaults to: {})

    Optional options to alter the chunk / switch behavior

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)

  • :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

Returns:

  • (Boolean)

    Returns true if the migration finishes

Raises:

  • (Error)

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/lhm.rb', line 36

def self.change_table(table_name, options = {}, &block)
  connection = Connection.new(adapter)

  origin = Table.parse(table_name, connection)
  invoker = Invoker.new(origin, connection)
  block.call(invoker.migrator)
  invoker.run(options)

  true
end

.setup(adapter) ⇒ Object



47
48
49
# File 'lib/lhm.rb', line 47

def self.setup(adapter)
  @@adapter = adapter
end