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

Methods included from Command

#run

Constructor Details

#initialize(table, connection = nil) ⇒ Migrator

Returns a new instance of Migrator.



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

def initialize(table, connection = nil)
  @connection = connection
  @origin = table
  @name = table.destination_name
  @statements = []
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



54
55
56
# File 'lib/lhm/migrator.rb', line 54

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

#add_index(columns) ⇒ 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.



99
100
101
# File 'lib/lhm/migrator.rb', line 99

def add_index(columns)
  ddl(index_ddl(columns))
end

#add_unique_index(columns) ⇒ 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.



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

def add_unique_index(columns)
  ddl(index_ddl(columns, :unique))
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



68
69
70
71
# File 'lib/lhm/migrator.rb', line 68

def change_column(name, definition)
  remove_column(name)
  add_column(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



40
41
42
# File 'lib/lhm/migrator.rb', line 40

def ddl(statement)
  statements << 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



82
83
84
# File 'lib/lhm/migrator.rb', line 82

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

#remove_index(columns) ⇒ 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.



132
133
134
# File 'lib/lhm/migrator.rb', line 132

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