Class: Localtower::Generators::ServiceObjects::InsertIndexes

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/generators/service_objects/insert_indexes.rb

Constant Summary collapse

USING =
[
  {
    label: 'default',
    name: 'default',
    example: 'using: :btree'
  },
  {
    name: 'hash',
    example: 'using: :hash'
  },
  {
    name: 'gin',
    example: 'using: :gin'
  },
  {
    name: 'gist',
    example: 'using: :gist'
  },
  {
    name: 'spgist',
    example: 'using: :spgist'
  },
  {
    name: 'brin',
    example: 'using: :brin'
  },
  {
    name: 'bloom',
    example: 'using: :bloom'
  }
]
UNIQUE =
[
  false,
  true
]
ALGO =
%w[
  concurrently
]

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ InsertIndexes

Returns a new instance of InsertIndexes.



46
47
48
# File 'lib/localtower/generators/service_objects/insert_indexes.rb', line 46

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#callObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/localtower/generators/service_objects/insert_indexes.rb', line 50

def call
  attributes.each do |attribute|
    attribute.each do |attr_key, options|
      line_str_original = File.read(Localtower::Tools.last_migration_pending).match(/((.*)add_index :(.*), :#{attr_key})/)[0]
      line_str = line_str_original.clone

      line_str = inser_using(line_str, options)
      line_str = inser_algorithm(line_str, options)
      line_str = inser_unique(line_str, options)

      content = File.read(Localtower::Tools.last_migration_pending).gsub(line_str_original, line_str)
      content = add_disable_ddl_transaction(content, options)

      File.write(Localtower::Tools.last_migration_pending, content)
    end
  end
end