diff --git a/app/services/exports/case_log_export_service.rb b/app/services/exports/case_log_export_service.rb index 397a7cd94..959adc79f 100644 --- a/app/services/exports/case_log_export_service.rb +++ b/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 diff --git a/db/migrate/20220519112604_add_empty_export_to_logs_exports.rb b/db/migrate/20220519112604_add_empty_export_to_logs_exports.rb new file mode 100644 index 000000000..cd0734360 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 0fa555d87..2a4f3c6d6 100644 --- a/db/schema.rb +++ b/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| diff --git a/spec/services/exports/case_log_export_service_spec.rb b/spec/services/exports/case_log_export_service_spec.rb index fd5c1704c..1efe5e28a 100644 --- a/spec/services/exports/case_log_export_service_spec.rb +++ b/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")) }