diff --git a/src/metabase/api/card.clj b/src/metabase/api/card.clj index 2d3c1582a9cc07cdc28ff283c2b6e128c5d7fda4..356f1227711d83117ca058308cf4f88a01d2d51a 100644 --- a/src/metabase/api/card.clj +++ b/src/metabase/api/card.clj @@ -335,6 +335,7 @@ :constraints constraints) options {:executed-by *current-user-id* :card-id card-id}] + (check-not-archived card) (qp/dataset-query query options))) (defendpoint POST "/:card-id/query" diff --git a/src/metabase/api/common.clj b/src/metabase/api/common.clj index 9ece0a037128038dcc00afc2d65282e10847130c..dd083ee41b08af240de94bfadcbc3c0c9c1a60be 100644 --- a/src/metabase/api/common.clj +++ b/src/metabase/api/common.clj @@ -292,3 +292,12 @@ (write-check (entity id))) ([entity id & other-conditions] (write-check (apply db/select-one entity :id id other-conditions)))) + + +;;; ------------------------------------------------------------ OTHER HELPER FNS ------------------------------------------------------------ + +(defn check-not-archived + "Check that the OBJECT is not `:archived`, or throw a `404`. Returns OBJECT as-is if check passes." + [object] + (u/prog1 object + (check-404 (not (:archived object))))) diff --git a/src/metabase/db.clj b/src/metabase/db.clj index b08881248741eb66ecaa6189b87282936903c344..a82e3a43e89c24f5d3554e8477c668e310a29958 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -564,6 +564,15 @@ (models/default-fields (resolve-entity entity)))) +(defn do-post-select + "Perform post-processing for objects fetched from the DB. + Convert results OBJECTS to ENTITY record types and call the entity's `post-select` method on them." + {:style/indent 1} + [entity objects] + (let [entity (resolve-entity entity)] + (vec (for [object objects] + (models/do-post-select entity object))))) + (defn simple-select "Select objects from the database. Like `select`, but doesn't offer as many conveniences, so you should use that instead. This calls `post-select` on the results. @@ -572,11 +581,10 @@ {:style/indent 1} [entity honeysql-form] (let [entity (resolve-entity entity)] - (vec (for [object (query (merge {:select (or (models/default-fields entity) - [:*]) - :from [entity]} - honeysql-form))] - (models/do-post-select entity object))))) + (do-post-select entity (query (merge {:select (or (models/default-fields entity) + [:*]) + :from [entity]} + honeysql-form))))) (defn simple-select-one "Select a single object from the database. Like `select-one`, but doesn't offer as many conveniences, so prefer that instead. diff --git a/src/metabase/models/dashboard.clj b/src/metabase/models/dashboard.clj index c08a9ce83c75ca577b321387341b77ca0f142b02..5c2144937f3e5b06809580475178f5532ebda092 100644 --- a/src/metabase/models/dashboard.clj +++ b/src/metabase/models/dashboard.clj @@ -2,7 +2,7 @@ (:require [clojure.data :refer [diff]] (metabase [db :as db] [events :as events]) - (metabase.models [card :as card] + (metabase.models [card :refer [Card], :as card] [dashboard-card :refer [DashboardCard], :as dashboard-card] [hydrate :refer [hydrate]] [interface :as i] @@ -59,7 +59,13 @@ "Return the `DashboardCards` associated with DASHBOARD, in the order they were created." {:hydrate :ordered_cards} [dashboard] - (db/select DashboardCard, :dashboard_id (u/get-id dashboard), {:order-by [[:created_at :asc]]})) + (db/do-post-select DashboardCard + (db/query {:select [:dashcard.*] + :from [[DashboardCard :dashcard]] + :join [[Card :card] [:= :dashcard.card_id :card.id]] + :where [:and [:= :dashcard.dashboard_id (u/get-id dashboard)] + [:= :card.archived false]] + :order-by [[:dashcard.created_at :asc]]}))) ;;; ## ---------------------------------------- PERSISTENCE FUNCTIONS ---------------------------------------- diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj index 9ac8a1365632114847ebc536ee49829e5b69b004..c7a4de3d27007808fcf698fa4ff22e7f7ef49bc4 100644 --- a/test/metabase/test/util.clj +++ b/test/metabase/test/util.clj @@ -89,6 +89,13 @@ [k (f v)]))))) +(defn- user-id [username] + (require 'metabase.test.data.users) + ((resolve 'metabase.test.data.users/user->id) username)) + +(defn- rasta-id [] (user-id :rasta)) + + (defprotocol ^:private WithTempDefaults (^:private with-temp-defaults [this])) @@ -96,10 +103,6 @@ WithTempDefaults {:with-temp-defaults (constantly {})}) -(defn- rasta-id [] - (require 'metabase.test.data.users) - ((resolve 'metabase.test.data.users/user->id) :rasta)) - (u/strict-extend (class Card) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id (rasta-id)