Skip to content
Snippets Groups Projects
Commit 38b71743 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

make sure that new card attributes extracted from query dictionary are always...

make sure that new card attributes extracted from query dictionary are always set automatically on pre-update and pre-insert and tidy up unit tests to make sure everything is passing.
parent 6606d3bc
Branches
Tags
No related merge requests found
......@@ -5,20 +5,18 @@
[metabase.models.card :refer [Card]]))
(defn- set-card-database-and-table-ids
"Upgrade for the `Card` model when `:database_id` and `:table_id` were added and needed populating.
"Upgrade for the `Card` model when `:database_id`, `:table_id`, and `:query_type` were added and needed populating.
This reads through all saved cards, extracts the JSON from the `:dataset_query`, and tries to populate
the values for `:database_id` and `:table_id` if possible."
the values for `:database_id`, `:table_id`, and `:query_type` if possible."
[]
;; only execute when `:database_id` column on all cards is `nil`
(when (= 0 (:cnt (first (k/select Card (k/aggregate (count :*) :cnt) (k/where (not= :database_id nil))))))
(log/info "Data migration: Setting database/table/type fields on all Cards.")
(doseq [{id :id {{table :source_table} :query :keys [database type]} :dataset_query} (db/sel :many [Card :id :dataset_query])]
(doseq [{id :id {:keys [type] :as dataset-query} :dataset_query} (db/sel :many [Card :id :dataset_query])]
(when type
(db/upd Card id
:query_type type
:database_id database
:table_id table)))))
;; simply resave the card with the dataset query which will automatically set the database, table, and type
(db/upd Card id :dataset_query dataset-query)))))
(defn run-all
"Run all coded data migrations."
......
......@@ -25,12 +25,27 @@
(extend-ICanReadWrite CardInstance :read :public-perms, :write :public-perms)
(defn- populate-query-fields [card]
(let [{{table-id :source_table} :query database-id :database query-type :type} (:dataset_query card)
defaults {:database_id database-id
:table_id table-id
:query_type (keyword query-type)}]
(if query-type
(merge defaults card)
card)))
(defentity Card
[(table :report_card)
(hydration-keys card)
(types :dataset_query :json, :display :keyword, :visualization_settings :json)
(types :display :keyword, :query_type :keyword, :dataset_query :json, :visualization_settings :json)
timestamped]
(pre-insert [_ card]
(populate-query-fields card))
(pre-update [_ card]
(populate-query-fields card))
(post-select [_ {:keys [creator_id] :as card}]
(map->CardInstance (assoc card
:creator (delay (User creator_id))
......
......@@ -54,7 +54,10 @@
:display "scalar"
:visualization_settings {:global {:title nil}}
:public_perms 0
:created_at $})
:created_at $
:database_id (db-id)
:table_id (id :categories)
:query_type "query"})
(post-card card-name)))
;; ## GET /api/card/:id
......@@ -84,7 +87,10 @@
:display "scalar"
:visualization_settings {:global {:title nil}}
:public_perms 0
:created_at $})
:created_at $
:database_id (db-id)
:table_id (id :categories)
:query_type "query"})
(let [{:keys [id]} (post-card card-name)]
((user->client :rasta) :get 200 (format "card/%d" id)))))
......
......@@ -126,7 +126,10 @@
:display "scalar"
:visualization_settings {:global {:title nil}}
:public_perms 0
:created_at $})
:created_at $
:database_id (db-id)
:table_id (id :categories)
:query_type "query"})
:updated_at $
:col nil
:id $
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment