From 435042b766be44c8e3628825c0af37d5cfd991c9 Mon Sep 17 00:00:00 2001 From: dal Date: Thu, 1 May 2025 08:10:15 -0600 Subject: [PATCH] migration handles orphaned metric_files better --- .../up.sql | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/api/migrations/2025-04-29-223656_add_data_source_id_to_metric_files/up.sql b/api/migrations/2025-04-29-223656_add_data_source_id_to_metric_files/up.sql index c79e0ccbb..300867e70 100644 --- a/api/migrations/2025-04-29-223656_add_data_source_id_to_metric_files/up.sql +++ b/api/migrations/2025-04-29-223656_add_data_source_id_to_metric_files/up.sql @@ -24,7 +24,27 @@ FROM metric_dataset md JOIN dataset_source ds ON ds.dataset_id = (md.first_dataset_id_str)::uuid -- Cast the text datasetId to UUID for joining WHERE metric_files.id = md.metric_id; --- Add the NOT NULL constraint after backfilling +-- Identify metric_files to be deleted (those that couldn't be backfilled) +CREATE TEMP TABLE metric_files_to_delete AS +SELECT id FROM metric_files +WHERE data_source_id IS NULL; + +-- Clean up related tables before deleting the metric_files +-- NOTE: Assumes the AssetTypeEnum value for metric files is 'MetricFile'. Verify this. +DELETE FROM metric_files_to_dashboard_files WHERE metric_file_id IN (SELECT id FROM metric_files_to_delete); +DELETE FROM metric_files_to_datasets WHERE metric_file_id IN (SELECT id FROM metric_files_to_delete); +DELETE FROM asset_permissions WHERE asset_id IN (SELECT id FROM metric_files_to_delete) AND asset_type = 'MetricFile'; +DELETE FROM collections_to_assets WHERE asset_id IN (SELECT id FROM metric_files_to_delete) AND asset_type = 'MetricFile'; +DELETE FROM user_favorites WHERE asset_id IN (SELECT id FROM metric_files_to_delete) AND asset_type = 'MetricFile'; + +-- Delete metric_files that couldn't be backfilled +DELETE FROM metric_files +WHERE id IN (SELECT id FROM metric_files_to_delete); + +-- Drop the temporary table +DROP TABLE metric_files_to_delete; + +-- Add the NOT NULL constraint after backfilling and deleting orphans ALTER TABLE metric_files ALTER COLUMN data_source_id SET NOT NULL;