Class: SlashMigrate::AddIndexMigration
- Inherits:
-
Object
- Object
- SlashMigrate::AddIndexMigration
- Defined in:
- app/services/slash_migrate/add_index_migration.rb
Overview
Builds a migration that adds an index to an existing table — single-column or composite, optionally unique, with an optional explicit name. add_index is reversible, so this stays a plain def change.
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
- #any? ⇒ Boolean
- #index_statement ⇒ Object
-
#initialize(table:, columns: [], unique: false, name: nil) ⇒ AddIndexMigration
constructor
A new instance of AddIndexMigration.
- #migration_basename ⇒ Object
- #migration_class_name ⇒ Object
- #migration_source ⇒ Object
- #unique? ⇒ Boolean
- #write! ⇒ Object
Constructor Details
#initialize(table:, columns: [], unique: false, name: nil) ⇒ AddIndexMigration
Returns a new instance of AddIndexMigration.
12 13 14 15 16 17 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 12 def initialize(table:, columns: [], unique: false, name: nil) @table = table.to_s @columns = Array(columns).map { |column| column.to_s.strip }.reject(&:empty?) @unique = unique @name = name.to_s.strip end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
10 11 12 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 10 def columns @columns end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
10 11 12 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 10 def table @table end |
Class Method Details
.from_params(table:, columns:, unique: nil, name: nil) ⇒ Object
6 7 8 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 6 def self.from_params(table:, columns:, unique: nil, name: nil) new(table: table, columns: columns, unique: %w[unique true 1].include?(unique.to_s), name: name) end |
Instance Method Details
#any? ⇒ Boolean
19 20 21 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 19 def any? @columns.any? end |
#index_statement ⇒ Object
35 36 37 38 39 40 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 35 def index_statement statement = "add_index :#{@table}, #{column_argument}" statement += ", unique: true" if unique? statement += ", name: #{@name.inspect}" unless @name.empty? statement end |
#migration_basename ⇒ Object
31 32 33 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 31 def migration_basename "add_index_on_#{@columns.join("_and_")}_to_#{@table}" end |
#migration_class_name ⇒ Object
27 28 29 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 27 def migration_class_name "AddIndexOn#{@columns.map(&:camelize).join("And")}To#{@table.camelize}" end |
#migration_source ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 42 def migration_source [ "class #{migration_class_name} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]", " def change", " #{index_statement}", " end", "end" ].join("\n") + "\n" end |
#unique? ⇒ Boolean
23 24 25 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 23 def unique? @unique end |
#write! ⇒ Object
52 53 54 |
# File 'app/services/slash_migrate/add_index_migration.rb', line 52 def write! [MigrationFileWriter.write(basename: migration_basename, source: migration_source)] end |