Class: MongodbLogger::Utils::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/mongodb_logger/utils/migrate.rb

Defined Under Namespace

Classes: MongoMigrateLogger

Instance Method Summary collapse

Constructor Details

#initializeMigrate

Returns a new instance of Migrate.



8
9
10
# File 'lib/mongodb_logger/utils/migrate.rb', line 8

def initialize
  raise "this task work only in Rails app" unless defined?(Rails)
end

Instance Method Details

#create_migration_collection(configuration) ⇒ Object



33
34
35
36
37
38
# File 'lib/mongodb_logger/utils/migrate.rb', line 33

def create_migration_collection(configuration)
  configuration.merge!({ 'collection' => "#{configuration['collection']}_copy_#{rand(100)}" })
  migrate_logger = MongoMigrateLogger.new(configuration)
  migrate_logger.mongo_adapter.reset_collection
  migrate_logger
end

#get_mongo_adapter_and_nameObject



27
28
29
30
31
# File 'lib/mongodb_logger/utils/migrate.rb', line 27

def get_mongo_adapter_and_name
  mongodb_logger = Rails.logger
  collection_name = mongodb_logger.db_configuration['collection'].dup
  [mongodb_logger.mongo_adapter, collection_name]
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongodb_logger/utils/migrate.rb', line 12

def run
  Progressbar.new.show("Importing data to new capped collection") do
    @mongo_adapter, collection_name = get_mongo_adapter_and_name
    @migrate_logger = create_migration_collection(Rails.logger.db_configuration)

    iterator, all_count = 0, @mongo_adapter.collection.find.count
    @mongo_adapter.collection.find.each do |row|
      @migrate_logger.mongo_adapter.collection.insert(row)
      progress (((iterator += 1).to_f / all_count.to_f) * 100).round
    end if all_count > 0
    progress 100
    @migrate_logger.mongo_adapter.rename_collection(collection_name, true)
  end
end