Module: Lab::Lims::Migrator

Defined in:
app/services/lab/lims/migrator.rb

Overview

Tools for performing a bulk import of data from LIMS’ databases to local OpenMRS database.

Migration sources supported:

- MySQL
- CouchDB

The sources above can be changed by setting the environment various MIGRATION_SOURCE to either mysql or couchdb.

Defined Under Namespace

Classes: CouchDbMigratorApi, MigrationWorker

Constant Summary collapse

MAX_THREADS =
ENV.fetch('MIGRATION_WORKERS', 6).to_i
MIGRATION_REJECTIONS_CSV_PATH =

NOTE: LIMS_LOG_PATH below is defined in worker.rb

Utils::LIMS_LOG_PATH.join('migration-rejections.csv')
MIGRATION_FAILURES_CSV_PATH =
Utils::LIMS_LOG_PATH.join('migration-failures.csv')
MIGRATION_LOG_PATH =
Utils::LIMS_LOG_PATH.join('migration.log')

Class Method Summary collapse

Class Method Details

.export_failuresObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/services/lab/lims/migrator.rb', line 176

def self.export_failures
  headers = ['doc_id', 'Accession number', 'NHID', 'Reason', 'Difference']
  rows = Lab::LimsFailedImport.all.map do |failure|
    [
      failure.lims_id,
      failure.tracking_number,
      failure.patient_nhid,
      failure.reason,
      failure.diff
    ]
  end

  save_csv(MIGRATION_FAILURES_CSV_PATH, headers: headers, rows: rows)
end

.export_rejections(rejections) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/services/lab/lims/migrator.rb', line 158

def self.export_rejections(rejections)
  headers = ['doc_id', 'Accession number', 'NHID', 'First name', 'Last name', 'Reason']
  rows = (rejections || []).map do |rejection|
    [
      rejection.order[:_id],
      rejection.order[:tracking_number],
      rejection.order[:patient][:id],
      rejection.order[:patient][:first_name],
      rejection.order[:patient][:last_name],
      rejection.reason
    ]
  end

  save_csv(MIGRATION_REJECTIONS_CSV_PATH, headers: headers, rows: rows)
end

.save_csv(filename, rows:, headers: nil) ⇒ Object



148
149
150
151
152
153
# File 'app/services/lab/lims/migrator.rb', line 148

def self.save_csv(filename, rows:, headers: nil)
  CSV.open(filename, File::WRONLY | File::CREAT) do |csv|
    csv << headers if headers
    rows.each { |row| csv << row }
  end
end

.start_migrationObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/services/lab/lims/migrator.rb', line 193

def self.start_migration
  Dir.mkdir(Utils::LIMS_LOG_PATH) unless File.exist?(Utils::LIMS_LOG_PATH)

  logger = LoggerMultiplexor.new(Logger.new($stdout), MIGRATION_LOG_PATH)
  logger.level = :debug
  Rails.logger = logger
  ActiveRecord::Base.logger = logger
  # CouchBum.logger = logger

  api_class = case ENV.fetch('MIGRATION_SOURCE', 'couchdb').downcase
              when 'couchdb' then CouchDbMigratorApi
              when 'mysql' then Api::MysqlApi
              else raise "Invalid MIGRATION_SOURCE: #{ENV['MIGRATION_SOURCE']}"
              end

  worker = MigrationWorker.new(api_class)
  worker.pull_orders(batch_size: 10_000)
ensure
  worker && export_rejections(worker.rejections)
  export_failures
end