Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move DataExport CSV data into Active Storage #9409

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions app/controllers/support_interface/data_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def create
end

def download
data_export = DataExport.find(params[:id])
data_export = DataExport.where.associated(:file_attachment).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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if data_export.file.attached? here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a slightly separate approach here. If the file doesn't exist, the .find call will fail.

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)
Nitemaeric marked this conversation as resolved.
Show resolved Hide resolved
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