Browse Source

Rename exports table

pull/2652/head
Kat 2 years ago committed by kosiakkatrina
parent
commit
a5a163ecdd
  1. 2
      app/models/export.rb
  2. 2
      app/models/logs_export.rb
  3. 2
      app/services/exports/export_service.rb
  4. 12
      app/services/exports/lettings_log_export_service.rb
  5. 12
      app/services/exports/user_export_service.rb
  6. 5
      db/migrate/20240802093255_rename_export_table.rb
  7. 18
      db/schema.rb
  8. 16
      spec/services/exports/lettings_log_export_service_spec.rb
  9. 14
      spec/services/exports/user_export_service_spec.rb

2
app/models/export.rb

@ -0,0 +1,2 @@
class Export < ApplicationRecord
end

2
app/models/logs_export.rb

@ -1,2 +0,0 @@
class LogsExport < ApplicationRecord
end

2
app/services/exports/export_service.rb

@ -24,7 +24,7 @@ module Exports
def get_daily_run_number def get_daily_run_number
today = Time.zone.today today = Time.zone.today
LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).select(:started_at).distinct.count + 1 Export.where(created_at: today.beginning_of_day..today.end_of_day).select(:started_at).distinct.count + 1
end end
def write_master_manifest(daily_run, archive_datetimes) def write_master_manifest(daily_run, archive_datetimes)

12
app/services/exports/lettings_log_export_service.rb

@ -11,9 +11,9 @@ module Exports
def export_xml_lettings_logs(full_update: false, collection_year: nil) def export_xml_lettings_logs(full_update: false, collection_year: nil)
archives_for_manifest = {} archives_for_manifest = {}
recent_export = LogsExport.order("started_at").last recent_export = Export.order("started_at").last
collection_years_to_export(collection_year).each do |collection| collection_years_to_export(collection_year).each do |collection|
base_number = LogsExport.where(empty_export: false, collection:).maximum(:base_number) || 1 base_number = Export.where(empty_export: false, collection:).maximum(:base_number) || 1
export = build_export_run(collection, base_number, full_update) export = build_export_run(collection, base_number, full_update)
archives = write_export_archive(export, collection, recent_export, full_update) archives = write_export_archive(export, collection, recent_export, full_update)
@ -30,22 +30,22 @@ module Exports
def build_export_run(collection, base_number, full_update) def build_export_run(collection, base_number, full_update)
@logger.info("Building export run for #{collection}") @logger.info("Building export run for #{collection}")
previous_exports_with_data = LogsExport.where(collection:, empty_export: false) previous_exports_with_data = Export.where(collection:, empty_export: false)
increment_number = previous_exports_with_data.where(base_number:).maximum(:increment_number) || 1 increment_number = previous_exports_with_data.where(base_number:).maximum(:increment_number) || 1
if full_update if full_update
base_number += 1 if LogsExport.any? # Only increment when it's not the first run base_number += 1 if Export.any? # Only increment when it's not the first run
increment_number = 1 increment_number = 1
else else
increment_number += 1 increment_number += 1
end end
if previous_exports_with_data.empty? if previous_exports_with_data.empty?
return LogsExport.new(collection:, base_number:, started_at: @start_time) return Export.new(collection:, base_number:, started_at: @start_time)
end end
LogsExport.new(collection:, started_at: @start_time, base_number:, increment_number:) Export.new(collection:, started_at: @start_time, base_number:, increment_number:)
end end
def get_archive_name(collection, base_number, increment) def get_archive_name(collection, base_number, increment)

12
app/services/exports/user_export_service.rb

