diff --git a/db/migrate/20240502035220_update_n8n_url.rb b/db/migrate/20240502035220_update_n8n_url.rb new file mode 100644 index 0000000000..df2aaf16e7 --- /dev/null +++ b/db/migrate/20240502035220_update_n8n_url.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# We got a new n8n server. +# But we still have some database rows with a URL to the old server. +class UpdateN8nUrl < ActiveRecord::Migration[7.0] + def up + execute <<~SQL.squish + UPDATE connected_apps + SET data = replace( + data::text, + 'n8n.openfoodnetwork.org.uk', + 'n8n.openfoodnetwork.org' + )::jsonb + WHERE data IS NOT NULL; + SQL + end +end diff --git a/spec/migrations/20240502035220_update_n8n_url_spec.rb b/spec/migrations/20240502035220_update_n8n_url_spec.rb new file mode 100644 index 0000000000..9735f1126d --- /dev/null +++ b/spec/migrations/20240502035220_update_n8n_url_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../db/migrate/20240502035220_update_n8n_url' + +describe UpdateN8nUrl do + # We may want to move this to a support file if this syntax is useful in + # other places. Reference: + # - https://stackoverflow.com/a/34969429/3377535 + RSpec::Matchers.define_negated_matcher :not_change, :change + + let(:enterprise) { create(:enterprise) } + let(:old_data) { + { + link: "https://link-to-form", + destroy: "https://n8n.openfoodnetwork.org.uk/webhook/remove-enterprise?key=abc&id=123", + } + } + let(:new_data) { + { + link: "https://link-to-form", + destroy: "https://n8n.openfoodnetwork.org/webhook/remove-enterprise?key=abc&id=123", + } + } + + it "updates old connected app links" do + app = ConnectedApp.create!(enterprise:, data: old_data,) + + expect { + subject.up + app.reload + }.to change { + app.data["destroy"] + }.to("https://n8n.openfoodnetwork.org/webhook/remove-enterprise?key=abc&id=123") + .and not_change { + app.data["link"] + } + end + + it "keeps new connected app links" do + app = ConnectedApp.create!(enterprise:, data: new_data,) + + expect { + subject.up + app.reload + }.not_to change { + app.data + } + end +end