Class: SlashMigrate::AddColumnsMigration
- Inherits:
-
Object
- Object
- SlashMigrate::AddColumnsMigration
- Defined in:
- app/services/slash_migrate/add_columns_migration.rb
Overview
Builds a migration that adds one or more columns to an existing table. add_column / add_reference are reversible, so this stays a plain def change.
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
- .from_params(table:, rows:) ⇒ Object
-
.resolve_self_reference(row, table) ⇒ Object
A self-reference here points at the table being modified, which already exists.
Instance Method Summary collapse
- #any? ⇒ Boolean
-
#initialize(table:, columns: []) ⇒ AddColumnsMigration
constructor
A new instance of AddColumnsMigration.
- #migration_basename ⇒ Object
- #migration_class_name ⇒ Object
- #migration_source ⇒ Object
- #write! ⇒ Object
Constructor Details
#initialize(table:, columns: []) ⇒ AddColumnsMigration
Returns a new instance of AddColumnsMigration.
21 22 23 24 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 21 def initialize(table:, columns: []) @table = table.to_s @columns = columns.reject(&:blank?) end |
Instance Attribute Details
#table ⇒ Object (readonly)
Returns the value of attribute table.
19 20 21 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 19 def table @table end |
Class Method Details
.from_params(table:, rows:) ⇒ Object
5 6 7 8 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 5 def self.from_params(table:, rows:) columns = Array(rows).map { |row| Column.from_params(resolve_self_reference(row, table)) } new(table: table, columns: columns) end |
.resolve_self_reference(row, table) ⇒ Object
A self-reference here points at the table being modified, which already exists. Reuse the create-table sentinel so the shared row partial works.
12 13 14 15 16 17 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 12 def self.resolve_self_reference(row, table) return row unless row[:to_table].to_s == MigrationBuilder::SELF_TABLE hash = row.respond_to?(:to_unsafe_h) ? row.to_unsafe_h : row.to_h hash.symbolize_keys.merge(to_table: table) end |
Instance Method Details
#any? ⇒ Boolean
26 27 28 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 26 def any? @columns.any? end |
#migration_basename ⇒ Object
38 39 40 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 38 def migration_basename @columns.one? ? "add_#{@columns.first.name}_to_#{@table}" : "add_columns_to_#{@table}" end |
#migration_class_name ⇒ Object
30 31 32 33 34 35 36 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 30 def migration_class_name if @columns.one? "Add#{@columns.first.name.camelize}To#{@table.camelize}" else "AddColumnsTo#{@table.camelize}" end end |
#migration_source ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 42 def migration_source lines = [] lines << "class #{migration_class_name} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]" lines << " def change" @columns.each do |column| lines << " #{column.add_statement(@table)}" lines << " #{column.index_statement(@table)}" if column.indexed? end lines << " end" lines << "end" lines.join("\n") + "\n" end |
#write! ⇒ Object
55 56 57 |
# File 'app/services/slash_migrate/add_columns_migration.rb', line 55 def write! [MigrationFileWriter.write(basename: migration_basename, source: migration_source)] end |