Skip to content

Commit

Permalink
Merge pull request #2329 from manyfold3d/faster-migration
Browse files Browse the repository at this point in the history
Speed up file data migration
  • Loading branch information
Floppy authored Jun 29, 2024
2 parents 3a7a287 + f59b86e commit 8b930b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
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

0 comments on commit 8b930b2

Please sign in to comment.