Browse Source

Removing gaps caused by empty exports (#591)

pull/593/head
Stéphane Meny 3 years ago committed by GitHub
parent
commit
b729fade99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/services/exports/case_log_export_service.rb
  2. 5
      db/migrate/20220519112604_add_empty_export_to_logs_exports.rb
  3. 3
      db/schema.rb
  4. 12
      spec/services/exports/case_log_export_service_spec.rb

9
app/services/exports/case_log_export_service.rb

@ -21,6 +21,7 @@ module Exports
export = build_export_run(start_time, full_update)
daily_run = get_daily_run_number
archive_datetimes = write_export_archive(export, case_logs)
export.empty_export = archive_datetimes.empty?
write_master_manifest(daily_run, archive_datetimes)
export.save!
end
@ -39,12 +40,14 @@ module Exports
end
def build_export_run(current_time, full_update)
if LogsExport.count.zero?
previous_exports_with_data = LogsExport.where(empty_export: false)
if previous_exports_with_data.empty?
return LogsExport.new(started_at: current_time)
end
base_number = LogsExport.maximum(:base_number)
increment_number = LogsExport.where(base_number:).maximum(:increment_number)
base_number = previous_exports_with_data.maximum(:base_number)
increment_number = previous_exports_with_data.where(base_number:).maximum(:increment_number)
if full_update
base_number += 1

5
db/migrate/20220519112604_add_empty_export_to_logs_exports.rb

@ -0,0 +1,5 @@
class AddEmptyExportToLogsExports < ActiveRecord::Migration[7.0]
def change
add_column :logs_exports, :empty_export, :boolean, default: false, null: false
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_05_18_115438) do
ActiveRecord::Schema[7.0].define(version: 2022_05_19_112604) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -263,6 +263,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_05_18_115438) do
t.datetime "started_at", precision: nil, null: false
t.integer "base_number", default: 1, null: false
t.integer "increment_number", default: 1, null: false
t.boolean "empty_export", default: false, null: false
end
create_table "organisation_las", force: :cascade do |t|

12
spec/services/exports/case_log_export_service_spec.rb

@ -166,7 +166,7 @@ RSpec.describe Exports::CaseLogExportService do
end
end
context "and a previous export has run the same day with logs" do
context "and a previous export has run the same day having case logs" do
before do
FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 2, 1))
export_service.export_case_logs
@ -203,6 +203,16 @@ RSpec.describe Exports::CaseLogExportService do
end
end
context "and a previous export has run having no case logs" do
before { export_service.export_case_logs }
it "doesn't increment the manifest number by 1" do
export_service.export_case_logs
expect(LogsExport.last.increment_number).to eq(1)
end
end
context "and the export has an error" do
before { allow(storage_service).to receive(:write_file).and_raise(StandardError.new("This is an exception")) }

Loading…
Cancel
Save