diff --git a/src/metabase/api/common.clj b/src/metabase/api/common.clj index 6266696070d5f4362d9c4cad02dedd37f0aa9fae..65954e5f40f1a20440a2217fc92ec6aa924ad9c1 100644 --- a/src/metabase/api/common.clj +++ b/src/metabase/api/common.clj @@ -26,7 +26,7 @@ "Primarily for debugging purposes. Evaulates BODY as if `*current-user*` was the User with USER-ID." [user-id & body] `(binding [*current-user-id* ~user-id - *current-user* (delay ((sel-fn :one "metabase.models.user/User" :id ~user-id))) ] + *current-user* (delay (sel :one 'metabase.models.user/User :id ~user-id))] ~@body)) (defn current-user-perms-for-org diff --git a/src/metabase/db.clj b/src/metabase/db.clj index 92c0cd3313e06acfbb5ce5d44e66f8ff537fef1f..67e04ed59f2b4df437b0caeb5527a38f4ac6feca 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -199,13 +199,6 @@ (->> (select entity-select-form# ~@forms) (map (partial post-select entity#)))))) ; map `post-select` over the results -(defmacro sel-fn - "Returns a memoized fn that calls `sel`." - [one-or-many entity & forms] - `(memoize - (fn [] - (sel ~one-or-many ~entity ~@forms)))) - ;; ## INS diff --git a/src/metabase/db/internal.clj b/src/metabase/db/internal.clj index 953bd6f267f0abfe9a85f49a4ae0ad63efd7741f..4250f5e4dbb3781781497132155789876d4fa9f6 100644 --- a/src/metabase/db/internal.clj +++ b/src/metabase/db/internal.clj @@ -30,12 +30,13 @@ [(first entity) (vec (rest entity))])) (def entity->korma - "Convert an ENTITY argument to `sel`/`sel-fn` into the form we should pass to korma `select` and to various multi-methods such as + "Convert an ENTITY argument to `sel` into the form we should pass to korma `select` and to various multi-methods such as `post-select`. * If entity is a vector like `[User :name]`, only keeps the first arg (`User`) * Converts fully-qualified entity name strings like `\"metabase.models.user/User\"` to the corresponding entity - and requires their namespace if needed." + and requires their namespace if needed. + * Symbols like `'metabase.models.user/User` are handled the same way as strings." (memoize (fn -entity->korma [entity] {:post [(= (type %) :korma.core/Entity)]} diff --git a/src/metabase/models/annotation.clj b/src/metabase/models/annotation.clj index 34dbd168e73a7d1bdb9451d39312714651eacbaf..fd7fbc750d4a08d4305fc3295a7419036561b09a 100644 --- a/src/metabase/models/annotation.clj +++ b/src/metabase/models/annotation.clj @@ -2,9 +2,9 @@ (:require [korma.core :refer :all] [metabase.db :refer :all] (metabase.models [common :refer :all] - [hydrate :refer [realize-json]] - [org :refer [Org]] - [user :refer [User]]) + [hydrate :refer [realize-json]] + [org :refer [Org]] + [user :refer [User]]) [metabase.util :as util])) @@ -28,6 +28,6 @@ (defmethod post-select Annotation [_ {:keys [organization_id author_id] :as annotation}] (-> annotation - ;; TODO - would probably be nice to associate a function which pulls the object the annotation points to - (assoc :author (sel-fn :one User :id author_id) - :organization (sel-fn :one Org :id organization_id)))) + ;; TODO - would probably be nice to associate a function which pulls the object the annotation points to + (assoc :author (delay (sel :one User :id author_id)) + :organization (delay (sel :one Org :id organization_id))))) diff --git a/src/metabase/models/card.clj b/src/metabase/models/card.clj index b6fa13dd6d5e42aaf4568384844775d2e646e155..4597c0806f2781130644988a0154fc1a2bfbc05a 100644 --- a/src/metabase/models/card.clj +++ b/src/metabase/models/card.clj @@ -28,6 +28,6 @@ (defmethod post-select Card [_ {:keys [organization_id creator_id] :as card}] (-> card (realize-json :dataset_query :visualization_settings) - (assoc :creator (sel-fn :one User :id creator_id) - :organization (sel-fn :one Org :id organization_id)) + (assoc :creator (delay (sel :one User :id creator_id)) + :organization (delay (sel :one Org :id organization_id))) assoc-permissions-sets)) diff --git a/src/metabase/models/card_favorite.clj b/src/metabase/models/card_favorite.clj index 1fc66ee257e711f23b025c0e1463779c8f7cc1a1..fff8d266442675e5fb0f8481f8d45ac9f12bcfae 100644 --- a/src/metabase/models/card_favorite.clj +++ b/src/metabase/models/card_favorite.clj @@ -10,8 +10,8 @@ (defmethod post-select CardFavorite [_ {:keys [card_id owner_id] :as card-favorite}] (assoc card-favorite - :owner (sel-fn :one User :id owner_id) - :card (sel-fn :one Card :id card_id))) + :owner (delay (sel :one User :id owner_id)) + :card (delay (sel :one Card :id card_id)))) (defmethod pre-insert CardFavorite [_ card-favorite] (let [defaults {:created_at (new-sql-timestamp) diff --git a/src/metabase/models/dashboard.clj b/src/metabase/models/dashboard.clj index 25794c378358f73a838697c7ed40b2f488764717..6061092495787db18504533b182e46f085a7aca1 100644 --- a/src/metabase/models/dashboard.clj +++ b/src/metabase/models/dashboard.clj @@ -21,10 +21,10 @@ (defmethod post-select Dashboard [_ {:keys [id creator_id organization_id description] :as dash}] (-> dash - (assoc :creator (sel-fn :one User :id creator_id) - :description (util/jdbc-clob->str description) - :organization (sel-fn :one Org :id organization_id) - :ordered_cards (sel-fn :many DashboardCard :dashboard_id id)) + (assoc :creator (delay (sel :one User :id creator_id)) + :description (util/jdbc-clob->str description) + :organization (delay (sel :one Org :id organization_id)) + :ordered_cards (delay (sel :many DashboardCard :dashboard_id id))) assoc-permissions-sets)) ; TODO - ordered_cards diff --git a/src/metabase/models/dashboard_card.clj b/src/metabase/models/dashboard_card.clj index 600c59a443c695e19af188cc55360ababad4ef88..7ec304a3997dbbcb3a21af1f1ee7a3156144a433 100644 --- a/src/metabase/models/dashboard_card.clj +++ b/src/metabase/models/dashboard_card.clj @@ -23,8 +23,8 @@ (-> dashcard (clojure.set/rename-keys {:sizex :sizeX ; mildly retarded: H2 columns are all uppercase, we're converting them :sizey :sizeY}) ; to all downcase, and the Angular app expected mixed-case names here - (assoc :card (sel-fn :one Card :id card_id) - :dashboard (sel-fn :one "metabase.models.dashboard/Dashboard" :id dashboard_id)))) + (assoc :card (delay (sel :one Card :id card_id)) + :dashboard (delay (sel :one 'metabase.models.dashboard/Dashboard :id dashboard_id))))) (defmethod pre-insert DashboardCard [_ dashcard] (let [defaults {:created_at (util/new-sql-timestamp) diff --git a/src/metabase/models/database.clj b/src/metabase/models/database.clj index 02a09312aa0995637f30feb42db60edbff61f5aa..5e7b96f0de9559047af9a824bcd70d08c13fbcbe 100644 --- a/src/metabase/models/database.clj +++ b/src/metabase/models/database.clj @@ -59,7 +59,7 @@ (defmethod post-select Database [_ {:keys [organization_id] :as db}] (-> db (realize-json :details) ; TODO wouldn't we want to actually strip this info instead of returning it? - (assoc* :organization (sel-fn :one Org :id organization_id) + (assoc* :organization (delay (sel :one Org :id organization_id)) :can_read (delay (org-can-read organization_id)) :can_write (delay (org-can-write organization_id)) :connection-details (delay (conn/connection-details <>)) diff --git a/src/metabase/models/field.clj b/src/metabase/models/field.clj index eb6613c37e09ce91a58286c32fc1db8a20f65721..dd83a8a0d41578c67571e438c173f2411a5e19cb 100644 --- a/src/metabase/models/field.clj +++ b/src/metabase/models/field.clj @@ -68,11 +68,11 @@ (defmethod post-select Field [_ {:keys [table_id] :as field}] (util/assoc* field - :table (sel-fn :one "metabase.models.table/Table" :id table_id) - :db (delay ((:db ((:table <>))))) - :can_read (delay @(:can_read ((:table <>)))) - :can_write (delay @(:can_write ((:table <>)))) - :count (delay (field-count <>)) + :table (delay (sel :one 'metabase.models.table/Table :id table_id)) + :db (delay ((:db ((:table <>))))) + :can_read (delay @(:can_read ((:table <>)))) + :can_write (delay @(:can_write ((:table <>)))) + :count (delay (field-count <>)) :distinct-count (delay (field-distinct-count <>)))) (defmethod pre-insert Field [_ field] diff --git a/src/metabase/models/org_perm.clj b/src/metabase/models/org_perm.clj index bc87ce8668e56a94d7ba605cf18de85c3fc4d6b6..3cd2b9ce8251858bcf9ea40b693eacad07179b25 100644 --- a/src/metabase/models/org_perm.clj +++ b/src/metabase/models/org_perm.clj @@ -10,8 +10,8 @@ (defmethod post-select OrgPerm [_ {:keys [organization_id user_id] :as org-perm}] (assoc org-perm - :organization (sel-fn :one Org :id organization_id) - :user (sel-fn :one "metabase.models.user/User" :id user_id))) + :organization (delay (sel :one Org :id organization_id)) + :user (delay (sel :one 'metabase.models.user/User :id user_id)))) (defn grant-org-perm diff --git a/src/metabase/models/table.clj b/src/metabase/models/table.clj index a96365dc2df2f508f1b3db580407e247eb5b59dd..3b4b1830318b08b66238a3b8f8d7f49a0c13ded7 100644 --- a/src/metabase/models/table.clj +++ b/src/metabase/models/table.clj @@ -37,11 +37,11 @@ (defmethod post-select Table [_ {:keys [id db db_id name] :as table}] (util/assoc* table - :db (or db (sel-fn :one db/Database :id db_id)) ; Check to see if `:db` is already set. In some cases we add a korma transform fn to `Table` - :fields (sel-fn :many Field :table_id id) ; and assoc :db if the DB has already been fetched, so we can re-use its DB connections. + :db (or db (delay (sel :one db/Database :id db_id))) ; Check to see if `:db` is already set. In some cases we add a korma transform fn to `Table` + :fields (delay (sel :many Field :table_id id)) ; and assoc :db if the DB has already been fetched, so we can re-use its DB connections. :jdbc-columns (delay (jdbc-columns ((:db <>)) name)) - :can_read (delay @(:can_read ((:db <>)))) - :can_write (delay @(:can_write ((:db <>)))) + :can_read (delay @(:can_read ((:db <>)))) + :can_write (delay @(:can_write ((:db <>)))) :korma-entity (delay (korma-entity <>)))) (defmethod pre-insert Table [_ table] diff --git a/src/metabase/models/user.clj b/src/metabase/models/user.clj index 11248420534aba5d9ac39e1f5811ca5f583a6235..d4ce0e1241da4374c20b990a4cff66cbb9c59329 100644 --- a/src/metabase/models/user.clj +++ b/src/metabase/models/user.clj @@ -28,9 +28,10 @@ (defn user-perms-for-org "Return the permissions level User with USER-ID has for Org with ORG-ID. - nil -> no permissions - :default -> default permissions - :admin -> admin permissions" + + nil -> no permissions + :default -> default permissions + :admin -> admin permissions" [user-id org-id] (when-let [{superuser? :is_superuser} (sel :one [User :is_superuser] :id user-id)] (if superuser? :admin @@ -39,9 +40,9 @@ (defmethod post-select User [_ {:keys [id] :as user}] (-> user - (assoc :org_perms (sel-fn :many OrgPerm :user_id id) + (assoc :org_perms (delay (sel :many OrgPerm :user_id id)) :perms-for-org (memoize (partial user-perms-for-org id)) - :common_name (str (:first_name user) " " (:last_name user))))) + :common_name (str (:first_name user) " " (:last_name user))))) (defmethod pre-insert User [_ {:keys [email password] :as user}] (assert (util/is-email? email))