From 1aba7b6d35d1459ddbfabd9617a629b0d5057de2 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Tue, 24 May 2022 14:42:14 -0500 Subject: [PATCH 1/4] Remove Answers that no longer have a Question --- db/migrate/20220523181437_fix_foreign_keys.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/db/migrate/20220523181437_fix_foreign_keys.rb b/db/migrate/20220523181437_fix_foreign_keys.rb index 28378acc..4f5dc344 100644 --- a/db/migrate/20220523181437_fix_foreign_keys.rb +++ b/db/migrate/20220523181437_fix_foreign_keys.rb @@ -5,6 +5,14 @@ def change remove_foreign_key :vocab_term_tags, :tags, on_update: :cascade, on_delete: :cascade remove_foreign_key :vocab_term_tags, :vocab_terms, on_update: :cascade, on_delete: :cascade + reversible do |dir| + dir.up do + Answer.where.not( + Question.where('"questions"."id" = "answers"."question_id"').arel.exists + ).delete_all + end + end + add_foreign_key :administrators, :users add_foreign_key :answers, :questions add_foreign_key :authors, :publications From 13ec489196d3e369fde067185607817bdc0e7ff9 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Tue, 24 May 2022 15:32:41 -0500 Subject: [PATCH 2/4] Remove CollaboratorSolutions that no longer have a Question --- db/migrate/20220523181437_fix_foreign_keys.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20220523181437_fix_foreign_keys.rb b/db/migrate/20220523181437_fix_foreign_keys.rb index 4f5dc344..d415d3c6 100644 --- a/db/migrate/20220523181437_fix_foreign_keys.rb +++ b/db/migrate/20220523181437_fix_foreign_keys.rb @@ -10,6 +10,10 @@ def change Answer.where.not( Question.where('"questions"."id" = "answers"."question_id"').arel.exists ).delete_all + + CollaboratorSolution.where.not( + Question.where('"questions"."id" = "collaborator_solutions"."question_id"').arel.exists + ).delete_all end end From 410e3238fb3049d53bc5e01f153092f96d54da08 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Tue, 24 May 2022 16:03:28 -0500 Subject: [PATCH 3/4] Remove StemAnswers that no longer have an Answer --- db/migrate/20220523181437_fix_foreign_keys.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20220523181437_fix_foreign_keys.rb b/db/migrate/20220523181437_fix_foreign_keys.rb index d415d3c6..45901c23 100644 --- a/db/migrate/20220523181437_fix_foreign_keys.rb +++ b/db/migrate/20220523181437_fix_foreign_keys.rb @@ -14,6 +14,10 @@ def change CollaboratorSolution.where.not( Question.where('"questions"."id" = "collaborator_solutions"."question_id"').arel.exists ).delete_all + + StemAnswer.where.not( + Answer.where('"answers"."id" = "stem_answers"."answer_id"').arel.exists + ).delete_all end end From 447ad0f6e9ea1de92b71568322d6ea4d8b5c6f95 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Tue, 24 May 2022 16:32:10 -0500 Subject: [PATCH 4/4] Remove Stems that no longer have a Question --- db/migrate/20220523181437_fix_foreign_keys.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/migrate/20220523181437_fix_foreign_keys.rb b/db/migrate/20220523181437_fix_foreign_keys.rb index 45901c23..2c62a115 100644 --- a/db/migrate/20220523181437_fix_foreign_keys.rb +++ b/db/migrate/20220523181437_fix_foreign_keys.rb @@ -18,6 +18,10 @@ def change StemAnswer.where.not( Answer.where('"answers"."id" = "stem_answers"."answer_id"').arel.exists ).delete_all + + Stem.where.not( + Question.where('"questions"."id" = "stems"."question_id"').arel.exists + ).delete_all end end