Class: Zdm::Table
- Inherits:
-
Object
- Object
- Zdm::Table
- Defined in:
- lib/zdm.rb
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#copy ⇒ Object
readonly
Returns the value of attribute copy.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#statements ⇒ Object
readonly
Returns the value of attribute statements.
Instance Method Summary collapse
- #add_column(name, definition) ⇒ Object
- #add_index(column_names, opts = {}) ⇒ Object
- #alter(definition) ⇒ Object
- #change_column(name, definition) ⇒ Object
- #ddl(statement) ⇒ Object
-
#initialize(name) ⇒ Table
constructor
A new instance of Table.
- #remove_column(name) ⇒ Object
- #remove_index(index_name) ⇒ Object
- #rename_column(old_name, new_name) ⇒ Object
Constructor Details
#initialize(name) ⇒ Table
Returns a new instance of Table.
113 114 115 116 117 118 |
# File 'lib/zdm.rb', line 113 def initialize(name) @origin = name @copy = "zdm_#{name}" @archive = "zdma_#{Time.now.strftime("%Y%m%d_%H%M%S%N")}_#{name}"[0...64] @statements = [] end |
Instance Attribute Details
#archive ⇒ Object (readonly)
Returns the value of attribute archive.
111 112 113 |
# File 'lib/zdm.rb', line 111 def archive @archive end |
#copy ⇒ Object (readonly)
Returns the value of attribute copy.
111 112 113 |
# File 'lib/zdm.rb', line 111 def copy @copy end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
111 112 113 |
# File 'lib/zdm.rb', line 111 def origin @origin end |
#statements ⇒ Object (readonly)
Returns the value of attribute statements.
111 112 113 |
# File 'lib/zdm.rb', line 111 def statements @statements end |
Instance Method Details
#add_column(name, definition) ⇒ Object
128 129 130 |
# File 'lib/zdm.rb', line 128 def add_column(name, definition) ddl('ALTER TABLE `%s` ADD COLUMN `%s` %s' % [@copy, name, definition]) end |
#add_index(column_names, opts = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/zdm.rb', line 144 def add_index(column_names, opts = {}) column_names = Array(column_names) index_name = opts[:name] || "index_#{@origin}_on_#{column_names.join('_and_')}" raise ArgumentError, "Index name '#{index_name}' on table #{@origin} is too long" if index_name.length > 64 index_type = opts[:type] || (opts[:unique] ? 'UNIQUE' : '') index_using = "USING #{opts[:using] || 'btree'}" index_columns = quoted_columns_for_index(column_names, opts).join(',') ddl("CREATE #{index_type} INDEX `#{index_name}` #{index_using} ON `#{@copy}` (#{index_columns})") end |
#alter(definition) ⇒ Object
124 125 126 |
# File 'lib/zdm.rb', line 124 def alter(definition) ddl('ALTER TABLE `%s` %s' % [@copy, definition]) end |
#change_column(name, definition) ⇒ Object
132 133 134 |
# File 'lib/zdm.rb', line 132 def change_column(name, definition) ddl('ALTER TABLE `%s` MODIFY COLUMN `%s` %s' % [@copy, name, definition]) end |
#ddl(statement) ⇒ Object
120 121 122 |
# File 'lib/zdm.rb', line 120 def ddl(statement) @statements << statement end |
#remove_column(name) ⇒ Object
136 137 138 |
# File 'lib/zdm.rb', line 136 def remove_column(name) ddl('ALTER TABLE `%s` DROP `%s`' % [@copy, name]) end |
#remove_index(index_name) ⇒ Object
154 155 156 |
# File 'lib/zdm.rb', line 154 def remove_index(index_name) ddl("ALTER TABLE `#{@copy}` DROP INDEX `#{index_name}`") end |
#rename_column(old_name, new_name) ⇒ Object
140 141 142 |
# File 'lib/zdm.rb', line 140 def rename_column(old_name, new_name) raise "Unsupported: you must first run a migration adding the column `#{new_name}`, deploy the code live, then run another migration at a later time to remove the column `#{old_name}`" end |