diff --git a/src/metabase/api/meta/table.clj b/src/metabase/api/meta/table.clj index 256f468fb60aa31d3e38923af7a4ce93629791e5..f7a4e4d2fb61e372c0155d4aef67aa3d23a5f4bd 100644 --- a/src/metabase/api/meta/table.clj +++ b/src/metabase/api/meta/table.clj @@ -1,18 +1,26 @@ (ns metabase.api.meta.table "/api/meta/table endpoints." - (:require [compojure.core :refer [GET]] + (:require [compojure.core :refer [GET PUT]] [korma.core :refer :all] + [medley.core :as m] [metabase.api.common :refer :all] [metabase.db :refer :all] (metabase.models [hydrate :refer :all] [database :refer [Database]] [field :refer [Field]] - [table :refer [Table]]))) + [table :refer [Table]]) + [metabase.util :as u])) (defendpoint GET "/:id" [id] (->404 (sel :one Table :id id) (hydrate :db))) +(defendpoint PUT "/:id" [id :as {body :body}] + (write-check Table id) + (check-500 (->> (u/select-non-nil-keys body :entity_name :entity_type :description) + (m/mapply upd Table id))) + (sel :one Table :id id)) + (defendpoint GET "/:id/fields" [id] (sel :many Field :table_id id)) @@ -27,5 +35,4 @@ (-> (sel :many Table :db_id [in db-ids] (order :name :ASC)) (simple-batched-hydrate Database :db_id :db)))) - (define-routes) diff --git a/src/metabase/models/table.clj b/src/metabase/models/table.clj index 1a6def2497e1b1114f4e508a4588d77f93f566d7..021fed4f4b9241bde8f2125b0644d7c1e9f9ead2 100644 --- a/src/metabase/models/table.clj +++ b/src/metabase/models/table.clj @@ -3,23 +3,24 @@ [metabase.db :refer :all] (metabase.models [database :as db] [field :refer [Field]]) - [metabase.util :as util])) + [metabase.util :as u])) (defentity Table (table :metabase_table)) -(defmethod post-select Table [_ {:keys [id db db_id] :as table}] - (util/assoc* table - :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. - :can_read (delay @(:can_read @(:db <>))) - :can_write (delay @(:can_write @(:db <>))))) +(defmethod post-select Table [_ {:keys [id db db_id description] :as table}] + (u/assoc* table + :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. + :description (u/jdbc-clob->str description) + :can_read (delay @(:can_read @(:db <>))) + :can_write (delay @(:can_write @(:db <>))))) (defmethod pre-insert Table [_ table] (assoc table - :created_at (util/new-sql-timestamp) - :updated_at (util/new-sql-timestamp))) + :created_at (u/new-sql-timestamp) + :updated_at (u/new-sql-timestamp))) (defmethod pre-cascade-delete Table [_ {:keys [id] :as table}] (cascade-delete Field :table_id id)) diff --git a/test/metabase/api/meta/table_test.clj b/test/metabase/api/meta/table_test.clj index 1f17a8a565b119bd8c118f6e5396e372975429ea..4f4dab6a3d4fcc038a886b28c4a997961f730b5d 100644 --- a/test/metabase/api/meta/table_test.clj +++ b/test/metabase/api/meta/table_test.clj @@ -6,7 +6,7 @@ (metabase.models [field :refer [Field]] [table :refer [Table]]) [metabase.test-data :refer :all] - [metabase.test.util :refer [match-$]])) + [metabase.test.util :refer [match-$ expect-eval-actual-first]])) ;; ## GET /api/meta/table?org ;; These should come back in alphabetical order and include relevant metadata @@ -119,3 +119,34 @@ :db_id (:id @test-db) :created_at $}) ((user->client :rasta) :get 200 (format "meta/table/%d/query_metadata" (table->id :categories)))) + + +;; ## PUT /api/meta/table/:id +(expect-eval-actual-first + [(match-$ (sel :one Table :id (table->id :users)) + {:description "What a nice table!" + :entity_type "person" + :db (match-$ @test-db + {:description nil + :organization_id $ + :name "Test Database" + :updated_at $ + :details $ + :id $ + :engine "h2" + :created_at $}) + :name "USERS" + :rows 15 + :updated_at $ + :entity_name "Userz" + :active true + :id $ + :db_id @db-id + :created_at $}) + true] + [(do ((user->client :crowberto) :put 200 (format "meta/table/%d" (table->id :users)) {:entity_name "Userz" + :entity_type "person" + :description "What a nice table!"}) + ((user->client :crowberto) :get 200 (format "meta/table/%d" (table->id :users)))) + ;; Now reset the Table to it's original state + (upd Table (table->id :users) :entity_name nil :entity_type nil :description nil)])