The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
fix: cannot save dashboard when dashboard was reverted to one with deleted filter (#68871)
Maksym Yakubychbbddb39150db
diff --git a/e2e/test/scenarios/filters-reproductions/dashboard-filters-reproductions.cy.spec.js b/e2e/test/scenarios/filters-reproductions/dashboard-filters-reproductions.cy.spec.jsindex 0b075f9beff..f68c23a49cd 100644--- a/e2e/test/scenarios/filters-reproductions/dashboard-filters-reproductions.cy.spec.js+++ b/e2e/test/scenarios/filters-reproductions/dashboard-filters-reproductions.cy.spec.js@@ -5444,3 +5444,146 @@ describe("issue 49319", () => {H.assertTableRowsCount(82);});});++describe("issue #66670", () => {+ const questionA = {+ name: "Question A",+ query: {+ "source-table": PRODUCTS_ID,+ limit: 10,+ },+ };++ const questionB = {+ name: "Question B",+ query: {+ "source-table": PRODUCTS_ID,+ aggregation: [["count"]],+ breakout: [["field", PRODUCTS.CATEGORY, null]],+ },+ };++ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ cy.intercept("PUT", "/api/dashboard/*").as("updateDashboard");+ cy.intercept("GET", "/api/revision*").as("revisionHistory");+ });++ it("should handle dashboard filter with permanently deleted question value source (metabase#66670)", () => {+ cy.log("Step 1: Create a new dashboard");+ H.createDashboard({ name: "Test Dashboard UXW-2494" }).then(+ ({ body: { id: dashboardId } }) => {+ cy.log("Step 2: Add Question A to the dashboard");+ H.createQuestion(questionA).then(({ body: { id: questionAId } }) => {+ H.updateDashboardCards({+ dashboard_id: dashboardId,+ cards: [+ {+ card_id: questionAId,+ row: 0,+ col: 0,+ size_x: 12,+ size_y: 8,+ },+ ],+ });++ cy.log("Step 3: Create Question B (not added to dashboard)");+ H.createQuestion(questionB).then(({ body: { id: questionBId } }) => {+ cy.log(+ "Step 4: Edit dashboard, add filter with Question B as value source",+ );+ H.visitDashboard(dashboardId);+ H.editDashboard();+ H.setFilter("Text or Category", "Is");++ // Map filter to Question A+ cy.findByText("Select…").click();+ H.popover().within(() => cy.findByText("Category").click());++ // Set filter value source to Question B+ H.setFilterQuestionSource({+ question: questionB.name,+ field: "Category",+ });++ cy.log("Step 5: Save the dashboard");+ H.saveDashboard();+ cy.wait("@updateDashboard");++ cy.log("Step 5b: Update the title and save again");+ H.editDashboard();+ cy.findByTestId("dashboard-header")+ .findByDisplayValue("Test Dashboard UXW-2494")+ .clear()+ .type("Updated Dashboard Title");+ H.saveDashboard();+ cy.wait("@updateDashboard");++ cy.log("Step 6: Move Question B to the trash");+ H.visitQuestion(questionBId);+ H.openQuestionActions();+ H.popover().findByText("Move to trash").click();+ H.modal().findByText("Move to trash").click();++ cy.log(+ "Step 8: Revert dashboard to earlier version where filter used Question B",+ );+ H.visitDashboard(dashboardId);+ H.openDashboardInfoSidebar();+ H.sidesheet().within(() => {+ cy.findByRole("tab", { name: "History" }).click();+ cy.wait("@revisionHistory");+ // Click revert on the version that added the filter with Question B source+ cy.findByTestId("dashboard-history-list")+ .findAllByLabelText(/revert to You edited this/i)+ .first()+ .click();+ });+ // Close sidesheet+ H.sidesheet().findByLabelText("Close").click();++ cy.log("Step 9: Permanently delete Question B from trash");+ cy.request("DELETE", `/api/card/${questionBId}`);++ cy.log("Step 10: Try to add another filter and save → Save fails");+ H.editDashboard();+ H.setFilter("Number");+ cy.findByText("Select…").click();+ H.popover().within(() => cy.findByText("Price").click());++ cy.intercept("PUT", `/api/dashboard/${dashboardId}`).as(+ "saveDashboard",+ );+ cy.findByTestId("edit-bar").button("Save").click();++ cy.wait("@saveDashboard").then((interception) => {+ expect(interception.response.statusCode).to.eq(200);+ });++ cy.log(+ "Step 11: Edit existing filter, click Edit → modal loads indefinitely",+ );+ H.visitDashboard(dashboardId);+ H.editDashboard();+ H.filterWidget({ isEditing: true }).first().click();+ H.dashboardParameterSidebar().findByText("Edit").click();++ H.modal().should("be.visible");+ H.modal().within(() => {+ cy.findByText("Where values should come from", {+ timeout: 10000,+ }).should("be.visible");+ cy.findByText("From connected fields").should("be.visible");+ cy.findByText("From another model or question").should(+ "be.visible",+ );+ cy.findByText("Custom list").should("be.visible");+ });+ });+ });+ },+ );+ });+});diff --git a/src/metabase/revisions/impl/dashboard.clj b/src/metabase/revisions/impl/dashboard.cljindex a44064169e6..c71d00a9cce 100644--- a/src/metabase/revisions/impl/dashboard.clj+++ b/src/metabase/revisions/impl/dashboard.clj@@ -78,28 +78,50 @@inactive-card-ids (set/difference card-ids active-card-ids)](remove #(contains? inactive-card-ids (:card_id %)) dashcards)))+(defn- clean-invalid-parameter-card-references+ "Given a list of parameters, remove card references from any parameter whose values_source_config.card_id+ references a card that is archived or does not exist. The parameter is reset to use connected fields."+ [parameters]+ (let [card-ids (set (keep #(get-in % [:values_source_config :card_id]) parameters))+ active-card-ids (when (seq card-ids)+ (t2/select-pks-set :model/Card+ {:where [:and+ [:in :id card-ids]+ [:= :archived false]]}))+ invalid-card-ids (set/difference card-ids active-card-ids)]+ (if (empty? invalid-card-ids)+ parameters+ (mapv (fn [param]+ (if (contains? invalid-card-ids (get-in param [:values_source_config :card_id]))+ (dissoc param :values_source_type :values_source_config)+ param))+ parameters))))+(defmethod revision/revert-to-revision! :model/Dashboard[model dashboard-id user-id serialized-dashboard]- ;; Update the dashboard description / name / permissions- ((get-method revision/revert-to-revision! :default) model dashboard-id user-id (dissoc serialized-dashboard :cards :tabs))- ;; Now update the tabs and cards as needed- (let [serialized-dashcards (:cards serialized-dashboard)- current-tabs (t2/select-fn-vec #(dissoc (t2.realize/realize %) :created_at :updated_at :entity_id :dashboard_id)- :model/DashboardTab :dashboard_id dashboard-id)- {:keys [old->new-tab-id]} (dashboard-tab/do-update-tabs! dashboard-id current-tabs (:tabs serialized-dashboard))- _ (dashboard/archive-or-unarchive-internal-dashboard-questions! dashboard-id serialized-dashcards)- serialized-dashcards (cond->> serialized-dashcards- true- (remove-invalid-dashcards dashboard-id)- ;; in case reverting result in new tabs being created,- ;; we need to remap the tab-id- (seq old->new-tab-id)- (map (fn [card]- (if-let [new-tab-id (get old->new-tab-id (:dashboard_tab_id card))]- (assoc card :dashboard_tab_id new-tab-id)- card))))]- (revert-dashcards dashboard-id serialized-dashcards))- serialized-dashboard)+ ;; Clean invalid parameter card references before reverting (cards may have been deleted since the revision)+ (let [cleaned-parameters (clean-invalid-parameter-card-references (or (:parameters serialized-dashboard) []))+ serialized-dashboard (assoc serialized-dashboard :parameters cleaned-parameters)]+ ;; Update the dashboard description / name / permissions+ ((get-method revision/revert-to-revision! :default) model dashboard-id user-id (dissoc serialized-dashboard :cards :tabs))+ ;; Now update the tabs and cards as needed+ (let [serialized-dashcards (:cards serialized-dashboard)+ current-tabs (t2/select-fn-vec #(dissoc (t2.realize/realize %) :created_at :updated_at :entity_id :dashboard_id)+ :model/DashboardTab :dashboard_id dashboard-id)+ {:keys [old->new-tab-id]} (dashboard-tab/do-update-tabs! dashboard-id current-tabs (:tabs serialized-dashboard))+ _ (dashboard/archive-or-unarchive-internal-dashboard-questions! dashboard-id serialized-dashcards)+ serialized-dashcards (cond->> serialized-dashcards+ true+ (remove-invalid-dashcards dashboard-id)+ ;; in case reverting result in new tabs being created,+ ;; we need to remap the tab-id+ (seq old->new-tab-id)+ (map (fn [card]+ (if-let [new-tab-id (get old->new-tab-id (:dashboard_tab_id card))]+ (assoc card :dashboard_tab_id new-tab-id)+ card))))]+ (revert-dashcards dashboard-id serialized-dashcards))+ serialized-dashboard))(defmethod revision/diff-strings :model/Dashboard[_model prev-dashboard dashboard]diff --git a/test/metabase/revisions/impl/dashboard_test.clj b/test/metabase/revisions/impl/dashboard_test.cljindex 6226c2b7d1c..75d1aab770d 100644--- a/test/metabase/revisions/impl/dashboard_test.clj+++ b/test/metabase/revisions/impl/dashboard_test.clj@@ -697,3 +697,99 @@:visualization_settings {:text "Metabase"}}}(t2/select-fn-set #(select-keys % [:card_id :visualization_settings]):model/DashboardCard :dashboard_id dashboard-id))))))++(deftest revert-dashboard-with-deleted-parameter-card-source-test+ (testing "revert should clean up parameters that reference deleted or archived cards (metabase#UXW-2494)"+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "A dashboard"+ :parameters []}+ :model/Card {value-source-card :id} {:name "Value source question"}+ :model/Card {will-remain-card :id} {:name "Will remain question"}]+ ;; 0. create initial dashboard revision+ (create-dashboard-revision! dashboard-id true)++ ;; 1. add two parameters - one with a card that will be deleted, one with a card that stays+ (t2/update! :model/Dashboard dashboard-id+ {:parameters [{:id "deleted-source"+ :name "Filter with deleted source"+ :slug "deleted_source"+ :type "category"+ :values_source_type "card"+ :values_source_config {:card_id value-source-card+ :value_field [:field 1 nil]}}+ {:id "valid-source"+ :name "Filter with valid source"+ :slug "valid_source"+ :type "category"+ :values_source_type "card"+ :values_source_config {:card_id will-remain-card+ :value_field [:field 2 nil]}}+ {:id "no-source"+ :name "Filter without card source"+ :slug "no_source"+ :type "category"}]})+ (create-dashboard-revision! dashboard-id false)++ ;; 2. remove the parameters (simulating user changing the filter settings)+ (t2/update! :model/Dashboard dashboard-id {:parameters []})+ (create-dashboard-revision! dashboard-id false)++ ;; 3. delete one card and archive another scenario - here we just delete+ (t2/delete! :model/Card value-source-card)++ ;; 4. revert to the revision that had parameters referencing the now-deleted card+ (revert-to-previous-revision! :model/Dashboard dashboard-id 2)++ (let [reverted-params (:parameters (t2/select-one :model/Dashboard :id dashboard-id))]+ (testing "parameter with deleted card source should have its source config removed"+ (let [deleted-source-param (first (filter #(= "deleted-source" (:id %)) reverted-params))]+ (is (some? deleted-source-param) "Parameter should still exist")+ (is (nil? (:values_source_type deleted-source-param)) "values_source_type should be removed")+ (is (nil? (:values_source_config deleted-source-param)) "values_source_config should be removed")))++ (testing "parameter with valid card source should remain unchanged"+ (let [valid-source-param (first (filter #(= "valid-source" (:id %)) reverted-params))]+ (is (some? valid-source-param) "Parameter should exist")+ (is (= :card (:values_source_type valid-source-param)) "values_source_type should be preserved")+ (is (= will-remain-card (get-in valid-source-param [:values_source_config :card_id]))+ "card_id should be preserved")))++ (testing "parameter without card source should remain unchanged"+ (let [no-source-param (first (filter #(= "no-source" (:id %)) reverted-params))]+ (is (some? no-source-param) "Parameter should exist")+ (is (nil? (:values_source_type no-source-param)) "values_source_type should remain nil")))))))++(deftest revert-dashboard-with-archived-parameter-card-source-test+ (testing "revert should also clean up parameters that reference archived cards"+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "A dashboard"+ :parameters []}+ :model/Card {will-be-archived-card :id} {:name "Will be archived"}]+ ;; 0. create initial dashboard revision+ (create-dashboard-revision! dashboard-id true)++ ;; 1. add a parameter with a card source+ (t2/update! :model/Dashboard dashboard-id+ {:parameters [{:id "archived-source"+ :name "Filter with archived source"+ :slug "archived_source"+ :type "category"+ :values_source_type "card"+ :values_source_config {:card_id will-be-archived-card+ :value_field [:field 1 nil]}}]})+ (create-dashboard-revision! dashboard-id false)++ ;; 2. remove the parameters+ (t2/update! :model/Dashboard dashboard-id {:parameters []})+ (create-dashboard-revision! dashboard-id false)++ ;; 3. archive the card+ (t2/update! :model/Card will-be-archived-card {:archived true})++ ;; 4. revert to the revision that had parameters referencing the now-archived card+ (revert-to-previous-revision! :model/Dashboard dashboard-id 2)++ (let [reverted-params (:parameters (t2/select-one :model/Dashboard :id dashboard-id))]+ (testing "parameter with archived card source should have its source config removed"+ (let [archived-source-param (first reverted-params)]+ (is (some? archived-source-param) "Parameter should still exist")+ (is (nil? (:values_source_type archived-source-param)) "values_source_type should be removed")+ (is (nil? (:values_source_config archived-source-param)) "values_source_config should be removed")))))))