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
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#renames ⇒ Object
readonly
Returns the value of attribute renames.
-
#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.
-
#filter(sql) ⇒ String
Filter the data that is copied into the new table by the provided SQL.
-
#initialize(table, connection = nil) ⇒ Migrator
constructor
A new instance of Migrator.
-
#remove_column(name) ⇒ Object
Remove a column from a table.
-
#remove_index(columns, index_name = nil) ⇒ Object
Remove an index from a table.
-
#rename_column(old, nu) ⇒ Object
Rename an existing column.
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #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 |
# File 'lib/lhm/migrator.rb', line 18 def initialize(table, connection = nil) @connection = connection @origin = table @name = table.destination_name @statements = [] @renames = {} end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
16 17 18 |
# File 'lib/lhm/migrator.rb', line 16 def conditions @conditions end |
#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 |
#renames ⇒ Object (readonly)
Returns the value of attribute renames.
16 17 18 |
# File 'lib/lhm/migrator.rb', line 16 def renames @renames 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
55 56 57 |
# File 'lib/lhm/migrator.rb', line 55 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
122 123 124 |
# File 'lib/lhm/migrator.rb', line 122 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
141 142 143 |
# File 'lib/lhm/migrator.rb', line 141 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
69 70 71 |
# File 'lib/lhm/migrator.rb', line 69 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
41 42 43 |
# File 'lib/lhm/migrator.rb', line 41 def ddl(statement) statements << statement end |
#filter(sql) ⇒ String
Filter the data that is copied into the new table by the provided SQL. This SQL will be inserted into the copy directly after the “from” statement - so be sure to use inner/outer join syntax and not cross joins.
179 180 181 |
# File 'lib/lhm/migrator.rb', line 179 def filter(sql) @conditions = sql end |
#remove_column(name) ⇒ Object
Remove a column from a table
103 104 105 |
# File 'lib/lhm/migrator.rb', line 103 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
159 160 161 162 163 164 165 |
# File 'lib/lhm/migrator.rb', line 159 def remove_index(columns, index_name = nil) columns = [columns].flatten.map(&:to_sym) from_origin = @origin.indices.find { |name, cols| cols.map(&:to_sym) == columns } index_name ||= from_origin[0] unless from_origin.nil? index_name ||= idx_name(@origin.name, columns) ddl('drop index `%s` on `%s`' % [index_name, @name]) end |
#rename_column(old, nu) ⇒ Object
Rename an existing column.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lhm/migrator.rb', line 83 def rename_column(old, nu) col = @origin.columns[old.to_s] definition = col[:type] definition += ' NOT NULL' unless col[:is_nullable] definition += " DEFAULT #{@connection.quote_value(col[:column_default])}" if col[:column_default] ddl('alter table `%s` change column `%s` `%s` %s' % [@name, old, nu, definition]) @renames[old.to_s] = nu.to_s end |