Skip to content

add rake task to update morpheus models #2198

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

Open
wants to merge 1 commit into
base: seek-1.16
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions lib/tasks/seek_upgrades.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ namespace :seek do
task upgrade_version_tasks: %i[
environment
db:seed:007_sample_attribute_types
db:seed:003_model_formats
db:seed:004_model_recommended_environments
Comment on lines 11 to +13
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

The seed tasks are ordered with 007_sample_attribute_types before 003 and 004, which may lead to missing lookup values if dependencies aren’t loaded in the correct order. Consider reordering these seeds numerically.

Suggested change
db:seed:007_sample_attribute_types
db:seed:003_model_formats
db:seed:004_model_recommended_environments
db:seed:003_model_formats
db:seed:004_model_recommended_environments
db:seed:007_sample_attribute_types

Copilot uses AI. Check for mistakes.

update_rdf
update_observation_unit_policies
fix_xlsx_marked_as_zip
add_policies_to_existing_sample_types
fix_previous_sample_type_permissions
update_morpheus_model
]

# these are the tasks that are executes for each upgrade as standard, and rarely change
Expand Down Expand Up @@ -91,6 +94,22 @@ namespace :seek do
end
end

task(update_morpheus_model: [:environment]) do
puts "... updating morpheus model"
Model.all.each do |model|
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

Using Model.all loads every record into memory at once. For large datasets, switch to Model.find_each or find_in_batches to mitigate memory bloat.

Suggested change
Model.all.each do |model|
Model.find_each do |model|

Copilot uses AI. Check for mistakes.

next unless model.is_morpheus_supported?
unless model.model_format
model.model_format = ModelFormat.find_by(title: 'Morpheus')
end
unless model.recommended_environment
model.recommended_environment = RecommendedModelEnvironment.find_by(title: 'Morpheus')
end
Comment on lines +101 to +106
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

If no ModelFormat titled 'Morpheus' exists, find_by returns nil and the subsequent update_column will silently set a null foreign key. Consider using find_by! or adding a guard to log or abort on missing reference.

Suggested change
unless model.model_format
model.model_format = ModelFormat.find_by(title: 'Morpheus')
end
unless model.recommended_environment
model.recommended_environment = RecommendedModelEnvironment.find_by(title: 'Morpheus')
end
begin
unless model.model_format
model.model_format = ModelFormat.find_by!(title: 'Morpheus')
end
unless model.recommended_environment
model.recommended_environment = RecommendedModelEnvironment.find_by!(title: 'Morpheus')
end
rescue ActiveRecord::RecordNotFound => e
puts "Error: #{e.message}. Ensure that the required 'Morpheus' records exist in the database."
abort("Aborting task due to missing 'Morpheus' records.")
end

Copilot uses AI. Check for mistakes.

model.save
model.update_column(:model_format_id, model.model_format_id)
model.update_column(:recommended_environment_id, model.recommended_environment_id)
Comment on lines +107 to +109
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

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

The code calls model.save and then individual update_column calls, resulting in multiple database writes. Consider updating both foreign keys together with update_columns or just relying on save after setting associations.

Suggested change
model.save
model.update_column(:model_format_id, model.model_format_id)
model.update_column(:recommended_environment_id, model.recommended_environment_id)
model.update_columns(model_format_id: model.model_format_id, recommended_environment_id: model.recommended_environment_id)

Copilot uses AI. Check for mistakes.

end
end

task(add_policies_to_existing_sample_types: [:environment]) do
puts '... Adding policies to existing sample types'
counter = 0
Expand Down
Loading