Skip to content

Commit

Permalink
Add "file" attachment to DataExport model
Browse files Browse the repository at this point in the history
Migrate previous `DataExport#data` values into `DataExport#file`.

Start populating the CSV content into the `DataExport#file` field via the `DataExporter` class calls.
  • Loading branch information
Nitemaeric committed Jun 3, 2024
1 parent 5ca08d3 commit daa2870
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/data_api/tad_data_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def exports

def serve_export(export)
export.update!(audit_comment: "File downloaded via API using token ID #{@authenticating_token.id}")
send_data export.data, filename: export.filename
send_data export.file.download, filename: export.filename
end

def statement_timeout
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/support_interface/data_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def create
def download
data_export = DataExport.find(params[:id])
data_export.update!(audit_comment: 'File downloaded')
send_data data_export.data, filename: data_export.filename, disposition: :attachment

redirect_to rails_blob_path(data_export.file, disposition: 'attachment')
end

def data_set_documentation
Expand Down
3 changes: 3 additions & 0 deletions app/models/data_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ class DataExport < ApplicationRecord
}.freeze

belongs_to :initiator, polymorphic: true, optional: true

has_one_attached :file

audited except: [:data]

def filename
Expand Down
12 changes: 12 additions & 0 deletions app/services/data_migrations/migrate_data_export_data_to_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module DataMigrations
class MigrateDataExportDataToFile
TIMESTAMP = 20240528140244
MANUAL_RUN = false

def change
DataExport.find_each do |data_export|
data_export.file.attach(io: CSV.new(data_export.data).to_io, filename: data_export.filename)
end
end
end
end
5 changes: 3 additions & 2 deletions app/workers/data_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def perform(importer_class, data_export_type, export_options = {})
Rails.logger.info 'Finished CSV generation'

Rails.logger.info 'Started writing CSV'

data_export.update!(
data: csv_data,
completed_at: Time.zone.now,
file: { io: CSV.new(csv_data).to_io, filename: data_export.filename },
completed_at: Time.current,
)

Rails.logger.info 'Finished writing CSV. Sidekiq done'
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/data.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DATA_MIGRATION_SERVICES = [
# do not delete or edit this line - services added below by generator
'DataMigrations::MigrateDataExportDataToFile',
'DataMigrations::RemoveCoursesNotOnPublish',
'DataMigrations::RemoveIncidentEvictionFeatureFlag',
'DataMigrations::RemoveCourseHasVacanciesFeatureFlag',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe DataMigrations::MigrateDataExportDataToFile do
let(:csv_string) { "email_template,send_count\nsign_in_email,1" }

it "updates all existing DataExport records 'data' fields and puts them into 'file' attachments" do
data_export = create(:data_export, data: csv_string, file: nil)

described_class.new.change

data_export.reload

expect(data_export.file).to be_attached
expect(data_export.file.download).to eq(csv_string)
end
end

0 comments on commit daa2870

Please sign in to comment.