From 71dd96b08447b707015ff3cab5ae445a8b973be6 Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Tue, 17 Oct 2023 18:00:04 +0300 Subject: [PATCH] Fix the widgets_bundle_widget table after upgrade --- application/src/main/data/upgrade/3.5.1/schema_update.sql | 2 +- application/src/main/data/upgrade/3.6.0/schema_update.sql | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/application/src/main/data/upgrade/3.5.1/schema_update.sql b/application/src/main/data/upgrade/3.5.1/schema_update.sql index f41e370ab5..fab91a4e67 100644 --- a/application/src/main/data/upgrade/3.5.1/schema_update.sql +++ b/application/src/main/data/upgrade/3.5.1/schema_update.sql @@ -189,7 +189,7 @@ DO $$ BEGIN IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'widget_type' and column_name='bundle_alias') THEN - INSERT INTO widgets_bundle_widget SELECT wb.id as widgets_bundle_id, wt.id as widget_type_id from widget_type wt left join widgets_bundle wb ON wt.bundle_alias = wb.alias ON CONFLICT (widgets_bundle_id, widget_type_id) DO NOTHING; + INSERT INTO widgets_bundle_widget SELECT wb.id as widgets_bundle_id, wt.id as widget_type_id from widget_type wt left join widgets_bundle wb ON wt.bundle_alias = wb.alias AND wt.tenant_id = wb.tenant_id ON CONFLICT (widgets_bundle_id, widget_type_id) DO NOTHING; ALTER TABLE widget_type DROP COLUMN IF EXISTS bundle_alias; END IF; END; diff --git a/application/src/main/data/upgrade/3.6.0/schema_update.sql b/application/src/main/data/upgrade/3.6.0/schema_update.sql index 0ad2d97b7a..ee1b311de2 100644 --- a/application/src/main/data/upgrade/3.6.0/schema_update.sql +++ b/application/src/main/data/upgrade/3.6.0/schema_update.sql @@ -24,3 +24,10 @@ ALTER TABLE notification DROP CONSTRAINT IF EXISTS fk_notification_request_id; ALTER TABLE notification DROP CONSTRAINT IF EXISTS fk_notification_recipient_id; CREATE INDEX IF NOT EXISTS idx_notification_notification_request_id ON notification(request_id); CREATE INDEX IF NOT EXISTS idx_notification_request_tenant_id ON notification_request(tenant_id); + +-- DELETE invalid records from M:N widgets_bundle_widget table caused by the bug in previous upgrade script; +DELETE +FROM widgets_bundle_widget wbw +WHERE (SELECT tenant_id FROM widgets_bundle wb WHERE wb.id = wbw.widgets_bundle_id) != + (SELECT tenant_id FROM widget_type WHERE id = wbw.widget_type_id); +