|
| 1 | +class RemoveOtherPaymentMethodOption < ActiveRecord::Migration[8.0] |
| 2 | + # The "Other" payment method has been removed from the form builder's options. |
| 3 | + # Existing payment_method fields still carry it as a stored answer option, so |
| 4 | + # drop those join rows. The "Other" AnswerOption record is shared across many |
| 5 | + # fields (sectors, colors, etc.), so it is left intact — only the payment |
| 6 | + # associations are removed. |
| 7 | + def up |
| 8 | + execute(<<~SQL.squish) |
| 9 | + DELETE FROM form_field_answer_options |
| 10 | + WHERE answer_option_id IN (SELECT id FROM answer_options WHERE name = 'Other') |
| 11 | + AND form_field_id IN (SELECT id FROM form_fields WHERE field_identifier = 'payment_method') |
| 12 | + SQL |
| 13 | + end |
| 14 | + |
| 15 | + def down |
| 16 | + # Re-attach "Other" to every payment_method field that no longer has it, |
| 17 | + # reusing the shared AnswerOption (creating it only if it is missing). |
| 18 | + execute(<<~SQL.squish) |
| 19 | + INSERT INTO answer_options (name, created_at, updated_at) |
| 20 | + SELECT 'Other', NOW(), NOW() |
| 21 | + WHERE NOT EXISTS (SELECT 1 FROM answer_options WHERE name = 'Other') |
| 22 | + SQL |
| 23 | + |
| 24 | + execute(<<~SQL.squish) |
| 25 | + INSERT INTO form_field_answer_options (form_field_id, answer_option_id, created_at, updated_at) |
| 26 | + SELECT ff.id, ao.id, NOW(), NOW() |
| 27 | + FROM form_fields ff |
| 28 | + CROSS JOIN (SELECT id FROM answer_options WHERE name = 'Other' LIMIT 1) ao |
| 29 | + WHERE ff.field_identifier = 'payment_method' |
| 30 | + AND NOT EXISTS ( |
| 31 | + SELECT 1 FROM form_field_answer_options ffao |
| 32 | + WHERE ffao.form_field_id = ff.id AND ffao.answer_option_id = ao.id |
| 33 | + ) |
| 34 | + SQL |
| 35 | + end |
| 36 | +end |
0 commit comments