Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

7.7 KB
Raw Blame
(ns metabase-enterprise.database-routing.api
  (:require
   [metabase-enterprise.database-routing.db :as database-routing.db]
   [metabase.api.common :as api]
   [metabase.api.macros :as api.macros]
   [metabase.api.routes.common :refer [+auth]]
   [metabase.driver :as driver]
   [metabase.driver.util :as driver.u]
   [metabase.events.core :as events]
   [metabase.settings.core :as setting]
   [metabase.util :as u]
   [metabase.util.malli.schema :as ms]
   [metabase.warehouses.core :as warehouses]))

;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
;; of the REST API
;;
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-query-params-use-kebab-case
                      :metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/destination-database"
  "Create new Destination Databases.

  Note that unlike the normal `POST /api/database` endpoint, this endpoint does not test that the database is actually
  reachable before adding it — destination details are not required to be valid at creation time, and an
  unreachable destination is fine."
  [_route-params
   {:keys [check_connection_details]} :- [:map {:closed true}
                                          [:check_connection_details {:optional true} ms/MaybeBooleanValue]]
   {:keys [router_database_id destinations]} :- [:map {:closed true}
                                                 [:router_database_id ms/PositiveInt]
                                                 [:destinations
                                                  [:sequential
                                                   [:map {:closed true}
                                                    [:name               ms/NonBlankString]
                                                    [:details            ms/DatabaseDetails]]]]]]
  (api/check-400 (database-routing.db/router-exists? router_database_id))
  (api/check-400 (not (database-routing.db/destination-name-exists? router_database_id (map :name destinations)))
                 "A destination database with that name already exists.")
  (let [{:keys [engine auto_run_queries is_on_demand] :as router-db} (database-routing.db/database router_database_id)]
    (if-let [invalid-destinations (->> destinations
                                       (keep (fn [{details :details n :name}]
                                               (try
                                                 (driver/validate-db-details! engine details)
                                                 (when check_connection_details
                                                   (let [details-or-error (warehouses/test-connection-details engine details)]
                                                     (when (false? (:valid details-or-error))
                                                       [n (dissoc details-or-error :valid)])))
                                                 (catch Throwable e
                                                   [n {:message (ex-message e)}]))))
                                       seq)]
      {:status 400
       :body (into {} invalid-destinations)}
      (u/prog1 (database-routing.db/insert-databases!
                (map (fn [{:keys [name details]}]
                       {:name               name
                        :engine             engine
                        :details            details
                        :auto_run_queries   auto_run_queries
                        :is_full_sync       false
                        :is_on_demand       is_on_demand
                        :cache_ttl          nil
                        :router_database_id router_database_id
                        :creator_id         api/*current-user-id*})
                     destinations))
        (doseq [database <>]
          (events/publish-event! :event/database-create {:object database
                                                         :user-id api/*current-user-id*
                                                         :details {:slug name
                                                                   :primary_db_name (:name router-db)
                                                                   :primary_db_id (:id router-db)}}))))))

(defn- delete-router!
  [db-id]
  (let [db (database-routing.db/database db-id)]
    (events/publish-event! :event/database-update {:object db
                                                   :previous-object db
                                                   :user-id api/*current-user-id*
                                                   :details {:db_routing :disabled}})
    (database-routing.db/delete-router! db-id)))

(defn- create-or-update-router!
  [db-id user-attribute]
  (let [db (database-routing.db/database db-id)]
    (when-not (driver.u/supports? (:engine db) :database-routing db)
      (throw (ex-info "This database does not support DB routing" {:status-code 400})))
    (events/publish-event! :event/database-update {:object db
                                                   :previous-object db
                                                   :user-id api/*current-user-id*
                                                   :details {:db_routing :enabled
                                                             :routing_attribute user-attribute}})
    (if (database-routing.db/router-for-database db-id)
      (database-routing.db/update-router-user-attribute! db-id user-attribute)
      (database-routing.db/insert-router! db-id user-attribute))))

;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :put "/router-database/:id"
  "Updates an existing Database with the `user_attribute` to route on. Will either:
  - turn an existing Database into a Router database
  - change the `user_attribute` used to route for an existing Router database, or
  - turn a Router database into a regular Database
  depending on the value of `user_attribute`"
  [{:keys [id]} :- [:map {:closed true} [:id ms/PositiveInt]]
   _query-params
   {:keys [user_attribute]} :- [:map {:closed true} [:user_attribute {:optional true} [:maybe ms/NonBlankString]]]]
  (let [db (database-routing.db/database id)]
    (api/check-404 db)
    (api/check-400 (not (:router_database_id db)) "Cannot make a destination database a router database")
    (api/check-400 (not (:uploads_enabled db)) "Cannot enable database routing for a database with uploads enabled")
    (api/check-400 (not (:write_data_details db)) "Cannot enable database routing for a database with a write connection configured")
    (api/check-400 (not (database-routing.db/transform-exists-for-source-database? id)) "Cannot enable database routing for a database with transforms")
    (setting/with-database db
      (api/check-400 (not (setting/get :persist-models-enabled)) "Cannot enable database routing for a database with model persistence enabled")
      (api/check-400 (not (setting/get :database-enable-actions)) "Cannot enable database routing for a database with actions enabled")))
  (if (nil? user_attribute)
    ;; delete the DatabaseRouter
    (delete-router! id)
    (create-or-update-router! id user_attribute)))

(def ^{:arglists '([request respond raise])} routes
  "`/api/ee/database-routing` routes"
  (api.macros/ns-handler *ns* api/+check-superuser +auth))