Class: SlashMigrate::DropColumnMigration

Inherits:
Object
  • Object
show all
Defined in:
app/services/slash_migrate/drop_column_migration.rb

Overview

Builds a migration that drops a column. remove_column is reversible only when given the column’s type (and we pass the rest of its definition too), so a rollback re-adds the column faithfully — exactly the lesson it teaches.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:, column:) ⇒ DropColumnMigration

Returns a new instance of DropColumnMigration.



8
9
10
11
# File 'app/services/slash_migrate/drop_column_migration.rb', line 8

def initialize(table:, column:)
  @table = table.to_s
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



6
7
8
# File 'app/services/slash_migrate/drop_column_migration.rb', line 6

def column
  @column
end

Instance Method Details

#migration_basenameObject



17
18
19
# File 'app/services/slash_migrate/drop_column_migration.rb', line 17

def migration_basename
  "remove_#{column.name}_from_#{@table}"
end

#migration_class_nameObject



13
14
15
# File 'app/services/slash_migrate/drop_column_migration.rb', line 13

def migration_class_name
  "Remove#{column.name.camelize}From#{@table.camelize}"
end

#migration_sourceObject



21
22
23
24
25
26
27
28
29
# File 'app/services/slash_migrate/drop_column_migration.rb', line 21

def migration_source
  [
    "class #{migration_class_name} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]",
    "  def change",
    "    #{column.remove_statement(@table)}",
    "  end",
    "end"
  ].join("\n") + "\n"
end

#write!Object



31
32
33
# File 'app/services/slash_migrate/drop_column_migration.rb', line 31

def write!
  [MigrationFileWriter.write(basename: migration_basename, source: migration_source)]
end