@ -10,10 +10,10 @@ module Exports
end end
def export_xml_users(full_update: false) def export_xml_users(full_update: false)
recent_export = LogsExport.order("started_at").last recent_export = Export.order("started_at").last
collection = "users" collection = "users"
base_number = LogsExport.where(empty_export: false, collection:).maximum(:base_number) || 1 base_number = Export.where(empty_export: false, collection:).maximum(:base_number) || 1
export = build_export_run(collection, base_number, full_update) export = build_export_run(collection, base_number, full_update)
archives_for_manifest = write_export_archive(export, collection, recent_export, full_update) archives_for_manifest = write_export_archive(export, collection, recent_export, full_update)
@ -27,22 +27,22 @@ module Exports
def build_export_run(collection, base_number, full_update) def build_export_run(collection, base_number, full_update)
@logger.info("Building export run for #{collection}") @logger.info("Building export run for #{collection}")
previous_exports_with_data = LogsExport.where(collection:, empty_export: false) previous_exports_with_data = Export.where(collection:, empty_export: false)
increment_number = previous_exports_with_data.where(base_number:).maximum(:increment_number) || 1 increment_number = previous_exports_with_data.where(base_number:).maximum(:increment_number) || 1
if full_update if full_update
base_number += 1 if LogsExport.any? # Only increment when it's not the first run base_number += 1 if Export.any? # Only increment when it's not the first run
increment_number = 1 increment_number = 1
else else
increment_number += 1 increment_number += 1
end end
if previous_exports_with_data.empty? if previous_exports_with_data.empty?
return LogsExport.new(collection:, base_number:, started_at: @start_time) return Export.new(collection:, base_number:, started_at: @start_time)
end end
LogsExport.new(collection:, started_at: @start_time, base_number:, increment_number:) Export.new(collection:, started_at: @start_time, base_number:, increment_number:)
end end
def get_archive_name(collection, base_number, increment) def get_archive_name(collection, base_number, increment)

5
db/migrate/20240802093255_rename_export_table.rb

@ -0,0 +1,5 @@
class RenameExportTable < ActiveRecord::Migration[7.0]
def change
rename_table :logs_exports, :exports
end
end

18
db/schema.rb

@ -77,6 +77,15 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_18_112702) do
t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id" t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id"
end end
create_table "exports", force: :cascade do |t|
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }
t.datetime "started_at", 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
t.string "collection"
end
create_table "la_rent_ranges", force: :cascade do |t| create_table "la_rent_ranges", force: :cascade do |t|
t.integer "ranges_rent_id" t.integer "ranges_rent_id"
t.integer "lettype" t.integer "lettype"
@ -412,15 +421,6 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_18_112702) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "logs_exports", force: :cascade do |t|
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }
t.datetime "started_at", 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
t.string "collection"
end
create_table "merge_request_organisations", force: :cascade do |t| create_table "merge_request_organisations", force: :cascade do |t|
t.integer "merge_request_id" t.integer "merge_request_id"
t.integer "merging_organisation_id" t.integer "merging_organisation_id"

16
spec/services/exports/lettings_log_export_service_spec.rb

@ -235,7 +235,7 @@ RSpec.describe Exports::LettingsLogExportService do
let(:expected_zip_filename2) { "core_2022_2023_apr_mar_f0001_inc0001.zip" } let(:expected_zip_filename2) { "core_2022_2023_apr_mar_f0001_inc0001.zip" }
before do before do
LogsExport.new(started_at: Time.zone.yesterday, base_number: 7, increment_number: 3, collection: 2021).save! Export.new(started_at: Time.zone.yesterday, base_number: 7, increment_number: 3, collection: 2021).save!
end end
it "generates multiple ZIP export files with different base numbers in the filenames" do it "generates multiple ZIP export files with different base numbers in the filenames" do
@ -277,8 +277,8 @@ RSpec.describe Exports::LettingsLogExportService do
it "creates a logs export record in a database with correct time" do it "creates a logs export record in a database with correct time" do
expect { export_service.export_xml_lettings_logs } expect { export_service.export_xml_lettings_logs }
.to change(LogsExport, :count).by(3) .to change(Export, :count).by(3)
expect(LogsExport.last.started_at).to be_within(2.seconds).of(start_time) expect(Export.last.started_at).to be_within(2.seconds).of(start_time)
end end
context "when this is the first export (full)" do context "when this is the first export (full)" do
@ -328,7 +328,7 @@ RSpec.describe Exports::LettingsLogExportService do
context "when this is a second export (partial)" do context "when this is a second export (partial)" do
before do before do
start_time = Time.zone.local(2022, 6, 1) start_time = Time.zone.local(2022, 6, 1)
LogsExport.new(started_at: start_time).save! Export.new(started_at: start_time).save!
end end
it "does not add any entry for the master manifest (no lettings logs)" do it "does not add any entry for the master manifest (no lettings logs)" do
@ -347,12 +347,12 @@ RSpec.describe Exports::LettingsLogExportService do
context "and we trigger another full update" do context "and we trigger another full update" do
it "increments the base number" do it "increments the base number" do
export_service.export_xml_lettings_logs(full_update: true) export_service.export_xml_lettings_logs(full_update: true)
expect(LogsExport.last.base_number).to eq(2) expect(Export.last.base_number).to eq(2)
end end
it "resets the increment number" do it "resets the increment number" do
export_service.export_xml_lettings_logs(full_update: true) export_service.export_xml_lettings_logs(full_update: true)
expect(LogsExport.last.increment_number).to eq(1) expect(Export.last.increment_number).to eq(1)
end end
it "returns a correct archives list for manifest file" do it "returns a correct archives list for manifest file" do
@ -372,7 +372,7 @@ RSpec.describe Exports::LettingsLogExportService do
it "doesn't increment the manifest number by 1" do it "doesn't increment the manifest number by 1" do
export_service.export_xml_lettings_logs export_service.export_xml_lettings_logs
expect(LogsExport.last.increment_number).to eq(1) expect(Export.last.increment_number).to eq(1)
end end
end end
@ -380,7 +380,7 @@ RSpec.describe Exports::LettingsLogExportService do
before do before do
FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 2, 1), updated_at: Time.zone.local(2022, 4, 27), values_updated_at: Time.zone.local(2022, 4, 29)) FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 2, 1), updated_at: Time.zone.local(2022, 4, 27), values_updated_at: Time.zone.local(2022, 4, 29))
FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 2, 1), updated_at: Time.zone.local(2022, 4, 27), values_updated_at: Time.zone.local(2022, 4, 29)) FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 2, 1), updated_at: Time.zone.local(2022, 4, 27), values_updated_at: Time.zone.local(2022, 4, 29))
LogsExport.create!(started_at: Time.zone.local(2022, 4, 28), base_number: 1, increment_number: 1) Export.create!(started_at: Time.zone.local(2022, 4, 28), base_number: 1, increment_number: 1)
end end
it "generates an XML manifest file with the expected content within the ZIP file" do it "generates an XML manifest file with the expected content within the ZIP file" do

