FIX: Handle invalid source data in CF migration (#172)

FIX: Handle invalid source data in CF migration (#172)

diff --git a/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table.rb b/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table.rb
index b44226f..8d0ba2a 100644
--- a/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table.rb
+++ b/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table.rb
@@ -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
diff --git a/spec/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table_spec.rb b/spec/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table_spec.rb
new file mode 100644
index 0000000..66b6e24
--- /dev/null
+++ b/spec/db/post_migrate/20210714173022_correctly_move_assignments_from_custom_fields_to_a_table_spec.rb
@@ -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

GitHub sha: 09b76552069646f89d9d3a87963536d033855c38

This commit appears in #172 which was approved by davidtaylorhq. It was merged by CvX.