FIX: Handle invalid source data in CF migration (#172)
This commit is contained in:
parent
b0a480fd35
commit
09b7655206
|
@ -14,14 +14,17 @@ class CorrectlyMoveAssignmentsFromCustomFieldsToATable < ActiveRecord::Migration
|
|||
|
||||
execute <<~SQL
|
||||
INSERT INTO assignments (assigned_to_id, assigned_by_user_id, topic_id, created_at, updated_at)
|
||||
SELECT (
|
||||
SELECT value::integer assigned_to_id
|
||||
FROM topic_custom_fields tcf1
|
||||
WHERE tcf1.name = 'assigned_to_id' AND tcf1.topic_id = tcf2.topic_id
|
||||
), value::integer assigned_by_id, topic_id, created_at, updated_at
|
||||
FROM topic_custom_fields tcf2
|
||||
WHERE name = 'assigned_by_id'
|
||||
ORDER BY created_at DESC
|
||||
SELECT
|
||||
assigned_to.value::integer,
|
||||
assigned_by.value::integer,
|
||||
assigned_by.topic_id,
|
||||
assigned_by.created_at,
|
||||
assigned_by.updated_at
|
||||
FROM topic_custom_fields assigned_by
|
||||
INNER JOIN topic_custom_fields assigned_to ON assigned_to.topic_id = assigned_by.topic_id
|
||||
WHERE assigned_by.name = 'assigned_by_id'
|
||||
AND assigned_to.name = 'assigned_to_id'
|
||||
ORDER BY assigned_by.created_at DESC
|
||||
ON CONFLICT DO NOTHING
|
||||
SQL
|
||||
end
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
require_relative "../../../db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table"
|
||||
|
||||
describe CorrectlyMoveAssignmentsFromCustomFieldsToATable do
|
||||
context "valid data" do
|
||||
it "should migrate the data correctly" do
|
||||
TopicCustomField.create!(topic_id: 99, name: "assigned_to_id", value: "50")
|
||||
TopicCustomField.create!(topic_id: 99, name: "assigned_by_id", value: "60")
|
||||
silence_stdout { CorrectlyMoveAssignmentsFromCustomFieldsToATable.new.up }
|
||||
|
||||
assignment = Assignment.first
|
||||
expect(assignment.topic_id).to eq(99)
|
||||
expect(assignment.assigned_to_id).to eq(50)
|
||||
expect(assignment.assigned_by_user_id).to eq(60)
|
||||
end
|
||||
end
|
||||
|
||||
context "no assigned_by data" do
|
||||
it "should migrate the data correctly" do
|
||||
TopicCustomField.create!(topic_id: 99, name: "assigned_to_id", value: "50")
|
||||
silence_stdout { CorrectlyMoveAssignmentsFromCustomFieldsToATable.new.up }
|
||||
|
||||
expect(Assignment.count).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
context "no assigned_to data" do
|
||||
it "should migrate the data correctly" do
|
||||
TopicCustomField.create!(topic_id: 99, name: "assigned_by_id", value: "60")
|
||||
silence_stdout { CorrectlyMoveAssignmentsFromCustomFieldsToATable.new.up }
|
||||
|
||||
expect(Assignment.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue