migration handles orphaned metric_files better

This commit is contained in:
dal 2025-05-01 08:10:15 -06:00
parent 5896a076a4
commit 435042b766
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 21 additions and 1 deletions

View File

@ -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 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; 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 TABLE metric_files
ALTER COLUMN data_source_id SET NOT NULL; ALTER COLUMN data_source_id SET NOT NULL;