Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions liquibase/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
<!-- Index the unindexed FK gtfsfile.gtfs_dataset_id; the seal criteria scan it per dataset (issue #1831). -->
<!-- Created CONCURRENTLY, so it needs its own transaction: hence the XML wrapper, as in feat_1432_indexes.xml -->
<include file="changes/feat_1831_indexes.xml" relativeToChangelogFile="true"/>
<!-- Remove Sao Paulo feeds as requested by feed owner -->
<include file="changes/remove_sao_paulo_feeds.sql" relativeToChangelogFile="true"/>
<!-- Keep this after all source table and schema changes so materialized views
are recreated from the final source schema. -->
<include file="materialized_views/materialized_views.xml" relativeToChangelogFile="true"/>
Expand Down
75 changes: 75 additions & 0 deletions liquibase/changes/remove_sao_paulo_feeds.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- Remove feeds mdb-3504 and mdb-3505 (Sao Paulo) and every record related to them.
--
-- Deleting the feed row cascades (ON DELETE CASCADE) to:
-- gtfsfeed / gtfsrealtimefeed, externalid, feed_license_change, feed_reliability_seal,
-- feedlocationgrouppoint, feedosmlocationgroup, feedrelatedlink, locationfeed,
-- officialstatushistory, redirectingid (as source or target), seal_criterion,
-- seal_criterion_snapshot, entitytypefeed, feedreference, gtfs_dataset_changelog,
-- gtfs_feed_availability_check, gtfsdataset
-- and from gtfsdataset to:
-- gtfsfile, notice, location_gtfsdataset, validationreportgtfsdataset
--
-- Not covered by cascades, handled explicitly below:
-- gtfsfeed.bounding_box_dataset_id / visualization_dataset_id (no ON DELETE rule)
-- config_value_feed (no foreign key to feed)
-- validationreport and feedinfo rows that would be left orphaned
-- feedsearch materialized view

CREATE TEMP TABLE sp_feeds_to_delete ON COMMIT DROP AS
SELECT f.id
FROM feed f
WHERE f.stable_id IN ('mdb-3504', 'mdb-3505');

CREATE TEMP TABLE sp_datasets_to_delete ON COMMIT DROP AS
SELECT g.id, g.feed_info_id
FROM gtfsdataset g
WHERE g.feed_id IN (SELECT id FROM sp_feeds_to_delete);

-- Validation reports used exclusively by the datasets being deleted
CREATE TEMP TABLE sp_validation_reports_to_delete ON COMMIT DROP AS
SELECT DISTINCT vrd.validation_report_id AS id
FROM validationreportgtfsdataset vrd
WHERE vrd.dataset_id IN (SELECT id FROM sp_datasets_to_delete)
AND NOT EXISTS (
SELECT 1
FROM validationreportgtfsdataset other
WHERE other.validation_report_id = vrd.validation_report_id
AND other.dataset_id NOT IN (SELECT id FROM sp_datasets_to_delete)
);

-- Feed info rows used exclusively by the datasets being deleted
CREATE TEMP TABLE sp_feed_infos_to_delete ON COMMIT DROP AS
SELECT DISTINCT d.feed_info_id AS id
FROM sp_datasets_to_delete d
WHERE d.feed_info_id IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM gtfsdataset other
WHERE other.feed_info_id = d.feed_info_id
AND other.id NOT IN (SELECT id FROM sp_datasets_to_delete)
);

-- Clear dataset references that have no ON DELETE rule
UPDATE gtfsfeed
SET bounding_box_dataset_id = NULL
WHERE bounding_box_dataset_id IN (SELECT id FROM sp_datasets_to_delete);

UPDATE gtfsfeed
SET visualization_dataset_id = NULL
WHERE visualization_dataset_id IN (SELECT id FROM sp_datasets_to_delete);

DELETE FROM config_value_feed
WHERE feed_id IN (SELECT id FROM sp_feeds_to_delete);

-- Cascades to all feed-level and dataset-level tables listed above
DELETE FROM feed
WHERE id IN (SELECT id FROM sp_feeds_to_delete);

-- Cascades to notice and featurevalidationreport
DELETE FROM validationreport
WHERE id IN (SELECT id FROM sp_validation_reports_to_delete);

DELETE FROM feedinfo
WHERE id IN (SELECT id FROM sp_feed_infos_to_delete);

REFRESH MATERIALIZED VIEW feedsearch;
Loading