The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.transforms-python.db
"Application database queries for the transforms-python module. Every function here is a direct Toucan 2 call with no
additional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."
(:require
[metabase.app-db.core :as mdb]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
(mu/defn table-database-ids
"The set of Database IDs of the Tables with `table-ids`."
[table-ids :- [:sequential ::lib.schema.id/table]]
(t2/select-fn-set :db_id [:model/Table :db_id] :id [:in table-ids]))
(mu/defn table-database-id
"The Database ID of the Table with `table-id`."
[table-id :- ::lib.schema.id/table]
(t2/select-one-fn :db_id (t2/table-name :model/Table) :id table-id))
(mu/defn database-engine
"The engine of the Database with `database-id`."
[database-id :- ::lib.schema.id/database]
(t2/select-one-fn :engine :model/Database :id [:auto/param database-id]))
(mu/defn update-run-message!
"Set the message of the TransformRun with `run-id`."
[run-id :- ms/PositiveInt
message :- [:maybe :string]]
(t2/update! :model/TransformRun :id run-id {:message message}))
(mu/defn python-library
"The PythonLibrary with `library-id`, or nil."
[library-id :- ms/PositiveInt]
(t2/select-one :model/PythonLibrary :id [:auto/param library-id]))
(mu/defn python-library-by-path
"The PythonLibrary at `path`, or nil."
[path :- :string]
(t2/select-one :model/PythonLibrary :path path))
(mu/defn upsert-python-library-source!
"Insert or update the PythonLibrary at `path`, setting its source to `source` and, on insert only, the columns in
`insert-values`. Returns the ID of the row."
[path :- :string
source :- :string
insert-values :- [:map {:closed true}
[:entity_id {:optional true} :string]
[:is_default {:optional true} :boolean]]]
(mdb/update-or-insert! :model/PythonLibrary
{:path path}
(fn [existing]
(cond-> {:path path :source source}
(nil? existing) (merge insert-values)))))
(mu/defn library-sources-by-path
"A map of path to source for every PythonLibrary."
[]
(t2/select-fn->fn :path :source :model/PythonLibrary))
(mu/defn top-level-fields-metadata
"The export metadata columns of the active top-level Fields of the Table with `table-id`, in database order."
[table-id :- ::lib.schema.id/table]
(t2/select [:model/Field :id :name :base_type :effective_type :semantic_type :database_type :database_position]
:table_id table-id
:active true
;; we are only interested in top-level objects, so filter out nested fields (parent or path)
:parent_id nil
:nfc_path nil
{:order-by [[:database_position :asc]]}))