14
spec/services/exports/user_export_service_spec.rb

@ -104,8 +104,8 @@ RSpec.describe Exports::UserExportService do
it "creates an export record in a database with correct time" do it "creates an export record in a database with correct time" do
expect { export_service.export_xml_users } expect { export_service.export_xml_users }
.to change(LogsExport, :count).by(1) .to change(Export, :count).by(1)
expect(LogsExport.last.started_at).to be_within(2.seconds).of(start_time) expect(Export.last.started_at).to be_within(2.seconds).of(start_time)
end end
context "when this is the first export (full)" do context "when this is the first export (full)" do
@ -150,7 +150,7 @@ RSpec.describe Exports::UserExportService do
context "when this is a second export (partial)" do context "when this is a second export (partial)" do
before do before do
start_time = Time.zone.local(2022, 6, 1) start_time = Time.zone.local(2022, 6, 1)
LogsExport.new(started_at: start_time).save! # this should be user export Export.new(started_at: start_time).save! # this should be user export
end end
it "does not add any entry for the master manifest (no users)" do it "does not add any entry for the master manifest (no users)" do
@ -168,12 +168,12 @@ RSpec.describe Exports::UserExportService do
context "and we trigger another full update" do context "and we trigger another full update" do
it "increments the base number" do it "increments the base number" do
export_service.export_xml_users(full_update: true) export_service.export_xml_users(full_update: true)
expect(LogsExport.last.base_number).to eq(2) expect(Export.last.base_number).to eq(2)
end end
it "resets the increment number" do it "resets the increment number" do
export_service.export_xml_users(full_update: true) export_service.export_xml_users(full_update: true)
expect(LogsExport.last.increment_number).to eq(1) expect(Export.last.increment_number).to eq(1)
end end
it "returns a correct archives list for manifest file" do it "returns a correct archives list for manifest file" do
@ -193,7 +193,7 @@ RSpec.describe Exports::UserExportService do
it "doesn't increment the manifest number by 1" do it "doesn't increment the manifest number by 1" do
export_service.export_xml_users export_service.export_xml_users
expect(LogsExport.last.increment_number).to eq(1) expect(Export.last.increment_number).to eq(1)
end end
end end
@ -201,7 +201,7 @@ RSpec.describe Exports::UserExportService do
before do before do
create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:) create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:)
create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:) create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:)
LogsExport.create!(started_at: Time.zone.local(2022, 4, 26), base_number: 1, increment_number: 1) Export.create!(started_at: Time.zone.local(2022, 4, 26), base_number: 1, increment_number: 1)
end end
it "generates an XML manifest file with the expected content within the ZIP file" do it "generates an XML manifest file with the expected content within the ZIP file" do

Loading…
Cancel
Save