Class: Keinaufwand::Sync::IndexManager
- Inherits:
-
Object
- Object
- Keinaufwand::Sync::IndexManager
- Defined in:
- lib/keinaufwand/sync_core/index_manager.rb
Constant Summary collapse
- TABLE_NAME =
"keinaufwand_records"
Instance Method Summary collapse
- #current_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
- #desired_indices ⇒ Object
-
#initialize(sync_config: [], adapter: :sqlite3) ⇒ IndexManager
constructor
A new instance of IndexManager.
- #migration_operations(connection = ActiveRecord::Base.connection, mode: :diff) ⇒ Object
- #migration_source(class_name: "UpdateKeinaufwandSyncIndexes", mode: :diff) ⇒ Object
- #missing_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
- #obsolete_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
Constructor Details
#initialize(sync_config: [], adapter: :sqlite3) ⇒ IndexManager
Returns a new instance of IndexManager.
6 7 8 9 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 6 def initialize(sync_config: [], adapter: :sqlite3) @sync_config = sync_config @adapter = adapter.to_sym end |
Instance Method Details
#current_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
20 21 22 23 24 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 20 def current_index_names(connection = ActiveRecord::Base.connection) return [] unless connection.table_exists?(TABLE_NAME) connection.indexes(TABLE_NAME).map(&:name).grep(/\Aidx_keinaufwand_/) end |
#desired_indices ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 11 def desired_indices @sync_config.each_with_object({}) do |item, indices| model = item.fetch("model") Array(item["indices"]).each do |attribute| indices.merge!(build_index(model, attribute)) end end end |
#migration_operations(connection = ActiveRecord::Base.connection, mode: :diff) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 46 def migration_operations(connection = ActiveRecord::Base.connection, mode: :diff) desired = desired_indices removals = obsolete_index_names(connection).map do |name| " remove_index :#{TABLE_NAME}, name: #{name.inspect} if index_name_exists?(:#{TABLE_NAME}, #{name.inspect})" end addition_names = mode.to_sym == :full ? desired.keys : missing_index_names(connection) additions = addition_names.map do |name| config = desired.fetch(name) " add_index :#{TABLE_NAME}, #{config[:expression].inspect}, name: #{name.inspect}, where: #{config[:where].inspect} unless index_name_exists?(:#{TABLE_NAME}, #{name.inspect})" end removals + additions end |
#migration_source(class_name: "UpdateKeinaufwandSyncIndexes", mode: :diff) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 34 def migration_source(class_name: "UpdateKeinaufwandSyncIndexes", mode: :diff) operations = migration_operations(mode: mode) <<~RUBY class #{class_name} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}] def change #{operations.join("\n")} end end RUBY end |
#missing_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
26 27 28 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 26 def missing_index_names(connection = ActiveRecord::Base.connection) desired_indices.keys - current_index_names(connection) end |
#obsolete_index_names(connection = ActiveRecord::Base.connection) ⇒ Object
30 31 32 |
# File 'lib/keinaufwand/sync_core/index_manager.rb', line 30 def obsolete_index_names(connection = ActiveRecord::Base.connection) current_index_names(connection) - desired_indices.keys end |