Skip to content

Commit

Permalink
Add data migration to nullify target fields on bs_request_actions
Browse files Browse the repository at this point in the history
Nullify `target_project_id` and `target_package_id` in
`bs_request_actions` if the project or/and package no longer exist.
  • Loading branch information
eduardoj committed Feb 3, 2025
1 parent 434c4b5 commit dbf6713
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class NullifyTargetsOnBsRequestActions < ActiveRecord::Migration[7.0]
# rubocop:disable Rails/SkipsModelValidations
def up
bs_request_actions = BsRequestAction.where('target_project_id IS NOT NULL AND target_package_id IS NULL')
bs_request_actions.in_batches do |batch|
batch.find_each do |action|
target_project = Project.find_by_name(action.target_project)
if target_project.nil?
action.update_attribute(:target_project_id, nil)
end
end
end

bs_request_actions = BsRequestAction.where('target_project_id IS NOT NULL AND target_package_id IS NOT NULL')
bs_request_actions.in_batches do |batch|
batch.find_each do |action|
target_project = Project.find_by_name(action.target_project)
if target_project.nil?
action.update_attribute(:target_project_id, nil)
action.update_attribute(:target_package_id, nil)
next
end

target_package = Package.find_by_project_and_name(action.target_project, action.target_package)
if target_package.nil?
action.update_attribute(:target_package_id, nil)
end
end
end
end
# rubocop:enable Rails/SkipsModelValidations

def down
raise ActiveRecord::IrreversibleMigration
end
end
2 changes: 1 addition & 1 deletion src/api/db/data_schema.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DataMigrate::Data.define(version: 20241017123303)
DataMigrate::Data.define(version: 20250131094818)

0 comments on commit dbf6713

Please sign in to comment.