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

Speed up file data migration #2329

Merged
merged 3 commits into from
Jun 29, 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
12 changes: 8 additions & 4 deletions app/models/model_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ def path_within_library
File.join(model.path, filename)
end

def attach_existing_file!
return if attachment.present?
def attach_existing_file!(refresh: true)
return if attachment.present? || !File.exist?(absolute_path)
attachment_attacher.set Shrine.uploaded_file(
storage: model.library.storage_key,
id: path_within_library,
metadata: {filename: basename(include_extension: true)}
metadata: {
filename: basename(include_extension: true),
size: attributes["size"],
mime_type: Mime::Type.lookup_by_extension(File.extname(filename).delete(".").downcase).to_s
}
)
attachment_attacher.refresh_metadata!
attachment_attacher.refresh_metadata! if refresh
save!
end

Expand Down
2 changes: 1 addition & 1 deletion db/data/20240615085913_move_file_data_into_shrine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MoveFileDataIntoShrine < ActiveRecord::Migration[7.0]
def up
ModelFile.find_each(&:attach_existing_file!)
ModelFile.find_each { |x| x.attach_existing_file!(refresh: false) }
end

def down
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/analysis/file_conversion_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
it "avoids filenames that already exist" do
allow(File).to receive(:exist?).with(file.absolute_path.gsub(".stl", ".3mf")).and_return(true).once
allow(File).to receive(:exist?).with(file.absolute_path.gsub(".stl", "-1.3mf")).and_return(true).once
allow(File).to receive(:exist?).with(file.absolute_path.gsub(".stl", "-2.3mf")).and_return(false).once
allow(File).to receive(:exist?).with(file.absolute_path.gsub(".stl", "-2.3mf")).and_return(false)
described_class.perform_now(file.id, :threemf)
expect(ModelFile.where.not(id: file.id).first.filename).to eq "files/awesome-2.3mf"
end
Expand Down
Loading