Class: Lhm::Migrator
- Inherits:
-
Object
- Object
- Lhm::Migrator
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#statements ⇒ Object
readonly
Returns the value of attribute statements.
Instance Method Summary collapse
-
#add_column(name, definition) ⇒ Object
Add a column to a table.
-
#add_index(columns) ⇒ Object
Add an index to a table.
-
#add_unique_index(columns) ⇒ Object
Add a unique index to a table.
-
#change_column(name, definition) ⇒ Object
Change an existing column to a new definition.
-
#ddl(statement) ⇒ Object
Alter a table with a custom statement.
-
#initialize(table, connection = nil) ⇒ Migrator
constructor
A new instance of Migrator.
-
#remove_column(name) ⇒ Object
Remove a column from a table.
-
#remove_index(columns) ⇒ Object
Remove an index from a table.
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #sql, #table?, #update
Methods included from Command
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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
16 17 18 |
# File 'lib/lhm/migrator.rb', line 16 def connection @connection end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/lhm/migrator.rb', line 16 def name @name end |
#statements ⇒ Object (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
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
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
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
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
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
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
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
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 |