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, index_name = nil) ⇒ Object
Add an index to a table.
-
#add_unique_index(columns, index_name = nil) ⇒ 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.
-
#insert_trigger(key, statement) ⇒ Object
Adds additional columns to the trigger that is created for inserts.
-
#join_on_insert(table, origin_field, destination_field, statement) ⇒ Object
Adds joins to the chunked insert.
-
#remove_column(name) ⇒ Object
Remove a column from a table.
-
#remove_index(columns, index_name = nil) ⇒ Object
Remove an index from a table.
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #sql, #table?, #update, #version_string
Methods included from Command
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
#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
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
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
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
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
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
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.
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
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
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
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 |