Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix legacy Library entity ID migration collisions (#74795)

Anderson5f0665db9b3f
diff --git a/resources/migrations/062/20260513_legacy_library_root_entity_ids.yaml b/resources/migrations/062/20260513_legacy_library_root_entity_ids.yaml
index eac42866ee5..b6bdbb7a0ed 100644
--- a/resources/migrations/062/20260513_legacy_library_root_entity_ids.yaml
+++ b/resources/migrations/062/20260513_legacy_library_root_entity_ids.yaml
@@ -3,6 +3,44 @@
databaseChangeLog:
- logicalFilePath: migrations/062_update_migrations.yaml
+ - changeSet:
+ id: v62.2026-05-13T11:59:58
+ author: andsalves
+ comment: Backfill type column value for Library collections
+ changes:
+ - sql:
+ sql: >-
+ UPDATE collection
+ SET type = 'library'
+ WHERE entity_id = 'librarylibrarylibrary' AND (type IS NULL OR type <> 'library');
+
+ UPDATE collection
+ SET type = 'library-data'
+ WHERE entity_id = 'librarylibrarydatadat' AND (type IS NULL OR type <> 'library-data');
+
+ UPDATE collection
+ SET type = 'library-metrics'
+ WHERE entity_id = 'librarylibrarymetrics' AND (type IS NULL OR type <> 'library-metrics');
+ rollback:
+ - empty: {}
+ - changeSet:
+ id: v62.2026-05-13T11:59:59
+ author: andsalves
+ comment: Make sure the root collection with library type and canonical entity_id is unique
+ changes:
+ - sql:
+ sql: >-
+ UPDATE collection
+ SET type = NULL
+ WHERE type = 'library' AND location = '/' AND entity_id != 'librarylibrarylibrary'
+ AND EXISTS (
+ SELECT 1 FROM (
+ SELECT entity_id FROM collection WHERE entity_id = 'librarylibrarylibrary'
+ ) existing
+ );
+ rollback:
+ - empty: {}
+
- changeSet:
id: v62.2026-05-13T12:00:00
author: andsalves
diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.clj
index 13629e33c21..d4e7f5228d6 100644
--- a/test/metabase/app_db/schema_migrations_test.clj
+++ b/test/metabase/app_db/schema_migrations_test.clj
@@ -2977,6 +2977,71 @@
(is (= "legacy-orphan-data"
(collection-entity-id orphan-data-id)))))))
+(deftest backfill-legacy-library-root-collection-entity-ids-test-4
+ (testing "pre-existing canonical Library rows with missing types prevent entity_id collisions"
+ (impl/test-migrations ["v62.2026-05-13T11:59:58" "v62.2026-05-13T12:00:02"] [migrate!]
+ (let [canonical-library-id (insert-legacy-library-collection! {:name "Pre-existing canonical Library"
+ :slug "canonical-library"
+ :type nil
+ :entity_id "librarylibrarylibrary"})
+ legacy-library-id (insert-legacy-library-collection! {:name "Legacy Library"
+ :slug "legacy-library"
+ :type "library"
+ :entity_id "legacy-library-root"})
+ canonical-data-id (insert-legacy-library-collection! {:name "Pre-existing canonical Data"
+ :slug "canonical-data"
+ :type nil
+ :location (str "/" canonical-library-id "/")
+ :entity_id "librarylibrarydatadat"})
+ legacy-data-id (insert-legacy-library-collection! {:name "Legacy Data"
+ :slug "legacy-data"
+ :type "library-data"
+ :location (str "/" canonical-library-id "/")
+ :entity_id "legacy-library-data"})
+ canonical-metrics-id (insert-legacy-library-collection! {:name "Pre-existing canonical Metrics"
+ :slug "canonical-metrics"
+ :type nil
+ :location (str "/" canonical-library-id "/")
+ :entity_id "librarylibrarymetrics"})
+ legacy-metrics-id (insert-legacy-library-collection! {:name "Legacy Metrics"
+ :slug "legacy-metrics"
+ :type "library-metrics"
+ :location (str "/" canonical-library-id "/")
+ :entity_id "legacy-library-metric"})]
+ (migrate!)
+ (is (= "library"
+ (t2/select-one-fn :type :collection :id canonical-library-id)))
+ (is (nil? (t2/select-one-fn :type :collection :id legacy-library-id)))
+ (is (= "library-data"
+ (t2/select-one-fn :type :collection :id canonical-data-id)))
+ (is (= "library-metrics"
+ (t2/select-one-fn :type :collection :id canonical-metrics-id)))
+ (is (= "librarylibrarylibrary"
+ (collection-entity-id canonical-library-id)))
+ (is (= "legacy-library-root"
+ (collection-entity-id legacy-library-id)))
+ (is (= "librarylibrarydatadat"
+ (collection-entity-id canonical-data-id)))
+ (is (= "legacy-library-data"
+ (collection-entity-id legacy-data-id)))
+ (is (= "librarylibrarymetrics"
+ (collection-entity-id canonical-metrics-id)))
+ (is (= "legacy-library-metric"
+ (collection-entity-id legacy-metrics-id)))))))
+
+(deftest backfill-legacy-library-root-collection-entity-ids-test-5
+ (testing "legacy Library root is backfilled when the canonical entity_id does not already exist"
+ (impl/test-migrations ["v62.2026-05-13T11:59:58" "v62.2026-05-13T12:00:00"] [migrate!]
+ (let [library-id (insert-legacy-library-collection! {:name "Library"
+ :slug "library"
+ :type "library"
+ :entity_id "legacy-library-root"})]
+ (migrate!)
+ (is (= "library"
+ (t2/select-one-fn :type :collection :id library-id)))
+ (is (= "librarylibrarylibrary"
+ (collection-entity-id library-id)))))))
+
(deftest heal-effective-type-drift-without-coercion-test
(testing "GHY-3388: heal metabase_field rows where coercion_strategy is NULL and effective_type
drifted away from base_type. The migration must repair such rows in both metabase_field