Class: Lhm::Migrator

Inherits:
Object
  • Object
show all
Includes:
Command, SqlHelper
Defined in:
lib/lhm/migrator.rb

Overview

Copies existing schema and applies changes using alter on the empty table. ‘run` returns a Migration which can be used for the remaining process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlHelper

#annotation, #idx_name, #idx_spec, #sql, #table?, #update, #version_string

Methods included from Command

#run

Constructor Details

#initialize(table, connection = nil) ⇒ Migrator

Returns a new instance of Migrator.



18
19
20
21
22
23
24
25
# File 'lib/lhm/migrator.rb', line 18

def initialize(table, connection = nil)
  @connection = connection
  @origin = table
  @name = table.destination_name
  @statements = []
  @insert_trigger_additions = {}
  @insert_joins = []
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



16
17
18
# File 'lib/lhm/migrator.rb', line 16

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/lhm/migrator.rb', line 16

def name
  @name
end

#statementsObject (readonly)

Returns the value of attribute statements.



16
17
18
# File 'lib/lhm/migrator.rb', line 16

def statements
  @statements
end

Instance Method Details

#add_column(name, definition) ⇒ Object

Add a column to a table

Examples:


Lhm.change_table(:users) do |m|
  m.add_column(:comment, "VARCHAR(12) DEFAULT '0'")
end

Parameters:

  • name (String)

    Name of the column to add

  • definition (String)

    Valid SQL column definition



89
90
91
# File 'lib/lhm/migrator.rb', line 89

def add_column(name, definition)
  ddl("alter table `%s` add column `%s` %s" % [@name, name, definition])
end

#add_index(columns, index_name = nil) ⇒ Object

Add an index to a table

Examples:


Lhm.change_table(:users) do |m|
  m.add_index(:comment)
  m.add_index([:username, :created_at])
  m.add_index("comment(10)")
end

Parameters:

  • columns (String, Symbol, Array<String, Symbol>)

    A column name given as String or Symbol. An Array of Strings or Symbols for compound indexes. It’s possible to pass a length limit.

  • index_name (String, Symbol) (defaults to: nil)

    Optional name of the index to be created



135
136
137
# File 'lib/lhm/migrator.rb', line 135

def add_index(columns, index_name = nil)
  ddl(index_ddl(columns, false, index_name))
end

#add_unique_index(columns, index_name = nil) ⇒ Object

Add a unique index to a table

Examples:


Lhm.change_table(:users) do |m|
  m.add_unique_index(:comment)
  m.add_unique_index([:username, :created_at])
  m.add_unique_index("comment(10)")
end

Parameters:

  • columns (String, Symbol, Array<String, Symbol>)

    A column name given as String or Symbol. An Array of Strings or Symbols for compound indexes. It’s possible to pass a length limit.

  • index_name (String, Symbol) (defaults to: nil)

    Optional name of the index to be created



154
155
156
# File 'lib/lhm/migrator.rb', line 154

def add_unique_index(columns, index_name = nil)
  ddl(index_ddl(columns, true, index_name))
end

#change_column(name, definition) ⇒ Object

Change an existing column to a new definition

Examples:


Lhm.change_table(:users) do |m|
  m.change_column(:comment, "VARCHAR(12) DEFAULT '0' NOT NULL")
end

Parameters:

  • name (String)

    Name of the column to change

  • definition (String)

    Valid SQL column definition



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

def change_column(name, definition)
  ddl("alter table `%s` modify column `%s` %s" % [@name, name, definition])
end

#ddl(statement) ⇒ Object

Note:

Don’t write the table name directly into the statement. Use the #name getter instead, because the alter statement will be executed against a temporary table.

Alter a table with a custom statement

Examples:


Lhm.change_table(:users) do |m|
  m.ddl("ALTER TABLE #{m.name} ADD COLUMN age INT(11)")
end

Parameters:

  • statement (String)

    SQL alter statement



42
43
44
# File 'lib/lhm/migrator.rb', line 42

def ddl(statement)
  statements << statement
end

#insert_trigger(key, statement) ⇒ Object

Adds additional columns to the trigger that is created for inserts.

Examples:


Lhm.change_table(:users) do |m|
  m.add_column(:comment, "VARCHAR(12) DEFAULT '0'")
  m.insert_trigger(:comment, "SELECT comment FROM people WHERE NEW.id = people.id")
end

Parameters:

  • key (String)

    Column to insert value

  • statement (String)

    Valid sql query, can use NEW to reference the row in trigger



75
76
77
# File 'lib/lhm/migrator.rb', line 75

def insert_trigger(key, statement)
  @insert_trigger_additions[key] = statement
end

#join_on_insert(table, origin_field, destination_field, statement) ⇒ Object

Adds joins to the chunked insert. Helpful if you would need to do an update after the change_table

Examples:


Lhm.change_table(:users) do |m|
  m.add_column(:comment, "VARCHAR(12) DEFAULT '0'")
  m.join_on_insert(:people, :description, :comment, "people.user_id = users.id")
end

Parameters:

  • table (String)

    Table to join to

  • origin_field (String)

    Column in the origin (joined) table

  • destination_field (String)

    Column in the destination table

  • statement (String)

    Valid sql join statement



60
61
62
# File 'lib/lhm/migrator.rb', line 60

def join_on_insert(table, origin_field, destination_field, statement)
  @insert_joins << { :table => table, :origin_field => origin_field, :destination_field => destination_field, :statement => statement }
end

#remove_column(name) ⇒ Object

Remove a column from a table

Examples:


Lhm.change_table(:users) do |m|
  m.remove_column(:comment)
end

Parameters:

  • name (String)

    Name of the column to delete



116
117
118
# File 'lib/lhm/migrator.rb', line 116

def remove_column(name)
  ddl("alter table `%s` drop `%s`" % [@name, name])
end

#remove_index(columns, index_name = nil) ⇒ Object

Remove an index from a table

Examples:


Lhm.change_table(:users) do |m|
  m.remove_index(:comment)
  m.remove_index([:username, :created_at])
end

Parameters:

  • columns (String, Symbol, Array<String, Symbol>)

    A column name given as String or Symbol. An Array of Strings or Symbols for compound indexes.

  • index_name (String, Symbol) (defaults to: nil)

    Optional name of the index to be removed



172
173
174
175
# File 'lib/lhm/migrator.rb', line 172

def remove_index(columns, index_name = nil)
  index_name ||= idx_name(@origin.name, columns)
  ddl("drop index `%s` on `%s`" % [index_name, @name])
end