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
Convert db.clj public functions to mu/defn with schemas (#82139)
Alexander Polyankinc1805286bb9f
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 6c68eefcd61..cd2030a6c58 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -146,6 +146,7 @@collectionsconfigevents+ libmodelspermissionsutil}@@ -158,7 +159,10 @@agent-api{:team "Metabot"- :api #{metabase.agent-api.api metabase.agent-api.init metabase.agent-api.usage}+ :api #{metabase.agent-api.api+ metabase.agent-api.init+ metabase.agent-api.schema+ metabase.agent-api.usage}:uses #{ai-tracingapiauth-identity@@ -222,6 +226,7 @@drivereid-translationinternal-stats+ liblib-bemodelspermissions@@ -291,6 +296,7 @@:uses #{api-scopeconfigevents+ libmodelssettingsutil}@@ -325,6 +331,7 @@:uses #{apiapp-dbevents+ libmodelspermissionsusers@@ -363,17 +370,17 @@{:team "UX West":api #{metabase.appearance.coremetabase.appearance.init}- :uses #{settings util}+ :uses #{lib settings util}:model-imports #{:model/Dashboard}}audit-app{:team "UX West"- :api #{metabase.audit-app.core- metabase.audit-app.init}+ :api #{metabase.audit-app.core metabase.audit-app.init}:friends #{enterprise/memoize-monitor}:uses #{apiapp-dbevents+ libmodelspremium-featuressettings@@ -407,14 +414,19 @@auth-identity{:team "UX West"- :api #{metabase.auth-identity.core metabase.auth-identity.db metabase.auth-identity.init}+ :api #{metabase.auth-identity.core+ metabase.auth-identity.db+ metabase.auth-identity.init+ metabase.auth-identity.schema}:uses #{channelevents+ liblogin-historymodelsnotificationpremium-featuresrequest+ usersutil}:model-exports #{:model/AuthIdentity}:model-imports #{:model/Session :model/User}}@@ -438,6 +450,7 @@:uses #{apiapp-dbcollections+ libpermissionsqueriesutil}@@ -464,9 +477,7 @@cache{:team "UX West"- :api #{metabase.cache.api- metabase.cache.core- metabase.cache.init}+ :api #{metabase.cache.api metabase.cache.core metabase.cache.init}:uses #{apiapp-dbconfig@@ -601,6 +612,7 @@audit-appconfigevents+ libmodelspermissionspremium-features@@ -633,6 +645,7 @@documentseid-translationevents+ liblib-bemodelsnotification@@ -665,6 +678,7 @@apichannelevents+ libmodelsrequestusers@@ -687,16 +701,16 @@content-translation{:team "UX West"- :api #{metabase.content-translation.models}+ :api #{metabase.content-translation.models metabase.content-translation.schema}:uses #{premium-featuresutil}:model-exports #{:model/ContentTranslation}}content-verification{:team "UX West"- :api #{metabase.content-verification.core- metabase.content-verification.init}+ :api #{metabase.content-verification.core metabase.content-verification.init}:uses #{app-db+ libmodelsutil}:model-exports #{:model/ModerationReview}@@ -809,6 +823,7 @@database-routingdriverevents+ librequestsyncutil@@ -837,6 +852,7 @@batch-processingcollectionsevents+ liblib-bemodelsparameters@@ -961,6 +977,7 @@{:team "Embedding":api #{metabase.embedding.initmetabase.embedding.jwt+ metabase.embedding.schemametabase.embedding.settingsmetabase.embedding.utilmetabase.embedding.validation}@@ -1004,7 +1021,7 @@{:team "Metabot":api #{metabase.entity-retrieval.coremetabase.entity-retrieval.mirror}- :uses #{premium-features}+ :uses #{premium-features util}:model-imports #{:model/OsiAiContext}}events@@ -1028,9 +1045,7 @@explorations{:team "UX West"- :api #{metabase.explorations.api- metabase.explorations.core- metabase.explorations.init}+ :api #{metabase.explorations.api metabase.explorations.core metabase.explorations.init}:uses #{analyticsanalytics-interfaceapi@@ -1103,6 +1118,7 @@:api #{metabase.glossary.api}:uses #{apievents+ libmodelsutil}:model-exports #{:model/Glossary}@@ -1127,8 +1143,7 @@indexed-entities{:team "UX West"- :api #{metabase.indexed-entities.api- metabase.indexed-entities.init}+ :api #{metabase.indexed-entities.api metabase.indexed-entities.init}:uses #{analyticsapidriver@@ -1150,6 +1165,7 @@metabase.indexes.reconcilemetabase.indexes.schema}:uses #{driver+ libmodelsutil}:model-exports #{:model/TableIndex}}@@ -1159,6 +1175,7 @@:api #{metabase.indexes-rest.api}:uses #{apiindexes+ libtransforms-baseutil}:model-imports #{:model/Database@@ -1178,8 +1195,7 @@internal-stats{:team "UX West":api #{metabase.internal-stats.core}- :uses #{app-db- models}+ :uses #{app-db models util}:model-imports #{:model/AiUsageLog:model/Card:model/Dashboard@@ -1316,13 +1332,12 @@login-history{:team "UX West"- :api #{metabase.login-history.api- metabase.login-history.core- metabase.login-history.init}+ :api #{metabase.login-history.api metabase.login-history.core metabase.login-history.init}:uses #{analyticsapiapp-dbchannel+ librequestsettingsutil}@@ -1334,6 +1349,7 @@metabase.mcp.callback-apimetabase.mcp.coremetabase.mcp.init+ metabase.mcp.schemametabase.mcp.usage}:model-exports #{:model/McpSessionLog:model/McpToolCallLog}@@ -1342,6 +1358,7 @@apiapp-dbconfig+ libllmmetabotoauth-server@@ -1355,7 +1372,7 @@measures{:team "Graphy"- :api #{metabase.measures.api}+ :api #{metabase.measures.api metabase.measures.schema}:uses #{apieventslib@@ -1376,6 +1393,7 @@metabase.metabot.coremetabase.metabot.initmetabase.metabot.metadata-perms+ metabase.metabot.schemametabase.metabot.selfmetabase.metabot.settingsmetabase.metabot.tools.entity-details}@@ -1473,9 +1491,7 @@model-persistence{:team "Graphy"- :api #{metabase.model-persistence.api- metabase.model-persistence.core- metabase.model-persistence.init}+ :api #{metabase.model-persistence.api metabase.model-persistence.core metabase.model-persistence.init}:uses #{apiapp-dbdriver@@ -1492,7 +1508,8 @@tasktask-historytracing- util}+ util+ warehouses}:model-exports #{:model/PersistedInfo}:model-imports #{:model/Card :model/Database}}@@ -1542,8 +1559,7 @@native-query-snippets{:team "Graphy"- :api #{metabase.native-query-snippets.api- metabase.native-query-snippets.core}+ :api #{metabase.native-query-snippets.api metabase.native-query-snippets.core}:uses #{apicollectionsevents@@ -1612,6 +1628,7 @@api-scopeappearanceevents+ libmcpmodelsrequest@@ -1682,6 +1699,7 @@remote-syncsettingstracing+ usersutilenterprise/advanced-permissions}:model-exports #{:model/ApplicationPermissionsRevision@@ -1776,9 +1794,7 @@:api #{metabase.public-sharing.coremetabase.public-sharing.initmetabase.public-sharing.validation}- :uses #{api- settings- util}+ :uses #{api settings util}:model-imports #{:model/Action:model/Card:model/Dashboard@@ -1811,9 +1827,7 @@pulse{:team "Gadget"- :api #{metabase.pulse.api- metabase.pulse.core- metabase.pulse.init}+ :api #{metabase.pulse.api metabase.pulse.core metabase.pulse.init}:uses #{apiapp-dbchannel@@ -2040,8 +2054,10 @@metabase.remote-sync.init}:uses #{apievents+ libmodels- premium-features}+ premium-features+ util}:model-imports #{:model/Collection}}request@@ -2055,7 +2071,8 @@apiconfigembedding- permissions ; FIXME+ lib+ permissionspremium-featuressessionsettings@@ -2107,10 +2124,12 @@:api #{metabase.sample-data.coremetabase.sample-data.init}:uses #{driver+ libpluginssettingssync- util}+ util+ warehouses}:model-imports #{:model/DataPermissions :model/Database :model/Table}}search@@ -2158,10 +2177,21 @@:model/Table:model/User}}+secrets{:team "Graphy":api #{metabase.secrets.core}- :uses #{api driver models premium-features util}}+ :uses #{api+ driver+ lib+ models+ premium-features+ util}}++ security-center+ {:team "Gadget"+ :api #{metabase.security-center.schema}+ :uses #{util}}segments{:team "Graphy"@@ -2199,6 +2229,7 @@driverembeddinginitialization-status+ liblib-bemcpoauth-server@@ -2228,6 +2259,7 @@channelconfigevents+ libpremium-featuresrequestsettings@@ -2246,6 +2278,7 @@app-dbconfigevents+ libmodelspremium-featuresutil@@ -2267,6 +2300,7 @@metabase.setup.init}:uses #{app-dbconfig+ libsettingsutil}:model-imports #{:model/User}}@@ -2279,6 +2313,7 @@appearanceauth-identityevents+ librequestsettingssetup@@ -2353,6 +2388,7 @@:uses #{apiauth-identityconfig+ libpermissionspremium-featuresrequest@@ -2433,11 +2469,10 @@task-history{:team "UX West"- :api #{metabase.task-history.api- metabase.task-history.core- metabase.task-history.init}+ :api #{metabase.task-history.api metabase.task-history.core metabase.task-history.init}:uses #{apiconfig+ libmodelspermissionspremium-features@@ -2469,9 +2504,11 @@lib-bellmmcp+ metabotpermissionspremium-featuressearch+ security-centersessionutil}:model-imports #{:model/AiUsageLog@@ -2502,12 +2539,12 @@timeline{:team "UX West"- :api #{metabase.timeline.api- metabase.timeline.core}+ :api #{metabase.timeline.api metabase.timeline.core}:uses #{analyticsapicollectionsevents+ libmodelsutil}:model-exports #{:model/Timeline}@@ -2610,6 +2647,7 @@metabase.transforms-rest.api.transform-jobmetabase.transforms-rest.api.transform-tag}:uses #{api+ libmodelsrequesttransforms@@ -2725,6 +2763,7 @@channelconfigevents+ libmodelsnotificationpermissions@@ -2751,6 +2790,7 @@collectionsconfigevents+ libmodelspermissionspremium-features@@ -2786,14 +2826,14 @@view-log{:team "UX West"- :api #{metabase.view-log.core- metabase.view-log.init}+ :api #{metabase.view-log.core metabase.view-log.init}:uses #{analyticsapiapp-dbaudit-appbatch-processingevents+ libmodelspremium-featuresquery-permissions@@ -2814,6 +2854,7 @@metabase.warehouse-schema.models.field-user-settingsmetabase.warehouse-schema.models.field-valuesmetabase.warehouse-schema.models.table+ metabase.warehouse-schema.schemametabase.warehouse-schema.table}:uses #{analyzeapi@@ -2879,7 +2920,8 @@{:team "UX West":api #{metabase.warehouses.coremetabase.warehouses.init- metabase.warehouses.models.database}+ metabase.warehouses.models.database+ metabase.warehouses.schema}:uses #{analytics-interfaceapiapp-db@@ -3008,7 +3050,8 @@setupsyncusers- util}+ util+ warehouses}:model-imports #{:model/ApiKey :model/Database :model/User}}enterprise/advanced-permissions@@ -3051,6 +3094,7 @@:api #{}:uses #{driverpremium-features+ utilenterprise/advanced-configenterprise/mfaenterprise/scim@@ -3085,6 +3129,7 @@permissionspluginspremium-features+ queriesquery-permissionsquery-processorsettings@@ -3093,6 +3138,8 @@syncusersutil+ warehouse-schema+ warehousesenterprise/serialization}:model-imports #{:model/AuditLog:model/Card@@ -3125,6 +3172,7 @@{:team "UX West":api #{metabase-enterprise.billing.api.routes}:uses #{api+ libpremium-featuresstore-apiutil}@@ -3134,6 +3182,7 @@{:team "UX West":api #{metabase-enterprise.cache.init}:uses #{cache+ libpremium-featuresquery-processortask@@ -3186,6 +3235,7 @@:api #{metabase-enterprise.content-verification.api.routes}:uses #{apicontent-verification+ libutilenterprise/api}:model-imports #{:model/Card :model/Dashboard}}@@ -3240,6 +3290,7 @@configdriverembeddings+ libmetabotmodelspremium-features@@ -3263,6 +3314,7 @@:uses #{apicollectionsevents+ libmodelspermissionspremium-features@@ -3280,6 +3332,7 @@metabase-enterprise.database-replication.init}:uses #{apidriver+ libpremium-featuressettingsutil@@ -3367,8 +3420,10 @@appearanceaudit-appembedding+ libpermissionspremium-features+ utilenterprise/sso}:model-imports #{:model/Card:model/Collection@@ -3396,6 +3451,7 @@collectionsentity-retrievalhealth-inspector+ libpremium-featuressearchtask@@ -3411,6 +3467,7 @@{:team "Gadget":api #{metabase-enterprise.erd.api}:uses #{api+ libmodelspermissionsutil@@ -3426,9 +3483,11 @@:uses #{analyticsanalytics-interfaceapi+ libpremium-featuressettingsutil+ warehousesenterprise/harbormaster}:model-imports #{:model/Database :model/Setting}}@@ -3466,13 +3525,16 @@:api #{metabase-enterprise.library.api}:uses #{apicollections- premium-features}+ lib+ premium-features+ util}:model-imports #{:model/Card :model/Collection :model/Table}}enterprise/mcp{:team "Metabot":api #{metabase-enterprise.mcp.init}:uses #{analytics+ libmcpmetabotpremium-features@@ -3504,6 +3566,7 @@mcpmetabot}:uses #{api+ liblib-bellmmetabot@@ -3527,6 +3590,7 @@:api #{metabase-enterprise.metabot-analytics.api}:uses #{apidriver+ libmetabotpermissionsquery-processor@@ -3547,6 +3611,7 @@channelconfigevents+ libpremium-featuresrequestsettings@@ -3581,6 +3646,7 @@app-dbcollectionsevents+ libmodelspremium-featuressearch@@ -3615,12 +3681,15 @@:uses #{analyticsapiapp-db+ dashboardseventsliblib-be+ measuresmodel-persistencemodelsqueries+ segmentssource-swaptasktransforms@@ -3640,8 +3709,7 @@enterprise/sandbox{:team "UX West"- :api #{metabase-enterprise.sandbox.api.routes- metabase-enterprise.sandbox.api.util}+ :api #{metabase-enterprise.sandbox.api.routes metabase-enterprise.sandbox.api.util}:uses #{apiapp-dbaudit-app@@ -3653,6 +3721,7 @@modelspermissionspremium-features+ queriesquery-processorrequesttenants@@ -3678,6 +3747,7 @@:uses #{analytics-interfaceapiapi-keys+ libmodelspermissionsserver@@ -3698,8 +3768,7 @@enterprise/security-center{:team "Gadget"- :api #{metabase-enterprise.security-center.api- metabase-enterprise.security-center.init}+ :api #{metabase-enterprise.security-center.api metabase-enterprise.security-center.init}:uses #{analyticsanalytics-interfaceapi@@ -3708,10 +3777,12 @@configdriverevents+ libmodelsnotificationpermissionspremium-features+ security-centersettingssystemtask@@ -3739,6 +3810,7 @@embeddingseventshealth-inspector+ liblib-bellmmetabot@@ -3783,7 +3855,8 @@enterprise/snippet-collections{:team "Graphy":api #{}- :uses #{models+ :uses #{lib+ modelsnative-query-snippetspermissionspremium-features@@ -3819,6 +3892,7 @@:uses #{analyticsapicollections+ libpremium-featuresqueriesrequest@@ -3835,11 +3909,13 @@app-dbauth-identityevents+ libmodelsrequestsettingssystemtask+ usersutil}:model-imports #{:model/AuthIdentity :model/Session :model/User}}@@ -3851,6 +3927,7 @@auth-identitycollectionsevents+ libmodelspermissionspremium-features@@ -3863,7 +3940,8 @@enterprise/transforms{:team "Graphy":api #{metabase-enterprise.transforms.api}- :uses #{premium-features+ :uses #{lib+ premium-featuresutilenterprise/transforms-inspector}:model-imports #{:model/Transform}}@@ -3920,6 +3998,7 @@{:team "UX West":api #{metabase-enterprise.upload-management.api}:uses #{api+ libmodelspermissionspremium-featuresdiff --git a/enterprise/backend/src/metabase_enterprise/action_v2/db.clj b/enterprise/backend/src/metabase_enterprise/action_v2/db.cljindex 84cc0182833..31a8a6567b7 100644--- a/enterprise/backend/src/metabase_enterprise/action_v2/db.clj+++ b/enterprise/backend/src/metabase_enterprise/action_v2/db.clj@@ -2,53 +2,59 @@"Application database queries for the action-v2 module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require+ [metabase-enterprise.action-v2.schema :as action-v2.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table table-id))-(defn active-table+(mu/defn active-table"The active Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id :active true))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn pk-fields-for-table+(mu/defn pk-fields-for-table"The active primary key Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :semantic_type :type/PK :active true))-(defn fields-by-name+(mu/defn fields-by-name"The Fields of the Table with `table-id` named one of `field-names`."- [table-id field-names]+ [table-id :- ::lib.schema.id/table+ field-names :- [:sequential :string]](t2/select :model/Field :table_id table-id :name [:in field-names]))-(defn active-fields-in-position-order+(mu/defn active-fields-in-position-order"The active Fields of the Table with `table-id`, in position order."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :active true {:order-by [[:position]]}))-(defn field-requirements-by-name+(mu/defn field-requirements-by-name"A map of name to the name, required flag, and base type of the Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn->fn :name identity [:model/Field :name :database_required :base_type] :table_id table-id))-(defn category-list-field-ids-by-name+(mu/defn category-list-field-ids-by-name"The `:id` and `:lower_name` rows of the category list Fields of the Table with `table-id` whose name matches oneof `names`, case-insensitively."- [table-id names]+ [table-id :- ::lib.schema.id/table+ names :- [:sequential :string]](t2/query {:select [:id [[:lower :name] :lower_name]]:from [(t2/table-name :model/Field)]:where [:and@@ -57,9 +63,9 @@[:in :has_field_values ["list" "auto-list"]][:= :semantic_type "type/Category"]]}))-(defn field-values-of-fields+(mu/defn field-values-of-fields"The value lists of the FieldValues of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select-fn-vec :values :model/FieldValues :field_id [:in field-ids]))(defn- scope-and-user-expr@@ -68,9 +74,11 @@(when scope [:= :scope scope])(when user-id [:= :user_id user-id])])-(defn next-undo-batch+(mu/defn next-undo-batch"The Undo rows of the newest not-undone (when `undo?`) or oldest undone batch of `user-id` in `scope`."- [undo? user-id scope]+ [undo? :- :boolean+ user-id :- ::lib.schema.id/user+ scope :- :string](t2/select :model/Undo:batch_num [:in^:allow-subquery@@ -83,10 +91,12 @@[:not :undone]:undone)]}]))-(defn batch-to-prune-from+(mu/defn batch-to-prune-from"The `:batch_num` row of the newest Undo batch beyond the `batches-to-keep` most recent ones, narrowed by theoptional `scope` and `user-id`, or nil."- [batches-to-keep scope user-id]+ [batches-to-keep :- ms/PositiveInt+ scope :- [:maybe :string]+ user-id :- [:maybe ::lib.schema.id/user]](t2/query {:select [:batch_num]:from [(t2/table-name :model/Undo)]:where (scope-and-user-expr scope user-id)@@ -95,56 +105,66 @@:limit 1:offset batches-to-keep}))-(defn batch-to-prune-from-for-rows+(mu/defn batch-to-prune-from-for-rows"The `:batch_num` row of the Undo row beyond the `rows-to-keep` most recent ones, or nil."- [rows-to-keep]+ [rows-to-keep :- ms/PositiveInt](t2/query {:select [:batch_num]:from [(t2/table-name :model/Undo)]:order-by [[:id :desc]]:limit 1:offset rows-to-keep}))-(defn delete-undo-batches-up-to!+(mu/defn delete-undo-batches-up-to!"Delete the Undo rows of batches up to `batch-num`, narrowed by the optional `scope` and `user-id`."- [batch-num scope user-id]+ [batch-num :- ms/IntGreaterThanOrEqualToZero+ scope :- [:maybe :string]+ user-id :- [:maybe ::lib.schema.id/user]](t2/delete! :model/Undo :batch_num [:<= batch-num] {:where (scope-and-user-expr scope user-id)}))-(defn delete-undone-batches-from!+(mu/defn delete-undone-batches-from!"Delete the undone Undo rows in `scope` from batch `batch-num` onwards."- [batch-num scope]+ [batch-num :- ms/PositiveInt+ scope :- :string](t2/delete! :model/Undo :batch_num [:>= batch-num] :scope scope :undone true))-(defn insert-undos!+(mu/defn insert-undos!"Insert the Undo `undos`."- [undos]+ [undos :- [:sequential+ ::action-v2.schema/undo.update]](t2/insert! :model/Undo undos))-(defn mark-batch-undone!+(mu/defn mark-batch-undone!"Set the undone flag of the Undo batch `batch-num` to `undone?`."- [batch-num undone?]+ [batch-num :- ms/PositiveInt+ undone? :- :boolean](t2/update! :model/Undo {:batch_num batch-num} {:undone undone?}))-(defn superseding-change-exists?+(mu/defn superseding-change-exists?"Whether a later (when `undo?`) or earlier Undo row for the rows `row-pks` of the Tables `table-ids` exists beyondbatch `batch-num` with the opposite undone state."- [undo? table-ids row-pks batch-num]+ [undo? :- :boolean+ table-ids :- [:set ::lib.schema.id/table]+ row-pks :- [:set :some]+ batch-num :- ms/PositiveInt](t2/exists? :model/Undo:table_id [:in table-ids]:row_pk [:in row-pks]:batch_num [(if undo? :> :<) batch-num]:undone (not undo?)))-(defn lock-sequence-next-val+(mu/defn lock-sequence-next-val"The next value of the raw sequence row named `sequence-name`, locked for update, or nil."- [sequence-name]+ [sequence-name :- :string](t2/select-one-fn :next_val [:sequences :next_val] :name sequence-name {:for :update}))-(defn set-sequence-next-val!+(mu/defn set-sequence-next-val!"Set the next value of the raw sequence row named `sequence-name`."- [sequence-name next-val]+ [sequence-name :- :string+ next-val :- :int](t2/update! :sequences {:name sequence-name} {:next_val next-val}))-(defn insert-sequence!+(mu/defn insert-sequence!"Insert a raw sequence row named `sequence-name` whose next value is `next-val`."- [sequence-name next-val]+ [sequence-name :- :string+ next-val :- :int](t2/insert! :sequences {:name sequence-name :next_val next-val}))diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/schema.clj b/enterprise/backend/src/metabase_enterprise/action_v2/schema.cljnew file mode 100644index 00000000000..c66e0f882ee--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/action_v2/schema.clj@@ -0,0 +1,49 @@+(ns metabase-enterprise.action-v2.schema+ "Malli schemas for the action-v2 module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::undo.row-pk+ "The `:row_pk` column of a Undo, decoded."+ :map)++(mr/def ::undo.raw-before+ "The `:raw_before` column of a Undo, decoded."+ :map)++(mr/def ::undo.raw-after+ "The `:raw_after` column of a Undo, decoded."+ :map)++(mr/def ::undo+ "A Undo as selected from the app DB: every column of `:data_edit_undo_chain`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:batch_num :int]+ [:table_id ::lib.schema.id/table]+ [:row_pk ::undo.row-pk]+ [:user_id ::lib.schema.id/user]+ [:scope [:or :keyword :string]]+ [:undoable :boolean]+ [:raw_before [:maybe ::undo.raw-before]]+ [:raw_after [:maybe ::undo.raw-after]]+ [:undone :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::undo.update+ "What an update (or insert) of a Undo accepts: every column of `:data_edit_undo_chain` except `id`, all optional."+ [:map {:closed true}+ [:batch_num {:optional true} [:maybe :int]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:row_pk {:optional true} [:maybe ::undo.row-pk]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:scope {:optional true} [:maybe [:or :keyword :string]]]+ [:undoable {:optional true} [:maybe :boolean]]+ [:raw_before {:optional true} [:maybe ::undo.raw-before]]+ [:raw_after {:optional true} [:maybe ::undo.raw-after]]+ [:undone {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/db.cljindex 797afb4ef81..62720d05919 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/db.clj@@ -2,12 +2,19 @@"Application database queries for the advanced-config module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase.api-keys.schema :as api-keys.schema][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.users.schema :as users.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn query-executions-in-month+(mu/defn query-executions-in-month"The raw query execution rows started in `month` of `year`, newest first."- [year month]+ [year :- ms/PositiveInt+ month :- ms/PositiveInt](let [date-part (fn [part-key part-value](if (= (mdb/db-type) :postgres)[:= [:date_part ^:allow-raw-sql [:inline (name part-key)] :started_at] part-value]@@ -18,62 +25,67 @@(date-part :year year)(date-part :month month)]})))-(defn api-key-by-name+(mu/defn api-key-by-name"The ApiKey named `api-key-name`, or nil."- [api-key-name]+ [api-key-name :- :string](t2/select-one :model/ApiKey :name api-key-name))-(defn api-key-prefix-exists?+(mu/defn api-key-prefix-exists?"Whether an ApiKey with `prefix` exists."- [prefix]+ [prefix :- :string](t2/exists? :model/ApiKey :key_prefix prefix))-(defn insert-api-key!- "Insert `api-key` and return the new instance."- [api-key]+(mu/defn insert-api-key!+ "Insert `api-key` and return the new instance. `::api-keys/unhashed-key` has a special meaning to the ApiKey+ model's before-insert hook: it is hashed into `:key` and used to derive `:key_prefix`."+ [api-key :- ::api-keys.schema/api-key.create](t2/insert-returning-instance! :model/ApiKey api-key))-(defn user-by-email+(mu/defn user-by-email"The User with `email`, or nil."- [email]+ [email :- :string](t2/select-one :model/User :email email))-(defn user-columns-by-email+(mu/defn user-columns-by-email"The `columns` of the User with `email`, or nil."- [columns email]+ [columns :- [:sequential :keyword]+ email :- :string](t2/select-one (into [:model/User] columns) :email email))-(defn insert-user!+(mu/defn insert-user!"Insert `user` and return the new instance."- [user]+ [user :- ::users.schema/user.update](t2/insert-returning-instance! :model/User user))-(defn update-user!+(mu/defn update-user!"Apply `changes` to the User with `user-id`."- [user-id changes]+ [user-id :- ::lib.schema.id/user+ changes :- ::users.schema/user.update](t2/update! :model/User user-id changes))-(defn sample-database-exists?+(mu/defn sample-database-exists?"Whether the sample Database exists."[](t2/exists? :model/Database :is_sample true))-(defn database-id-by-engine-and-name+(mu/defn database-id-by-engine-and-name"The ID of the Database of `engine` named `database-name`, or nil."- [engine database-name]+ [engine :- :string+ database-name :- :string](t2/select-one-pk :model/Database :engine engine :name database-name))-(defn insert-database!+(mu/defn insert-database!"Insert `database` and return the new instance."- [database]+ [database :- ::warehouses.schema/database.update](t2/insert-returning-instance! :model/Database database))-(defn update-database!+(mu/defn update-database!"Apply `changes` to the Database with `database-id`."- [database-id changes]+ [database-id :- ::lib.schema.id/database+ changes :- ::warehouses.schema/database.update](t2/update! :model/Database database-id changes))-(defn delete-database!+(mu/defn delete-database!"Delete the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/delete! :model/Database database-id))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.cljindex 499eebdc7c2..cd6ca6d57bc 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/db.clj@@ -2,22 +2,27 @@"Application database queries for the advanced-permissions module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn group-manager?+(mu/defn group-manager?"Whether the User with `user-id` manages the PermissionsGroup with `group-id`."- [user-id group-id]+ [user-id :- ::lib.schema.id/user+ group-id :- ms/PositiveInt](t2/select-one-fn :is_group_manager :model/PermissionsGroupMembership :user_id user-id :group_id group-id))-(defn blocked-database-ids-for-group+(mu/defn blocked-database-ids-for-group"The subset of `database-ids` the group with `group-id` is blocked from viewing."- [group-id database-ids]+ [group-id :- ms/PositiveInt+ database-ids :- [:sequential ::lib.schema.id/database]](t2/select-fn-set :db_id :model/DataPermissions:perm_type :perms/view-data:perm_value :blocked@@ -25,14 +30,16 @@:db_id [:in database-ids]{:select-distinct [:db_id]}))-(defn impersonated-database-ids-for-group+(mu/defn impersonated-database-ids-for-group"The subset of `database-ids` the group with `group-id` accesses through connection impersonation."- [group-id database-ids]+ [group-id :- ms/PositiveInt+ database-ids :- [:sequential ::lib.schema.id/database]](t2/select-fn-set :db_id :model/ConnectionImpersonation :group_id group-id :db_id [:in database-ids]))-(defn sandboxed-database-ids-for-group+(mu/defn sandboxed-database-ids-for-group"The `:db_id` rows of the Databases among `database-ids` the group with `group-id` has a sandbox on."- [group-id database-ids]+ [group-id :- ms/PositiveInt+ database-ids :- [:sequential ::lib.schema.id/database]](t2/query {:select [[:t.db_id :db_id]]:from [[(t2/table-name :model/Sandbox) :s]]:join [[(t2/table-name :model/Table) :t] [:= :s.table_id :t.id]]@@ -40,28 +47,29 @@[:= :s.group_id group-id][:in :t.db_id database-ids]]}))-(defn blocked-group-ids+(mu/defn blocked-group-ids"The subset of `group-ids` blocked from viewing some Database."- [group-ids]+ [group-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :group_id :model/DataPermissions:perm_type :perms/view-data:perm_value :blocked:group_id [:in group-ids]{:select-distinct [:group_id]}))-(defn impersonated-group-ids+(mu/defn impersonated-group-ids"The subset of `group-ids` with a connection impersonation."- [group-ids]+ [group-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :group_id :model/ConnectionImpersonation :group_id [:in group-ids]))-(defn sandboxed-group-ids+(mu/defn sandboxed-group-ids"The subset of `group-ids` with a sandbox."- [group-ids]+ [group-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :group_id :model/Sandbox :group_id [:in group-ids]))-(defn blocked-group-ids-for-database+(mu/defn blocked-group-ids-for-database"The subset of `group-ids` blocked from viewing the Database with `database-id`."- [database-id group-ids]+ [database-id :- ::lib.schema.id/database+ group-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :group_id :model/DataPermissions:db_id database-id:perm_type :perms/view-data@@ -69,9 +77,10 @@:group_id [:in group-ids]{:select-distinct [:group_id]}))-(defn sandboxed-group-ids-for-database+(mu/defn sandboxed-group-ids-for-database"The `:group_id` rows of the groups among `group-ids` with a sandbox on the Database with `database-id`."- [database-id group-ids]+ [database-id :- ::lib.schema.id/database+ group-ids :- [:sequential ms/PositiveInt]](t2/query {:select [[:s.group_id :group_id]]:from [[(t2/table-name :model/Sandbox) :s]]:join [[(t2/table-name :model/Table) :t] [:= :t.id :s.table_id]]@@ -79,7 +88,7 @@[:in :s.group_id group-ids][:= :t.db_id database-id]]}))-(defn application-permissions+(mu/defn application-permissions"The Permissions rows for the root object and every application object."[](t2/select :model/Permissions@@ -87,12 +96,12 @@[:= :object "/"][:like :object (h2x/literal "/application/%")]]}))-(defn user-group-memberships+(mu/defn user-group-memberships"The group ID (as `:id`) and manager flag of the memberships of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select [:model/PermissionsGroupMembership [:group_id :id] :is_group_manager] :user_id user-id))-(defn managed-group-ids+(mu/defn managed-group-ids"The IDs of the PermissionsGroups the User with `user-id` manages."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id user-id :is_group_manager true))diff --git a/enterprise/backend/src/metabase_enterprise/agent_api/db.clj b/enterprise/backend/src/metabase_enterprise/agent_api/db.cljindex 925968bf48b..d614d395e09 100644--- a/enterprise/backend/src/metabase_enterprise/agent_api/db.clj+++ b/enterprise/backend/src/metabase_enterprise/agent_api/db.clj@@ -2,14 +2,17 @@"Application database queries for the agent-api module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.agent-api.schema :as agent-api.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn insert-call-log!+(mu/defn insert-call-log!"Insert the AgentApiCallLog `row`."- [row]+ [row :- ::agent-api.schema/agent-api-call-log.update](t2/insert! :model/AgentApiCallLog row))-(defn delete-call-logs-created-before!+(mu/defn delete-call-logs-created-before!"Delete the AgentApiCallLogs created before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/AgentApiCallLog {:where [:< :created_at cutoff]}))diff --git a/enterprise/backend/src/metabase_enterprise/analytics/db.clj b/enterprise/backend/src/metabase_enterprise/analytics/db.cljindex 00a5a0fe0b6..ea9971d538f 100644--- a/enterprise/backend/src/metabase_enterprise/analytics/db.clj+++ b/enterprise/backend/src/metabase_enterprise/analytics/db.clj@@ -2,14 +2,15 @@"Application database queries for the analytics module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn database-with-engine-exists?+(mu/defn database-with-engine-exists?"Whether a Database whose engine is one of `engines` exists."- [engines]+ [engines :- [:sequential :keyword]](t2/exists? :model/Database :engine [:in engines]))-(defn sandbox-exists?+(mu/defn sandbox-exists?"Whether any Sandbox exists."[](t2/exists? :model/Sandbox))diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj b/enterprise/backend/src/metabase_enterprise/audit_app/audit.cljindex 3042232b89b..d2d7e173801 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj@@ -67,8 +67,8 @@;; one transaction so a crash can't commit the database row yet skip the permissions guard below —;; the next boot would see the database and no-op, leaving the stale permissions with no re-run path(t2/with-transaction [_conn]- (audit-app.db/insert-database! {:is_audit true- :id id+ (audit-app.db/insert-database! id+ {:is_audit true:name default-db-name:description "Internal Audit DB used to power metabase analytics.":engine enginediff --git a/enterprise/backend/src/metabase_enterprise/audit_app/db.clj b/enterprise/backend/src/metabase_enterprise/audit_app/db.cljindex b79713f2b2f..17b16d6d9a7 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/db.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/db.clj@@ -2,67 +2,76 @@"Application database queries for the audit-app module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for transactions and connections."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn audit-database+(mu/defn audit-database"The audit Database, or nil."[](t2/select-one :model/Database :is_audit true))-(defn non-audit-database-named+(mu/defn non-audit-database-named"The non-audit Database named `database-name`, or nil."- [database-name]+ [database-name :- :string](t2/select-one :model/Database :name database-name :is_audit false))-(defn insert-database!- "Insert the Database `row`."- [row]- (t2/insert! :model/Database row))+(mu/defn insert-database!+ "Insert the Database `row` with the fixed `id`."+ [id :- ::lib.schema.id/database+ row :- ::warehouses.schema/database.update]+ (t2/insert! :model/Database (assoc row :id id)))-(defn insert-returning-database!+(mu/defn insert-returning-database!"Insert the Database `row` and return the new instance."- [row]+ [row :- ::warehouses.schema/database.update](t2/insert-returning-instance! :model/Database row))-(defn set-database-engine!+(mu/defn set-database-engine!"Set the engine of the Database with `database-id`."- [database-id engine]+ [database-id :- ::lib.schema.id/database+ engine :- :string](t2/update! :model/Database database-id {:engine engine}))-(defn delete-database!+(mu/defn delete-database!"Delete the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/delete! :model/Database :id database-id))-(defn delete-audit-databases!+(mu/defn delete-audit-databases!"Delete every audit Database."[](t2/delete! :model/Database :is_audit true))-(defn delete-permissions-for-database!+(mu/defn delete-permissions-for-database!"Delete the Permissions rows on the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/delete! :model/Permissions {:where [:like :object (str "%/db/" database-id "/%")]}))-(defn tables-of-database-named+(mu/defn tables-of-database-named"The Tables of the Database with `database-id` named one of `table-names`."- [database-id table-names]+ [database-id :- ::lib.schema.id/database+ table-names :- [:or [:set :string] [:sequential :string]]](t2/select :model/Table :db_id database-id :name [:in table-names]))-(defn tables-of-database-in-id-order+(mu/defn tables-of-database-in-id-order"The `:id`, `:name`, `:schema`, and `:active` of the Tables of the Database with `database-id`, in ID order."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Table :id :name :schema :active] :db_id database-id {:order-by [[:id :asc]]}))-(defn active-public-table-exists?+(mu/defn active-public-table-exists?"Whether the Database with `database-id` has an active Table in the `public` schema."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Table :db_id database-id :schema "public" :active true))-(defn table-ids-to-downcase+(mu/defn table-ids-to-downcase"The IDs of the Metabase-managed Tables of the Database with `database-id` that have no lower-cased counterpartyet."- [database-id]+ [database-id :- ::lib.schema.id/database](mapv :id(t2/query {:select [:table.id]:from [[(t2/table-name :model/Table) :table]]@@ -82,15 +91,15 @@[:= :table.schema nil]]][:= :self_table.name [:lower :table.name]]]}]]]})))-(defn downcase-tables!+(mu/defn downcase-tables!"Move the Tables with `table-ids` to the `public` schema and lower-case their names."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/update! :model/Table :id [:in table-ids] {:schema "public" :name [:lower :name]}))-(defn field-ids-to-downcase+(mu/defn field-ids-to-downcase"The IDs of the Fields of the Metabase-managed Tables of the Database with `database-id` that have nolower-cased counterpart yet."- [database-id]+ [database-id :- ::lib.schema.id/database](mapv :id(t2/query {:select [:field.id]:from [[(t2/table-name :model/Field) :field]]@@ -112,24 +121,24 @@[:= :table.schema nil]]][:= :self_field.name [:lower :field.name]]]}]]]})))-(defn downcase-fields!+(mu/defn downcase-fields!"Lower-case the names of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/update! :model/Field :id [:in field-ids] {:name [:lower :name]}))-(defn clear-table-schemas!+(mu/defn clear-table-schemas!"Clear the schema of the Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/update! :model/Table {:db_id database-id} {:schema nil}))-(defn upcase-tables!+(mu/defn upcase-tables!"Upper-case the schemas and names of the Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/update! :model/Table {:db_id database-id} {:schema [:upper :schema] :name [:upper :name]}))-(defn upcase-fields!+(mu/defn upcase-fields!"Upper-case the names of the Fields of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/update! :model/Field{:table_id[:in@@ -138,74 +147,76 @@:where [:= :db_id database-id]}]}{:name [:upper :name]}))-(defn update-table!+(mu/defn update-table!"Apply `changes` to the Table with `table-id`."- [table-id changes]+ [table-id :- ::lib.schema.id/table+ changes :- (mut/select-keys ::warehouse-schema.schema/table.update [:active :name :schema :is_defective_duplicate])](t2/update! :model/Table table-id changes))-(defn delete-table!+(mu/defn delete-table!"Delete the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/delete! :model/Table table-id))-(defn field-names-of-table+(mu/defn field-names-of-table"The `:id` and `:name` of the Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select [:model/Field :id :name] :table_id table-id))-(defn card-result-metadata-reducible+(mu/defn card-result-metadata-reducible"Reducible raw `:id` and `:result_metadata` rows of the Cards of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-select [(t2/table-name :model/Card) :id :result_metadata] :database_id database-id))-(defn cards-of-table+(mu/defn cards-of-table"The Cards on the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Card :table_id table-id))-(defn table-ids-referenced-by-cards+(mu/defn table-ids-referenced-by-cards"The distinct `:table_id` rows of the Cards on the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/query {:select-distinct [:table_id]:from [(t2/table-name :model/Card)]:where [:in :table_id table-ids]}))-(defn update-card!+(mu/defn update-card!"Apply `changes` to the Card with `card-id`."- [card-id changes]+ [card-id :- ::lib.schema.id/card+ changes :- (mut/select-keys ::queries.schema/card.update [:dataset_query :result_metadata])](t2/update! :model/Card card-id changes))-(defn first-superuser+(mu/defn first-superuser"The `:id` and `:email` of the oldest superuser, or nil."[](t2/select-one [:model/User :id :email] :is_superuser true {:order-by [[:id :asc]]}))-(defn collection-by-entity-id+(mu/defn collection-by-entity-id"The Collection with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one :model/Collection :entity_id entity-id))-(defn delete-analytics-collections!+(mu/defn delete-analytics-collections!"Delete the Collections of the analytics namespace."[](t2/delete! :model/Collection :namespace "analytics"))-(defn delete-pulse-channel-recipients-of-user!+(mu/defn delete-pulse-channel-recipients-of-user!"Delete the PulseChannelRecipients of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/PulseChannelRecipient :user_id user-id))-(defn delete-notification-recipients-of-user!+(mu/defn delete-notification-recipients-of-user!"Delete the NotificationRecipients of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/NotificationRecipient :user_id user-id))-(defn archive-pulses-of-creator!+(mu/defn archive-pulses-of-creator!"Archive the unarchived Pulses created by the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/update! :model/Pulse {:creator_id user-id, :archived false} {:archived true}))-(defn deactivate-notifications-of-creator!+(mu/defn deactivate-notifications-of-creator!"Deactivate the active Notifications created by the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/update! :model/Notification {:creator_id user-id :active true} {:active false}))diff --git a/enterprise/backend/src/metabase_enterprise/billing/db.clj b/enterprise/backend/src/metabase_enterprise/billing/db.cljindex 5137a88d46b..51ee5ea0fbc 100644--- a/enterprise/backend/src/metabase_enterprise/billing/db.clj+++ b/enterprise/backend/src/metabase_enterprise/billing/db.clj@@ -2,14 +2,16 @@"Application database queries for the billing module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn active-personal-user-count+(mu/defn active-personal-user-count"The number of active personal Users."[](t2/count :model/User :is_active true :type :personal))-(defn user-email+(mu/defn user-email"The email of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :email :model/User :id user-id))diff --git a/enterprise/backend/src/metabase_enterprise/cache/db.clj b/enterprise/backend/src/metabase_enterprise/cache/db.cljindex ffd2d108b24..2bb3785343d 100644--- a/enterprise/backend/src/metabase_enterprise/cache/db.clj+++ b/enterprise/backend/src/metabase_enterprise/cache/db.clj@@ -2,12 +2,30 @@"Application database queries for the cache module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn card-cache-config+(def ^:private DurationScope+ "One `{:model :model-id :rerun-cutoff}` scope, as built by `duration-scopes`."+ [:map {:closed true}+ [:model :string]+ [:model-id ms/PositiveInt]+ [:rerun-cutoff ms/TemporalInstant]])++(def ^:private CacheConfigChanges+ "The keys callers pass to [[update-cache-config!]]."+ [:map {:closed true}+ [:next_run_at {:optional true} [:maybe ms/TemporalInstant]]+ [:invalidated_at {:optional true} ms/TemporalInstant]])++(mu/defn card-cache-config"The most specific CacheConfig applying to the Card with `card-id` on the Dashboard with `dashboard-id` in theDatabase with `database-id`, or nil."- [card-id dashboard-id database-id]+ [card-id :- ::lib.schema.id/card+ dashboard-id :- [:maybe ::lib.schema.id/dashboard]+ database-id :- [:maybe ::lib.schema.id/database]](let [qs (for [[i model model-id] [[1 "question" card-id][2 "dashboard" dashboard-id][3 "database" database-id]@@ -26,25 +44,27 @@:order-by :ordering:limit [:inline 1]})))-(defn cards-by-id+(mu/defn cards-by-id"A map of ID to Card for `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn identity :model/Card :id [:in card-ids]))-(defn router-database-ids+(mu/defn router-database-ids"The subset of `database-ids` that are database routers."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select-fn-set :database_id :model/DatabaseRouter :database_id [:in database-ids]))-(defn duration-cache-configs+(mu/defn duration-cache-configs"The duration CacheConfigs that refresh automatically."[](t2/select :model/CacheConfig :strategy :duration :refresh_automatically true))-(defn duration-queries-to-rerun+(mu/defn duration-queries-to-rerun"The query definitions to rerun for the duration cache `scopes`, each `{:model :model-id :rerun-cutoff}`, countingonly executions started after `started-after`; `parameterized?` selects parameterized or plain queries."- [scopes started-after parameterized?]+ [scopes :- [:sequential DurationScope]+ started-after :- ms/TemporalInstant+ parameterized? :- :boolean](t2/select:model/Query{:select [:u.query :u.cache-hash :u.card-id :u.dashboard-id :u.count]@@ -82,10 +102,11 @@:group-by [:q.query_hash :q.query :qc.query_hash :qe.card_id :qe.dashboard_id]}})}:u]]}))-(defn scheduled-base-query-to-rerun+(mu/defn scheduled-base-query-to-rerun"The unparameterized query definition of the Card with `card-id` executed most recently after `started-after`, ornil."- [card-id started-after]+ [card-id :- ::lib.schema.id/card+ started-after :- ms/TemporalInstant](t2/select-one :model/Query{:select [:q.query [:qe.card_id :card-id]]:from [[(t2/table-name :model/Query) :q]]@@ -99,9 +120,11 @@:order-by [[:qe.started_at :desc]]:limit 1}))-(defn scheduled-parameterized-queries-to-rerun+(mu/defn scheduled-parameterized-queries-to-rerun"The `limit` most common parameterized query definitions of the Card with `card-id` executed after `rerun-cutoff`."- [card-id rerun-cutoff limit]+ [card-id :- ::lib.schema.id/card+ rerun-cutoff :- ms/TemporalInstant+ limit :- ms/PositiveInt](t2/select :model/Query{:select [:q.query [:qe.card_id :card-id]]:from [[(t2/table-name :model/Query) :q]]@@ -119,26 +142,28 @@[[:min :qe.started_at] :asc]]:limit limit}))-(defn delete-query-caches!+(mu/defn delete-query-caches!"Delete the QueryCache entries with `query-hashes`."- [query-hashes]+ [query-hashes :- [:sequential bytes?]](t2/delete! :model/QueryCache :query_hash [:in query-hashes]))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn cache-configs-ready-to-run+(mu/defn cache-configs-ready-to-run"The CacheConfigs of `strategy` whose next run is unset or due at `now`."- [strategy now]+ [strategy :- :keyword+ now :- ms/TemporalInstant](t2/select :model/CacheConfig:strategy strategy{:where [:or[:= :next_run_at nil][:<= :next_run_at now]]}))-(defn update-cache-config!+(mu/defn update-cache-config!"Apply `changes` to the CacheConfig with `cache-config-id`."- [cache-config-id changes]+ [cache-config-id :- ms/PositiveInt+ changes :- CacheConfigChanges](t2/update! :model/CacheConfig {:id cache-config-id} changes))diff --git a/enterprise/backend/src/metabase_enterprise/content_translation/db.clj b/enterprise/backend/src/metabase_enterprise/content_translation/db.cljindex 552de869ae3..6457227e67f 100644--- a/enterprise/backend/src/metabase_enterprise/content_translation/db.clj+++ b/enterprise/backend/src/metabase_enterprise/content_translation/db.clj@@ -2,14 +2,17 @@"Application database queries for the content-translation module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [malli.util :as mut]+ [metabase.content-translation.schema :as content-translation.schema]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn delete-all-translations!- "Delete every ContentTranslation."+(mu/defn delete-all-translations!+ "Delete every ContentTranslation, returning the number deleted."[](t2/delete! :model/ContentTranslation))-(defn insert-translations!- "Insert the ContentTranslation `rows`."- [rows]+(mu/defn insert-translations!+ "Insert the ContentTranslation `rows`, returning the number inserted."+ [rows :- [:sequential (mut/select-keys ::content-translation.schema/content-translation.update [:locale :msgid :msgstr])]](t2/insert! :model/ContentTranslation rows))diff --git a/enterprise/backend/src/metabase_enterprise/content_verification/db.clj b/enterprise/backend/src/metabase_enterprise/content_verification/db.cljindex b16ef2f1784..4fbc10067db 100644--- a/enterprise/backend/src/metabase_enterprise/content_verification/db.clj+++ b/enterprise/backend/src/metabase_enterprise/content_verification/db.clj@@ -2,14 +2,16 @@"Application database queries for the content-verification module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn card-exists?+(mu/defn card-exists?"Whether a Card with `id` exists."- [id]+ [id :- ::lib.schema.id/card](t2/exists? :model/Card id))-(defn dashboard-exists?+(mu/defn dashboard-exists?"Whether a Dashboard with `id` exists."- [id]+ [id :- ::lib.schema.id/dashboard](t2/exists? :model/Dashboard id))diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/db.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/db.cljindex 2caee3f57ed..2c149b59f8c 100644--- a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/db.clj+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/db.clj@@ -2,6 +2,9 @@"Application database queries for the custom-viz-plugin module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [metabase-enterprise.custom-viz-plugin.schema :as custom-viz-plugin.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private non-blob-columns@@ -10,52 +13,53 @@:manifest :metabase_version :bundle_hash :dev_bundle_url:created_at :updated_at])-(defn non-blob-plugin+(mu/defn non-blob-plugin"The CustomVizPlugin with `plugin-id` without its bundle, or nil."- [plugin-id]+ [plugin-id :- ms/PositiveInt](t2/select-one non-blob-columns :id plugin-id))-(defn non-blob-plugins+(mu/defn non-blob-plugins"Every CustomVizPlugin without its bundle, ordered by display name."[](t2/select non-blob-columns {:order-by [[:display_name :asc]]}))-(defn active-enabled-non-blob-plugins+(mu/defn active-enabled-non-blob-plugins"The active, enabled CustomVizPlugins without their bundle, ordered by display name."[](t2/select non-blob-columns :status :active :enabled true {:order-by [[:display_name :asc]]}))-(defn plugin-by-identifier+(mu/defn plugin-by-identifier"The CustomVizPlugin with `identifier`, or nil."- [identifier]+ [identifier :- :string](t2/select-one :model/CustomVizPlugin :identifier identifier))-(defn plugin-identifier-exists?+(mu/defn plugin-identifier-exists?"Whether a CustomVizPlugin with `identifier` exists."- [identifier]+ [identifier :- :string](t2/exists? :model/CustomVizPlugin :identifier identifier))-(defn plugin-bundle+(mu/defn plugin-bundle"The bundle bytes of the CustomVizPlugin with `plugin-id`."- [plugin-id]+ [plugin-id :- ms/PositiveInt](t2/select-one-fn :bundle :model/CustomVizPlugin :id plugin-id))-(defn plugin-dev-bundle-url+(mu/defn plugin-dev-bundle-url"The dev bundle URL of the CustomVizPlugin with `plugin-id`."- [plugin-id]+ [plugin-id :- ms/PositiveInt](t2/select-one-fn :dev_bundle_url :model/CustomVizPlugin :id plugin-id))-(defn insert-plugin!+(mu/defn insert-plugin!"Insert `plugin` and return the new instance."- [plugin]+ [plugin :- ::custom-viz-plugin.schema/custom-viz-plugin.update](t2/insert-returning-instance! :model/CustomVizPlugin plugin))-(defn update-plugin!- "Apply `changes` to the CustomVizPlugin with `plugin-id`."- [plugin-id changes]+(mu/defn update-plugin!+ "Apply `changes` to the CustomVizPlugin with `plugin-id`, returning the number updated."+ [plugin-id :- ms/PositiveInt+ changes :- ::custom-viz-plugin.schema/custom-viz-plugin.update](t2/update! :model/CustomVizPlugin plugin-id changes))-(defn delete-plugin!- "Delete the CustomVizPlugin with `plugin-id`."- [plugin-id]+(mu/defn delete-plugin!+ "Delete the CustomVizPlugin with `plugin-id`, returning the number deleted."+ [plugin-id :- ms/PositiveInt](t2/delete! :model/CustomVizPlugin :id plugin-id))diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/schema.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/schema.cljnew file mode 100644index 00000000000..c41139b9e7d--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/schema.clj@@ -0,0 +1,44 @@+(ns metabase-enterprise.custom-viz-plugin.schema+ "Malli schemas for the custom-viz-plugin module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::custom-viz-plugin.manifest+ "The `:manifest` column of a CustomVizPlugin, decoded."+ :map)++(mr/def ::custom-viz-plugin+ "A CustomVizPlugin as selected from the app DB: every column of `:custom_viz_plugin`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:identifier :string]+ [:display_name :string]+ [:status [:or :keyword :string]]+ [:error_message [:maybe :string]]+ [:bundle [:maybe [:or bytes? :string]]]+ [:bundle_hash [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:enabled :boolean]+ [:icon [:maybe :string]]+ [:manifest [:maybe ::custom-viz-plugin.manifest]]+ [:metabase_version [:maybe :string]]+ [:dev_bundle_url [:maybe :string]]])++(mr/def ::custom-viz-plugin.update+ "What an update (or insert) of a CustomVizPlugin accepts: every column of `:custom_viz_plugin` except `id`, all optional."+ [:map {:closed true}+ [:identifier {:optional true} [:maybe :string]]+ [:display_name {:optional true} [:maybe :string]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:error_message {:optional true} [:maybe :string]]+ [:bundle {:optional true} [:maybe [:or bytes? :string]]]+ [:bundle_hash {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:enabled {:optional true} [:maybe :boolean]]+ [:icon {:optional true} [:maybe :string]]+ [:manifest {:optional true} [:maybe ::custom-viz-plugin.manifest]]+ [:metabase_version {:optional true} [:maybe :string]]+ [:dev_bundle_url {:optional true} [:maybe :string]]])diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.cljindex 1bf071a4a13..d1e7b51d99f 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj@@ -2,6 +2,9 @@"Application database queries for the data-apps module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [metabase-enterprise.data-apps.schema :as data-apps.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private non-blob-columns@@ -10,71 +13,73 @@:bundle_hash :last_synced_sha :last_synced_at :sync_error:created_at :updated_at])-(defn non-blob-data-app+(mu/defn non-blob-data-app"The DataApp with `data-app-id` without its bundle, or nil."- [data-app-id]+ [data-app-id :- ms/PositiveInt](t2/select-one non-blob-columns :id data-app-id))-(defn non-blob-data-app-by-slug+(mu/defn non-blob-data-app-by-slug"The DataApp named `slug` without its bundle, or nil."- [slug]+ [slug :- :string](t2/select-one non-blob-columns :name slug))-(defn enabled-non-blob-data-app-by-slug+(mu/defn enabled-non-blob-data-app-by-slug"The enabled DataApp named `slug` without its bundle, or nil."- [slug]+ [slug :- :string](t2/select-one non-blob-columns :name slug :enabled true))-(defn non-blob-data-apps+(mu/defn non-blob-data-apps"Every DataApp without its bundle, ordered by display name; only the enabled, error-free ones when `available?`."- [available?]+ [available? :- [:maybe :boolean]](t2/select non-blob-columns(cond-> {:order-by [[:display_name :asc]]}available? (assoc :where [:and[:= :enabled true][:= :sync_error nil]]))))-(defn data-app-bundle+(mu/defn data-app-bundle"The bundle bytes of the DataApp with `data-app-id`."- [data-app-id]+ [data-app-id :- ms/PositiveInt](t2/select-one-fn :bundle :model/DataApp :id data-app-id))-(defn data-apps-sync-info+(mu/defn data-apps-sync-info"The sync-relevant columns of every DataApp."[](t2/select [:model/DataApp :name :display_name :description :allowed_hosts :bundle_path :bundle_hash :sync_error]))-(defn data-app-exists?+(mu/defn data-app-exists?"Whether a DataApp named `slug` exists."- [slug]+ [slug :- :string](t2/exists? :model/DataApp :name slug))-(defn insert-data-app!+(mu/defn insert-data-app!"Insert the DataApp `row`."- [row]+ [row :- ::data-apps.schema/data-app.update](t2/insert! :model/DataApp row))-(defn update-data-app!+(mu/defn update-data-app!"Apply `changes` to the DataApp with `data-app-id`."- [data-app-id changes]+ [data-app-id :- ms/PositiveInt+ changes :- ::data-apps.schema/data-app.update](t2/update! :model/DataApp :id data-app-id changes))-(defn update-data-app-by-slug!+(mu/defn update-data-app-by-slug!"Apply `changes` to the DataApp named `slug`."- [slug changes]+ [slug :- :string+ changes :- ::data-apps.schema/data-app.update](t2/update! :model/DataApp :name slug changes))-(defn delete-data-app-by-slug!+(mu/defn delete-data-app-by-slug!"Delete the DataApp named `slug`, returning the number deleted."- [slug]+ [slug :- :string](t2/delete! :model/DataApp :name slug))-(defn delete-data-apps-not-named!+(mu/defn delete-data-apps-not-named!"Delete the DataApps whose name is not one of `slugs`, returning the number deleted."- [slugs]+ [slugs :- [:set :string]](t2/delete! :model/DataApp :name [:not-in slugs]))-(defn delete-all-data-apps!+(mu/defn delete-all-data-apps!"Delete every DataApp, returning the number deleted."[](t2/delete! :model/DataApp))diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.cljindex f97919e9d5f..0524fc834c9 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj@@ -31,6 +31,24 @@(derive :metabase/model)(derive :hook/timestamped?))+(defn- default-last-synced-at+ "A sync that records `last_synced_sha` happened now unless it says otherwise."+ [row]+ (cond-> row+ (and (contains? row :last_synced_sha) (not (contains? row :last_synced_at)))+ (assoc :last_synced_at (mi/now))))++(t2/define-before-insert :model/DataApp+ [data-app]+ (default-last-synced-at data-app))++(t2/define-before-update :model/DataApp+ [data-app]+ (let [changes (t2/changes data-app)]+ (cond-> data-app+ (and (contains? changes :last_synced_sha) (not (contains? changes :last_synced_at)))+ (assoc :last_synced_at (mi/now)))))+;; Reads always see `allowed_hosts` as a vector, never nil — a row synced before;; the column existed has NULL until it's re-synced. Guard on `contains?` so;; selects that don't fetch the column (e.g. `select-one-fn :bundle`) are leftdiff --git a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj b/enterprise/backend/src/metabase_enterprise/data_apps/schema.cljnew file mode 100644index 00000000000..2dcbb2249f0--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj@@ -0,0 +1,40 @@+(ns metabase-enterprise.data-apps.schema+ "Malli schemas for the data-apps module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::data-app+ "A DataApp as selected from the app DB: every column of `:data_app`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:display_name :string]+ [:allowed_hosts [:maybe [:sequential :string]]]+ [:bundle_path :string]+ [:bundle [:maybe [:or bytes? :string]]]+ [:bundle_hash [:maybe :string]]+ [:last_synced_sha [:maybe :string]]+ [:last_synced_at [:maybe ms/TemporalInstant]]+ [:sync_error [:maybe :string]]+ [:enabled :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:description [:maybe :string]]])++(mr/def ::data-app.update+ "What an update (or insert) of a DataApp accepts: every column of `:data_app` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:display_name {:optional true} [:maybe :string]]+ [:allowed_hosts {:optional true} [:maybe [:sequential :string]]]+ [:bundle_path {:optional true} [:maybe :string]]+ [:bundle {:optional true} [:maybe [:or bytes? :string]]]+ [:bundle_hash {:optional true} [:maybe :string]]+ [:last_synced_sha {:optional true} [:maybe :string]]+ [:last_synced_at {:optional true} [:maybe ms/TemporalInstant]]+ [:sync_error {:optional true} [:maybe :string]]+ [:enabled {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:description {:optional true} [:maybe :string]]])diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/sync.clj b/enterprise/backend/src/metabase_enterprise/data_apps/sync.cljindex 44f04edfcc1..d92d9263cbc 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/sync.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/sync.clj@@ -150,7 +150,6 @@(upsert-by-name! slug (assoc fields:bundle bytes:last_synced_sha sha- :last_synced_at :%now:sync_error nil))(app-content-changed? existing fields)))(catch Throwable ediff --git a/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj b/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.cljindex 64bbc4bf3b4..845940c2782 100644--- a/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj+++ b/enterprise/backend/src/metabase_enterprise/data_complexity_score/db.clj@@ -2,13 +2,19 @@"Application database queries for the data-complexity-score module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [java-time.api :as t]+ [malli.util :as mut]+ [metabase-enterprise.data-complexity-score.schema :as data-complexity-score.schema][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn active-field-counts-by-table+(mu/defn active-field-counts-by-table"Rows of `:table_id` and `:field_count` of active Fields for `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/query {:select [:table_id [:%count.* :field_count]]:from [:metabase_field]:where [:and@@ -16,17 +22,17 @@[:in :table_id table-ids]]:group-by [:table_id]}))-(defn unarchived-measure-names+(mu/defn unarchived-measure-names"The Table ID and name of the unarchived Measures on the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Measure :table_id :name] :archived false :table_id [:in table-ids]))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id collection-id))-(defn verified-card-ids+(mu/defn verified-card-ids"The IDs of the Cards whose most recent moderation review is verified."[](t2/select-fn-set :moderated_item_id :model/ModerationReview@@ -34,63 +40,66 @@:most_recent true:status "verified"))-(defn official-collection-ids+(mu/defn official-collection-ids"The IDs of the official Collections."[](t2/select-fn-set :id :model/Collection :authority_level "official"))-(defn routed-child-database-ids+(mu/defn routed-child-database-ids"The IDs of the Databases that are routing destinations."[](t2/select-fn-set :id :model/Database :router_database_id [:not= nil]))-(defn universe-cards+(mu/defn universe-cards"The ID, name, type, and Collection of the unarchived metric and model Cards outside the Database with`audit-database-id`."- [audit-database-id]+ [audit-database-id :- ::lib.schema.id/database](t2/select [:model/Card :id :name :type :collection_id :card_schema]:type [:in ["metric" "model"]]:archived false:database_id [:not= audit-database-id]))-(defn universe-tables+(mu/defn universe-tables"The scoring columns of the active Tables outside the Database with `audit-database-id`."- [audit-database-id]+ [audit-database-id :- ::lib.schema.id/database](t2/select [:model/Table :id :name :collection_id :is_published :visibility_type :db_id :data_layer :data_authority]:active true:db_id [:not= audit-database-id]))-(defn metabot-by-entity-id+(mu/defn metabot-by-entity-id"The Metabot with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one :model/Metabot :entity_id entity-id))-(defn latest-score-entry+(mu/defn latest-score-entry"The most recent DataComplexityScore of `source` for `fingerprint`, or nil."- [fingerprint source]+ [fingerprint :- :string+ source :- :string](t2/select-one :model/DataComplexityScore :fingerprint fingerprint :source source {:order-by [[:id :desc]]}))-(defn score-entry+(mu/defn score-entry"The DataComplexityScore with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/DataComplexityScore :id id))-(defn scored-within-hours?+(mu/defn scored-within-hours?"Whether a DataComplexityScore of `source` for `fingerprint` was created within the last `hours` hours ofdatabase time."- [fingerprint source hours]+ [fingerprint :- :string+ source :- :string+ hours :- number?](t2/exists? :model/DataComplexityScore{:where [:and[:= :fingerprint fingerprint][:= :source source][:>= :created_at (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- hours) :hour)]]}))-(defn insert-score!+(mu/defn insert-score!"Insert `score-entry` and return its ID."- [score-entry]+ [score-entry :- (mut/select-keys ::data-complexity-score.schema/data-complexity-score.update [:fingerprint :source :score_data])](t2/insert-returning-pk! :model/DataComplexityScore score-entry))-(defn delete-scores-created-before!- "Delete the DataComplexityScores created before `cutoff`, returning the number deleted."- [cutoff]- (t2/delete! :model/DataComplexityScore {:where [:< :created_at cutoff]}))+(mu/defn delete-scores-older-than!+ "Delete the DataComplexityScores created more than `months` months ago, returning the number deleted."+ [months :- ms/PositiveInt]+ (t2/delete! :model/DataComplexityScore {:where [:< :created_at (t/minus (t/offset-date-time) (t/months months))]}))diff --git a/enterprise/backend/src/metabase_enterprise/data_complexity_score/schema.clj b/enterprise/backend/src/metabase_enterprise/data_complexity_score/schema.cljnew file mode 100644index 00000000000..f0e6b0df627--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/data_complexity_score/schema.clj@@ -0,0 +1,26 @@+(ns metabase-enterprise.data-complexity-score.schema+ "Malli schemas for the data-complexity-score module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::data-complexity-score.score-data+ "The `:score_data` column of a DataComplexityScore, decoded."+ :map)++(mr/def ::data-complexity-score+ "A DataComplexityScore as selected from the app DB: every column of `:data_complexity_score`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:fingerprint :string]+ [:score_data ::data-complexity-score.score-data]+ [:created_at ms/TemporalInstant]+ [:source [:or :keyword :string]]])++(mr/def ::data-complexity-score.update+ "What an update (or insert) of a DataComplexityScore accepts: every column of `:data_complexity_score` except `id`, all optional."+ [:map {:closed true}+ [:fingerprint {:optional true} [:maybe :string]]+ [:score_data {:optional true} [:maybe ::data-complexity-score.score-data]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:source {:optional true} [:maybe [:or :keyword :string]]]])diff --git a/enterprise/backend/src/metabase_enterprise/data_complexity_score/task/complexity_score_trimmer.clj b/enterprise/backend/src/metabase_enterprise/data_complexity_score/task/complexity_score_trimmer.cljindex 61fbcaa7f26..ecf691428e0 100644--- a/enterprise/backend/src/metabase_enterprise/data_complexity_score/task/complexity_score_trimmer.clj+++ b/enterprise/backend/src/metabase_enterprise/data_complexity_score/task/complexity_score_trimmer.clj@@ -4,12 +4,10 @@[clojurewerkz.quartzite.jobs :as jobs][clojurewerkz.quartzite.schedule.cron :as cron][clojurewerkz.quartzite.triggers :as triggers]- [java-time.api :as t][metabase-enterprise.data-complexity-score.db :as data-complexity-score.db][metabase.task.core :as task][metabase.util.log :as log])(:import- (java.sql Timestamp)(org.quartz DisallowConcurrentExecution)))(set! *warn-on-reflection* true)@@ -22,16 +20,10 @@(def ^:private retention-months 3)-(defn- retention-cutoff-timestamp- [months]- (Timestamp/valueOf ^java.time.LocalDateTime- (t/minus (t/local-date-time) (t/months months))))-(defn- trim-old-complexity-score-data- (let [cutoff (retention-cutoff-timestamp retention-months)- deleted (data-complexity-score.db/delete-scores-created-before! cutoff)]+ (let [deleted (data-complexity-score.db/delete-scores-older-than! retention-months)](log/infof "Data Complexity Score cleanup complete. Deleted %d rows." (or deleted 0))))(task/defjob ^{DisallowConcurrentExecution truediff --git a/enterprise/backend/src/metabase_enterprise/data_studio/db.clj b/enterprise/backend/src/metabase_enterprise/data_studio/db.cljindex 2362eb4ffcc..ba8d8ea1a7d 100644--- a/enterprise/backend/src/metabase_enterprise/data_studio/db.clj+++ b/enterprise/backend/src/metabase_enterprise/data_studio/db.clj@@ -4,8 +4,17 @@(:require[clojure.string :as str][metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))+(def ^:private TableSelectors+ [:map {:closed true}+ [:database-ids {:optional true} [:maybe [:or [:set ::lib.schema.id/database] [:sequential ::lib.schema.id/database]]]]+ [:table-ids {:optional true} [:maybe [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]]+ [:schema-ids {:optional true} [:maybe [:or [:set :string] [:sequential :string]]]]])+(defn- table-selectors-expr"Matches the Tables selected by `database-ids`, `table-ids`, and `schema-ids` (each `\"<db-id>:<schema>\"`)."[{:keys [database-ids table-ids schema-ids]}]@@ -21,11 +30,13 @@[selectors]^:allow-subquery {:select [:id] :from [(t2/table-name :model/Table)] :where (table-selectors-expr selectors)})-(defn remapped-table-ids-reducible+(mu/defn remapped-table-ids-reducible"Reducible `:table_id` rows of the Tables reachable from `tables` through FK remapping Dimensions, from the`input-field` side to the `output-field` side (`:source_field` or `:target_field`), excluding `tables` themselves.`tables` is a set of Table IDs or a selectors map (see [[table-ids-matching-selectors]])."- [input-field output-field tables]+ [input-field :- :keyword+ output-field :- :keyword+ tables :- [:or [:set ms/PositiveInt] TableSelectors]](let [input-table-id (keyword (name input-field) "table_id")output-table-id (keyword (name output-field) "table_id")table-ids (if (map? tables)@@ -48,10 +59,12 @@[:in input-table-id table-ids]not-in-tables]})))-(defn table-ids-matching-selectors+(mu/defn table-ids-matching-selectors"The IDs of the Tables selected by `selectors` (`{:database-ids :table-ids :schema-ids}`) plus, when given, the`extra-table-ids` that are unpublished (`:unpublished` mode) or any of them (`:any` mode)."- [selectors extra-table-ids extra-mode]+ [selectors :- TableSelectors+ extra-table-ids :- [:maybe [:set ::lib.schema.id/table]]+ extra-mode :- [:enum :unpublished :any]](t2/select-pks-set :model/Table{:where (let [selector-expr (table-selectors-expr selectors)](if (seq extra-table-ids)@@ -60,48 +73,51 @@:any [:in :id extra-table-ids])]selector-expr))}))-(defn published-table-ids+(mu/defn published-table-ids"The IDs of the published Tables among `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-pks-set :model/Table :id [:in table-ids] :is_published true))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection collection-id))-(defn latest-table-publishing-event+(mu/defn latest-table-publishing-event"The most recent publish or unpublish AuditLog event for `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/AuditLog :timestamp :topic :user_id]:topic [:in [:table-publish :table-unpublish]]:model "Table":model_id table-id{:order-by [[:timestamp :desc] [:id :desc]]}))-(defn user-name-and-email+(mu/defn user-name-and-email"The id, first name, last name, and email of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :first_name :last_name :email] user-id))-(defn publish-tables!- "Publish the Tables with `table-ids` into the Collection with `collection-id`."- [table-ids collection-id]+(mu/defn publish-tables!+ "Publish the Tables with `table-ids` into the Collection with `collection-id`, returning the number updated."+ [table-ids :- [:set ::lib.schema.id/table]+ collection-id :- ::lib.schema.id/collection](t2/update! :model/Table :id [:in table-ids] {:collection_id collection-id, :is_published true}))-(defn unpublish-tables!- "Unpublish the Tables with `table-ids` and detach them from their Collection."- [table-ids]+(mu/defn unpublish-tables!+ "Unpublish the Tables with `table-ids` and detach them from their Collection, returning the number updated."+ [table-ids :- [:set ::lib.schema.id/table]](t2/update! :model/Table :id [:in table-ids] {:collection_id nil, :is_published false}))-(defn published-table-visible-to-user?+(mu/defn published-table-visible-to-user?"Whether the Table with `table-id` is published in a Collection the User with `user-id` can read."- [table-id user-id superuser?]+ [table-id :- ::lib.schema.id/table+ user-id :- ::lib.schema.id/user+ superuser? :- :boolean](t2/exists? :model/Table{:where [:and[:= :id table-id]@@ -110,7 +126,7 @@:collection_id {} {:current-user-id user-id:is-superuser? superuser?})]}))-(defn any-published-table-visible?+(mu/defn any-published-table-visible?"Whether the current user can read the Collection of any published Table."[](t2/exists? :model/Table@@ -118,9 +134,9 @@[:= :is_published true](collection/visible-collection-filter-clause :collection_id)]}))-(defn published-table-visible-in-database?+(mu/defn published-table-visible-in-database?"Whether the current user can read the Collection of any published Table in the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Table{:where [:and[:= :db_id database-id]diff --git a/enterprise/backend/src/metabase_enterprise/database_replication/db.clj b/enterprise/backend/src/metabase_enterprise/database_replication/db.cljindex 380bd6d14ef..c01168868d7 100644--- a/enterprise/backend/src/metabase_enterprise/database_replication/db.clj+++ b/enterprise/backend/src/metabase_enterprise/database_replication/db.clj@@ -2,9 +2,11 @@"Application database queries for the database-replication module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/db.clj b/enterprise/backend/src/metabase_enterprise/database_routing/db.cljindex 26163cc3020..d9b401444ff 100644--- a/enterprise/backend/src/metabase_enterprise/database_routing/db.clj+++ b/enterprise/backend/src/metabase_enterprise/database_routing/db.clj@@ -2,74 +2,82 @@"Application database queries for the database-routing module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn database-name+(mu/defn database-name"The name of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :name :model/Database database-id))-(defn destination-database?+(mu/defn destination-database?"Whether the Database with `database-id` is a routing destination."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id :router_database_id [:not= nil]))-(defn destination-database-id+(mu/defn destination-database-id"The ID of the destination Database of the router with `router-database-id` named `database-name`, or nil."- [router-database-id database-name]+ [router-database-id :- ::lib.schema.id/database+ database-name :- :string](t2/select-one-pk :model/Database :router_database_id router-database-id :name database-name))-(defn destination-name-exists?+(mu/defn destination-name-exists?"Whether the router with `router-database-id` already has a destination Database named one of `names`."- [router-database-id names]+ [router-database-id :- ::lib.schema.id/database+ names :- [:sequential :string]](t2/exists? :model/Database :router_database_id router-database-id :name [:in names]))-(defn insert-databases!+(mu/defn insert-databases!"Insert the Database `rows` and return the new instances."- [rows]+ [rows :- [:sequential+ ::warehouses.schema/database.update]](t2/insert-returning-instances! :model/Database rows))-(defn transform-exists-for-source-database?+(mu/defn transform-exists-for-source-database?"Whether a Transform reads from the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Transform :source_database_id database-id))-(defn router-exists?+(mu/defn router-exists?"Whether the Database with `database-id` is a router."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/DatabaseRouter :database_id database-id))-(defn router-for-database+(mu/defn router-for-database"The DatabaseRouter of the Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/DatabaseRouter :database_id database-id))-(defn router-user-attribute+(mu/defn router-user-attribute"The user attribute the router of the Database with `database-id` routes on."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :user_attribute :model/DatabaseRouter :database_id database-id))-(defn router-user-attributes-by-database+(mu/defn router-user-attributes-by-database"A map of Database ID to routing user attribute for `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select-fn->fn :database_id :user_attribute :model/DatabaseRouter :database_id [:in database-ids]))-(defn insert-router!+(mu/defn insert-router!"Insert a DatabaseRouter for the Database with `database-id` routing on `user-attribute`."- [database-id user-attribute]+ [database-id :- ::lib.schema.id/database+ user-attribute :- :string](t2/insert! :model/DatabaseRouter {:database_id database-id :user_attribute user-attribute}))-(defn update-router-user-attribute!+(mu/defn update-router-user-attribute!"Set the routing user attribute of the router of the Database with `database-id`."- [database-id user-attribute]+ [database-id :- ::lib.schema.id/database+ user-attribute :- :string](t2/update! :model/DatabaseRouter :database_id database-id {:user_attribute user-attribute}))-(defn delete-router!+(mu/defn delete-router!"Delete the DatabaseRouter of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/delete! :model/DatabaseRouter :database_id database-id))diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/schema.clj b/enterprise/backend/src/metabase_enterprise/database_routing/schema.cljnew file mode 100644index 00000000000..e624b05237b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/database_routing/schema.clj@@ -0,0 +1,19 @@+(ns metabase-enterprise.database-routing.schema+ "Malli schemas for the database-routing module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::database-router+ "A DatabaseRouter as selected from the app DB: every column of `:db_router`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:database_id ::lib.schema.id/database]+ [:user_attribute :string]])++(mr/def ::database-router.update+ "What an update (or insert) of a DatabaseRouter accepts: every column of `:db_router` except `id`, all optional."+ [:map {:closed true}+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:user_attribute {:optional true} [:maybe :string]]])diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj b/enterprise/backend/src/metabase_enterprise/dependencies/db.cljindex c62cf4faa9f..df6878452e4 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj@@ -3,15 +3,49 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require[clojure.set :as set]+ [malli.util :as mut][metabase-enterprise.dependencies.dependency-types :as deps.dependency-types]+ [metabase-enterprise.dependencies.schema :as dependencies.schema][metabase.app-db.core :as mdb][metabase.collections.models.collection :as collection][metabase.collections.models.collection.root :as collection.root]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.permissions.core :as perms]+ [metabase.queries.schema :as queries.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))+(def ^:private VisibleOpts+ "Opts consumed by [[visible-entities-expr]]: the current user (`:user-id`, `:is-superuser?`,+ `:is-data-analyst?`), plus whether to include archived items (`:include-archived-items`,+ default `:exclude`)."+ [:map {:closed true}+ [:user-id ::lib.schema.id/user]+ [:is-superuser? {:optional true} [:maybe :boolean]]+ [:is-data-analyst? {:optional true} [:maybe :boolean]]+ [:include-archived-items {:optional true} [:enum :exclude :all :only]]])++(def ^:private EntityType+ "A dependency-type keyword, or the equivalent string. Every column these entity types are filtered against uses+ Toucan's keyword transform, which coerces either representation, so callers (and tests) sometimes pass a plain+ string instead of the keyword."+ [:or ::deps.dependency-types/dependency-types :string])++(def ^:private RestrictionSpec+ "The edge-restriction filter spec consumed by [[edge-restriction-expr]]: nil for no restriction, or a map+ combining `:visible` ([[VisibleOpts]]), `:broken?`, `:types` (a set of dependency types), and/or+ `:entity-type` + `:ids` (restricting to specific entities of one type)."+ [:maybe+ [:map {:closed true}+ [:visible {:optional true} VisibleOpts]+ [:broken? {:optional true} :boolean]+ [:types {:optional true} [:set EntityType]]+ [:entity-type {:optional true} EntityType]+ [:ids {:optional true} [:sequential ::deps.dependency-types/entity-id]]]])+;;; ------------------------------------------------ Graph edge restrictions ------------------------------------------------;;; The dependency graph is traversed generically over every entity type the module knows about; these private;;; helpers build the permission and type-allowlist fragments the graph and item-list queries below restrict edges@@ -169,13 +203,22 @@1 (first fragments)(into [:and] fragments))))-(defn dependency-edges+(mu/defn dependency-edges"The Dependencies from the entities of type `entity-type` with `entity-ids`, where `src-type`/`src-id` name theDependency columns identifying those entities and `dst-type`/`dst-id` name the columns identifying the relatedentities on the other side of the edge, optionally restricted by `destination-restriction` (applied to`dst-type`/`dst-id`) and `source-restriction` (applied to `src-type`/`src-id`) — see [[edge-restriction-expr]] for thefilter spec shape."- [{:keys [src-type src-id dst-type dst-id entity-type entity-ids destination-restriction source-restriction]}]+ [{:keys [src-type src-id dst-type dst-id entity-type entity-ids destination-restriction source-restriction]}+ :- [:map {:closed true}+ [:src-type [:enum :from_entity_type :to_entity_type]]+ [:src-id [:enum :from_entity_id :to_entity_id]]+ [:dst-type [:enum :from_entity_type :to_entity_type]]+ [:dst-id [:enum :from_entity_id :to_entity_id]]+ [:entity-type EntityType]+ [:entity-ids [:or [:set ::deps.dependency-types/entity-id] [:sequential ::deps.dependency-types/entity-id]]]+ [:destination-restriction RestrictionSpec]+ [:source-restriction RestrictionSpec]]](t2/select :model/Dependency{:where (into [:and[:= src-type (name entity-type)]@@ -184,10 +227,12 @@[(edge-restriction-expr dst-type dst-id destination-restriction)(edge-restriction-expr src-type src-id source-restriction)])}))-(defn finding-errors-from-sources+(mu/defn finding-errors-from-sources"The AnalysisFindingErrors caused by any of the entities `source-entity-type` `source-entity-ids`, whose analyzedentity is visible to the user described by `user-id`, `is-superuser?`, and `is-data-analyst?`."- [source-entity-type source-entity-ids {:keys [user-id is-superuser? is-data-analyst?]}]+ [source-entity-type :- EntityType+ source-entity-ids :- [:sequential ::deps.dependency-types/entity-id]+ {:keys [user-id is-superuser? is-data-analyst?]} :- VisibleOpts](t2/select :model/AnalysisFindingError{:where [:and[:= :source_entity_type (name source-entity-type)]@@ -196,10 +241,12 @@{:user-id user-id :is-superuser? is-superuser?:is-data-analyst? is-data-analyst?})]}))-(defn finding-errors-for-entities-with-visible-sources+(mu/defn finding-errors-for-entities-with-visible-sources"The AnalysisFindingErrors analyzing any of the entities `entity-type` `entity-ids`, excluding those whose sourceentity exists but is not visible to the user described by `user-id`, `is-superuser?`, and `is-data-analyst?`."- [entity-type entity-ids {:keys [user-id is-superuser? is-data-analyst?]}]+ [entity-type :- EntityType+ entity-ids :- [:sequential ::deps.dependency-types/entity-id]+ {:keys [user-id is-superuser? is-data-analyst?]} :- VisibleOpts](t2/select :model/AnalysisFindingError{:where [:and[:= :analyzed_entity_type (name entity-type)]@@ -414,14 +461,30 @@:left-join (build-left-joins join all-joins):where (into [:and join-filter item-visible-expr] (keep identity) exprs)}))-(defn dependency-item-ids+(def ^:private DependencyItemParams+ "Params shared by [[dependency-item-ids]] and [[dependency-item-count]]."+ [:map {:closed true}+ [:query-type [:enum :unreferenced :breaking]]+ [:entity-types [:sequential ::deps.dependency-types/dependency-types]]+ [:card-types {:optional true} [:maybe [:sequential [:or :keyword :string]]]]+ [:search-text {:optional true} [:maybe :string]]+ [:include-personal-collections? :boolean]+ [:sort-column {:optional true} [:maybe [:enum :name :location :dependents-errors :dependents-with-errors]]]+ [:sort-direction [:enum :asc :desc]]+ [:offset ms/IntGreaterThanOrEqualToZero]+ [:limit ms/PositiveInt]+ [:user-id ::lib.schema.id/user]+ [:is-superuser? {:optional true} [:maybe :boolean]]+ [:is-data-analyst? {:optional true} [:maybe :boolean]]])++(mu/defn dependency-item-ids"A page of `[entity-type entity-id]` pairs for `query-type` (`:unreferenced` or `:breaking`), restricted to`entity-types`, `card-types` (applied only to `:card` entities), `search-text` (nil for none), and`include-personal-collections?`; sorted by `sort-column` (`:name`, `:location`, `:dependents-errors`, or`:dependents-with-errors`) in `sort-direction`, skipping `offset` and returning up to `limit`. Entities arerestricted to those visible to the user described by `user-id`, `is-superuser?`, and `is-data-analyst?`; throwswhen `user-id` is missing."- [{:keys [entity-types sort-direction offset limit] :as params}]+ [{:keys [entity-types sort-direction offset limit] :as params} :- DependencyItemParams](let [union-query ^:allow-subquery {:union-all (mapv #(dependency-item-select (assoc params :entity-type %))entity-types)}](->> (t2/query (assoc union-query@@ -430,22 +493,30 @@:limit limit))(map (fn [{:keys [entity_id entity_type]}] [(keyword entity_type) entity_id])))))-(defn dependency-item-count+(mu/defn dependency-item-count"The total count of items matching the same criteria as [[dependency-item-ids]] (ignoring sort direction, offset,and limit); throws when `user-id` is missing."- [{:keys [entity-types] :as params}]+ [{:keys [entity-types] :as params} :- DependencyItemParams](let [union-query ^:allow-subquery {:union-all (mapv #(dependency-item-select (assoc params :entity-type %))entity-types)}](-> (t2/query {:select [[:%count.* :total]] :from [[union-query :subquery]]})first:total)))-(defn broken-entity-pairs+(mu/defn broken-entity-pairs"The `[:analyzed_entity_type :analyzed_entity_id]` pairs whose analysis failed and were caused by the entity`source-entity-type` `source-entity-id`, restricted to `dependent-types` and `dependent-card-types` (each nilfor no restriction), visible to the user described by `user-id`, `is-superuser?`, and `is-data-analyst?`."[{:keys [source-entity-type source-entity-id dependent-types dependent-card-types- user-id is-superuser? is-data-analyst?]}]+ user-id is-superuser? is-data-analyst?]}+ :- [:map {:closed true}+ [:source-entity-type EntityType]+ [:source-entity-id ms/PositiveInt]+ [:dependent-types {:optional true} [:maybe [:sequential :string]]]+ [:dependent-card-types {:optional true} [:maybe [:sequential :string]]]+ [:user-id ::lib.schema.id/user]+ [:is-superuser? {:optional true} [:maybe :boolean]]+ [:is-data-analyst? {:optional true} [:maybe :boolean]]]](t2/query(cond-> {:select-distinct [[:afe.analyzed_entity_type :entity_type] [:afe.analyzed_entity_id :entity_id]]:from [[:analysis_finding_error :afe]]@@ -472,124 +543,153 @@;;; ---------------------------------------------------- Entities -----------------------------------------------------(defn instances+(mu/defn instances"The instances of the entity type `entity-type` with `ids`."- [entity-type ids]+ [entity-type :- ::deps.dependency-types/dependency-types+ ids :- [:set ::deps.dependency-types/entity-id]](t2/select (deps.dependency-types/dependency-type->model entity-type) :id [:in ids]))-(defn instances-with-columns+(mu/defn instances-with-columns"The `columns` of the instances of the entity type `entity-type` with `ids`."- [entity-type columns ids]+ [entity-type :- ::deps.dependency-types/dependency-types+ columns :- [:sequential :keyword]+ ids :- [:sequential ::deps.dependency-types/entity-id]](t2/select (into [(deps.dependency-types/dependency-type->model entity-type)] columns) :id [:in ids]))-(defn instance-with-columns+(mu/defn instance-with-columns"The `columns` of the instance of the entity type `entity-type` with `id`, or nil."- [entity-type columns id]+ [entity-type :- ::deps.dependency-types/dependency-types+ columns :- [:sequential :keyword]+ id :- ::deps.dependency-types/entity-id](t2/select-one (into [(deps.dependency-types/dependency-type->model entity-type)] columns) :id id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-types-by-id+(mu/defn card-types-by-id"A map of Card ID to type for `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-fn->fn :id :type [:model/Card :id :type :card_schema] :id [:in card-ids]))-(defn card-database-ids+(mu/defn card-database-ids"The `:id`, `:database_id`, and `:card_schema` of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :database_id :card_schema] :id [:in card-ids]))-(defn set-card-result-metadata!- "Set the result metadata of the Card with `card-id`."- [card-id result-metadata]+(mu/defn set-card-result-metadata!+ "Set the result metadata of the Card with `card-id`, returning the number updated."+ [card-id :- ::lib.schema.id/card+ result-metadata :- [:maybe ::queries.schema/card.result-metadata]](t2/update! :model/Card card-id {:result_metadata result-metadata}))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn table-database-ids+(mu/defn table-database-ids"The `:id` and `:db_id` of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :db_id] :id [:in table-ids]))-(defn table-id-by-name+(mu/defn table-id-by-name"The ID of the Table named `table-name` in `schema` of the Database with `db-id`, or nil."- [db-id schema table-name]+ [db-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/select-one-fn :id :model/Table :db_id db-id :schema schema :name table-name))-(defn transform-sources+(mu/defn transform-sources"The `:id` and `:source` of the Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:sequential ::lib.schema.id/transform]](t2/select [:model/Transform :id :source] :id [:in transform-ids]))-(defn transform-ids-of-source-database+(mu/defn transform-ids-of-source-database"The IDs of the Transforms reading from the Database with `db-id`."- [db-id]+ [db-id :- ::lib.schema.id/database](t2/select-pks-set :model/Transform :source_database_id db-id));;; -------------------------------------------------- Dependencies ---------------------------------------------------(defn dependencies-from+(mu/defn dependencies-from"The `:id`, `:to_entity_type`, and `:to_entity_id` of the Dependencies of the entity `entity-type` `entity-id`."- [entity-type entity-id]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/select [:model/Dependency :id :to_entity_type :to_entity_id]:from_entity_type entity-type:from_entity_id entity-id))-(defn dependency-exists?+(mu/defn dependency-exists?"Whether the entity `from-type` `from-id` depends on the entity `to-type` `to-id`."- [from-type from-id to-type to-id]+ [from-type :- EntityType+ from-id :- ms/PositiveInt+ to-type :- EntityType+ to-id :- ms/PositiveInt](t2/exists? :model/Dependency:from_entity_type from-type :from_entity_id from-id:to_entity_type to-type :to_entity_id to-id))-(defn insert-dependencies!- "Insert the Dependency `rows`."- [rows]+(mu/defn insert-dependencies!+ "Insert the Dependency `rows`, returning the number inserted."+ [rows :- [:sequential [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:from_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:from_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:to_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:to_entity_id {:optional true} [:maybe ms/PositiveInt]]]]](t2/insert! :model/Dependency rows))-(defn retarget-dependency!+(mu/defn retarget-dependency!"Point the Dependency of the entity `from-type` `from-id` on the entity `old-to-type` `old-to-id` at the entity- `new-to-type` `new-to-id`."- [from-type from-id old-to-type old-to-id new-to-type new-to-id]+ `new-to-type` `new-to-id`, returning the number updated."+ [from-type :- EntityType+ from-id :- ms/PositiveInt+ old-to-type :- EntityType+ old-to-id :- ms/PositiveInt+ new-to-type :- EntityType+ new-to-id :- ms/PositiveInt](t2/update! :model/Dependency{:from_entity_type from-type :from_entity_id from-id:to_entity_type old-to-type :to_entity_id old-to-id}{:to_entity_type new-to-type :to_entity_id new-to-id}))-(defn delete-dependencies!- "Delete the Dependencies with `dependency-ids`."- [dependency-ids]+(mu/defn delete-dependencies!+ "Delete the Dependencies with `dependency-ids`, returning the number deleted."+ [dependency-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/Dependency :id [:in dependency-ids]))-(defn delete-dependency!- "Delete the Dependency of the entity `from-type` `from-id` on the entity `to-type` `to-id`."- [from-type from-id to-type to-id]+(mu/defn delete-dependency!+ "Delete the Dependency of the entity `from-type` `from-id` on the entity `to-type` `to-id`, returning the number+ deleted."+ [from-type :- EntityType+ from-id :- ms/PositiveInt+ to-type :- EntityType+ to-id :- ms/PositiveInt](t2/delete! :model/Dependency:from_entity_type from-type :from_entity_id from-id:to_entity_type to-type :to_entity_id to-id))-(defn delete-dependencies-from!- "Delete the Dependencies of the entity `entity-type` `entity-id`."- [entity-type entity-id]+(mu/defn delete-dependencies-from!+ "Delete the Dependencies of the entity `entity-type` `entity-id`, returning the number deleted."+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/delete! :model/Dependency :from_entity_type entity-type :from_entity_id entity-id))-(defn downstream-table-ids-of-transform+(mu/defn downstream-table-ids-of-transform"The IDs of the Tables that depend on the Transform with `transform-id`."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-fn-set :from_entity_id :model/Dependency:from_entity_type :table:to_entity_type :transform:to_entity_id transform-id))-(defn delete-table-dependencies-on-transform!- "Delete the Dependencies of the Tables with `table-ids` on the Transform with `transform-id`."- [table-ids transform-id]+(mu/defn delete-table-dependencies-on-transform!+ "Delete the Dependencies of the Tables with `table-ids` on the Transform with `transform-id`, returning the+ number deleted."+ [table-ids :- [:sequential ::lib.schema.id/table]+ transform-id :- ::lib.schema.id/transform](t2/delete! :model/Dependency:from_entity_type :table:from_entity_id [:in table-ids]@@ -598,20 +698,23 @@;;; ------------------------------------------------ Dependency status -------------------------------------------------(defn dependency-status+(mu/defn dependency-status"The DependencyStatus of the entity `entity-type` `entity-id`, or nil."- [entity-type entity-id]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/select-one :model/DependencyStatus :entity_type entity-type :entity_id entity-id))-(defn delete-dependency-status!- "Delete the DependencyStatus of the entity `entity-type` `entity-id`."- [entity-type entity-id]+(mu/defn delete-dependency-status!+ "Delete the DependencyStatus of the entity `entity-type` `entity-id`, returning the number deleted."+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/delete! :model/DependencyStatus :entity_type entity-type :entity_id entity-id))-(defn mark-dependency-status-stale!+(mu/defn mark-dependency-status-stale!"Mark the DependencyStatus of `entity-type` `entity-id` as stale for dependency recalculation, creating it if itdoesn't exist. Resets retry state so previously-failed entities get a fresh chance."- [entity-type entity-id]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](mdb/update-or-insert!:model/DependencyStatus{:entity_type entity-type :entity_id entity-id}@@ -620,10 +723,12 @@{:stale true :fail_count 0 :next_retry_at nil :terminal false}{:stale true :dependency_analysis_version 0}))))-(defn upsert-dependency-status!+(mu/defn upsert-dependency-status!"Upsert the DependencyStatus of `entity-type` `entity-id`, setting stale=false, `dependency_analysis_version` to`current-version`, and clearing any failure state."- [entity-type entity-id current-version]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt+ current-version :- ms/PositiveInt](mdb/update-or-insert!:model/DependencyStatus{:entity_type entity-type :entity_id entity-id}@@ -633,24 +738,29 @@:fail_count 0:next_retry_at nil})))-(defn record-dependency-status-failure!+(mu/defn record-dependency-status-failure!"Upsert the DependencyStatus of `entity-type` `entity-id`, creating it if needed. `update-fn` receives theexisting DependencyStatus row (or nil if none exists) and must return the columns to set."- [entity-type entity-id update-fn]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt+ update-fn :- fn?](mdb/update-or-insert!:model/DependencyStatus{:entity_type entity-type :entity_id entity-id}update-fn))-(defn pending-retry-exists?+(mu/defn pending-retry-exists?"Whether a non-terminal DependencyStatus is waiting for a retry."[](t2/exists? :model/DependencyStatus :terminal false :next_retry_at [:not= nil]))-(defn instances-for-dependency-calculation+(mu/defn instances-for-dependency-calculation"Up to `batch-size` instances of the entity type `entity-type` without a DependencyStatus, or whose status isstale or below `current-version`, is not terminal, and whose retry time has passed at `now`; stale ones first."- [entity-type batch-size current-version now]+ [entity-type :- ::deps.dependency-types/dependency-types+ batch-size :- ms/PositiveInt+ current-version :- ms/PositiveInt+ now :- ms/TemporalInstant](let [model (deps.dependency-types/dependency-type->model entity-type)table-name (t2/table-name model)id-field (keyword (name table-name) "id")@@ -676,45 +786,57 @@;;; ------------------------------------------------ Analysis findings -------------------------------------------------(defn finding-id+(mu/defn finding-id"The ID of the AnalysisFinding of the entity `entity-type` `entity-id`, or nil."- [entity-type entity-id]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/select-one-fn :id [:model/AnalysisFinding :id]:analyzed_entity_type entity-type:analyzed_entity_id entity-id))-(defn insert-finding!- "Insert the AnalysisFinding `row`."- [row]+(mu/defn insert-finding!+ "Insert the AnalysisFinding `row`, returning the number inserted."+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:analyzed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:analysis_version {:optional true} ms/PositiveInt]+ [:result {:optional true} :boolean]+ [:stale {:optional true} :boolean]+ [:analyzed_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:analyzed_entity_id {:optional true} [:maybe ms/PositiveInt]]]](t2/insert! :model/AnalysisFinding row))-(defn update-finding!- "Apply `changes` to the AnalysisFinding with `finding-id`."- [finding-id changes]+(mu/defn update-finding!+ "Apply `changes` to the AnalysisFinding with `finding-id`, returning the number updated."+ [finding-id :- ms/PositiveInt+ changes :- (mut/select-keys ::dependencies.schema/analysis-finding.update [:analyzed_at :analysis_version :result :stale])](t2/update! :model/AnalysisFinding finding-id changes))-(defn mark-findings-stale!- "Mark the AnalysisFindings of the entities `entity-type` `entity-ids` as stale."- [entity-type entity-ids]+(mu/defn mark-findings-stale!+ "Mark the AnalysisFindings of the entities `entity-type` `entity-ids` as stale, returning the number updated."+ [entity-type :- EntityType+ entity-ids :- [:sequential ms/PositiveInt]](t2/update! :model/AnalysisFinding:analyzed_entity_type entity-type:analyzed_entity_id [:in entity-ids]{:stale true}))-(defn stale-finding-exists?+(mu/defn stale-finding-exists?"Whether a stale AnalysisFinding exists."[](t2/exists? :model/AnalysisFinding :stale true))-(defn stale-finding-count+(mu/defn stale-finding-count"The number of stale AnalysisFindings."[](t2/count :model/AnalysisFinding :stale true))-(defn instances-for-analysis+(mu/defn instances-for-analysis"Up to `batch-size` instances of the entity type `entity-type` whose AnalysisFinding is stale, missing, or below`current-version`; stale ones first, then the longest unanalyzed."- [entity-type batch-size current-version]+ [entity-type :- ::deps.dependency-types/dependency-types+ batch-size :- ms/PositiveInt+ current-version :- ms/PositiveInt](let [model (deps.dependency-types/dependency-type->model entity-type)table-name (t2/table-name model)id-field (keyword (name table-name) "id")@@ -734,10 +856,10 @@[:analysis_finding.analyzed_at :asc]]:limit batch-size})))-(defn table-ids-with-outdated-findings+(mu/defn table-ids-with-outdated-findings"The IDs of the Tables of the Database with `db-id` whose dependents were analyzed before the Table or one ofits Fields last changed."- [db-id]+ [db-id :- ::lib.schema.id/database](t2/select-fn-set :table_id :model/AnalysisFinding{:select [:field_updates/table_id]:from [[^:allow-subquery {:select [[:table/id :table_id]@@ -766,22 +888,25 @@;;; --------------------------------------------- Analysis finding errors ----------------------------------------------(defn finding-errors-for-entity+(mu/defn finding-errors-for-entity"The AnalysisFindingErrors of the entity `entity-type` `entity-id`."- [entity-type entity-id]+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/select :model/AnalysisFindingError :analyzed_entity_type entity-type :analyzed_entity_id entity-id))-(defn finding-errors-from-source+(mu/defn finding-errors-from-source"The AnalysisFindingErrors caused by the entity `source-type` `source-id`."- [source-type source-id]+ [source-type :- [:maybe :metabase.lib.schema.validate/source-entity-type]+ source-id :- ms/PositiveInt](t2/select :model/AnalysisFindingError :source_entity_type source-type :source_entity_id source-id))-(defn insert-finding-errors!- "Insert the AnalysisFindingError `rows`."- [rows]+(mu/defn insert-finding-errors!+ "Insert the AnalysisFindingError `rows`, returning the number inserted."+ [rows :- [:sequential (mut/select-keys ::dependencies.schema/analysis-finding-error.update [:analyzed_entity_type :analyzed_entity_id :error_type :error_detail :source_entity_type :source_entity_id])]](t2/insert! :model/AnalysisFindingError rows))-(defn delete-finding-errors-for-entity!- "Delete the AnalysisFindingErrors of the entity `entity-type` `entity-id`."- [entity-type entity-id]+(mu/defn delete-finding-errors-for-entity!+ "Delete the AnalysisFindingErrors of the entity `entity-type` `entity-id`, returning the number deleted."+ [entity-type :- EntityType+ entity-id :- ms/PositiveInt](t2/delete! :model/AnalysisFindingError :analyzed_entity_type entity-type :analyzed_entity_id entity-id))diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.cljindex 7211b1dd3fa..3a531cbcf5a 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj@@ -14,6 +14,17 @@(t2/deftransforms :model/AnalysisFinding{:analyzed_entity_type mi/transform-keyword})+(t2/define-before-insert :model/AnalysisFinding+ [finding]+ (merge {:analyzed_at (mi/now)} finding))++(t2/define-before-update :model/AnalysisFinding+ [finding]+ (let [changes (t2/changes finding)]+ (cond-> finding+ (and (contains? changes :result) (not (contains? changes :analyzed_at)))+ (assoc :analyzed_at (mi/now)))))+(def ^:dynamic *current-analysis-finding-version*"Current version of the query validation logic.This should be incremented when the analysis logic changes.@@ -45,8 +56,7 @@Also writes individual errors to the analysis_finding_error table with source information."[type instance-id result finding-details](t2/with-transaction [_conn]- (let [update {:analyzed_at (mi/now)- :analysis_version *current-analysis-finding-version*+ (let [update {:analysis_version *current-analysis-finding-version*:result result:stale false}existing-id (dependencies.db/finding-id type instance-id)]diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/schema.clj b/enterprise/backend/src/metabase_enterprise/dependencies/schema.cljindex 38a61d1e866..7826c3b9257 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/schema.clj@@ -2,7 +2,8 @@(:require[metabase.documents.schema :as documents.schema][metabase.lib.schema.id :as lib.schema.id]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::upstream-deps[:map@@ -14,3 +15,85 @@[:document {:optional true} [:set ::documents.schema/document.id]][:sandbox {:optional true} [:set ::lib.schema.id/sandbox]][:segment {:optional true} [:set ::lib.schema.id/segment]]])++(mr/def ::analysis-finding+ "A AnalysisFinding as selected from the app DB: every column of `:analysis_finding`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:analyzed_entity_type [:or :keyword :string]]+ [:analyzed_entity_id ms/PositiveInt]+ [:analysis_version :int]+ [:analyzed_at ms/TemporalInstant]+ [:result :boolean]+ [:stale :boolean]])++(mr/def ::analysis-finding.update+ "What an update (or insert) of a AnalysisFinding accepts: every column of `:analysis_finding` except `id`, all optional."+ [:map {:closed true}+ [:analyzed_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:analyzed_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:analysis_version {:optional true} [:maybe :int]]+ [:analyzed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:result {:optional true} [:maybe :boolean]]+ [:stale {:optional true} [:maybe :boolean]]])++(mr/def ::analysis-finding-error+ "A AnalysisFindingError as selected from the app DB: every column of `:analysis_finding_error`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:analyzed_entity_type [:or :keyword :string]]+ [:analyzed_entity_id ms/PositiveInt]+ [:error_type [:or :keyword :string]]+ [:error_detail [:maybe :string]]+ [:source_entity_type [:maybe [:or :keyword :string]]]+ [:source_entity_id [:maybe ms/PositiveInt]]])++(mr/def ::analysis-finding-error.update+ "What an update (or insert) of a AnalysisFindingError accepts: every column of `:analysis_finding_error` except `id`, all optional."+ [:map {:closed true}+ [:analyzed_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:analyzed_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:error_type {:optional true} [:maybe [:or :keyword :string]]]+ [:error_detail {:optional true} [:maybe :string]]+ [:source_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_entity_id {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::dependency+ "A Dependency as selected from the app DB: every column of `:dependency`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:from_entity_type [:or :keyword :string]]+ [:from_entity_id ms/PositiveInt]+ [:to_entity_type [:or :keyword :string]]+ [:to_entity_id ms/PositiveInt]])++(mr/def ::dependency.update+ "What an update (or insert) of a Dependency accepts: every column of `:dependency` except `id`, all optional."+ [:map {:closed true}+ [:from_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:from_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:to_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:to_entity_id {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::dependency-status+ "A DependencyStatus as selected from the app DB: every column of `:dependency_status`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:entity_type [:or :keyword :string]]+ [:entity_id ms/PositiveInt]+ [:dependency_analysis_version :int]+ [:stale :boolean]+ [:fail_count :int]+ [:next_retry_at [:maybe ms/TemporalInstant]]+ [:terminal :boolean]])++(mr/def ::dependency-status.update+ "What an update (or insert) of a DependencyStatus accepts: every column of `:dependency_status` except `id`, all optional."+ [:map {:closed true}+ [:entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:dependency_analysis_version {:optional true} [:maybe :int]]+ [:stale {:optional true} [:maybe :boolean]]+ [:fail_count {:optional true} [:maybe :int]]+ [:next_retry_at {:optional true} [:maybe ms/TemporalInstant]]+ [:terminal {:optional true} [:maybe :boolean]]])diff --git a/enterprise/backend/src/metabase_enterprise/embedding_hub/db.clj b/enterprise/backend/src/metabase_enterprise/embedding_hub/db.cljindex 752c67f93eb..7fb65163889 100644--- a/enterprise/backend/src/metabase_enterprise/embedding_hub/db.clj+++ b/enterprise/backend/src/metabase_enterprise/embedding_hub/db.clj@@ -2,32 +2,35 @@"Application database queries for the embedding-hub module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn user-database-exists?+(mu/defn user-database-exists?"Whether a non-sample, non-audit Database exists."[](t2/exists? :model/Database {:where [:and[:= :is_sample false][:= :is_audit false]]}))-(defn sample-database-id+(mu/defn sample-database-id"The ID of the sample Database, or nil."[](t2/select-one-pk :model/Database :is_sample true))-(defn upload-table-exists-in-database?+(mu/defn upload-table-exists-in-database?"Whether the Database with `database-id` holds an active uploaded Table."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Table {:where [:and[:= :active true][:= :is_upload true][:= :db_id database-id]]}))-(defn user-dashboard-exists?+(mu/defn user-dashboard-exists?"Whether an unarchived Dashboard exists other than `example-dashboard-id` and outside the Collections with`excluded-collection-ids`."- [example-dashboard-id excluded-collection-ids]+ [example-dashboard-id :- [:maybe ::lib.schema.id/dashboard]+ excluded-collection-ids :- [:maybe [:sequential ::lib.schema.id/collection]]](t2/exists? :model/Dashboard {:where (cond-> [:and[:= :archived false]]example-dashboard-id (conj [:not= :id example-dashboard-id])@@ -35,15 +38,15 @@[:is :collection_id nil][:not-in :collection_id excluded-collection-ids]]))}))-(defn sandbox-exists?+(mu/defn sandbox-exists?"Whether any Sandbox exists."[](t2/exists? :model/Sandbox))-(defn user-model-exists?+(mu/defn user-model-exists?"Whether an unarchived model Card exists outside the sample Collections and the Collection with`audit-collection-id`."- [audit-collection-id]+ [audit-collection-id :- [:maybe ::lib.schema.id/collection]](t2/exists? :model/Card {:where [:and[:= :type "model"][:= :archived false]@@ -58,48 +61,48 @@[:= :sample_coll.id :report_card.collection_id]]}]]][:is :collection_id nil]]]}))-(defn active-tenant-exists?+(mu/defn active-tenant-exists?"Whether an active Tenant exists."[](t2/exists? :model/Tenant :is_active true))-(defn shared-tenant-collection-exists?+(mu/defn shared-tenant-collection-exists?"Whether an unarchived shared tenant Collection exists."[](t2/exists? :model/Collection {:where [:and[:= :namespace "shared-tenant-collection"][:= :archived false]]}))-(defn shared-tenant-collection-id+(mu/defn shared-tenant-collection-id"The ID of an unarchived shared tenant Collection, or nil."[](t2/select-one-pk :model/Collection {:where [:and[:= :namespace "shared-tenant-collection"][:= :archived false]]}))-(defn unarchived-dashboard-exists-in-collection?+(mu/defn unarchived-dashboard-exists-in-collection?"Whether an unarchived Dashboard exists in the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/exists? :model/Dashboard {:where [:and[:= :collection_id collection-id][:= :archived false]]}))-(defn impersonation-exists?+(mu/defn impersonation-exists?"Whether any ConnectionImpersonation exists."[](t2/exists? :model/ConnectionImpersonation))-(defn database-router-exists?+(mu/defn database-router-exists?"Whether any DatabaseRouter exists."[](t2/exists? :model/DatabaseRouter))-(defn embedded-card-exists?+(mu/defn embedded-card-exists?"Whether any Card has embedding enabled."[](t2/exists? :model/Card :enable_embedding true))-(defn embedded-dashboard-exists?+(mu/defn embedded-dashboard-exists?"Whether any Dashboard has embedding enabled."[](t2/exists? :model/Dashboard :enable_embedding true))diff --git a/enterprise/backend/src/metabase_enterprise/entity_retrieval/db.clj b/enterprise/backend/src/metabase_enterprise/entity_retrieval/db.cljindex 57b430c29cb..f1a7882b7a4 100644--- a/enterprise/backend/src/metabase_enterprise/entity_retrieval/db.clj+++ b/enterprise/backend/src/metabase_enterprise/entity_retrieval/db.clj@@ -2,12 +2,16 @@"Application database queries for the entity-retrieval module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn library-cards-in-collections+(mu/defn library-cards-in-collections"The ID, name, description, and type of the unarchived metric and model Cards in the Collections with`collection-ids`, optionally narrowed to just `id`."- [collection-ids id]+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ id :- [:maybe ::lib.schema.id/card]](t2/select [:model/Card :id :name :description :type :card_schema]{:where [:and[:in :collection_id collection-ids]@@ -15,10 +19,11 @@[:in :type ["metric" "model"]](when id [:= :id id])]}))-(defn library-tables-in-collections+(mu/defn library-tables-in-collections"The ID, names, and description of the active published Tables in the Collections with `collection-ids`, optionallynarrowed to just `id`."- [collection-ids id]+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ id :- [:maybe ::lib.schema.id/table]](t2/select [:model/Table :id :name :display_name :description]{:where [:and[:in :collection_id collection-ids]@@ -26,44 +31,47 @@[:= :active true](when id [:= :id id])]}))-(defn library-measures-of-tables+(mu/defn library-measures-of-tables"The ID, name, and description of the unarchived Measures on the Tables with `table-ids`, optionally narrowed tojust `id`."- [table-ids id]+ [table-ids :- [:sequential ::lib.schema.id/table]+ id :- [:maybe ::lib.schema.id/measure]](t2/select [:model/Measure :id :name :description]{:where [:and[:in :table_id table-ids][:= :archived false](when id [:= :id id])]}))-(defn library-segments-of-tables+(mu/defn library-segments-of-tables"The ID, name, and description of the unarchived Segments on the Tables with `table-ids`, optionally narrowed tojust `id`."- [table-ids id]+ [table-ids :- [:sequential ::lib.schema.id/table]+ id :- [:maybe ::lib.schema.id/segment]](t2/select [:model/Segment :id :name :description]{:where [:and[:in :table_id table-ids][:= :archived false](when id [:= :id id])]}))-(defn measure-table-id+(mu/defn measure-table-id"The Table ID of the Measure with `id`."- [id]+ [id :- ::lib.schema.id/measure](t2/select-one-fn :table_id :model/Measure :id id))-(defn segment-table-id+(mu/defn segment-table-id"The Table ID of the Segment with `id`."- [id]+ [id :- ::lib.schema.id/segment](t2/select-one-fn :table_id :model/Segment :id id))-(defn ai-contexts+(mu/defn ai-contexts"The entity type, entity ID, and AI context of every OsiAiContext."[](t2/select [:model/OsiAiContext :entity_type :entity_local_id :ai_context]))-(defn ai-context+(mu/defn ai-context"The AI context of the entity of `entity-type` with `entity-local-id`."- [entity-local-id entity-type]+ [entity-local-id :- ms/PositiveInt+ entity-type :- :string](t2/select-one-fn :ai_context :model/OsiAiContext:entity_local_id entity-local-id:entity_type entity-type))diff --git a/enterprise/backend/src/metabase_enterprise/erd/db.clj b/enterprise/backend/src/metabase_enterprise/erd/db.cljindex e16d23b4838..b3ec7668523 100644--- a/enterprise/backend/src/metabase_enterprise/erd/db.clj+++ b/enterprise/backend/src/metabase_enterprise/erd/db.clj@@ -2,33 +2,44 @@"Application database queries for the erd module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][metabase.warehouse-schema.models.table :as schema.table][toucan2.core :as t2]))-(defn active-tables-in-database+(def ^:private ActiveTablesOpts+ "The optional restrictions [[active-tables-in-database]] accepts."+ [:map {:closed true}+ [:table-ids {:optional true} [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]+ [:schema {:optional true} [:maybe :string]]])++(mu/defn active-tables-in-database"The `columns` of the active Tables in the Database with `database-id`. `opts` may restrict the result: a`:table-ids` key restricts to those IDs (even when its value is empty), and a `:schema` key restricts to thatschema (`\"\"` matches both nil and empty-string schemas)."- [columns database-id {:keys [table-ids schema] :as opts}]- (t2/select :model/Table- {:select columns- :where (cond-> [:and [:= :db_id database-id] [:= :active true]]- (contains? opts :table-ids) (conj [:in :id table-ids])- (contains? opts :schema) (conj (if (= schema "")- [:or [:= :schema nil] [:= :schema ""]]- [:= :schema schema])))}))+ [columns :- [:sequential :keyword]+ database-id :- ::lib.schema.id/database+ opts :- [:maybe ActiveTablesOpts]]+ (let [{:keys [table-ids schema]} opts]+ (t2/select :model/Table+ {:select columns+ :where (cond-> [:and [:= :db_id database-id] [:= :active true]]+ (contains? opts :table-ids) (conj [:in :id table-ids])+ (contains? opts :schema) (conj (if (= schema "")+ [:or [:= :schema nil] [:= :schema ""]]+ [:= :schema schema])))})))-(defn active-fields-for-tables+(mu/defn active-fields-for-tables"The active Fields of the Tables with `table-ids`, in field order."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Field {:where [:and[:in :table_id table-ids][:= :active true]]:order-by schema.table/field-order-rule}))-(defn active-fields+(mu/defn active-fields"The active Fields with `field-ids`."- [field-ids]+ [field-ids :- [:or [:set ::lib.schema.id/field] [:sequential ::lib.schema.id/field]]](t2/select :model/Field {:where [:and[:in :id field-ids][:= :active true]]}))diff --git a/enterprise/backend/src/metabase_enterprise/gsheets/db.clj b/enterprise/backend/src/metabase_enterprise/gsheets/db.cljindex 9f0c98b4fa5..478e7ce6d4c 100644--- a/enterprise/backend/src/metabase_enterprise/gsheets/db.clj+++ b/enterprise/backend/src/metabase_enterprise/gsheets/db.clj@@ -2,24 +2,28 @@"Application database queries for the gsheets module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn attached-dwh-database-id+(mu/defn attached-dwh-database-id"The ID of the attached data warehouse Database, or nil."[](t2/select-one-fn :id :model/Database :is_attached_dwh true))-(defn attached-dwh-database+(mu/defn attached-dwh-database"The attached data warehouse Database, or nil."[](t2/select-one :model/Database :is_attached_dwh true))-(defn setting+(mu/defn setting"The Setting with `setting-key`, or nil."- [setting-key]+ [setting-key :- :string](t2/select-one :model/Setting :key setting-key))-(defn update-database!- "Apply `changes` to the Database with `database-id`."- [database-id changes]+(mu/defn update-database!+ "Apply `changes` to the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ changes :- ::warehouses.schema/database.update](t2/update! :model/Database database-id changes))diff --git a/enterprise/backend/src/metabase_enterprise/impersonation/db.clj b/enterprise/backend/src/metabase_enterprise/impersonation/db.cljindex 54bd3636f52..45eeec52ea0 100644--- a/enterprise/backend/src/metabase_enterprise/impersonation/db.clj+++ b/enterprise/backend/src/metabase_enterprise/impersonation/db.clj@@ -2,37 +2,47 @@"Application database queries for the impersonation module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [malli.util :as mut]+ [metabase-enterprise.impersonation.schema :as impersonation.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn impersonation+(mu/defn impersonation"The ConnectionImpersonation with `impersonation-id`, or nil."- [impersonation-id]+ [impersonation-id :- ms/PositiveInt](t2/select-one :model/ConnectionImpersonation :id impersonation-id))-(defn impersonation-for-group-and-database+(mu/defn impersonation-for-group-and-database"The ConnectionImpersonation of the group with `group-id` on the Database with `database-id`, or nil."- [group-id database-id]+ [group-id :- ms/PositiveInt+ database-id :- ::lib.schema.id/database](t2/select-one :model/ConnectionImpersonation :group_id group-id :db_id database-id))-(defn all-impersonations+(mu/defn all-impersonations"Every ConnectionImpersonation, in ID order."[](t2/select :model/ConnectionImpersonation {:order-by [[:id :asc]]}))-(defn impersonations-for-groups+(mu/defn impersonations-for-groups"The ConnectionImpersonations of the groups with `group-ids`."- [group-ids]+ [group-ids :- [:set ms/PositiveInt]](t2/select :model/ConnectionImpersonation :group_id [:in group-ids]))-(defn impersonations-for-groups-and-database+(mu/defn impersonations-for-groups-and-database"The ConnectionImpersonations of the groups with `group-ids` on the Database with `database-id`."- [group-ids database-id]+ [group-ids :- [:set ms/PositiveInt]+ database-id :- ::lib.schema.id/database](t2/select :model/ConnectionImpersonation :group_id [:in group-ids] :db_id database-id))-(defn impersonations-matching+(mu/defn impersonations-matching"The ConnectionImpersonations narrowed by the optional `database-id`, `group-id`, and `group-ids`, excluding theDatabase with `excluded-database-id` when given."- [database-id group-id group-ids excluded-database-id]+ [database-id :- [:maybe ::lib.schema.id/database]+ group-id :- [:maybe ms/PositiveInt]+ group-ids :- [:maybe [:sequential ms/PositiveInt]]+ excluded-database-id :- [:maybe ::lib.schema.id/database]](t2/select :model/ConnectionImpersonation{:where [:and(when database-id [:= :db_id database-id])@@ -40,29 +50,31 @@(when group-ids [:in :group_id group-ids])(when excluded-database-id [:not [:= :db_id excluded-database-id]])]}))-(defn impersonation-exists-for-database?+(mu/defn impersonation-exists-for-database?"Whether the Database with `database-id` has a ConnectionImpersonation."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/ConnectionImpersonation :db_id database-id))-(defn insert-impersonation!+(mu/defn insert-impersonation!"Insert `impersonation` and return the new instance."- [impersonation]+ [impersonation :- (mut/select-keys ::impersonation.schema/connection-impersonation.update [:db_id :group_id :attribute])](first (t2/insert-returning-instances! :model/ConnectionImpersonation impersonation)))-(defn delete-impersonation!+(mu/defn delete-impersonation!"Delete the ConnectionImpersonation with `impersonation-id`."- [impersonation-id]+ [impersonation-id :- ms/PositiveInt](t2/delete! :model/ConnectionImpersonation :id impersonation-id))-(defn delete-impersonations-for-group-and-database!+(mu/defn delete-impersonations-for-group-and-database!"Delete the ConnectionImpersonations of the group with `group-id` on the Database with `database-id`."- [group-id database-id]+ [group-id :- ms/PositiveInt+ database-id :- ::lib.schema.id/database](t2/delete! :model/ConnectionImpersonation :group_id group-id :db_id database-id))-(defn view-data-permission-values+(mu/defn view-data-permission-values"The set of database-level view-data permission values `group-ids` hold on the Database with `database-id`."- [database-id group-ids]+ [database-id :- ::lib.schema.id/database+ group-ids :- [:set ms/PositiveInt]](t2/select-fn-set :perm_value :model/DataPermissions{:where [:and[:= :db_id database-id]@@ -70,12 +82,13 @@[:= :perm_type "perms/view-data"][:in :group_id group-ids]]}))-(defn group-ids-for-user- "The IDs of the PermissionsGroups the User with `user-id` belongs to."- [user-id]+(mu/defn group-ids-for-user+ "The IDs of the PermissionsGroups the User with `user-id` belongs to, or nil when `user-id` is nil (e.g. no+ current user, such as an internal or unauthenticated context)."+ [user-id :- [:maybe ::lib.schema.id/user]](t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id user-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))diff --git a/enterprise/backend/src/metabase_enterprise/impersonation/schema.clj b/enterprise/backend/src/metabase_enterprise/impersonation/schema.cljnew file mode 100644index 00000000000..e99afe45fcd--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/impersonation/schema.clj@@ -0,0 +1,21 @@+(ns metabase-enterprise.impersonation.schema+ "Malli schemas for the impersonation module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::connection-impersonation+ "A ConnectionImpersonation as selected from the app DB: every column of `:connection_impersonations`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:db_id ::lib.schema.id/database]+ [:group_id ms/PositiveInt]+ [:attribute [:maybe :string]]])++(mr/def ::connection-impersonation.update+ "What an update (or insert) of a ConnectionImpersonation accepts: every column of `:connection_impersonations` except `id`, all optional."+ [:map {:closed true}+ [:db_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:attribute {:optional true} [:maybe :string]]])diff --git a/enterprise/backend/src/metabase_enterprise/library/db.clj b/enterprise/backend/src/metabase_enterprise/library/db.cljindex c3156f579c6..23e013a4a8f 100644--- a/enterprise/backend/src/metabase_enterprise/library/db.clj+++ b/enterprise/backend/src/metabase_enterprise/library/db.clj@@ -3,19 +3,21 @@additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require[metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn card-types-in-collections+(mu/defn card-types-in-collections"The set of Card types present in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-fn-set :type [:model/Card :type] :collection_id [:in collection-ids]))-(defn published-table-in-collections?+(mu/defn published-table-in-collections?"Whether a published Table exists in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/exists? :model/Table :is_published true :collection_id [:in collection-ids]))-(defn library-collections+(mu/defn library-collections"The readable Library, Library-data, and Library-metrics Collections, ordered by name."[](t2/select :model/Collection@@ -31,12 +33,12 @@:archive-operation-id nil})]:order-by [[:%lower.name :asc]]}))-(defn collection-type+(mu/defn collection-type"The type of the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one-fn :type [:model/Collection :type] :id collection-id))-(defn unarchived-card-collection-types-reducible+(mu/defn unarchived-card-collection-types-reducible"Reducible distinct Collection ID and Card type pairs of the unarchived Cards."[](t2/reducible-query {:select-distinct [:collection_id :type]diff --git a/enterprise/backend/src/metabase_enterprise/mcp/db.clj b/enterprise/backend/src/metabase_enterprise/mcp/db.cljindex db284d48c53..5819f652ebd 100644--- a/enterprise/backend/src/metabase_enterprise/mcp/db.clj+++ b/enterprise/backend/src/metabase_enterprise/mcp/db.clj@@ -2,39 +2,50 @@"Application database queries for the mcp module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.mcp.schema :as mcp.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn session-log-exists?+(mu/defn session-log-exists?"Whether an McpSessionLog with `session-id` exists."- [session-id]+ [session-id :- :string](t2/exists? :model/McpSessionLog :id session-id))-(defn session-client-identity+(mu/defn session-client-identity"The client name and version of the McpSessionLog with `session-id`, or nil."- [session-id]+ [session-id :- :string](t2/select-one [:model/McpSessionLog :client_name :client_version] :id session-id))-(defn insert-session-log!+(mu/defn insert-session-log!"Insert the McpSessionLog `row`."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} :string]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:client_name {:optional true} [:maybe :string]]+ [:client_version {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]]](t2/insert! :model/McpSessionLog row))-(defn end-session-log!+(mu/defn end-session-log!"Stamp `ended_at` on the McpSessionLog with `session-id`."- [session-id]+ [session-id :- :string](t2/update! :model/McpSessionLog :id session-id {:ended_at :%now}))-(defn insert-tool-call-log!+(mu/defn insert-tool-call-log!"Insert the McpToolCallLog `row`."- [row]+ [row :- ::mcp.schema/mcp-tool-call-log.update](t2/insert! :model/McpToolCallLog row))-(defn delete-tool-call-logs-created-before!+(mu/defn delete-tool-call-logs-created-before!"Delete the McpToolCallLogs created before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/McpToolCallLog {:where [:< :created_at cutoff]}))-(defn delete-session-logs-created-before!+(mu/defn delete-session-logs-created-before!"Delete the McpSessionLogs created before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/McpSessionLog {:where [:< :created_at cutoff]}))diff --git a/enterprise/backend/src/metabase_enterprise/metabot/db.clj b/enterprise/backend/src/metabase_enterprise/metabot/db.cljindex ac85307f3bd..91032a4e5c5 100644--- a/enterprise/backend/src/metabase_enterprise/metabot/db.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot/db.clj@@ -2,8 +2,12 @@"Application database queries for the metabot module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.metabot.schema :as metabot.schema][metabase.permissions.core :as perms][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(defn- default-group-ids@@ -19,19 +23,20 @@[:not-in :group_id (default-group-ids)][:in :group_id (conj (default-group-ids) (u/the-id (perms/admin-group)))]))-(defn all-groups+(mu/defn all-groups"Every PermissionsGroup, in ID order."[](t2/select :model/PermissionsGroup {:order-by [[:id :asc]]}))-(defn all-stored-permissions+(mu/defn all-stored-permissions"Every MetabotPermissions row, ordered by group and permission type."[](t2/select :model/MetabotPermissions {:order-by [[:group_id :asc] [:perm_type :asc]]}))-(defn visible-permissions-for-user+(mu/defn visible-permissions-for-user"The MetabotPermissions rows of the groups of the User with `user-id` that the mode selected by `advanced?` shows."- [user-id advanced?]+ [user-id :- ::lib.schema.id/user+ advanced? :- :boolean](t2/select :model/MetabotPermissions{:where [:and[:in :group_id@@ -41,40 +46,47 @@:where [:= :user_id user-id]}](visible-groups-expr advanced?)]}))-(defn permission-exists?+(mu/defn permission-exists?"Whether the group with `group-id` has a MetabotPermissions row of `perm-type`."- [group-id perm-type]+ [group-id :- ms/PositiveInt+ perm-type :- :keyword](t2/exists? :model/MetabotPermissions :group_id group-id :perm_type perm-type))-(defn update-permission-value!+(mu/defn update-permission-value!"Set the value of the MetabotPermissions row of `perm-type` for the group with `group-id`."- [group-id perm-type perm-value]+ [group-id :- ms/PositiveInt+ perm-type :- :keyword+ perm-value :- :keyword](t2/update! :model/MetabotPermissions {:group_id group-id :perm_type perm-type} {:perm_value perm-value}))-(defn insert-permission!+(mu/defn insert-permission!"Insert the MetabotPermissions `row`."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:perm_type {:optional true} [:maybe [:or :keyword :string]]]+ [:perm_value {:optional true} [:maybe [:or :keyword :string]]]]](t2/insert! :model/MetabotPermissions row))-(defn delete-hidden-group-permissions!+(mu/defn delete-hidden-group-permissions!"Delete the MetabotPermissions rows of the groups the mode selected by `advanced?` hides."- [advanced?]+ [advanced? :- :boolean](t2/delete! :model/MetabotPermissions {:where [:not (visible-groups-expr advanced?)]}))-(defn group-limits+(mu/defn group-limits"Every MetabotGroupLimit, in group order."[](t2/select :model/MetabotGroupLimit {:order-by [[:group_id :asc]]}))-(defn group-limit+(mu/defn group-limit"The MetabotGroupLimit of the group with `group-id`, or nil."- [group-id]+ [group-id :- ms/PositiveInt](t2/select-one :model/MetabotGroupLimit :group_id group-id))-(defn max-usage-for-user+(mu/defn max-usage-for-user"The `:max_usage` row holding the largest group limit of the User with `user-id`, or nil if any of their groupsis unlimited."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/query-one {:select [[[:case[:= [[:count :*]] [[:count :gl.max_usage]]][[:max :gl.max_usage]]]@@ -83,49 +95,57 @@:left-join [[:metabot_group_limit :gl] [:= :pgm.group_id :gl.group_id]]:where [:= :pgm.user_id user-id]}))-(defn insert-group-limit!+(mu/defn insert-group-limit!"Insert the MetabotGroupLimit `row`."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:max_usage {:optional true} [:maybe :int]]]](t2/insert! :model/MetabotGroupLimit row))-(defn update-group-limit!+(mu/defn update-group-limit!"Set the maximum usage of the MetabotGroupLimit with `limit-id`."- [limit-id max-usage]+ [limit-id :- ms/PositiveInt+ max-usage :- [:maybe :int]](t2/update! :model/MetabotGroupLimit limit-id {:max_usage max-usage}))-(defn delete-group-limit!+(mu/defn delete-group-limit!"Delete the MetabotGroupLimit of the group with `group-id`."- [group-id]+ [group-id :- ms/PositiveInt](t2/delete! :model/MetabotGroupLimit :group_id group-id))-(defn instance-limit+(mu/defn instance-limit"The MetabotInstanceLimit of the Tenant with `tenant-id` (nil for the instance-wide limit), or nil."- [tenant-id]+ [tenant-id :- [:maybe ms/PositiveInt]](t2/select-one :model/MetabotInstanceLimit :tenant_id tenant-id))-(defn tenant-limits+(mu/defn tenant-limits"The MetabotInstanceLimits of tenants, ordered by tenant."[](t2/select :model/MetabotInstanceLimit :tenant_id [:not= nil] {:order-by [[:tenant_id :asc]]}))-(defn insert-instance-limit!+(mu/defn insert-instance-limit!"Insert the MetabotInstanceLimit `row`."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:max_usage {:optional true} [:maybe :int]]]](t2/insert! :model/MetabotInstanceLimit row))-(defn update-instance-limit!+(mu/defn update-instance-limit!"Set the maximum usage of the MetabotInstanceLimit with `limit-id`."- [limit-id max-usage]+ [limit-id :- ms/PositiveInt+ max-usage :- [:maybe :int]](t2/update! :model/MetabotInstanceLimit limit-id {:max_usage max-usage}))-(defn delete-instance-limit!+(mu/defn delete-instance-limit!"Delete the MetabotInstanceLimit of the Tenant with `tenant-id`."- [tenant-id]+ [tenant-id :- [:maybe ms/PositiveInt]](t2/delete! :model/MetabotInstanceLimit :tenant_id tenant-id))-(defn insert-usage-log!+(mu/defn insert-usage-log!"Insert the AiUsageLog `row`."- [row]+ [row :- ::metabot.schema/ai-usage-log.update](t2/insert! :model/AiUsageLog row))(defn- usage-window-expr@@ -135,36 +155,40 @@(when user-id [:= :user_id user-id])(when tenant-id [:= :tenant_id tenant-id])])-(defn usage-token-sum+(mu/defn usage-token-sum"The `:sum` row of tokens logged since `period-start`, narrowed by the optional `user-id` and `tenant-id`."- [period-start user-id tenant-id]+ [period-start :- ms/TemporalInstant+ user-id :- [:maybe ::lib.schema.id/user]+ tenant-id :- [:maybe ms/PositiveInt]](t2/query-one {:select [[[:sum :total_tokens] :sum]]:from [:ai_usage_log]:where (usage-window-expr period-start user-id tenant-id)}))-(defn usage-message-count+(mu/defn usage-message-count"The `:cnt` row of messages logged since `period-start`, narrowed by the optional `user-id` and `tenant-id`."- [period-start user-id tenant-id]+ [period-start :- ms/TemporalInstant+ user-id :- [:maybe ::lib.schema.id/user]+ tenant-id :- [:maybe ms/PositiveInt]](t2/query-one {:select [[[:count :*] :cnt]]:from [:ai_usage_log]:where (usage-window-expr period-start user-id tenant-id)}))-(defn delete-usage-logs-created-before!+(mu/defn delete-usage-logs-created-before!"Delete the AiUsageLogs created before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/AiUsageLog {:where [:< :created_at cutoff]}))-(defn transform+(mu/defn transform"The Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/Transform :id transform-id))-(defn transforms+(mu/defn transforms"The Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:sequential ::lib.schema.id/transform]](t2/select :model/Transform :id [:in transform-ids]))-(defn cards+(mu/defn cards"The Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select :model/Card :id [:in card-ids]))diff --git a/enterprise/backend/src/metabase_enterprise/metabot/schema.clj b/enterprise/backend/src/metabase_enterprise/metabot/schema.cljnew file mode 100644index 00000000000..a432cd5eda9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/metabot/schema.clj@@ -0,0 +1,46 @@+(ns metabase-enterprise.metabot.schema+ "Malli schemas for the metabot module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::metabot-group-limit+ "A MetabotGroupLimit as selected from the app DB: every column of `:metabot_group_limit`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:group_id ms/PositiveInt]+ [:max_usage :int]])++(mr/def ::metabot-group-limit.update+ "What an update (or insert) of a MetabotGroupLimit accepts: every column of `:metabot_group_limit` except `id`, all optional."+ [:map {:closed true}+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:max_usage {:optional true} [:maybe :int]]])++(mr/def ::metabot-instance-limit+ "A MetabotInstanceLimit as selected from the app DB: every column of `:metabot_instance_limit`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:max_usage [:maybe :int]]])++(mr/def ::metabot-instance-limit.update+ "What an update (or insert) of a MetabotInstanceLimit accepts: every column of `:metabot_instance_limit` except `id`, all optional."+ [:map {:closed true}+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:max_usage {:optional true} [:maybe :int]]])++(mr/def ::metabot-permissions+ "A MetabotPermissions as selected from the app DB: every column of `:metabot_permissions`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:group_id ms/PositiveInt]+ [:perm_type [:or :keyword :string]]+ [:perm_value [:or :keyword :string]]])++(mr/def ::metabot-permissions.update+ "What an update (or insert) of a MetabotPermissions accepts: every column of `:metabot_permissions` except `id`, all optional."+ [:map {:closed true}+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:perm_type {:optional true} [:maybe [:or :keyword :string]]]+ [:perm_value {:optional true} [:maybe [:or :keyword :string]]]])diff --git a/enterprise/backend/src/metabase_enterprise/metabot_analytics/db.clj b/enterprise/backend/src/metabase_enterprise/metabot_analytics/db.cljindex 5d34d1fab37..8f126d255da 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_analytics/db.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_analytics/db.clj@@ -2,15 +2,18 @@"Application database queries for the metabot-analytics module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.permissions.core :as perms][metabase.query-processor.parameters.dates :as qp.parameters.dates][metabase.util.date-2 :as u.date][metabase.util.i18n :refer [tru]]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn conversation+(mu/defn conversation"The MetabotConversation with `conversation-id`, or nil."- [conversation-id]+ [conversation-id :- :string](t2/select-one :model/MetabotConversation :id conversation-id))(defn- date-range-expr@@ -104,11 +107,20 @@[:core_user :u] [:= :u.id :c.user_id]]:group-by [:c.id]})-(defn list-conversations+(mu/defn list-conversations"A page of conversation summary rows (see [[conversation-list-select]]), restricted to `user-id`, `group-id`,`tenant-id`, and `date` (each nil for no restriction), sorted by the allow-listed `sort-by` column name in`sort-direction` (`:asc` or `:desc`), skipping `offset` and returning up to `limit`."- [{:keys [user-id group-id tenant-id date sort-by sort-direction offset limit]}]+ [{:keys [user-id group-id tenant-id date sort-by sort-direction offset limit]}+ :- [:map {:closed true}+ [:user-id {:optional true} [:maybe ::lib.schema.id/user]]+ [:group-id {:optional true} [:maybe ms/PositiveInt]]+ [:tenant-id {:optional true} [:maybe ms/PositiveInt]]+ [:date {:optional true} [:maybe :string]]+ [:sort-by {:optional true} [:maybe :string]]+ [:sort-direction {:optional true} [:enum :asc :desc]]+ [:offset {:optional true} ms/IntGreaterThanOrEqualToZero]+ [:limit {:optional true} ms/PositiveInt]]](let [where (conversation-list-expr {:user-id user-id :group-id group-id :tenant-id tenant-id :date date})sort-exprs (get sort-columns sort-by [:c.created_at])order-by (conj (mapv #(vector % sort-direction) sort-exprs)@@ -120,29 +132,34 @@:offset offset)where (assoc :where where)))))-(defn conversation-count+(mu/defn conversation-count"The total count of conversations matching the same criteria as [[list-conversations]] (ignoring sort, offset,and limit)."- [{:keys [user-id group-id tenant-id date]}]+ [{:keys [user-id group-id tenant-id date]}+ :- [:map {:closed true}+ [:user-id {:optional true} [:maybe ::lib.schema.id/user]]+ [:group-id {:optional true} [:maybe ms/PositiveInt]]+ [:tenant-id {:optional true} [:maybe ms/PositiveInt]]+ [:date {:optional true} [:maybe :string]]]](let [where (conversation-list-expr {:user-id user-id :group-id group-id :tenant-id tenant-id :date date})](:count (t2/query-one (cond-> {:select [[[:count :*] :count]]:from [[:metabot_conversation :c]]}where (assoc :where where))))))-(defn messages-for-conversation+(mu/defn messages-for-conversation"The MetabotMessages of the MetabotConversation with `conversation-id`, oldest first."- [conversation-id]+ [conversation-id :- :string](t2/select :model/MetabotMessage :conversation_id conversation-id {:order-by [[:created_at :asc] [:id :asc]]}))-(defn message-data-for-conversations+(mu/defn message-data-for-conversations"The conversation, data, and data version of the MetabotMessages of the MetabotConversations with`conversation-ids`."- [conversation-ids]+ [conversation-ids :- [:sequential :string]](t2/select [:model/MetabotMessage :conversation_id :data :data_version] :conversation_id [:in conversation-ids]))-(defn feedback-for-conversation+(mu/defn feedback-for-conversation"The MetabotFeedback rows on the messages of the MetabotConversation with `conversation-id`, oldest first."- [conversation-id]+ [conversation-id :- :string](t2/select :model/MetabotFeedback{:select [:metabot_feedback.id:metabot_feedback.message_iddiff --git a/enterprise/backend/src/metabase_enterprise/mfa/db.clj b/enterprise/backend/src/metabase_enterprise/mfa/db.cljindex b1659f07de7..bf637bd8783 100644--- a/enterprise/backend/src/metabase_enterprise/mfa/db.clj+++ b/enterprise/backend/src/metabase_enterprise/mfa/db.clj@@ -3,7 +3,12 @@additional logic, so the rest of the module only touches `toucan2.core` for transactions."(:require[clojure.string :as str]+ [malli.util :as mut]+ [metabase.auth-identity.schema :as auth-identity.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private totp-provider "totp")@@ -53,65 +58,80 @@[:like :%lower.last_name pattern][:like :%lower.email pattern]]))))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User :id user-id))-(defn user-email+(mu/defn user-email"The email of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :email :model/User :id user-id))-(defn lock-user+(mu/defn lock-user"The `:id` row of the User with `user-id`, locked for update."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id] :id user-id {:for :update}))-(defn totp-identity+(mu/defn totp-identity"The TOTP AuthIdentity of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/AuthIdentity :user_id user-id :provider totp-provider))-(defn lock-totp-identity+(mu/defn lock-totp-identity"The TOTP AuthIdentity of the User with `user-id`, locked for update, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/AuthIdentity :user_id user-id :provider totp-provider {:for :update}))-(defn password-credentials+(mu/defn password-credentials"The password credentials of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :credentials :model/AuthIdentity :user_id user-id :provider "password"))-(defn insert-auth-identity!- "Insert the AuthIdentity `row`."- [row]+(mu/defn insert-auth-identity!+ "Insert the AuthIdentity `row`, returning the number inserted."+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:provider {:optional true} [:maybe [:or :keyword :string]]]+ [:credentials {:optional true} [:maybe :map]]+ [:metadata {:optional true} [:maybe :map]]+ [:provider_id {:optional true} [:maybe :string]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:confirmed_at {:optional true} [:maybe ms/TemporalInstant]]]](t2/insert! :model/AuthIdentity row))-(defn update-auth-identity!- "Apply `changes` to the AuthIdentity with `auth-identity-id`."- [auth-identity-id changes]+(mu/defn update-auth-identity!+ "Apply `changes` to the AuthIdentity with `auth-identity-id`, returning the number updated."+ [auth-identity-id :- ms/PositiveInt+ changes :- (mut/select-keys ::auth-identity.schema/auth-identity.update [:credentials :confirmed_at])](t2/update! :model/AuthIdentity auth-identity-id changes))-(defn delete-totp-identity!+(mu/defn delete-totp-identity!"Delete the TOTP AuthIdentity of the User with `user-id`, returning the number deleted."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/AuthIdentity :user_id user-id :provider totp-provider))-(defn confirmed-totp-count+(mu/defn confirmed-totp-count"The number of confirmed TOTP enrollments."[](t2/count :model/AuthIdentity :provider totp-provider :confirmed_at [:not= nil]))-(defn unenrolled-user-count+(mu/defn unenrolled-user-count"The number of active personal Users without a confirmed TOTP enrollment."[](t2/count :model/User {:where unenrolled-user-where}))-(defn user-list+(mu/defn user-list"The name-ordered admin list of enrolled (with their enrollment time) or unenrolled Users matching `search`, pagedby the optional `limit` and `offset`."- [enrolled? search limit offset]+ [enrolled? :- :boolean+ search :- [:maybe :string]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/User(cond-> {:select (cond-> list-columns enrolled? (into enrolled-at-select)):where (user-list-where enrolled? search)@@ -121,7 +141,8 @@;; a nil limit would emit `LIMIT NULL`limit (assoc :limit limit :offset offset))))-(defn user-list-count+(mu/defn user-list-count"The number of enrolled or unenrolled Users matching `search`."- [enrolled? search]+ [enrolled? :- :boolean+ search :- [:maybe :string]](t2/count :model/User {:where (user-list-where enrolled? search)}))diff --git a/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj b/enterprise/backend/src/metabase_enterprise/permission_debug/db.cljindex 5feca7177b7..17444ac26a8 100644--- a/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj+++ b/enterprise/backend/src/metabase_enterprise/permission_debug/db.clj@@ -2,17 +2,19 @@"Application database queries for the permission-debug module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.permissions.core :as perms]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn user-superuser?+(mu/defn user-superuser?"Whether the User with `user-id` is a superuser."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :is_superuser :model/User :id user-id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))(defn- blocked-tables-select@@ -31,14 +33,26 @@{:user-id user-id :is-superuser? false}permissions-granting)]]]})-(defn blocked-tables-in-database+(mu/defn blocked-tables-in-database"The `[db-name schema table-name group-name]` rows of every Table in the Database with `database-id` blocked (per- `permissions-blocking`) for the User with `user-id`, excluding those granted (per `permissions-granting`)."- [user-id database-id permissions-blocking permissions-granting]+ `permissions-blocking`) for the User with `user-id`, excluding those granted (per `permissions-granting`).++ `permissions-blocking`/`permissions-granting` are plain permission-type -> permission-value (or+ `[value :most|:least]`) mappings, per `perms/PermissionMapping`; db.clj builds the Honey SQL from them."+ [user-id :- ::lib.schema.id/user+ database-id :- ::lib.schema.id/database+ permissions-blocking :- perms/PermissionMapping+ permissions-granting :- perms/PermissionMapping](t2/query (blocked-tables-select user-id [:= :blocked.db_id database-id] permissions-blocking permissions-granting)))-(defn blocked-tables-among+(mu/defn blocked-tables-among"The `[db-name schema table-name group-name]` rows of the Tables with `table-ids` blocked (per- `permissions-blocking`) for the User with `user-id`, excluding those granted (per `permissions-granting`)."- [user-id table-ids permissions-blocking permissions-granting]+ `permissions-blocking`) for the User with `user-id`, excluding those granted (per `permissions-granting`).++ `permissions-blocking`/`permissions-granting` are plain permission-type -> permission-value (or+ `[value :most|:least]`) mappings, per `perms/PermissionMapping`; db.clj builds the Honey SQL from them."+ [user-id :- ::lib.schema.id/user+ table-ids :- [:set ::lib.schema.id/table]+ permissions-blocking :- perms/PermissionMapping+ permissions-granting :- perms/PermissionMapping](t2/query (blocked-tables-select user-id [:in :blocked.id table-ids] permissions-blocking permissions-granting)))diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/db.cljindex 148aae75981..625b562ddea 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj@@ -2,25 +2,62 @@"Application database queries for the remote-sync module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase-enterprise.remote-sync.schema :as remote-sync.schema][metabase.collections.core :as collections]+ [metabase.collections.schema :as collections.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn eligible-children+(def ^:private Conditions+ "A map of column to value (possibly nil) or Toucan 2 operator-vector value, or nil for none."+ [:maybe [:map-of :keyword [:maybe [:or :string :int :boolean :keyword sequential?]]]])++(def ^:private RemovalOpts+ "The `:scope-key`, `:synced-collection-ids`, `:entity-ids`, and `:removal-conditions` describing which rows an+ import removes (see [[removal-exprs]])."+ [:map {:closed true}+ [:scope-key [:maybe :keyword]]+ [:synced-collection-ids [:maybe [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]]]+ [:entity-ids [:maybe [:or [:set :string] [:sequential :string]]]]+ [:removal-conditions Conditions]])++(def ^:private ModelId+ "A `:model_id`: the primary key of the referenced entity, or the `-1` sentinel+ (`metabase-enterprise.remote-sync.settings/transforms-root-id`) standing in for the virtual Transforms root+ Collection."+ [:or ms/PositiveInt [:= -1]])++(def ^:private Path+ "A `{:db_name :schema :table_name :field_name}` path used to locate a Table or Field."+ [:map+ [:db_name :string]+ [:schema {:optional true} [:maybe :string]]+ [:table_name :string]+ [:field_name {:optional true} :string]])++(mu/defn eligible-children"The instances of `model-key` whose `fk` column equals `model-id`, additionally matching `conditions` (a map ofcolumn to value or Toucan 2 operator-vector value, or nil for none)."- [model-key fk model-id conditions]+ [model-key :- :keyword+ fk :- :keyword+ model-id :- ms/PositiveInt+ conditions :- Conditions](apply t2/select model-key fk model-id (mapcat identity conditions)))-(defn ids-where+(mu/defn ids-where"The IDs of the instances of `model-key` matching `conditions` (a map of column to value or Toucan 2operator-vector value, or nil for every instance)."- [model-key conditions]+ [model-key :- :keyword+ conditions :- Conditions](apply t2/select-fn-set :id model-key (mapcat identity conditions)))-(defn count-where+(mu/defn count-where"The number of instances of `model-key` matching `conditions` (a map of column to value or Toucan 2operator-vector value, or nil for every instance)."- [model-key conditions]+ [model-key :- :keyword+ conditions :- Conditions](apply t2/count model-key (mapcat identity conditions)))(defn- removal-condition-exprs@@ -45,10 +82,11 @@(seq entity-ids) (conj [:not-in :entity_id entity-ids]):always (into (removal-condition-exprs removal-conditions)))))-(defn delete-removed-instances!+(mu/defn delete-removed-instances!"Deletes the `model-key` rows an import removes (see [[removal-exprs]]); a no-op for a scoped model with nosynced collections, and a delete of every row when nothing restricts it."- [model-key removal-opts]+ [model-key :- :keyword+ removal-opts :- RemovalOpts](when-let [exprs (removal-exprs removal-opts)](if (seq exprs)(t2/delete! model-key {:where (if (= 1 (count exprs)) (first exprs) (into [:and] exprs))})@@ -71,50 +109,64 @@(let [id-column (keyword (str (name (t2/table-name model-key)) ".id"))](into [:and (unsynced-anti-join-expr model-type id-column)] (removal-exprs removal-opts))))-(defn unsynced-instance-count+(mu/defn unsynced-instance-count"The number of `model-key` rows [[delete-removed-instances!]] would remove (see [[removal-exprs]]) that alsohave no RemoteSyncObject of `model-type` in synced status — unsynced local work an import would otherwise wipeout."- [model-key model-type removal-opts]+ [model-key :- :keyword+ model-type :- :string+ removal-opts :- RemovalOpts](t2/count model-key {:where (unsynced-instance-expr model-key model-type removal-opts)}))-(defn unsynced-instance-names+(mu/defn unsynced-instance-names"Up to `limit` names of the rows [[unsynced-instance-count]] counts."- [model-key model-type removal-opts limit]+ [model-key :- :keyword+ model-type :- :string+ removal-opts :- RemovalOpts+ limit :- ms/PositiveInt](t2/select-fn-vec :name model-key {:where (unsynced-instance-expr model-key model-type removal-opts):limit limit}))-(defn instance+(mu/defn instance"The instance of `model` with `id`, or nil."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one model :id id))-(defn instance-with-columns+(mu/defn instance-with-columns"The `columns` of the instance of `model` with `id`, or nil."- [model columns id]+ [model :- :keyword+ columns :- [:sequential :keyword]+ id :- ms/PositiveInt](t2/select-one (into [model] columns) :id id))-(defn instance-names+(mu/defn instance-names"The `:id` and `:name` of the instances of `model` with `ids`."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select [model :id :name] :id [:in ids]))-(defn instances-in-collections+(mu/defn instances-in-collections"The instances of `model` in the Collections with `collection-ids`, excluding those archived under the optional`archived-key` column."- [model collection-ids archived-key]+ [model :- :keyword+ collection-ids :- [:sequential ::lib.schema.id/collection]+ archived-key :- [:maybe :keyword]](t2/select model {:where [:and[:in :collection_id collection-ids](when archived-key [:= archived-key false])]}))-(defn instances-with-columns-by-entity-ids+(mu/defn instances-with-columns-by-entity-ids"The `columns` of the instances of `model` with `entity-ids`."- [model columns entity-ids]+ [model :- :keyword+ columns :- [:sequential :keyword]+ entity-ids :- [:set :string]](t2/select (into [model] columns) :entity_id [:in entity-ids]))-(defn delete-instances!+(mu/defn delete-instances!"Delete the instances of `model` with `ids`."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/delete! model :id [:in ids]))(defn- tracking-select-parts@@ -136,17 +188,19 @@:from [[:measure :s]]:join [[:metabase_table :t] [:= :s.table_id :t.id]]}))-(defn tracking-details-by-id+(mu/defn tracking-details-by-id"The name, table id, collection id, and table name of the `model-key` (Field, Segment, or Measure) instance with`model-id`, or nil."- [model-key model-id]+ [model-key :- :keyword+ model-id :- ms/PositiveInt](let [{:keys [alias select from join]} (tracking-select-parts model-key)](first (t2/query {:select select :from from :join join :where [:= (keyword alias "id") model-id]}))))-(defn tracking-details-by-entity-ids+(mu/defn tracking-details-by-entity-ids"The `:id`, name, table id, collection id, and table name of the `model-key` (Segment or Measure) instances with`entity-ids`."- [model-key entity-ids]+ [model-key :- :keyword+ entity-ids :- [:or [:set :string] [:sequential :string]]](let [{:keys [alias select from join]} (tracking-select-parts model-key)id-column (keyword alias "id")](t2/query {:select (into [id-column] select)@@ -154,24 +208,28 @@:join join:where [:in (keyword alias "entity_id") entity-ids]})))-(defn entity-id+(mu/defn entity-id"The entity ID of the instance of `model` with `id`."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one-fn :entity_id model :id id))-(defn entity-ids-by-id+(mu/defn entity-ids-by-id"A map of ID to entity ID for the instances of `model` with `ids`."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :entity_id model :id [:in ids]))-(defn existing-entity-ids+(mu/defn existing-entity-ids"The subset of `entity-ids` that instances of `model` have."- [model entity-ids]+ [model :- :keyword+ entity-ids :- [:or [:set :string] [:sequential :string]]](t2/select-fn-set :entity_id model :entity_id [:in entity-ids]))-(defn ids-by-entity-ids+(mu/defn ids-by-entity-ids"The IDs of the instances of `model` with `entity-ids`."- [model entity-ids]+ [model :- :keyword+ entity-ids :- [:set :string]](t2/select-pks-vec model :entity_id [:in entity-ids]))(defn- path-expr@@ -188,35 +246,35 @@(and has-field? field_name)(conj [:= :f.name field_name]))))))-(defn tables-at-paths+(mu/defn tables-at-paths"The `:id`, `:name`, and `:collection_id` rows of the Tables at `paths` (`{:db_name :schema :table_name}`)."- [paths]+ [paths :- [:sequential Path]](t2/query {:select [:t.id :t.name :t.collection_id]:from [[:metabase_table :t]]:join [[:metabase_database :db] [:= :db.id :t.db_id]]:where (path-expr paths false)}))-(defn fields-at-paths+(mu/defn fields-at-paths"The `:id`, `:name`, `:table_id`, `:collection_id`, and `:table_name` rows of the Fields at `paths`(`{:db_name :schema :table_name :field_name}`)."- [paths]+ [paths :- [:sequential Path]](t2/query {:select [:f.id :f.name :f.table_id [:t.collection_id :collection_id] [:t.name :table_name]]:from [[:metabase_field :f]]:join [[:metabase_table :t] [:= :t.id :f.table_id][:metabase_database :db] [:= :db.id :t.db_id]]:where (path-expr paths true)}))-(defn card-types+(mu/defn card-types"The `:id`, `:type`, and `:card_schema` of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :type :card_schema] :id [:in card-ids]))-(defn field-user-settings-exist?+(mu/defn field-user-settings-exist?"Whether the Field with `field-id` has FieldUserSettings."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/exists? :model/FieldUserSettings :field_id field-id))-(defn snippets+(mu/defn snippets"The `:id`, `:name`, and `:collection_id` of every NativeQuerySnippet."[](t2/select [:model/NativeQuerySnippet :id :name :collection_id]))@@ -228,57 +286,57 @@(for [collection collections][:like :location (str (collections/location-path collection) "%")])))-(defn collections+(mu/defn collections"The Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select :model/Collection :id [:in collection-ids]))-(defn collections-by-id+(mu/defn collections-by-id"A map of ID to the ID, name, location, and personal owner of the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select-pk->fn identity [:model/Collection :id :name :location :personal_owner_id] :id [:in collection-ids]))-(defn collection-sync-states+(mu/defn collection-sync-states"The `:id` and `:is_remote_synced` of the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select [:model/Collection :id :is_remote_synced] :id [:in collection-ids]))-(defn collection-name-and-id+(mu/defn collection-name-and-id"The `:name` and `:collection_id` (its own ID) of the Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one [:model/Collection :name [:id :collection_id]] :id collection-id))-(defn library-collection+(mu/defn library-collection"The Library Collection of `library-type`, or nil."- [library-type]+ [library-type :- :string](t2/select-one :model/Collection :type library-type))-(defn snippet-collections+(mu/defn snippet-collections"The `:id` and `:name` of the Collections of the snippets namespace."[](t2/select [:model/Collection :id :name] :namespace "snippets"))-(defn snippet-collection-ids+(mu/defn snippet-collection-ids"The IDs of the Collections of the snippets namespace."[](t2/select-pks-set :model/Collection :namespace "snippets"))-(defn collections-in-namespace+(mu/defn collections-in-namespace"The `:id` and `:entity_id` of the Collections of `namespace-name`."- [namespace-name]+ [namespace-name :- :string](t2/select [:model/Collection :id :entity_id] :namespace namespace-name))-(defn collection-ids-in-namespace+(mu/defn collection-ids-in-namespace"The IDs of the Collections of `namespace-name`."- [namespace-name]+ [namespace-name :- :string](t2/select-pks-vec :model/Collection :namespace namespace-name))-(defn remote-synced-collection-ids+(mu/defn remote-synced-collection-ids"The IDs of the remote-synced Collections."[](t2/select-pks-vec :model/Collection :is_remote_synced true))-(defn unarchived-remote-synced-root-collection-ids+(mu/defn unarchived-remote-synced-root-collection-ids"The IDs of the unarchived remote-synced root Collections."[](t2/select-fn-set :id :model/Collection@@ -287,40 +345,40 @@[:= :location "/"][:not :archived]]}))-(defn unarchived-root-collection-ids-in-namespace+(mu/defn unarchived-root-collection-ids-in-namespace"The IDs of the unarchived root Collections of `namespace-name`."- [namespace-name]+ [namespace-name :- :string](t2/select-fn-set :id :model/Collection{:where [:and[:= :namespace namespace-name][:= :location "/"][:not :archived]]}))-(defn subtree-collection-ids+(mu/defn subtree-collection-ids"The IDs of `collections` and all of their descendants."- [collections]+ [collections :- [:sequential ::collections.schema/collection]](t2/select-pks-set :model/Collection {:where (subtree-expr collections)}))-(defn remote-synced-subtree-collection-ids+(mu/defn remote-synced-subtree-collection-ids"The IDs of the remote-synced Collections among `collections` and their descendants."- [collections]+ [collections :- [:sequential ::collections.schema/collection]](t2/select-pks-set :model/Collection{:where [:and[:= :is_remote_synced true](subtree-expr collections)]}))-(defn mark-subtree-remote-synced!+(mu/defn mark-subtree-remote-synced!"Mark `collections` and all of their descendants as remote-synced."- [collections]+ [collections :- [:sequential ::collections.schema/collection]](t2/query {:update (t2/table-name :model/Collection):set {:is_remote_synced true}:where [:and[:= :is_remote_synced false](subtree-expr collections)]}))-(defn unmark-collections-remote-synced!+(mu/defn unmark-collections-remote-synced!"Mark the Collections with `collection-ids` as not remote-synced."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/query {:update (t2/table-name :model/Collection):set {:is_remote_synced false}:where [:in :id collection-ids]}))@@ -339,137 +397,151 @@[:and [:= :model_type model_type] [:= :model_id model_id]]))rows))-(defn rso+(mu/defn rso"The RemoteSyncObject of the entity `model-type` `model-id`, or nil."- [model-type model-id]+ [model-type :- :string+ model-id :- ModelId](t2/select-one :model/RemoteSyncObject :model_type model-type :model_id model-id))-(defn lock-rso+(mu/defn lock-rso"The RemoteSyncObject of the entity `model-type` `model-id`, locked for update, or nil."- [model-type model-id]+ [model-type :- :string+ model-id :- ModelId](t2/select-one :model/RemoteSyncObject{:where [:and [:= :model_type model-type] [:= :model_id model-id]]:for :update}))-(defn rso-by-file-path+(mu/defn rso-by-file-path"The RemoteSyncObject at `file-path`, or nil."- [file-path]+ [file-path :- :string](t2/select-one :model/RemoteSyncObject :file_path file-path))-(defn rso-exists?+(mu/defn rso-exists?"Whether the entity `model-type` `model-id` has a RemoteSyncObject."- [model-type model-id]+ [model-type :- :string+ model-id :- ModelId](t2/exists? :model/RemoteSyncObject :model_type model-type :model_id model-id))-(defn rso-of-type-exists?+(mu/defn rso-of-type-exists?"Whether any entity of `model-type` has a RemoteSyncObject."- [model-type]+ [model-type :- :string](t2/exists? :model/RemoteSyncObject :model_type model-type))-(defn rso-count-of-type+(mu/defn rso-count-of-type"The number of RemoteSyncObjects of `model-type`."- [model-type]+ [model-type :- :string](t2/count :model/RemoteSyncObject :model_type model-type))-(defn rso-keys+(mu/defn rso-keys"The `:id`, `:model_type`, and `:model_id` of every RemoteSyncObject."[](t2/select [:model/RemoteSyncObject :id :model_type :model_id]))-(defn departed-rso-keys+(mu/defn departed-rso-keys"The `:id`, `:model_type`, and `:model_id` of the RemoteSyncObjects pending removal or deletion."[](t2/select [:model/RemoteSyncObject :id :model_type :model_id] :status [:in ["removed" "delete"]]))-(defn all-rso-ids+(mu/defn all-rso-ids"The IDs of every RemoteSyncObject."[](t2/select-pks-set :model/RemoteSyncObject))-(defn unsynced-rsos+(mu/defn unsynced-rsos"The RemoteSyncObjects whose status is not synced."[](t2/select :model/RemoteSyncObject {:where [:not= :status "synced"]}))-(defn dirty-rso-exists?+(mu/defn dirty-rso-exists?"Whether a RemoteSyncObject of a model type other than `excluded-model-types` is not synced."- [excluded-model-types]+ [excluded-model-types :- [:set :string]](t2/exists? :model/RemoteSyncObject{:where [:and[:not= :status "synced"](when (seq excluded-model-types)[:not-in :model_type excluded-model-types])]}))-(defn dirty-rsos+(mu/defn dirty-rsos"The RemoteSyncObjects of model types other than `excluded-model-types` that are not synced."- [excluded-model-types]+ [excluded-model-types :- [:set :string]](t2/select :model/RemoteSyncObject{:where [:and[:not= :status "synced"](when (seq excluded-model-types)[:not-in :model_type excluded-model-types])]}))-(defn tracked-model-ids+(mu/defn tracked-model-ids"The model IDs of the RemoteSyncObjects of `model-type`."- [model-type]+ [model-type :- :string](t2/select-fn-set :model_id :model/RemoteSyncObject :model_type model-type))-(defn rsos-of-models+(mu/defn rsos-of-models"The RemoteSyncObjects of the entities of `model-type` with `model-ids`."- [model-type model-ids]+ [model-type :- :string+ model-ids :- [:sequential ms/PositiveInt]](t2/select :model/RemoteSyncObject :model_type model-type :model_id [:in model-ids]))-(defn active-child-rsos+(mu/defn active-child-rsos"The RemoteSyncObjects of `model-type` under the Table with `table-id` that are not pending removal or deletion."- [model-type table-id]+ [model-type :- :string+ table-id :- ::lib.schema.id/table](t2/select :model/RemoteSyncObject:model_type model-type:model_table_id table-id:status [:not-in ["removed" "delete"]]))-(defn content-rso-statuses+(mu/defn content-rso-statuses"The `:id` and `:status` of the RemoteSyncObjects of the Collections with `collection-ids` and their contents."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select [:model/RemoteSyncObject :id :status] {:where (contents-rso-expr collection-ids)}))-(defn removed-content-rso-ids+(mu/defn removed-content-rso-ids"The IDs of the RemoteSyncObjects pending removal among those of the Collections with `collection-ids` and theircontents."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pks-set :model/RemoteSyncObject{:where [:and[:= :status "removed"](contents-rso-expr collection-ids)]}))-(defn insert-rso!+(mu/defn insert-rso!"Insert the RemoteSyncObject `row`."- [row]+ [row :- ::remote-sync.schema/remote-sync-object.update](t2/insert! :model/RemoteSyncObject row))-(defn insert-rsos!+(mu/defn insert-rsos!"Insert the RemoteSyncObject `rows`."- [rows]+ [rows :- [:sequential ::remote-sync.schema/remote-sync-object.update]](t2/insert! :model/RemoteSyncObject rows))-(defn update-rso!+(mu/defn update-rso!"Apply `changes` to the RemoteSyncObject with `rso-id`."- [rso-id changes]+ [rso-id :- ms/PositiveInt+ changes :- ::remote-sync.schema/remote-sync-object.update](t2/update! :model/RemoteSyncObject rso-id changes))-(defn set-rsos-status!+(mu/defn set-rsos-status!"Set the status of the RemoteSyncObjects with `rso-ids` to `status` as of `timestamp`."- [rso-ids status timestamp]+ [rso-ids :- [:sequential ms/PositiveInt]+ status :- :string+ timestamp :- ms/TemporalInstant](t2/update! :model/RemoteSyncObject :id [:in rso-ids] {:status status :status_changed_at timestamp}))-(defn mark-all-rsos-synced!+(mu/defn mark-all-rsos-synced!"Mark every RemoteSyncObject as synced as of `timestamp`."- [timestamp]+ [timestamp :- ms/TemporalInstant](t2/update! :model/RemoteSyncObject {:status "synced" :status_changed_at timestamp}))-(defn mark-rsos-synced!+(mu/defn mark-rsos-synced!"Mark the RemoteSyncObjects with `rso-ids` as synced as of `timestamp`, writing the `:file_path` and`:content_hash` of those in `metadata-by-id` and keeping the existing values of the others."- [rso-ids metadata-by-id timestamp]+ [rso-ids :- [:sequential ms/PositiveInt]+ metadata-by-id :- [:map-of ms/PositiveInt+ [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:file_path {:optional true} [:maybe :string]]+ [:content_hash {:optional true} [:maybe :string]]]]+ timestamp :- ms/TemporalInstant](t2/update! :model/RemoteSyncObject{:id [:in (vec rso-ids)]}(cond-> {:status "synced" :status_changed_at timestamp}@@ -487,59 +559,61 @@metadata-by-id)[:else :content_hash]))))))-(defn delete-rso!+(mu/defn delete-rso!"Delete the RemoteSyncObject with `rso-id`."- [rso-id]+ [rso-id :- ms/PositiveInt](t2/delete! :model/RemoteSyncObject rso-id))-(defn delete-rsos!+(mu/defn delete-rsos!"Delete the RemoteSyncObjects with `rso-ids`."- [rso-ids]+ [rso-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/RemoteSyncObject :id [:in rso-ids]))-(defn delete-rso-of!+(mu/defn delete-rso-of!"Delete the RemoteSyncObject of the entity `model-type` `model-id`."- [model-type model-id]+ [model-type :- :string+ model-id :- ModelId](t2/delete! :model/RemoteSyncObject :model_type model-type :model_id model-id))-(defn delete-rsos-of-type!+(mu/defn delete-rsos-of-type!"Delete the RemoteSyncObjects of `model-type`."- [model-type]+ [model-type :- :string](t2/delete! :model/RemoteSyncObject :model_type model-type))-(defn delete-rsos-of-models!+(mu/defn delete-rsos-of-models!"Delete the RemoteSyncObjects of the entities of `model-type` with `model-ids`."- [model-type model-ids]+ [model-type :- :string+ model-ids :- [:set ms/PositiveInt]](t2/delete! :model/RemoteSyncObject :model_type model-type :model_id [:in model-ids]))-(defn delete-rsos-of-keys!- "Delete the RemoteSyncObjects of the `[{:model_type :model_id}]` `rows`."- [rows]+(mu/defn delete-rsos-of-keys!+ "Delete the RemoteSyncObjects keyed by the `:model_type`/`:model_id` of `rows` (other keys are ignored)."+ [rows :- [:sequential [:map [:model_type :string] [:model_id ms/PositiveInt]]]](t2/delete! :model/RemoteSyncObject {:where (rso-keys-expr rows)}))-(defn delete-all-rsos!+(mu/defn delete-all-rsos!"Delete every RemoteSyncObject."[](t2/delete! :model/RemoteSyncObject))-(defn task+(mu/defn task"The RemoteSyncTask with `task-id`, or nil."- [task-id]+ [task-id :- ms/PositiveInt](t2/select-one :model/RemoteSyncTask task-id))-(defn lock-task+(mu/defn lock-task"The RemoteSyncTask with `task-id`, locked for update, or nil."- [task-id]+ [task-id :- ms/PositiveInt](t2/select-one :model/RemoteSyncTask :id task-id {:for :update}))-(defn task-cancelled?+(mu/defn task-cancelled?"The cancelled flag of the RemoteSyncTask with `task-id`."- [task-id]+ [task-id :- ms/PositiveInt](t2/select-one-fn :cancelled :model/RemoteSyncTask :id task-id))-(defn current-task+(mu/defn current-task"The newest started, unfinished RemoteSyncTask that reported progress after `progress-cutoff`, or nil."- [progress-cutoff]+ [progress-cutoff :- ms/TemporalInstant](t2/select-one :model/RemoteSyncTask{:where [:and[:<> :started_at nil]@@ -549,7 +623,7 @@:order-by [[:started_at :desc][:id :desc]]}))-(defn most-recent-task+(mu/defn most-recent-task"The newest started RemoteSyncTask, or nil."[](t2/select-one :model/RemoteSyncTask@@ -559,7 +633,7 @@:order-by [[:started_at :desc][:id :desc]]}))-(defn last-successful-task+(mu/defn last-successful-task"The newest finished RemoteSyncTask that was neither cancelled nor failed and recorded a version, or nil."[](t2/select-one :model/RemoteSyncTask@@ -572,34 +646,47 @@:order-by [[:started_at :desc][:id :desc]]}))-(defn insert-task!+(mu/defn insert-task!"Insert `task` and return the new instance."- [task]+ [task :- ::remote-sync.schema/remote-sync-task.update](t2/insert-returning-instance! :model/RemoteSyncTask task))-(defn update-task!+(mu/defn update-task!"Apply `changes` to the RemoteSyncTask with `task-id`."- [task-id changes]+ [task-id :- ms/PositiveInt+ changes :- ::remote-sync.schema/remote-sync-task.update](t2/update! :model/RemoteSyncTask task-id changes))-(defn supersede-stale-tasks!- "Cancel and end at `now` the started, unfinished RemoteSyncTasks that last reported progress before `cutoff`."- [cutoff now]+(mu/defn end-task!+ "Apply `changes` to the RemoteSyncTask with `task-id` and mark it ended now."+ [task-id :- ms/PositiveInt+ changes :- ::remote-sync.schema/remote-sync-task.update]+ (t2/update! :model/RemoteSyncTask task-id (assoc changes :ended_at :%now)))++(mu/defn report-task-progress!+ "Record `progress` for the RemoteSyncTask with `task-id`, stamping the progress report time."+ [task-id :- ms/PositiveInt+ progress :- number?]+ (t2/update! :model/RemoteSyncTask task-id {:progress progress, :last_progress_report_at :%now}))++(mu/defn supersede-stale-tasks!+ "Cancel and end now the started, unfinished RemoteSyncTasks that last reported progress before `cutoff`."+ [cutoff :- ms/TemporalInstant](t2/query {:update (t2/table-name :model/RemoteSyncTask):set {:cancelled true- :ended_at now+ :ended_at :%now:error_message "Superseded after staleness timeout"}:where [:and[:<> :started_at nil][:= :ended_at nil][:< :last_progress_report_at cutoff]]}))-(defn delete-tasks-started-before!+(mu/defn delete-tasks-started-before!"Delete the RemoteSyncTasks started before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/RemoteSyncTask {:where [:< :started_at cutoff]}))-(defn users-by-id+(mu/defn users-by-id"A map of User ID to User for `user-ids`."- [user-ids]+ [user-ids :- [:sequential [:maybe ::lib.schema.id/user]]](t2/select-pk->fn identity :model/User :id [:in user-ids]))diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/models/remote_sync_task.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/models/remote_sync_task.cljindex 10b5b5a2247..e1b2d6fc579 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/models/remote_sync_task.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/models/remote_sync_task.clj@@ -31,7 +31,7 @@[task](when-let [existing (current-task)](throw (ex-info "A running task exists" {:existing-task existing})))- task)+ (merge {:started_at (mi/now)} task));;; ------------------------------------------- Helper Functions -------------------------------------------@@ -49,8 +49,7 @@[additional-fields :- [:map]]](remote-sync.db/insert-task! (merge {:sync_task_type sync-task-type:initiated_by user-id- :progress 0- :started_at (mi/now)}+ :progress 0}additional-fields)))(defn cancel-sync-task!@@ -63,10 +62,9 @@This signal will be checked in update-progress! to stop further processing. Note that the worker thread mustmanually check this flag rather than being interrupted, as interrupting Quartz threads can cause issues."[task-id]- (remote-sync.db/update-task! task-id- {:cancelled true- :ended_at (mi/now)- :error_message "Task cancelled"}))+ (remote-sync.db/end-task! task-id+ {:cancelled true+ :error_message "Task cancelled"}))(defn update-progress!"Updates the progress of a sync task.@@ -80,9 +78,7 @@(when (true? (remote-sync.db/task-cancelled? task-id))(throw (ex-info "Remote sync task has been cancelled" {:task-id task-id:cancelled? true})))- (remote-sync.db/update-task! task-id- {:progress progress- :last_progress_report_at (mi/now)}))+ (remote-sync.db/report-task-progress! task-id progress))(def ^:private default-progress-throttle-ms"Minimum ms between throttled (non-boundary) progress writes."@@ -136,10 +132,9 @@Returns the number of rows updated (should be 1 if successful)."([task-id] (complete-sync-task! task-id nil))([task-id outcome]- (remote-sync.db/update-task! task-id- {:progress 1.0- :ended_at (mi/now)- :outcome outcome})))+ (remote-sync.db/end-task! task-id+ {:progress 1.0+ :outcome outcome})))(defn fail-sync-task!"Marks a sync task as failed.@@ -148,9 +143,8 @@Returns the number of rows updated (should be 1 if successful)."[task-id error-msg]- (remote-sync.db/update-task! task-id- {:ended_at (mi/now)- :error_message error-msg}))+ (remote-sync.db/end-task! task-id+ {:error_message error-msg}))(defn current-task"Gets the current active sync task.@@ -177,7 +171,7 @@[](let [cutoff (t/minus (t/offset-date-time)(t/millis (setting/get :remote-sync-task-time-limit-ms)))]- (remote-sync.db/supersede-stale-tasks! cutoff (mi/now))))+ (remote-sync.db/supersede-stale-tasks! cutoff)))(defn most-recent-task"Gets the most recently run task, including currently running tasks.@@ -265,9 +259,8 @@Returns the number of rows updated (should be 1 if successful)."[task-id conflicts]- (remote-sync.db/update-task! task-id- {:ended_at (mi/now)- :conflicts conflicts}))+ (remote-sync.db/end-task! task-id+ {:conflicts conflicts}));;; ------------------------------------------- Hydration -------------------------------------------diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/schema.cljindex 47b008de305..a45c869222e 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj@@ -1,5 +1,9 @@(ns metabase-enterprise.remote-sync.schema- "Malli schemas for remote sync API request and response bodies.")+ "Malli schemas for remote sync API request and response bodies."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]));;; ------------------------------------------- Task Schemas -------------------------------------------@@ -164,3 +168,69 @@"Schema for POST /test-connection response."[:map[:status [:= :success]]])++(mr/def ::remote-sync-object+ "A RemoteSyncObject as selected from the app DB: every column of `:remote_sync_object`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model_type [:or :keyword :string]]+ [:model_id [:maybe :int]]+ [:status [:or :keyword :string]]+ [:status_changed_at ms/TemporalInstant]+ [:model_name :string]+ [:model_collection_id [:maybe ::lib.schema.id/collection]]+ [:model_display [:maybe [:or :keyword :string]]]+ [:model_table_id [:maybe ::lib.schema.id/table]]+ [:model_table_name [:maybe :string]]+ [:file_path [:maybe :string]]+ [:content_hash [:maybe :string]]])++(mr/def ::remote-sync-object.update+ "What an update (or insert) of a RemoteSyncObject accepts: every column of `:remote_sync_object` except `id`, all optional."+ [:map {:closed true}+ [:model_type {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:status_changed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:model_name {:optional true} [:maybe :string]]+ [:model_collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:model_display {:optional true} [:maybe [:or :keyword :string]]]+ [:model_table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:model_table_name {:optional true} [:maybe :string]]+ [:file_path {:optional true} [:maybe :string]]+ [:content_hash {:optional true} [:maybe :string]]])++(mr/def ::remote-sync-task.outcome+ "The `:outcome` column of a RemoteSyncTask, decoded."+ :map)++(mr/def ::remote-sync-task+ "A RemoteSyncTask as selected from the app DB: every column of `:remote_sync_task`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:sync_task_type [:or :keyword :string]]+ [:progress [:maybe number?]]+ [:cancelled :boolean]+ [:started_at ms/TemporalInstant]+ [:ended_at [:maybe ms/TemporalInstant]]+ [:last_progress_report_at ms/TemporalInstant]+ [:initiated_by [:maybe ::lib.schema.id/user]]+ [:error_message [:maybe :string]]+ [:version [:maybe :string]]+ [:conflicts [:maybe [:sequential :string]]]+ [:outcome [:maybe ::remote-sync-task.outcome]]])++(mr/def ::remote-sync-task.update+ "What an update (or insert) of a RemoteSyncTask accepts: every column of `:remote_sync_task` except `id`, all optional."+ [:map {:closed true}+ [:sync_task_type {:optional true} [:maybe [:or :keyword :string]]]+ [:progress {:optional true} [:maybe number?]]+ [:cancelled {:optional true} [:maybe :boolean]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:ended_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_progress_report_at {:optional true} [:maybe ms/TemporalInstant]]+ [:initiated_by {:optional true} [:maybe ::lib.schema.id/user]]+ [:error_message {:optional true} [:maybe :string]]+ [:version {:optional true} [:maybe :string]]+ [:conflicts {:optional true} [:maybe [:sequential :string]]]+ [:outcome {:optional true} [:maybe ::remote-sync-task.outcome]]])diff --git a/enterprise/backend/src/metabase_enterprise/replacement/db.clj b/enterprise/backend/src/metabase_enterprise/replacement/db.cljindex fe49d29d30e..c97dc8ee7ab 100644--- a/enterprise/backend/src/metabase_enterprise/replacement/db.clj+++ b/enterprise/backend/src/metabase_enterprise/replacement/db.clj@@ -2,50 +2,63 @@"Application database queries for the replacement module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions."(:require+ [metabase-enterprise.replacement.schema :as replacement.schema][metabase.app-db.core :as mdb]+ [metabase.dashboards.schema :as dashboards.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.measures.schema :as measures.schema]+ [metabase.queries.schema :as queries.schema]+ [metabase.segments.schema :as segments.schema]+ [metabase.transforms.schema :as transforms.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema][toucan2.core :as t2]))-(defn run+(mu/defn run"The ReplacementRun with `run-id`, or nil."- [run-id]+ [run-id :- ms/PositiveInt](t2/select-one :model/ReplacementRun :id run-id))-(defn runs+(mu/defn runs"The ReplacementRuns, newest first, restricted to the `is-active` flag when given."- [is-active]+ [is-active :- [:maybe :boolean]](t2/select :model/ReplacementRun(cond-> {:order-by [[:start_time :desc]]}(some? is-active) (assoc :where [:= :is_active is-active]))))-(defn active-run+(mu/defn active-run"The active ReplacementRun, or nil."[](t2/select-one :model/ReplacementRun :is_active true))-(defn run-active-flag+(mu/defn run-active-flag"The `:is_active` row of the ReplacementRun with `run-id`, or nil."- [run-id]+ [run-id :- ms/PositiveInt](t2/select-one [:model/ReplacementRun :is_active] :id run-id))-(defn insert-run!+(mu/defn insert-run!"Insert `run` and return the new instance."- [run]+ [run :- ::replacement.schema/replacement-run.update](t2/insert-returning-instance! :model/ReplacementRun run))-(defn update-run!- "Apply `changes` to the ReplacementRun with `run-id`."- [run-id changes]+(mu/defn update-run!+ "Apply `changes` to the ReplacementRun with `run-id`, returning the number updated."+ [run-id :- ms/PositiveInt+ changes :- ::replacement.schema/replacement-run.update](t2/update! :model/ReplacementRun :id run-id changes))-(defn update-active-run!- "Apply `changes` to the ReplacementRun with `run-id` if it is active."- [run-id changes]+(mu/defn update-active-run!+ "Apply `changes` to the ReplacementRun with `run-id` if it is active, returning the number updated."+ [run-id :- ms/PositiveInt+ changes :- ::replacement.schema/replacement-run.update](t2/update! :model/ReplacementRun :id run-id :is_active true changes))-(defn time-out-active-runs-older-than!- "Mark the active ReplacementRuns started more than `age` `unit`s ago as timed out."- [age unit]+(mu/defn time-out-active-runs-older-than!+ "Mark the active ReplacementRuns started more than `age` `unit`s ago as timed out, returning the number updated."+ [age :- ms/PositiveInt+ unit :- :keyword](t2/update! :model/ReplacementRun:is_active true:start_time [:< (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit)]@@ -54,147 +67,155 @@:end_time :%now:message "Timed out by metabase"}))-(defn cards-with-ids+(mu/defn cards-with-ids"The Cards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Card :id [:in ids]))-(defn tables-with-ids+(mu/defn tables-with-ids"The Tables with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Table :id [:in ids]))-(defn dashboards-with-ids+(mu/defn dashboards-with-ids"The Dashboards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Dashboard :id [:in ids]))-(defn transforms-with-ids+(mu/defn transforms-with-ids"The Transforms with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Transform :id [:in ids]))-(defn segments-with-ids+(mu/defn segments-with-ids"The Segments with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Segment :id [:in ids]))-(defn measures-with-ids+(mu/defn measures-with-ids"The Measures with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/Measure :id [:in ids]))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn cards-by-id+(mu/defn cards-by-id"A map of Card ID to Card for `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn identity :model/Card :id [:in card-ids]))-(defn card-database-id+(mu/defn card-database-id"The Database ID of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :database_id :model/Card :id card-id))-(defn card-with-table-exists?+(mu/defn card-with-table-exists?"Whether one of the Cards with `card-ids` is on the Table with `table-id`."- [card-ids table-id]+ [card-ids :- [:set ::lib.schema.id/card]+ table-id :- ::lib.schema.id/table](t2/exists? :model/Card :id [:in card-ids] :table_id table-id))-(defn update-card!- "Apply `changes` to the Card with `card-id`."- [card-id changes]+(mu/defn update-card!+ "Apply `changes` to the Card with `card-id`, returning the number updated."+ [card-id :- ::lib.schema.id/card+ changes :- ::queries.schema/card.update](t2/update! :model/Card card-id changes))-(defn transform+(mu/defn transform"The Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/Transform :id transform-id))-(defn update-transform!- "Apply `changes` to the Transform with `transform-id`."- [transform-id changes]+(mu/defn update-transform!+ "Apply `changes` to the Transform with `transform-id`, returning the number updated."+ [transform-id :- ::lib.schema.id/transform+ changes :- ::transforms.schema/transform.update](t2/update! :model/Transform transform-id changes))-(defn segment+(mu/defn segment"The Segment with `segment-id`, or nil."- [segment-id]+ [segment-id :- ::lib.schema.id/segment](t2/select-one :model/Segment :id segment-id))-(defn update-segment!- "Apply `changes` to the Segment with `segment-id`."- [segment-id changes]+(mu/defn update-segment!+ "Apply `changes` to the Segment with `segment-id`, returning the number updated."+ [segment-id :- ::lib.schema.id/segment+ changes :- ::segments.schema/segment.update](t2/update! :model/Segment segment-id changes))-(defn measure+(mu/defn measure"The Measure with `measure-id`, or nil."- [measure-id]+ [measure-id :- ::lib.schema.id/measure](t2/select-one :model/Measure :id measure-id))-(defn update-measure!- "Apply `changes` to the Measure with `measure-id`."- [measure-id changes]+(mu/defn update-measure!+ "Apply `changes` to the Measure with `measure-id`, returning the number updated."+ [measure-id :- ::lib.schema.id/measure+ changes :- ::measures.schema/measure.update](t2/update! :model/Measure measure-id changes))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn update-dashboard!- "Apply `changes` to the Dashboard with `dashboard-id`."- [dashboard-id changes]+(mu/defn update-dashboard!+ "Apply `changes` to the Dashboard with `dashboard-id`, returning the number updated."+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- ::dashboards.schema/dashboard.update](t2/update! :model/Dashboard dashboard-id changes))-(defn dashboard-cards+(mu/defn dashboard-cards"The DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardCard :dashboard_id dashboard-id))-(defn update-dashboard-card!- "Apply `changes` to the DashboardCard with `dashcard-id`."- [dashcard-id changes]+(mu/defn update-dashboard-card!+ "Apply `changes` to the DashboardCard with `dashcard-id`, returning the number updated."+ [dashcard-id :- ::lib.schema.id/dashcard+ changes :- ::dashboards.schema/dashboard-card.update](t2/update! :model/DashboardCard dashcard-id changes))-(defn table-database-id+(mu/defn table-database-id"The Database ID of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table :id table-id))-(defn active-fields-of-table+(mu/defn active-fields-of-table"The active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :active true))-(defn active-field-ids-of-table+(mu/defn active-field-ids-of-table"The IDs of the active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field :table_id table-id :active true))-(defn active-fk-to-fields-exists?+(mu/defn active-fk-to-fields-exists?"Whether an active Field points at one of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/exists? :model/Field :fk_target_field_id [:in field-ids] :active true))-(defn update-field!- "Apply `changes` to the Field with `field-id`."- [field-id changes]+(mu/defn update-field!+ "Apply `changes` to the Field with `field-id`, returning the number updated."+ [field-id :- ::lib.schema.id/field+ changes :- ::warehouse-schema.schema/field.update](t2/update! :model/Field field-id changes))-(defn sandbox-exists-for-table?+(mu/defn sandbox-exists-for-table?"Whether a Sandbox is defined on the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/exists? :model/Sandbox :table_id table-id))-(defn sandbox-card-ids+(mu/defn sandbox-card-ids"The IDs of the Cards Sandboxes are built on."[](t2/select-fn-set :card_id :model/Sandbox :card_id [:not= nil]))-(defn persisted-info-for-card+(mu/defn persisted-info-for-card"The PersistedInfo of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/PersistedInfo :card_id card-id))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.clj b/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.cljindex b46790e1a26..1d270b899e1 100644--- a/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.clj+++ b/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.clj@@ -13,6 +13,16 @@(derive :model/ReplacementRun :metabase/model)+(def ^:private terminal-statuses+ #{:succeeded :failed :canceled :timeout})++(t2/define-before-update :model/ReplacementRun+ [run]+ (let [changes (t2/changes run)]+ (cond-> run+ (and (contains? terminal-statuses (keyword (:status changes))) (not (contains? changes :end_time)))+ (assoc :end_time (mi/now)))))+(t2/deftransforms :model/ReplacementRun{:status mi/transform-keyword:source_entity_type mi/transform-keyword@@ -48,8 +58,7 @@(replacement.db/update-active-run! run-id{:status :succeeded:progress 1.0- :is_active nil- :end_time :%now}))+ :is_active nil}))(defn fail-run!"Mark the active run as failed."@@ -57,7 +66,6 @@(replacement.db/update-active-run! run-id{:status :failed:is_active nil- :end_time :%now:message message}))(defn cancel-run!@@ -66,7 +74,6 @@(replacement.db/update-active-run! run-id{:status :canceled:is_active nil- :end_time :%now:message "Canceled by user"}))(defn timeout-old-runs!diff --git a/enterprise/backend/src/metabase_enterprise/replacement/schema.clj b/enterprise/backend/src/metabase_enterprise/replacement/schema.cljindex 2024027d9bb..feedc407c82 100644--- a/enterprise/backend/src/metabase_enterprise/replacement/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/replacement/schema.clj@@ -57,3 +57,34 @@[:success :boolean][:errors {:optional true} [:sequential ::error-type]][:column_mappings {:optional true} [:sequential ::column-mapping]]])++(mr/def ::replacement-run+ "A ReplacementRun as selected from the app DB: every column of `:source_replacement_run`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_entity_type [:or :keyword :string]]+ [:source_entity_id ms/PositiveInt]+ [:target_entity_type [:or :keyword :string]]+ [:target_entity_id ms/PositiveInt]+ [:status [:or :keyword :string]]+ [:is_active [:maybe :boolean]]+ [:progress [:maybe number?]]+ [:message [:maybe :string]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:start_time ms/TemporalInstant]+ [:end_time [:maybe ms/TemporalInstant]]])++(mr/def ::replacement-run.update+ "What an update (or insert) of a ReplacementRun accepts: every column of `:source_replacement_run` except `id`, all optional."+ [:map {:closed true}+ [:source_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:target_entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:target_entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:progress {:optional true} [:maybe number?]]+ [:message {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:start_time {:optional true} [:maybe ms/TemporalInstant]]+ [:end_time {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/db.clj b/enterprise/backend/src/metabase_enterprise/sandbox/db.cljindex d0269500662..8906fb05e7c 100644--- a/enterprise/backend/src/metabase_enterprise/sandbox/db.clj+++ b/enterprise/backend/src/metabase_enterprise/sandbox/db.clj@@ -2,37 +2,46 @@"Application database queries for the sandbox module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions."(:require+ [malli.util :as mut]+ [metabase-enterprise.sandbox.schema :as sandbox.schema][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.users.schema :as users.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn sandbox+(mu/defn sandbox"The Sandbox with `sandbox-id`, or nil."- [sandbox-id]+ [sandbox-id :- ::lib.schema.id/sandbox](t2/select-one :model/Sandbox :id sandbox-id))-(defn sandboxes+(mu/defn sandboxes"Every Sandbox, in ID order."[](t2/select :model/Sandbox {:order-by [[:id :asc]]}))-(defn sandbox-for-group-and-table+(mu/defn sandbox-for-group-and-table"The Sandbox of the group with `group-id` on the Table with `table-id`, or nil."- [group-id table-id]+ [group-id :- ms/PositiveInt+ table-id :- ::lib.schema.id/table](t2/select-one :model/Sandbox :group_id group-id :table_id table-id))-(defn sandboxes-for-groups-and-table+(mu/defn sandboxes-for-groups-and-table"The Sandboxes of the groups with `group-ids` on the Table with `table-id`."- [group-ids table-id]+ [group-ids :- [:set ms/PositiveInt]+ table-id :- ::lib.schema.id/table](t2/select :model/Sandbox :group_id [:in group-ids] :table_id table-id))-(defn sandboxes-using-card+(mu/defn sandboxes-using-card"The `:id` and `:table_id` of the Sandboxes built on the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select [:model/Sandbox :id :table_id] :card_id card-id))-(defn user-sandboxes-with-group-ids+(mu/defn user-sandboxes-with-group-ids"The Sandboxes of the groups of the User with `user-id`, each with the `:group_id` of the membership."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select :model/Sandbox{:select [[:pgm.group_id :group_id][:s.*]]@@ -41,10 +50,13 @@:where [:and[:= :pgm.user_id user-id]]}))-(defn sandboxes-with-table-info+(mu/defn sandboxes-with-table-info"The group, Table, Database, and schema of the Sandboxes of the optional `group-id` or `group-ids` in the optionalDatabase `db-id`, excluding the Database `excluded-db-id` when given."- [group-id group-ids db-id excluded-db-id]+ [group-id :- [:maybe ms/PositiveInt]+ group-ids :- [:maybe [:sequential ms/PositiveInt]]+ db-id :- [:maybe ::lib.schema.id/database]+ excluded-db-id :- [:maybe ::lib.schema.id/database]](t2/select :model/Sandbox{:select [:s.group_id :s.table_id :t.db_id :t.schema]:from [[:sandboxes :s]]@@ -55,10 +67,11 @@(when db-id [:= :t.db_id db-id])(when excluded-db-id [:not [:= :t.db_id excluded-db-id]])]}))-(defn candidate-sandboxes-for-groups-and-databases+(mu/defn candidate-sandboxes-for-groups-and-databases"The `:id`, `:group_id`, `:table_id`, `:db_id`, and `:schema` of the Sandboxes of the groups with `group-ids` onTables of the Databases with `db-ids`."- [group-ids db-ids]+ [group-ids :- [:set ms/PositiveInt]+ db-ids :- [:set ::lib.schema.id/database]](mdb/query{:select [[:sandboxes.id :id][:sandboxes.group_id :group_id]@@ -72,47 +85,54 @@[:in :sandboxes.group_id group-ids][:in :table.db_id db-ids]]}))-(defn insert-sandbox!+(mu/defn insert-sandbox!"Insert `sandbox` and return the new instance."- [sandbox]+ [sandbox :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:table_id ::lib.schema.id/table]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:group_id ms/PositiveInt]+ [:attribute_remappings {:optional true} [:maybe :map]]]](first (t2/insert-returning-instances! :model/Sandbox sandbox)))-(defn update-sandbox!- "Apply `changes` to the Sandbox with `sandbox-id`."- [sandbox-id changes]+(mu/defn update-sandbox!+ "Apply `changes` to the Sandbox with `sandbox-id`, returning the number updated."+ [sandbox-id :- ::lib.schema.id/sandbox+ changes :- (mut/select-keys ::sandbox.schema/sandbox.update [:card_id :attribute_remappings])](t2/update! :model/Sandbox sandbox-id changes))-(defn delete-sandbox!- "Delete the Sandbox with `sandbox-id`."- [sandbox-id]+(mu/defn delete-sandbox!+ "Delete the Sandbox with `sandbox-id`, returning the number deleted."+ [sandbox-id :- ::lib.schema.id/sandbox](t2/delete! :model/Sandbox :id sandbox-id))-(defn delete-sandboxes!- "Delete the Sandboxes with `sandbox-ids`."- [sandbox-ids]+(mu/defn delete-sandboxes!+ "Delete the Sandboxes with `sandbox-ids`, returning the number deleted."+ [sandbox-ids :- [:set ::lib.schema.id/sandbox]](t2/delete! :model/Sandbox :id [:in sandbox-ids]))-(defn impersonations-for-groups+(mu/defn impersonations-for-groups"The ConnectionImpersonations of the groups with `group-ids`."- [group-ids]+ [group-ids :- [:set ms/PositiveInt]](t2/select :model/ConnectionImpersonation :group_id [:in group-ids]))-(defn user-group-ids+(mu/defn user-group-ids"The IDs of the groups of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id user-id))-(defn personal-user+(mu/defn personal-user"The personal User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User :id user-id :type :personal))-(defn set-user-login-attributes!+(mu/defn set-user-login-attributes!"Set the login attributes of the User with `user-id`, returning the number of rows updated."- [user-id login-attributes]+ [user-id :- ::lib.schema.id/user+ login-attributes :- [:maybe users.schema/LoginAttributes]](t2/update! :model/User user-id {:login_attributes login-attributes}))-(defn user-attributes-reducible+(mu/defn user-attributes-reducible"Reducible merged JWT and login attribute maps of the Users that have any."[](t2/select-fn-reducible (comp (partial apply merge)@@ -126,50 +146,53 @@[:not= :login_attributes nil][:not= :login_attributes "{}"]]]}))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn tables-of-database+(mu/defn tables-of-database"The `:id`, `:db_id`, and `:schema` of the Tables of the Database with `db-id`, restricted to `schema` when`schema-only?`."- [db-id schema-only? schema]+ [db-id :- ::lib.schema.id/database+ schema-only? :- :boolean+ schema :- [:maybe :string]](t2/select [:model/Table :id :db_id :schema]{:where [:and[:= :db_id db-id](when schema-only?[:= :schema schema])]}))-(defn database-of-table+(mu/defn database-of-table"The Database of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Database:id ^:allow-subquery {:select [:t.db_id]:from [[(t2/table-name :model/Table) :t]]:where [:= :t.id table-id]}))-(defn fields-of-table-named+(mu/defn fields-of-table-named"The `:id` and `:name` of the Fields of the Table with `table-id` named one of `field-names`."- [table-id field-names]+ [table-id :- ::lib.schema.id/table+ field-names :- [:set :string]](t2/select [:model/Field :id :name] :table_id table-id :name [:in field-names]))-(defn cards-by-id+(mu/defn cards-by-id"A map of Card ID to the query, result metadata, and schema of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn identity [:model/Card :id :dataset_query :result_metadata :card_schema] :id [:in card-ids]))-(defn cards-result-metadata+(mu/defn cards-result-metadata"The `:id`, `:result_metadata`, and `:card_schema` of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card :id :result_metadata :card_schema] :id [:in card-ids]))-(defn card-result-metadata+(mu/defn card-result-metadata"The result metadata of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :result_metadata :model/Card :id card-id))-(defn sandboxing-cards+(mu/defn sandboxing-cards"The `:id`, `:dataset_query`, `:database_id`, and `:card_schema` of the Cards Sandboxes are built on."[](t2/select :model/Card@@ -179,7 +202,8 @@:from [[(t2/table-name :model/Sandbox) :s]]:where [:= :s.card_id :c.id]}]}))-(defn set-card-result-metadata!- "Set the result metadata of the Card with `card-id`."- [card-id result-metadata]+(mu/defn set-card-result-metadata!+ "Set the result metadata of the Card with `card-id`, returning the number updated."+ [card-id :- ::lib.schema.id/card+ result-metadata :- [:maybe ::queries.schema/card.result-metadata]](t2/update! :model/Card card-id {:result_metadata result-metadata}))diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/schema.clj b/enterprise/backend/src/metabase_enterprise/sandbox/schema.cljindex 856e8d8ed93..967e19dc20c 100644--- a/enterprise/backend/src/metabase_enterprise/sandbox/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/sandbox/schema.clj@@ -32,6 +32,23 @@{:in (comp mi/json-in normalize-attribute-remappings):out (comp normalize-attribute-remappings mi/json-out-without-keywordization)})+(mr/def ::sandbox.attribute-remappings+ "The `:attribute_remappings` column of a Sandbox, decoded."+ :map)+(mr/def ::sandbox- [:map- [:id ::lib.schema.id/sandbox]])+ "A Sandbox as selected from the app DB: every column of `:sandboxes`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:group_id ms/PositiveInt]+ [:table_id ::lib.schema.id/table]+ [:card_id [:maybe ::lib.schema.id/card]]+ [:attribute_remappings [:maybe ::sandbox.attribute-remappings]]])++(mr/def ::sandbox.update+ "What an update (or insert) of a Sandbox accepts: every column of `:sandboxes` except `id`, all optional."+ [:map {:closed true}+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:attribute_remappings {:optional true} [:maybe ::sandbox.attribute-remappings]]])diff --git a/enterprise/backend/src/metabase_enterprise/scim/db.clj b/enterprise/backend/src/metabase_enterprise/scim/db.cljindex 086e8116858..cda85effe7f 100644--- a/enterprise/backend/src/metabase_enterprise/scim/db.clj+++ b/enterprise/backend/src/metabase_enterprise/scim/db.clj@@ -2,7 +2,14 @@"Application database queries for the scim module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for transactions."(:require+ [malli.util :as mut]+ [metabase.api-keys.schema :as api-keys.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.permissions.schema :as permissions.schema]+ [metabase.users.schema :as users.schema][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private user-columns@@ -13,19 +20,19 @@"Required columns when fetching groups for SCIM."[:model/PermissionsGroup :id :name :entity_id])-(defn scim-api-key+(mu/defn scim-api-key"The SCIM ApiKey, or nil."[](t2/select-one :model/ApiKey :scope :scim))-(defn delete-scim-api-keys!- "Delete every SCIM ApiKey."+(mu/defn delete-scim-api-keys!+ "Delete every SCIM ApiKey, returning the number deleted."[](t2/delete! :model/ApiKey :scope :scim))-(defn insert-api-key!+(mu/defn insert-api-key!"Insert `api-key` and return the new instance."- [api-key]+ [api-key :- ::api-keys.schema/api-key.create](t2/insert-returning-instance! :model/ApiKey api-key))(defn- personal-user-expr@@ -33,55 +40,59 @@[:and [:= :type "personal"](when email [:= :%lower.email (u/lower-case-en email)])])-(defn scim-user-by-entity-id+(mu/defn scim-user-by-entity-id"The SCIM columns of the personal User with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one user-columns :entity_id entity-id {:where [:= :type "personal"]}))-(defn scim-user-by-email+(mu/defn scim-user-by-email"The SCIM columns of the User with `email`, or nil."- [email]+ [email :- :string](t2/select-one user-columns :email (u/lower-case-en email)))-(defn scim-users+(mu/defn scim-users"The SCIM columns of the personal Users, narrowed to the optional `email` (case-insensitive), paged by `limit`and `offset` in ID order."- [email limit offset]+ [email :- [:maybe :string]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select user-columns{:where (personal-user-expr email):limit limit:offset offset:order-by [[:id :asc]]}))-(defn scim-user-count+(mu/defn scim-user-count"The number of personal Users, narrowed to the optional `email` (case-insensitive)."- [email]+ [email :- [:maybe :string]](t2/count :model/User {:where (personal-user-expr email)}))-(defn user-email-exists?+(mu/defn user-email-exists?"Whether a User with `email` (case-insensitive) exists."- [email]+ [email :- :string](t2/exists? :model/User :%lower.email (u/lower-case-en email)))-(defn user-ids-by-entity-ids+(mu/defn user-ids-by-entity-ids"The IDs of the Users with `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](t2/select-fn-set :id :model/User {:where [:in :entity_id entity-ids]}))-(defn insert-user!- "Insert the User `row`."- [row]+(mu/defn insert-user!+ "Insert the User `row`, returning the number inserted."+ [row :- ::users.schema/user.update](t2/insert! :model/User row))-(defn update-user!- "Apply `changes` to the User with `user-id`."- [user-id changes]+(mu/defn update-user!+ "Apply `changes` to the User with `user-id`, returning the number updated."+ [user-id :- ::lib.schema.id/user+ changes :- ::users.schema/user.update](t2/update! :model/User user-id changes))-(defn user-group-memberships+(mu/defn user-group-memberships"Rows of User ID, group name, and group entity ID for the memberships of the Users with `user-ids`, excluding thegroups with `excluded-group-ids`."- [user-ids excluded-group-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]+ excluded-group-ids :- [:sequential ms/PositiveInt]](t2/select [:model/PermissionsGroupMembership :pgm.user_id :pg.name :pg.entity_id]{:from [[:permissions_group_membership :pgm]]:join [[:permissions_group :pg] [:= :pg.id :group_id]]@@ -95,50 +106,56 @@(map (fn [group-id] [:not= :id group-id]))excluded-group-ids))-(defn scim-group-by-entity-id+(mu/defn scim-group-by-entity-id"The SCIM columns of the PermissionsGroup with `entity-id` other than `excluded-group-ids`, or nil."- [entity-id excluded-group-ids]+ [entity-id :- :string+ excluded-group-ids :- [:sequential ms/PositiveInt]](t2/select-one group-columns :entity_id entity-id {:where (manageable-group-expr excluded-group-ids nil)}))-(defn scim-groups+(mu/defn scim-groups"The SCIM columns of the PermissionsGroups other than `excluded-group-ids`, narrowed to the optional `group-name`,paged by `limit` and `offset` in ID order."- [excluded-group-ids group-name limit offset]+ [excluded-group-ids :- [:sequential ms/PositiveInt]+ group-name :- [:maybe :string]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select group-columns{:where (manageable-group-expr excluded-group-ids group-name):limit limit:offset offset:order-by [[:id :asc]]}))-(defn scim-group-count+(mu/defn scim-group-count"The number of PermissionsGroups other than `excluded-group-ids`, narrowed to the optional `group-name`."- [excluded-group-ids group-name]+ [excluded-group-ids :- [:sequential ms/PositiveInt]+ group-name :- [:maybe :string]](t2/count :model/PermissionsGroup {:where (manageable-group-expr excluded-group-ids group-name)}))-(defn group-name-exists?+(mu/defn group-name-exists?"Whether a PermissionsGroup with `group-name` (case-insensitive) exists."- [group-name]+ [group-name :- :string](t2/exists? :model/PermissionsGroup :%lower.name (u/lower-case-en group-name)))-(defn insert-group!+(mu/defn insert-group!"Insert `group` and return the new instance."- [group]+ [group :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :entity_id :magic_group_type :is_tenant_group])](first (t2/insert-returning-instances! :model/PermissionsGroup group)))-(defn update-group!- "Apply `changes` to the PermissionsGroup with `group-id`."- [group-id changes]+(mu/defn update-group!+ "Apply `changes` to the PermissionsGroup with `group-id`, returning the number updated."+ [group-id :- ms/PositiveInt+ changes :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :entity_id :magic_group_type :is_tenant_group])](t2/update! :model/PermissionsGroup group-id changes))-(defn delete-group!- "Delete the PermissionsGroup with `group-id`."- [group-id]+(mu/defn delete-group!+ "Delete the PermissionsGroup with `group-id`, returning the number deleted."+ [group-id :- ms/PositiveInt](t2/delete! :model/PermissionsGroup group-id))-(defn group-members+(mu/defn group-members"Rows of group ID, member email, and member entity ID for the memberships of the PermissionsGroups with`group-ids`."- [group-ids]+ [group-ids :- [:sequential ms/PositiveInt]](t2/select [:model/PermissionsGroupMembership :pgm.group_id :u.email :u.entity_id]{:from [[:permissions_group_membership :pgm]]:join [[:core_user :u] [:= :u.id :pgm.user_id]]diff --git a/enterprise/backend/src/metabase_enterprise/search/scoring.clj b/enterprise/backend/src/metabase_enterprise/search/scoring.cljindex 3cad9c34979..46837a71c39 100644--- a/enterprise/backend/src/metabase_enterprise/search/scoring.clj+++ b/enterprise/backend/src/metabase_enterprise/search/scoring.clj@@ -24,8 +24,8 @@(defenterprise scorers"Return the select-item expressions used to calculate the score for each search result.":feature :none- [search-ctx]- (merge (appdb.scoring/base-scorers search-ctx) (additional-scorers)))+ [search-ctx view-count-percentiles]+ (merge (appdb.scoring/base-scorers search-ctx view-count-percentiles) (additional-scorers)));; ------------ LEGACY ----------diff --git a/enterprise/backend/src/metabase_enterprise/security_center/api.clj b/enterprise/backend/src/metabase_enterprise/security_center/api.cljindex 2c23d0cf765..22e04467312 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/api.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/api.clj@@ -5,7 +5,6 @@[metabase-enterprise.security-center.db :as security-center.db][metabase-enterprise.security-center.models.security-advisory :as security-advisory][metabase-enterprise.security-center.notification :as notification]- [metabase-enterprise.security-center.schema :as security-center.schema][metabase-enterprise.security-center.settings :as settings][metabase-enterprise.security-center.task.sync-advisories :as sync-advisories][metabase.api.common :as api]@@ -13,6 +12,7 @@[metabase.api.routes.common :as routes.common :refer [+auth]][metabase.notification.models :as models.notification][metabase.premium-features.core :as premium-features]+ [metabase.security-center.schema :as security-center.schema][metabase.util.i18n :refer [tru]][metabase.util.malli.schema :as ms][toucan2.core :as t2])diff --git a/enterprise/backend/src/metabase_enterprise/security_center/db.clj b/enterprise/backend/src/metabase_enterprise/security_center/db.cljindex 49e174667ab..a4bf059c3c1 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/db.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/db.clj@@ -2,60 +2,78 @@"Application database queries for the security-center module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [malli.util :as mut][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.security-center.schema :as security-center.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn advisory+(mu/defn advisory"The SecurityAdvisory with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/SecurityAdvisory :id id))-(defn advisory-by-advisory-id+(mu/defn advisory-by-advisory-id"The SecurityAdvisory with the external `advisory-id`, or nil."- [advisory-id]+ [advisory-id :- :string](t2/select-one :model/SecurityAdvisory :advisory_id advisory-id))-(defn max-advisory-updated-at+(mu/defn max-advisory-updated-at"The latest `updated_at` across every SecurityAdvisory, or nil when there are none."[](:updated_at (t2/query-one {:select [[[:max :updated_at] :updated_at]]:from [:security_advisory]})))-(defn advisories-newest-first+(mu/defn advisories-newest-first"Every SecurityAdvisory, newest first."[](t2/select :model/SecurityAdvisory {:order-by [[:published_at :desc]]}))-(defn advisories-reducible+(mu/defn advisories-reducible"Reducible SecurityAdvisories."[](t2/reducible-select :model/SecurityAdvisory))-(defn advisories-with-statuses-reducible+(mu/defn advisories-with-statuses-reducible"Reducible severity and acknowledgement time of the SecurityAdvisories whose match status is one of `statuses`."- [statuses]+ [statuses :- [:set [:or :keyword :string]]](t2/reducible-select [:model/SecurityAdvisory :severity :acknowledged_at] :match_status [:in statuses]))-(defn unacknowledged-advisories-with-statuses+(mu/defn unacknowledged-advisories-with-statuses"The unacknowledged SecurityAdvisories whose match status is one of `statuses`."- [statuses]+ [statuses :- [:sequential [:or :keyword :string]]](t2/select :model/SecurityAdvisory :acknowledged_at nil :match_status [:in statuses]))-(defn unacknowledged-advisories-by-advisory-ids+(mu/defn unacknowledged-advisories-by-advisory-ids"The unacknowledged SecurityAdvisories with the external `advisory-ids`."- [advisory-ids]+ [advisory-ids :- [:set :string]](t2/select :model/SecurityAdvisory :advisory_id [:in advisory-ids] :acknowledged_at nil))-(defn update-advisory!- "Apply `changes` to the SecurityAdvisory with `id`."- [id changes]+(mu/defn update-advisory!+ "Apply `changes` to the SecurityAdvisory with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::security-center.schema/security-advisory.update [:match_status :last_evaluated_at :acknowledged_by :acknowledged_at :last_notified_at])](t2/update! :model/SecurityAdvisory id changes))-(defn upsert-advisory!+(mu/defn record-advisory-evaluation!+ "Apply `changes` to the SecurityAdvisory with `id` and stamp `last_evaluated_at` with now, returning the number+ updated."+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::security-center.schema/security-advisory.update [:match_status :acknowledged_by :acknowledged_at])]+ (t2/update! :model/SecurityAdvisory id (assoc changes :last_evaluated_at :%now)))++(mu/defn record-advisory-notification!+ "Set `last_notified_at` of the SecurityAdvisory with `id` to now, returning the number updated."+ [id :- ms/PositiveInt]+ (t2/update! :model/SecurityAdvisory id {:last_notified_at :%now}))++(mu/defn upsert-advisory!"Insert or update a SecurityAdvisory by `:advisory_id`. On insert, `:match_status` starts as `:unknown` until thematching engine evaluates it. On update, sets `advisory`'s columns, leaving `:match_status`, `:last_evaluated_at`,and acknowledgement fields (which `advisory` doesn't include) untouched."- [advisory]+ [advisory :- ::security-center.schema/security-advisory.update](mdb/update-or-insert! :model/SecurityAdvisory{:advisory_id (:advisory_id advisory)}(fn [existing]@@ -63,7 +81,7 @@advisory(assoc advisory :match_status :unknown)))))-(defn user-summaries-by-id+(mu/defn user-summaries-by-id"A map of ID to the ID, names, and email of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-fn->fn :id identity [:model/User :id :first_name :last_name :email] :id [:in user-ids]))diff --git a/enterprise/backend/src/metabase_enterprise/security_center/fetch.clj b/enterprise/backend/src/metabase_enterprise/security_center/fetch.cljindex d1ce73ba771..c0bd50a78fb 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/fetch.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/fetch.clj@@ -6,9 +6,9 @@[clojure.set :as set][java-time.api :as t][metabase-enterprise.security-center.db :as security-center.db]- [metabase-enterprise.security-center.schema :as security-center.schema][metabase.config.core :as config][metabase.premium-features.core :as premium-features]+ [metabase.security-center.schema :as security-center.schema][metabase.util.json :as json][metabase.util.log :as log][metabase.util.malli :as mu]diff --git a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj b/enterprise/backend/src/metabase_enterprise/security_center/matching.cljindex c65f973244d..d794a6d0fe0 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/matching.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/matching.clj@@ -3,11 +3,10 @@and combines with version checks to determine match status."(:require[metabase-enterprise.security-center.db :as security-center.db]- [metabase-enterprise.security-center.schema :as schema][metabase.app-db.core :as mdb][metabase.config.core :as config][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]- [metabase.models.interface :as mi]+ [metabase.security-center.schema :as schema][metabase.util.connection :as u.connection][metabase.util.log :as log][metabase.util.malli :as mu]@@ -148,11 +147,10 @@reactivated? (and (#{:active :error} match-status)currently-unaffected(some? (:acknowledged_at advisory)))]- (security-center.db/update-advisory! (:id advisory)- (cond-> {:match_status match-status- :last_evaluated_at (mi/now)}- reactivated? (assoc :acknowledged_at nil- :acknowledged_by nil))))))))+ (security-center.db/record-advisory-evaluation! (:id advisory)+ (cond-> {:match_status match-status}+ reactivated? (assoc :acknowledged_at nil+ :acknowledged_by nil))))))))(defn evaluate-all-advisories!"Re-evaluate every advisory, including acknowledged ones — an acked@@ -166,6 +164,5 @@(evaluate-advisory! advisory instance-version)(catch Exception e(log/warnf "Error evaluating advisory %s: %s" (:advisory_id advisory) (ex-message e))- (security-center.db/update-advisory! (:id advisory)- {:match_status :error- :last_evaluated_at (mi/now)}))))))))+ (security-center.db/record-advisory-evaluation! (:id advisory)+ {:match_status :error}))))))))diff --git a/enterprise/backend/src/metabase_enterprise/security_center/models/security_advisory.clj b/enterprise/backend/src/metabase_enterprise/security_center/models/security_advisory.cljindex 3d92b7ceae4..0768d9430b4 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/models/security_advisory.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/models/security_advisory.clj@@ -24,6 +24,13 @@[advisory](assoc advisory :fetched_at (mi/now)))+(t2/define-before-update :model/SecurityAdvisory+ [advisory]+ (let [changes (t2/changes advisory)]+ (cond-> advisory+ (and (some? (:acknowledged_by changes)) (not (contains? changes :acknowledged_at)))+ (assoc :acknowledged_at (mi/now)))))+(methodical/defmethod t2/batched-hydrate [:model/SecurityAdvisory :acknowledged_by_user]"Hydrate `:acknowledged_by_user` from the `:acknowledged_by` FK to a User map with `:id`, `:common_name`, and `:email`."[_model k advisories]@@ -43,18 +50,15 @@[advisory user-id](when (:acknowledged_at advisory)(throw (ex-info "Advisory already acknowledged" {:status-code 409})))- (let [now (mi/now)]- (security-center.db/update-advisory! (:id advisory)- {:acknowledged_by user-id- :acknowledged_at now})- (events/publish-event! :event/security-advisory-acknowledge- {:object advisory- :user-id user-id})- (analytics/track-event! :snowplow/simple_event- {:event "security_advisory_acknowledged"- :event_detail (name (:severity advisory))})- (-> (security-center.db/advisory (:id advisory))- (t2/hydrate :acknowledged_by_user))))+ (security-center.db/update-advisory! (:id advisory) {:acknowledged_by user-id})+ (events/publish-event! :event/security-advisory-acknowledge+ {:object advisory+ :user-id user-id})+ (analytics/track-event! :snowplow/simple_event+ {:event "security_advisory_acknowledged"+ :event_detail (name (:severity advisory))})+ (-> (security-center.db/advisory (:id advisory))+ (t2/hydrate :acknowledged_by_user)))(defn acknowledge-many!"Acknowledge multiple security advisories by their advisory_id strings. Skips already-acknowledgeddiff --git a/enterprise/backend/src/metabase_enterprise/security_center/notification.clj b/enterprise/backend/src/metabase_enterprise/security_center/notification.cljindex 3f3776a6066..4ecd70a1e0c 100644--- a/enterprise/backend/src/metabase_enterprise/security_center/notification.clj+++ b/enterprise/backend/src/metabase_enterprise/security_center/notification.clj@@ -15,7 +15,6 @@[metabase.analytics.core :as analytics][metabase.channel.settings :as channel.settings][metabase.events.core :as events]- [metabase.models.interface :as mi][metabase.notification.core :as notification][metabase.permissions.core :as perms][metabase.settings.core :as setting]@@ -169,7 +168,7 @@(try(notification/send-notification! notif :notification/sync? true)(track-notification-sent! notif triggered-from "success")- (security-center.db/update-advisory! (:id advisory) {:last_notified_at (mi/now)})+ (security-center.db/record-advisory-notification! (:id advisory))(catch Exception e(track-notification-sent! notif triggered-from "failure")(throw e))))))diff --git a/enterprise/backend/src/metabase_enterprise/security_center/schema.clj b/enterprise/backend/src/metabase_enterprise/security_center/schema.cljdeleted file mode 100644index f770fe2610e..00000000000--- a/enterprise/backend/src/metabase_enterprise/security_center/schema.clj+++ /dev/null@@ -1,39 +0,0 @@-(ns metabase-enterprise.security-center.schema- "Malli schemas for the Security Center module."- (:require- [metabase.util.malli.registry :as mr]))--(mr/def ::advisory-id- [:string {:min 1}])--(mr/def ::semver- [:re #"^\d+(?:\.\d+)*$"])--(mr/def ::severity- [:enum :critical :high :medium :low])--(mr/def ::match-status- [:enum :unknown :active :resolved :not_affected :error])--(mr/def ::version-range- "A single affected version range with inclusive min and exclusive fixed."- [:map- [:min ::semver]- [:fixed ::semver]])--(mr/def ::affected-versions- [:sequential ::version-range])--(mr/def ::download-jar-url- "A downloadable JAR for a given fixed version."- [:map- [:version ::semver]- [:url :string]])--(mr/def ::download-jar-urls- [:sequential ::download-jar-url])--(mr/def ::matching-query- "HoneySQL query keyed by dialect. nil means affects all instances.- Stored as EDN to preserve keywords that HoneySQL requires for identifiers/operators."- [:maybe [:map-of :keyword :map]])diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/appdb_scoring.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/appdb_scoring.cljnew file mode 100644index 00000000000..4eaeb4f7d28--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/appdb_scoring.clj@@ -0,0 +1,14 @@+(ns metabase-enterprise.semantic-search.appdb-scoring+ "The app-DB based scorers of semantic search results. Expression building only:+ `metabase-enterprise.semantic-search.db` runs the query, so this namespace must not require it."+ (:require+ [metabase.search.config :as search.config]+ [metabase.search.scoring :as search.scoring]))++(defn appdb-scorers+ "The appdb-based scorers for search ranking results. Like `base-scorers`, but for scorers that need to query the appdb."+ [search-ctx]+ (when-not (search.scoring/no-scoring-required? search-ctx)+ {:bookmarked search.scoring/bookmark-score-expr+ :user-recency (search.scoring/inverse-duration+ (search.scoring/user-recency-expr search-ctx) [:now] search.config/stale-time-in-days)}))diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/db.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/db.cljindex 4753f157c7e..536e30986c6 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/db.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/db.clj@@ -2,21 +2,27 @@"Application database queries for the semantic-search module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require+ [malli.util :as mut]+ [metabase-enterprise.semantic-search.appdb-scoring :as appdb-scoring]+ [metabase-enterprise.semantic-search.schema :as semantic-search.schema][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id][metabase.search.scoring :as search.scoring]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn library-root-collections+(mu/defn library-root-collections"The ID and type of the top-level Collections whose type is one of `types`."- [types]+ [types :- [:sequential :string]](t2/select [:model/Collection :id :type] :type [:in types] :location "/"))-(defn descendant-collection-ids+(mu/defn descendant-collection-ids"The IDs of the Collections under the top-level Collection with `root-id`."- [root-id]+ [root-id :- ms/PositiveInt](t2/select-pks-set :model/Collection :location [:like (str "/" root-id "/%")]))-(defn curated-tables-reducible+(mu/defn curated-tables-reducible"Reducible ID, published flag, data layer, and data authority of the active Tables that are published orauthoritative."[]@@ -26,38 +32,39 @@[:or [:= :is_published true][:= :data_authority ^:allow-raw-sql [:inline "authoritative"]]]]}))-(defn official-collection-ids+(mu/defn official-collection-ids"The IDs of the official Collections."[](t2/select-pks-set :model/Collection :authority_level :official))-(defn unarchived-dashboard-ids-in-collections-reducible+(mu/defn unarchived-dashboard-ids-in-collections-reducible"Reducible `:id` rows of the unarchived Dashboards in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/reducible-select [:model/Dashboard :id]{:where [:and [:= :archived false][:in :collection_id collection-ids]]}))-(defn collection-owners-and-locations+(mu/defn collection-owners-and-locations"The ID, owner, and location of the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select [:model/Collection :id :personal_owner_id :location] :id [:in collection-ids]))-(defn personal-collection-owners+(mu/defn personal-collection-owners"The ID and owner of the personal Collections among `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select [:model/Collection :id :personal_owner_id]:id [:in collection-ids]:personal_owner_id [:not= nil]))-(defn collection-locations+(mu/defn collection-locations"The ID and location of the raw collection rows with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select [:collection :id :location] :id [:in collection-ids]))-(defn instances+(mu/defn instances"The instances of `model` with `ids`."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select model :id [:in ids]))(defn- search-doc-select@@ -78,21 +85,27 @@[:search_index.model :model]]:from [:search_index]})-(defn appdb-scored-rows- "The `:id`/`:model` rows of `search-results` (each `{:id :model}`) augmented with the SELECT expressions of- `scorers` (a map of scorer key to SELECT expression, see- `metabase-enterprise.semantic-search.scoring/appdb-scorers`) evaluated under `search-ctx`, joining bookmark- tables when `:bookmarked` is among `scorers`."- [search-results search-ctx scorers]- (t2/query (cond-> (search.scoring/with-scores search-ctx scorers (search-index-select search-results))- (:bookmarked scorers) (search.scoring/join-bookmarks (:current-user-id search-ctx)))))+(mu/defn appdb-scored-rows+ "The `:id`/`:model` rows of `search-results` augmented with the app-DB scorers of `search-ctx` (see+ `metabase-enterprise.semantic-search.appdb-scoring/appdb-scorers`), joining the bookmark tables when `:bookmarked`+ is among them."+ [search-results :- [:sequential [:map {:closed true}+ [:id [:or :string ms/PositiveInt]]+ [:model :string]]]+ search-ctx :- [:map+ [:current-user-id {:optional true} [:maybe ms/PositiveInt]]+ [:context {:optional true} [:maybe :keyword]]+ [:weights {:optional true} [:maybe [:map-of :keyword number?]]]]]+ (let [scorers (appdb-scoring/appdb-scorers search-ctx)]+ (t2/query (cond-> (search.scoring/with-scores search-ctx scorers (search-index-select search-results))+ (:bookmarked scorers) (search.scoring/join-bookmarks (:current-user-id search-ctx))))))-(defn insert-token-tracking!+(mu/defn insert-token-tracking!"Insert the SemanticSearchTokenTracking `row`."- [row]+ [row :- (mut/select-keys ::semantic-search.schema/semantic-search-token-tracking.update [:model_name :request_type :total_tokens])](t2/insert! :model/SemanticSearchTokenTracking row))-(defn delete-token-tracking-created-before!+(mu/defn delete-token-tracking-created-before!"Delete the SemanticSearchTokenTracking rows created before `cutoff`."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/SemanticSearchTokenTracking {:where [:< :created_at cutoff]}))diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/index.cljindex b51d783a2ef..46bab180779 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj@@ -8,6 +8,7 @@[honey.sql :as sql][honey.sql.helpers :as sql.helpers][java-time.api :as t]+ [metabase-enterprise.semantic-search.appdb-scoring :as appdb-scoring][metabase-enterprise.semantic-search.db :as semantic-search.db];; TODO: extract schema code to go under db.migration[metabase-enterprise.semantic-search.embedding :as embedding]@@ -1261,7 +1262,7 @@(mapv search/collapse-id)))filter-time-ms (u/since-ms filter-timer)- appdb-scorers (scoring/appdb-scorers search-context)+ appdb-scorers (appdb-scoring/appdb-scorers search-context)appdb-scores-timer (u/start-timer)final-results (->> filtered-results(scoring/with-appdb-scores search-context appdb-scorers weights))diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/schema.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/schema.cljnew file mode 100644index 00000000000..73ddc07f954--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/schema.clj@@ -0,0 +1,22 @@+(ns metabase-enterprise.semantic-search.schema+ "Malli schemas for the semantic-search module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::semantic-search-token-tracking+ "A SemanticSearchTokenTracking as selected from the app DB: every column of `:semantic_search_token_tracking`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model_name :string]+ [:request_type [:maybe [:or :keyword :string]]]+ [:created_at ms/TemporalInstant]+ [:total_tokens :int]])++(mr/def ::semantic-search-token-tracking.update+ "What an update (or insert) of a SemanticSearchTokenTracking accepts: every column of `:semantic_search_token_tracking` except `id`, all optional."+ [:map {:closed true}+ [:model_name {:optional true} [:maybe :string]]+ [:request_type {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:total_tokens {:optional true} [:maybe :int]]])diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/scoring.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/scoring.cljindex 0a1325c5a51..32d81b796c5 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/scoring.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/scoring.clj@@ -186,14 +186,6 @@;; #{"segment" "database" "action" "indexed-entity"}(set/difference (set search.spec/search-models) appdb-scorer-models))-(defn appdb-scorers- "The appdb-based scorers for search ranking results. Like `base-scorers`, but for scorers that need to query the appdb."- [search-ctx]- (when-not (search.scoring/no-scoring-required? search-ctx)- {:bookmarked search.scoring/bookmark-score-expr- :user-recency (search.scoring/inverse-duration- (search.scoring/user-recency-expr search-ctx) [:now] search.config/stale-time-in-days)}))-(defn with-appdb-scores"Add appdb-based scores to `search-results` and re-sort the results based on the new combined scores.@@ -208,7 +200,8 @@(if-not (and (seq search-results-to-score)(seq appdb-scorers))search-results- (->> (semantic-search.db/appdb-scored-rows search-results-to-score search-ctx appdb-scorers)+ (->> (semantic-search.db/appdb-scored-rows (mapv #(select-keys % [:id :model]) search-results-to-score)+ search-ctx)(update-with-appdb-scores weights (keys appdb-scorers) search-results)(sort-by :score >)vec))))diff --git a/enterprise/backend/src/metabase_enterprise/serialization/db.clj b/enterprise/backend/src/metabase_enterprise/serialization/db.cljindex a04e5ab0d45..67d83e18c6a 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/db.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/db.clj@@ -2,45 +2,52 @@"Application database queries for the serialization module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model-level helpers."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn all-users+(mu/defn all-users"Every User."[](t2/select :model/User))-(defn collection-id-row+(mu/defn collection-id-row"The `:collection_id` row of the instance of `model` with `id`, or nil."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one [model :collection_id] :id id))-(defn existing-ids+(mu/defn existing-ids"The subset of `ids` for which an instance of `model` exists."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select-pks-set model {:where [:in :id ids]}))-(defn instance-exists?+(mu/defn instance-exists?"Whether an instance of `model` whose primary key is `id` exists."- [model id]+ [model :- :keyword+ id :- [:maybe ms/PositiveInt]](t2/exists? model (first (t2/primary-keys model)) id))-(defn root-collections-for-user+(mu/defn root-collections-for-user"The top-level non-analytics Collections that are not personal or belong to the User with `user-id`."- [user-id]+ [user-id :- [:maybe ::lib.schema.id/user]](t2/select :model/Collection {:where [:and [:= :location "/"][:or [:= :personal_owner_id nil][:= :personal_owner_id user-id]][:or [:= :namespace nil][:!= :namespace "analytics"]]]}))-(defn analytics-root-collections+(mu/defn analytics-root-collections"The Collections in the analytics namespace."[](t2/select :model/Collection {:where [:= :namespace "analytics"]}))-(defn card-ids-in-collections+(mu/defn card-ids-in-collections"The subset of `card-ids` living in the Collections with `collection-ids`."- [card-ids collection-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]+ collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pks-set :model/Card {:where [:and[:in :id card-ids][:in :collection_id collection-ids]]}))diff --git a/enterprise/backend/src/metabase_enterprise/snippet_collections/db.clj b/enterprise/backend/src/metabase_enterprise/snippet_collections/db.cljindex 86eb0541f38..2957a93b0ea 100644--- a/enterprise/backend/src/metabase_enterprise/snippet_collections/db.clj+++ b/enterprise/backend/src/metabase_enterprise/snippet_collections/db.clj@@ -2,11 +2,13 @@"Application database queries for the snippet-collections module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn snippet-with-collection-id+(mu/defn snippet-with-collection-id"The `:collection_id` of the NativeQuerySnippet with `id`, or nil if no such NativeQuerySnippet exists. Unlike abare column lookup, this distinguishes a missing snippet (nil) from one filed under the root collection(`{:collection_id nil}`)."- [id]+ [id :- ::lib.schema.id/native-query-snippet](t2/select-one [:model/NativeQuerySnippet :collection_id] :id id))diff --git a/enterprise/backend/src/metabase_enterprise/sso/db.clj b/enterprise/backend/src/metabase_enterprise/sso/db.cljindex dcb03e7eca5..3b1b516cb4e 100644--- a/enterprise/backend/src/metabase_enterprise/sso/db.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/db.clj@@ -2,42 +2,50 @@"Application database queries for the sso module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn session-user-email-and-source+(mu/defn session-user-email-and-source"The email and SSO source of the User owning the Session with `session-key-hashed`, or nil."- [session-key-hashed]+ [session-key-hashed :- :string](t2/query-one {:select [:u.email :u.sso_source]:from [[:core_user :u]]:join [[:core_session :session] [:= :u.id :session.user_id]]:where [:= :key_hashed session-key-hashed]}))-(defn delete-session!- "Delete the Session with `session-key-hashed`."- [session-key-hashed]+(mu/defn delete-session!+ "Delete the Session with `session-key-hashed`, returning the number deleted."+ [session-key-hashed :- :string](t2/delete! :model/Session :key_hashed session-key-hashed))-(defn group-ids-by-name+(mu/defn group-ids-by-name"The IDs of the PermissionsGroups named one of `group-names`."- [group-names]+ [group-names :- [:set :string]](t2/select-pks-set :model/PermissionsGroup :name [:in group-names]))-(defn insert-relay-state!- "Insert the SsoRelayState `row`."- [row]+(mu/defn insert-relay-state!+ "Insert the SsoRelayState `row`, returning the number inserted."+ [row :- [:map {:closed true}+ [:id :string]+ [:continue_url [:maybe :string]]+ [:origin [:maybe :string]]+ [:embedding :boolean]+ [:expires_at ms/TemporalInstant]]](t2/insert! :model/SsoRelayState row))-(defn unexpired-relay-state+(mu/defn unexpired-relay-state"The SsoRelayState with `hashed-key` expiring after `now`, or nil."- [hashed-key now]+ [hashed-key :- :string+ now :- ms/TemporalInstant](t2/select-one :model/SsoRelayState :id hashed-key :expires_at [:> now]))-(defn delete-relay-state!- "Delete the SsoRelayState with `hashed-key`."- [hashed-key]+(mu/defn delete-relay-state!+ "Delete the SsoRelayState with `hashed-key`, returning the number deleted."+ [hashed-key :- :string](t2/delete! :model/SsoRelayState :id hashed-key))-(defn delete-relay-states-expired-at!+(mu/defn delete-relay-states-expired-at!"Delete the SsoRelayStates expired at or before `now`, returning the number deleted."- [now]+ [now :- ms/TemporalInstant](t2/delete! :model/SsoRelayState :expires_at [:<= now]))diff --git a/enterprise/backend/src/metabase_enterprise/sso/schema.clj b/enterprise/backend/src/metabase_enterprise/sso/schema.cljnew file mode 100644index 00000000000..c738e35f971--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/sso/schema.clj@@ -0,0 +1,24 @@+(ns metabase-enterprise.sso.schema+ "Malli schemas for the sso module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::sso-relay-state+ "A SsoRelayState as selected from the app DB: every column of `:sso_relay_state`."+ [:map {:closed true}+ [:id :string]+ [:continue_url :string]+ [:origin [:maybe :string]]+ [:embedding :boolean]+ [:expires_at ms/TemporalInstant]+ [:created_at ms/TemporalInstant]])++(mr/def ::sso-relay-state.update+ "What an update (or insert) of a SsoRelayState accepts: every column of `:sso_relay_state` except `id`, all optional."+ [:map {:closed true}+ [:continue_url {:optional true} [:maybe :string]]+ [:origin {:optional true} [:maybe :string]]+ [:embedding {:optional true} [:maybe :boolean]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/enterprise/backend/src/metabase_enterprise/stale/db.clj b/enterprise/backend/src/metabase_enterprise/stale/db.cljindex 48774e8b98c..51a913c2ea9 100644--- a/enterprise/backend/src/metabase_enterprise/stale/db.clj+++ b/enterprise/backend/src/metabase_enterprise/stale/db.clj@@ -2,21 +2,23 @@"Application database queries for the stale module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection collection-id))-(defn collections-by-id+(mu/defn collections-by-id"A map of ID to Collection for `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select-pk->fn identity :model/Collection :id [:in collection-ids]))-(defn stale-cards+(mu/defn stale-cards"The listing columns of the Cards with `card-ids`, with their latest moderation status."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card:id:dashboard_id@@ -47,9 +49,9 @@:moderated_status]]:id [:in card-ids]))-(defn stale-dashboards+(mu/defn stale-dashboards"The listing columns of the Dashboards with `dashboard-ids`."- [dashboard-ids]+ [dashboard-ids :- [:set ::lib.schema.id/dashboard]](t2/select [:model/Dashboard:id:description@@ -71,11 +73,15 @@[union-queries][[^:allow-subquery {:union-all union-queries} :dummy_alias]])-(defn stale-content-rows+(mu/defn stale-content-rows"A page of `:id`/`:model` rows from the union of `union-queries`, sorted by `sort-column` (`:name` or`:last_used_at`) in `sort-direction`, skipping `offset` and returning up to `limit` (either may be nil for norestriction)."- [union-queries sort-column sort-direction limit offset]+ [union-queries :- [:sequential :map]+ sort-column :- [:enum :name :last_used_at]+ sort-direction :- [:enum :asc :desc]+ limit :- [:maybe :int]+ offset :- [:maybe :int]](t2/query (cond-> {:select [:id :model]:from (stale-content-union union-queries):order-by [[(case sort-column@@ -85,8 +91,8 @@(some? limit) (assoc :limit limit)(some? offset) (assoc :offset offset))))-(defn stale-content-count+(mu/defn stale-content-count"The total count of rows across every page [[stale-content-rows]] would return for `union-queries`."- [union-queries]+ [union-queries :- [:sequential :map]](:count (t2/query-one {:select [[:%count.* :count]]:from (stale-content-union union-queries)})))diff --git a/enterprise/backend/src/metabase_enterprise/support_access_grants/db.clj b/enterprise/backend/src/metabase_enterprise/support_access_grants/db.cljindex 0081b59162a..6515780e75f 100644--- a/enterprise/backend/src/metabase_enterprise/support_access_grants/db.clj+++ b/enterprise/backend/src/metabase_enterprise/support_access_grants/db.clj@@ -2,19 +2,26 @@"Application database queries for the support-access-grants module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require+ [malli.util :as mut]+ [metabase-enterprise.support-access-grants.schema :as support-access-grants.schema]+ [metabase.auth-identity.schema :as auth-identity.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.users.schema :as users.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn active-grant-exists?+(mu/defn active-grant-exists?"Whether a SupportAccessGrantLog is unrevoked and ends after `now`."- [now]+ [now :- ms/TemporalInstant](t2/exists? :model/SupportAccessGrantLog :revoked_at nil :grant_end_timestamp [:> now]))-(defn grant+(mu/defn grant"The SupportAccessGrantLog with `grant-id`, or nil."- [grant-id]+ [grant-id :- ms/PositiveInt](t2/select-one :model/SupportAccessGrantLog :id grant-id))-(defn current-grant+(mu/defn current-grant"The newest unrevoked SupportAccessGrantLog that has not ended, or nil."[](t2/select-one :model/SupportAccessGrantLog@@ -32,10 +39,14 @@(when (seq conditions)(into [:and] conditions))))-(defn grants-page+(mu/defn grants-page"The newest-first SupportAccessGrantLogs, optionally narrowed to `ticket-number` and `user-id` and excluding revokedgrants unless `include-revoked?`, paged by `limit` and `offset`."- [include-revoked? ticket-number user-id limit offset]+ [include-revoked? :- [:maybe :boolean]+ ticket-number :- [:maybe :string]+ user-id :- [:maybe ::lib.schema.id/user]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](let [where (grants-where include-revoked? ticket-number user-id)](t2/select :model/SupportAccessGrantLog(cond-> {:limit limit@@ -43,85 +54,104 @@:order-by [[:created_at :desc]]}where (assoc :where where)))))-(defn grant-count+(mu/defn grant-count"The number of SupportAccessGrantLogs [[grants-page]] would page through."- [include-revoked? ticket-number user-id]+ [include-revoked? :- [:maybe :boolean]+ ticket-number :- [:maybe :string]+ user-id :- [:maybe ::lib.schema.id/user]](let [where (grants-where include-revoked? ticket-number user-id)](t2/count :model/SupportAccessGrantLog(cond-> {}where (assoc :where where)))))-(defn insert-grant!+(mu/defn insert-grant!"Insert `grant` and return the new instance."- [grant]+ [grant :- (mut/select-keys ::support-access-grants.schema/support-access-grant-log.update [:user_id :ticket_number :notes :grant_start_timestamp :grant_end_timestamp])](t2/insert-returning-instance! :model/SupportAccessGrantLog grant))-(defn update-grant!- "Apply `changes` to the SupportAccessGrantLog with `grant-id`."- [grant-id changes]+(mu/defn update-grant!+ "Apply `changes` to the SupportAccessGrantLog with `grant-id`, returning the number updated."+ [grant-id :- ms/PositiveInt+ changes :- (mut/select-keys ::support-access-grants.schema/support-access-grant-log.update [:revoked_at :revoked_by_user_id])](t2/update! :model/SupportAccessGrantLog grant-id changes))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User user-id))-(defn user-by-email+(mu/defn user-by-email"The User with `email`, or nil."- [email]+ [email :- :string](t2/select-one :model/User :email email))-(defn user-superuser-flag-by-email+(mu/defn user-superuser-flag-by-email"The `:id` and `:is_superuser` of the User with `email`, or nil."- [email]+ [email :- :string](t2/select-one [:model/User :id :is_superuser] :email email))-(defn user-names-and-emails+(mu/defn user-names-and-emails"A map of User ID to first name and email for `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn #(select-keys % [:first_name :email]) [:model/User :id :first_name :email] :id [:in user-ids]))-(defn insert-user!+(mu/defn insert-user!"Insert `user` and return the new instance."- [user]+ [user :- (mut/select-keys ::users.schema/user.update [:email :first_name :last_name :is_superuser])](t2/insert-returning-instance! :model/User user))-(defn update-user!- "Apply `changes` to the User with `user-id`."- [user-id changes]+(mu/defn update-user!+ "Apply `changes` to the User with `user-id`, returning the number updated."+ [user-id :- ::lib.schema.id/user+ changes :- (mut/select-keys ::users.schema/user.update [:is_active :is_superuser])](t2/update! :model/User user-id changes))-(defn session-exists-for-user?+(mu/defn session-exists-for-user?"Whether the User with `user-id` has a Session."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/exists? :model/Session :user_id user-id))-(defn delete-sessions-of-user!- "Delete the Sessions of the User with `user-id`."- [user-id]+(mu/defn delete-sessions-of-user!+ "Delete the Sessions of the User with `user-id`, returning the number deleted."+ [user-id :- ::lib.schema.id/user](t2/delete! :model/Session :user_id user-id))-(defn auth-identity-ids-of-user+(mu/defn auth-identity-ids-of-user"The IDs of the AuthIdentities of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-pks-vec :model/AuthIdentity :user_id user-id))-(defn support-access-auth-identity-id+(mu/defn support-access-auth-identity-id"The ID of the support-access-grant AuthIdentity of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-pk :model/AuthIdentity :user_id user-id :provider "support-access-grant"))-(defn insert-auth-identity!- "Insert the AuthIdentity `row`."- [row]+(mu/defn insert-auth-identity!+ "Insert the AuthIdentity `row`, returning the number inserted."+ [row :- (mut/select-keys ::auth-identity.schema/auth-identity.update [:user_id :provider :provider_id :expires_at :credentials :metadata])](t2/insert! :model/AuthIdentity row))-(defn update-auth-identity!- "Apply `changes` to the AuthIdentity with `auth-identity-id`."- [auth-identity-id changes]+(mu/defn update-auth-identity!+ "Apply `changes` to the AuthIdentity with `auth-identity-id`, returning the number updated. `changes` may be a+ whole AuthIdentity instance re-saved after a partial edit (e.g. [[metabase.auth-identity.providers.emailed-secret/mark-token-consumed]]+ round-trips the full row it was given), so every column is accepted."+ [auth-identity-id :- ms/PositiveInt+ changes :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:user_id {:optional true} ::lib.schema.id/user]+ [:provider {:optional true} [:maybe [:or :keyword :string]]]+ [:provider_id {:optional true} [:maybe :string]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]+ [:confirmed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} ms/TemporalInstant]+ [:updated_at {:optional true} ms/TemporalInstant]+ [:credentials {:optional true} [:maybe :map]]+ [:metadata {:optional true} [:maybe :map]]]](t2/update! :model/AuthIdentity auth-identity-id changes))-(defn expire-auth-identities!- "Set the expiry of the AuthIdentities with `auth-identity-ids` to `expires-at`."- [auth-identity-ids expires-at]+(mu/defn expire-auth-identities!+ "Set the expiry of the AuthIdentities with `auth-identity-ids` to `expires-at`, returning the number updated."+ [auth-identity-ids :- [:sequential ms/PositiveInt]+ expires-at :- ms/TemporalInstant](t2/update! :model/AuthIdentity :id [:in auth-identity-ids] {:expires_at expires-at}))diff --git a/enterprise/backend/src/metabase_enterprise/support_access_grants/schema.clj b/enterprise/backend/src/metabase_enterprise/support_access_grants/schema.cljindex ebe2dbd560c..74bf9cc513a 100644--- a/enterprise/backend/src/metabase_enterprise/support_access_grants/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/support_access_grants/schema.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.support-access-grants.schema"Malli schemas for support access grant API request and response bodies."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]))@@ -53,3 +54,30 @@(mr/def ::current-grant-response"Schema for GET /api/ee/support-access-grants/current response."[:maybe ::grant-response])++(mr/def ::support-access-grant-log+ "A SupportAccessGrantLog as selected from the app DB: every column of `:support_access_grant_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:ticket_number [:maybe :string]]+ [:notes [:maybe :string]]+ [:grant_start_timestamp ms/TemporalInstant]+ [:grant_end_timestamp ms/TemporalInstant]+ [:revoked_at [:maybe ms/TemporalInstant]]+ [:revoked_by_user_id [:maybe ::lib.schema.id/user]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::support-access-grant-log.update+ "What an update (or insert) of a SupportAccessGrantLog accepts: every column of `:support_access_grant_log` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:ticket_number {:optional true} [:maybe :string]]+ [:notes {:optional true} [:maybe :string]]+ [:grant_start_timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:grant_end_timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:revoked_at {:optional true} [:maybe ms/TemporalInstant]]+ [:revoked_by_user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/enterprise/backend/src/metabase_enterprise/tenants/db.clj b/enterprise/backend/src/metabase_enterprise/tenants/db.cljindex 199ac216cc6..a313550118b 100644--- a/enterprise/backend/src/metabase_enterprise/tenants/db.clj+++ b/enterprise/backend/src/metabase_enterprise/tenants/db.clj@@ -2,32 +2,40 @@"Application database queries for the tenants module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods, and transactions."(:require+ [malli.util :as mut]+ [metabase-enterprise.tenants.schema :as tenants.schema]+ [metabase.collections.schema :as collections.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn tenant+(mu/defn tenant"The Tenant with `tenant-id`, or nil."- [tenant-id]+ [tenant-id :- ms/PositiveInt](t2/select-one :model/Tenant :id tenant-id))-(defn tenant-by-slug+(mu/defn tenant-by-slug"The Tenant with `slug`, or nil."- [slug]+ [slug :- :string](t2/select-one :model/Tenant :slug slug))-(defn tenant-slug+(mu/defn tenant-slug"The slug of the Tenant with `tenant-id`."- [tenant-id]+ [tenant-id :- ms/PositiveInt](t2/select-one-fn :slug :model/Tenant :id tenant-id))-(defn tenant-collection-id+(mu/defn tenant-collection-id"The root Collection ID of the Tenant with `tenant-id`."- [tenant-id]+ [tenant-id :- ms/PositiveInt](t2/select-one-fn :tenant_collection_id :model/Tenant :id tenant-id))-(defn tenants-page+(mu/defn tenants-page"The Tenants in ID order, restricted by `status` (`\"all\"`, `\"active\"`, or `\"deactivated\"`) and paged by theoptional `limit` and `offset`."- [status limit offset]+ [status :- [:enum "all" "active" "deactivated"]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/Tenant (cond-> {:order-by [[:id :asc]]:where (case status"all" [:= [:inline 1] [:inline 1]]@@ -35,7 +43,7 @@"deactivated" [:= :is_active false])}limit (assoc :limit limit :offset offset))))-(defn tenant-attributes-reducible+(mu/defn tenant-attributes-reducible"Reducible attribute maps of the Tenants that have attributes."[](t2/select-fn-reducible :attributes [:model/Tenant :attributes]@@ -43,41 +51,44 @@[:not= :attributes nil][:not= :attributes "{}"]]}))-(defn tenant-names-and-ids-by-collection+(mu/defn tenant-names-and-ids-by-collection"A map of root Collection ID to `[name id]` for the Tenants owning `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-fn->fn :tenant_collection_id (juxt :name :id) :model/Tenant :tenant_collection_id [:in collection-ids]))-(defn active-tenant-exists?+(mu/defn active-tenant-exists?"Whether the Tenant with `tenant-id` exists and is active."- [tenant-id]+ [tenant-id :- ms/PositiveInt](t2/exists? :model/Tenant :id tenant-id :is_active true))-(defn tenant-name-or-slug-exists?+(mu/defn tenant-name-or-slug-exists?"Whether a Tenant named `tenant-name` or with `slug` exists."- [tenant-name slug]+ [tenant-name :- :string+ slug :- :string](t2/exists? :model/Tenant {:where [:or[:= :slug slug][:= :name tenant-name]]}))-(defn other-tenant-named?+(mu/defn other-tenant-named?"Whether a Tenant other than `tenant-id` is named `tenant-name`."- [tenant-name tenant-id]+ [tenant-name :- :string+ tenant-id :- ms/PositiveInt](t2/exists? :model/Tenant :name tenant-name :id [:not= tenant-id]))-(defn insert-tenant!+(mu/defn insert-tenant!"Insert `tenant` and return the new instance."- [tenant]+ [tenant :- (mut/select-keys ::tenants.schema/tenant.update [:name :slug :attributes])](t2/insert-returning-instance! :model/Tenant tenant))-(defn update-tenant!- "Apply `changes` to the Tenant with `tenant-id`."- [tenant-id changes]+(mu/defn update-tenant!+ "Apply `changes` to the Tenant with `tenant-id`, returning the number updated."+ [tenant-id :- ms/PositiveInt+ changes :- (mut/select-keys ::tenants.schema/tenant.update [:name :attributes :is_active])](t2/update! :model/Tenant {:id tenant-id} changes))-(defn active-member-counts+(mu/defn active-member-counts"Rows of `:tenant_id` and `:count` of active personal Users for `tenant-ids`."- [tenant-ids]+ [tenant-ids :- [:sequential ms/PositiveInt]](t2/query {:select [[:tenant_id] [[:count :*] :count]]:from [(t2/table-name :model/User)]:where [:and@@ -86,39 +97,41 @@:is_active]:group-by [:tenant_id]}))-(defn deactivate-tenant-users!- "Deactivate the active Users of the Tenant with `tenant-id`, marking them as deactivated with it."- [tenant-id]+(mu/defn deactivate-tenant-users!+ "Deactivate the active Users of the Tenant with `tenant-id`, marking them as deactivated with it, returning the+ number updated."+ [tenant-id :- ms/PositiveInt](t2/update! :model/User {:is_active true :tenant_id tenant-id} {:is_active false :deactivated_with_tenant true}))-(defn reactivate-tenant-users!- "Reactivate the Users of the Tenant with `tenant-id` that were deactivated with it."- [tenant-id]+(mu/defn reactivate-tenant-users!+ "Reactivate the Users of the Tenant with `tenant-id` that were deactivated with it, returning the number updated."+ [tenant-id :- ms/PositiveInt](t2/update! :model/User {:is_active false :tenant_id tenant-id :deactivated_with_tenant true}{:is_active true :deactivated_with_tenant nil}))-(defn user-tenant-id+(mu/defn user-tenant-id"The Tenant ID of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :tenant_id :model/User :id user-id))-(defn collection-with-archived-state+(mu/defn collection-with-archived-state"The Collection with `collection-id` if its archived flag is `archived?`, or nil."- [collection-id archived?]+ [collection-id :- ::lib.schema.id/collection+ archived? :- :boolean](t2/select-one :model/Collection :id collection-id :archived archived?))-(defn descendant-collection-ids+(mu/defn descendant-collection-ids"The IDs of the Collections under the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-pks-set :model/Collection :location [:like (str "/" collection-id "/%")]))-(defn insert-collection!+(mu/defn insert-collection!"Insert `collection` and return its ID."- [collection]+ [collection :- ::collections.schema/collection.update](t2/insert-returning-pk! :model/Collection collection))-(defn delete-permissions-with-objects!- "Delete the Permissions rows for `objects`."- [objects]+(mu/defn delete-permissions-with-objects!+ "Delete the Permissions rows for `objects`, returning the number deleted."+ [objects :- [:sequential :string]](t2/query-one {:delete-from :permissions:where [:in :object objects]}))diff --git a/enterprise/backend/src/metabase_enterprise/tenants/schema.clj b/enterprise/backend/src/metabase_enterprise/tenants/schema.cljnew file mode 100644index 00000000000..7552aeb6682--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/tenants/schema.clj@@ -0,0 +1,33 @@+(ns metabase-enterprise.tenants.schema+ "Malli schemas for the tenants module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::tenant.attributes+ "The `:attributes` column of a Tenant, decoded."+ [:map-of :string [:maybe [:or :string number? :boolean]]])++(mr/def ::tenant+ "A Tenant as selected from the app DB: every column of `:tenant`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:slug :string]+ [:is_active :boolean]+ [:updated_at ms/TemporalInstant]+ [:created_at ms/TemporalInstant]+ [:attributes [:maybe ::tenant.attributes]]+ [:tenant_collection_id ::lib.schema.id/collection]])++(mr/def ::tenant.update+ "What an update (or insert) of a Tenant accepts: every column of `:tenant` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:slug {:optional true} [:maybe :string]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:attributes {:optional true} [:maybe ::tenant.attributes]]+ [:tenant_collection_id {:optional true} [:maybe ::lib.schema.id/collection]]])diff --git a/enterprise/backend/src/metabase_enterprise/transforms/db.clj b/enterprise/backend/src/metabase_enterprise/transforms/db.cljindex c715e7e8f79..b33a46dd569 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/db.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/db.clj@@ -2,16 +2,21 @@"Application database queries for the transforms module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn cache-table-dependencies!- "Store `dependencies-json` as the cached table dependencies of the Transform with `transform-id`."- [transform-id dependencies-json]+(mu/defn cache-table-dependencies!+ "Store `dependencies-json` as the cached table dependencies of the Transform with `transform-id`, returning the+ number updated."+ [transform-id :- ::lib.schema.id/transform+ dependencies-json :- :string](t2/update! (t2/table-name :model/Transform) transform-id {:table_dependencies dependencies-json}))-(defn succeeded-run-counts-by-meter+(mu/defn succeeded-run-counts-by-meter"Rows of `:metered_as` and `:cnt` of the TransformRuns that succeeded on `date`."- [date]+ [date :- ms/TemporalInstant](t2/query {:select [:r.metered_as [[:count :r.id] :cnt]]:from [[:transform_run :r]]:where [:anddiff --git a/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj b/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.cljindex a878f8b1ace..53a61b69740 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_inspector/db.clj@@ -2,19 +2,21 @@"Application database queries for the transforms-inspector module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn table-source-rows+(mu/defn table-source-rows"The ID, name, schema, and Database ID of the Tables with `table-ids`, as source info."- [table-ids]+ [table-ids :- [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]](t2/select [:model/Table [:id :table-id] [:name :table-name] :schema [:db_id :db-id]] :id [:in table-ids]))-(defn database-engine+(mu/defn database-engine"The engine of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :engine :model/Database :id database-id))-(defn active-fields-for-table+(mu/defn active-fields-for-table"The active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :active true))diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/db.cljindex bfc4e2d6e89..f65ceabc769 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/db.clj@@ -3,58 +3,63 @@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]))-(defn table-database-ids+(mu/defn table-database-ids"The set of Database IDs of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select-fn-set :db_id [:model/Table :db_id] :id [:in table-ids]))-(defn table-database-id+(mu/defn table-database-id"The Database ID of the raw table row with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id (t2/table-name :model/Table) :id table-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn database-engine+(mu/defn database-engine"The engine of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :engine :model/Database database-id))-(defn update-run-message!+(mu/defn update-run-message!"Set the message of the TransformRun with `run-id`."- [run-id message]+ [run-id :- ms/PositiveInt+ message :- [:maybe :string]](t2/update! :model/TransformRun :id run-id {:message message}))-(defn python-library+(mu/defn python-library"The PythonLibrary with `library-id`, or nil."- [library-id]+ [library-id :- ms/PositiveInt](t2/select-one :model/PythonLibrary library-id))-(defn python-library-by-path+(mu/defn python-library-by-path"The PythonLibrary at `path`, or nil."- [path]+ [path :- :string](t2/select-one :model/PythonLibrary :path path))-(defn upsert-python-library-source!+(mu/defn upsert-python-library-source!"Insert or update the PythonLibrary at `path`, setting its source to `source`. Returns the ID of the row."- [path source]+ [path :- :string+ source :- :string](mdb/update-or-insert! :model/PythonLibrary{:path path}(constantly {:path path :source source})))-(defn library-sources-by-path+(mu/defn library-sources-by-path"A map of path to source for every PythonLibrary."[](t2/select-fn->fn :path :source :model/PythonLibrary))-(defn top-level-fields-metadata+(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]+ [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 truediff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/schema.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/schema.cljnew file mode 100644index 00000000000..925fb8f0d92--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/schema.clj@@ -0,0 +1,24 @@+(ns metabase-enterprise.transforms-python.schema+ "Malli schemas for the transforms-python module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::python-library+ "A PythonLibrary as selected from the app DB: every column of `:python_library`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:path :string]+ [:source [:maybe [:or :keyword :string]]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:entity_id :string]])++(mr/def ::python-library.update+ "What an update (or insert) of a PythonLibrary accepts: every column of `:python_library` except `id`, all optional."+ [:map {:closed true}+ [:path {:optional true} [:maybe :string]]+ [:source {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:entity_id {:optional true} [:maybe :string]]])diff --git a/enterprise/backend/src/metabase_enterprise/upload_management/db.clj b/enterprise/backend/src/metabase_enterprise/upload_management/db.cljindex 12e9db3bf1c..7f4a8092795 100644--- a/enterprise/backend/src/metabase_enterprise/upload_management/db.clj+++ b/enterprise/backend/src/metabase_enterprise/upload_management/db.clj@@ -2,24 +2,26 @@"Application database queries for the upload-management module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn attached-dwh-database-id+(mu/defn attached-dwh-database-id"The ID of the attached data warehouse Database, or nil."[](t2/select-one-fn :id :model/Database :is_attached_dwh true))-(defn non-upload-tables-for-database+(mu/defn non-upload-tables-for-database"The active Tables of the Database with `database-id` that were not uploaded."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select :model/Table :db_id database-id :active true :is_upload false))-(defn upload-tables+(mu/defn upload-tables"The active uploaded Tables, ordered by name."[](t2/select :model/Table :active true :is_upload true {:order-by [[:name :asc]]}))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table table-id))diff --git a/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.cljindex cc4dd965337..50b6bd4663c 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj@@ -3,14 +3,14 @@(:require[clojure.test :refer :all][metabase-enterprise.semantic-search.test-util :as semantic.tu]+ [metabase.metabot.db :as metabot.db][metabase.metabot.tools.search :as search][metabase.permissions.core :as perms][metabase.search.core :as search-core][metabase.search.engine :as search.engine][metabase.search.ingestion :as search.ingestion][metabase.search.test-util :as search.tu]- [metabase.test :as mt]- [toucan2.core :as t2]))+ [metabase.test :as mt]))(use-fixtures :once #'semantic.tu/once-fixture)@@ -109,9 +109,9 @@(testing "search with metabot verified-or-curated content flag"(let [metabot {:entity_id "test-bot":use_verified_content true}]- (with-redefs [t2/select-one (fn [model & _]- (is (= :model/Metabot model) "Should query for Metabot model")- metabot)+ (with-redefs [metabot.db/metabot-by-entity-id (fn [entity-id]+ (is (= "test-bot" entity-id) "Should look up the Metabot")+ metabot)search-core/search (fn [context];; use_verified_content now drives the curated filter, not :verified(is (true? (:curated? context)))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.cljindex b869909dbdf..ee994b5c666 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj@@ -377,7 +377,8 @@(is (some #(str/includes? % "collection") (keys files-after-export)))(is (some #(str/includes? % "card") (keys files-after-export))))(t2/delete! :model/RemoteSyncTask :id (:id export-task))- (let [import-task (t2/with-connection [_conn (app-db/app-db) (t2/insert-returning-instance! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})])+ (let [import-task (t2/with-connection [_conn (app-db/app-db)]+ (t2/insert-returning-instance! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)}))import-result (impl/import! (source.p/snapshot mock-main) (:id import-task))](remote-sync.task/complete-sync-task! (:id import-task))(is (= :success (:status import-result)))diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.cljindex ef7e3881121..92e6b70f91c 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj@@ -12,8 +12,8 @@[metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb][metabase.search.appdb.core :as appdb]+ [metabase.search.db :as search.db][metabase.search.engine :as search.engine]- [metabase.search.in-place.legacy :as in-place.legacy][metabase.search.in-place.scoring :as in-place.scoring][metabase.search.settings :as search.settings][metabase.test :as mt])@@ -131,7 +131,7 @@(fn [result _]{:result (dissoc result :score):score (:score result)})- in-place.legacy/results+ search.db/in-place-search-reducible(fn [_](reset! legacy-called? true)(reify clojure.lang.IReduceInitdiff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.cljindex 93d2bb1c1e1..b7c25053d5f 100644--- a/mage/src/mage/modules.clj+++ b/mage/src/mage/modules.clj@@ -91,8 +91,8 @@analyticsanalytics-interfaceapi- api-scopeapi-keys+ api-scopeappearanceaudit-appauth-identity@@ -120,16 +120,19 @@explorationsformattergeojson+ indexed-entitiesinitialization-statusinterestingnessinternal-statsllmlogin-historymcp+ measuresmetabotmqnotificationoauth-server+ osipermissionspremium-featurespublic-sharing@@ -139,6 +142,7 @@sample-datasearchsecrets+ segmentsserversessionsettings@@ -158,7 +162,8 @@utilversionview-log- warehouse-schema})+ warehouse-schema+ xrays})(defn- affected-modules"Set of modules that are direct or indirect dependents of `modules`, and thus are affected by changes to them.diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.cljindex 6e706a8f615..119fffecdd3 100644--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/db.clj@@ -2,24 +2,29 @@"Application database queries for the bigquery-cloud-sdk driver. Every function here is a direct Toucan 2 call withno additional logic, so the rest of the driver never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.warehouses.schema :as warehouses.schema];; the driver persists dataset-filter and project-id migrations of its Database details back to the app DB^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn update-database-details!- "Set the details of the Database with `database-id`."- [database-id details]+(mu/defn update-database-details!+ "Set the details of the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ details :- ::warehouses.schema/database.details](t2/update! :model/Database database-id {:details details}))-(defn set-table-schemas!+(mu/defn set-table-schemas!"Set the schema of the Tables of the Database with `database-id` that do not have `schema` yet, overwriting any- stale schema they already have."- [database-id schema]+ stale schema they already have, returning the number updated."+ [database-id :- ::lib.schema.id/database+ schema :- :string](t2/query-one {:update (t2/table-name :model/Table):set {:schema schema}:where [:and@@ -28,7 +33,8 @@[:= :schema nil][:not= :schema schema]]]}))-(defn active-partitioned-field-exists?+(mu/defn active-partitioned-field-exists?"Whether the Table with `table-id` has an active database-partitioned Field named `field-name`."- [table-id field-name]+ [table-id :- ::lib.schema.id/table+ field-name :- :string](t2/exists? :model/Field :table_id table-id :name field-name :database_partitioned true :active true))diff --git a/src/metabase/actions/db.clj b/src/metabase/actions/db.cljindex eecf2681d7e..7bb2fc58591 100644--- a/src/metabase/actions/db.clj+++ b/src/metabase/actions/db.clj@@ -2,232 +2,241 @@"Application database queries for the actions module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.actions.schema :as actions.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn database-for-action+(mu/defn database-for-action"The Database of the model Card of the Action with `action-id`, or nil."- [action-id]+ [action-id :- ::lib.schema.id/action](t2/select-one :model/Database {:select [:db.*]:from :action:join [[:report_card :card] [:= :card.id :action.model_id][:metabase_database :db] [:= :db.id :card.database_id]]:where [:= :action.id action-id]}))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id [:model/Table :db_id] table-id))-(defn card-query+(mu/defn card-query"The query of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :dataset_query :model/Card :id card-id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn cards+(mu/defn cards"The Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select :model/Card :id [:in card-ids]))-(defn cards-by-id+(mu/defn cards-by-id"A map of Card id to Card for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-pk->fn identity :model/Card :id [:in card-ids]))-(defn card-type+(mu/defn card-type"The type of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :type [:model/Card :type :card_schema] :id card-id))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn dashcard-in-dashboard+(mu/defn dashcard-in-dashboard"The DashboardCard with `dashcard-id` on the Dashboard with `dashboard-id`, or nil."- [dashcard-id dashboard-id]+ [dashcard-id :- ::lib.schema.id/dashcard+ dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/DashboardCard :id dashcard-id :dashboard_id dashboard-id))-(defn dashcard-dashboard-id+(mu/defn dashcard-dashboard-id"The `:dashboard_id` of the DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one-fn :dashboard_id [:model/DashboardCard :dashboard_id] dashcard-id))-(defn dashcard-action-id+(mu/defn dashcard-action-id"The Action id of the DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one-fn :action_id :model/DashboardCard :id dashcard-id))-(defn delete-dashcards-for-action!- "Delete the DashboardCards of the Action with `action-id`."- [action-id]+(mu/defn delete-dashcards-for-action!+ "Delete the DashboardCards of the Action with `action-id`, returning the number deleted."+ [action-id :- ::lib.schema.id/action](t2/delete! :model/DashboardCard :action_id action-id))-(defn insert-action!+(mu/defn insert-action!"Insert the Action `row` and return the inserted instance."- [row]+ [row :- ::actions.schema/action.for-insert](t2/insert-returning-instance! :model/Action row))-(defn update-action!- "Apply `changes` to the Action with `action-id`."- [action-id changes]+(mu/defn update-action!+ "Apply `changes` to the Action with `action-id`, returning the number updated."+ [action-id :- ::lib.schema.id/action+ changes :- ::actions.schema/action.for-update](t2/update! :model/Action action-id changes))-(defn insert-query-action!- "Insert the QueryAction `action`."- [action]+(mu/defn insert-query-action!+ "Insert the QueryAction `action`, returning the number inserted."+ [action :- ::actions.schema/query-action.update](t2/insert! :model/QueryAction action))-(defn insert-http-action!- "Insert the HTTPAction `action`."- [action]+(mu/defn insert-http-action!+ "Insert the HTTPAction `action`, returning the number inserted."+ [action :- ::actions.schema/httpaction.update](t2/insert! :model/HTTPAction action))-(defn insert-implicit-action!- "Insert the ImplicitAction `action`."- [action]+(mu/defn insert-implicit-action!+ "Insert the ImplicitAction `action`, returning the number inserted."+ [action :- ::actions.schema/implicit-action.update](t2/insert! :model/ImplicitAction action))-(defn update-query-action!- "Apply `changes` to the QueryAction with `action-id`."- [action-id changes]+(mu/defn update-query-action!+ "Apply `changes` to the QueryAction with `action-id`, returning the number updated."+ [action-id :- ::lib.schema.id/action+ changes :- ::actions.schema/query-action.update](t2/update! :model/QueryAction action-id changes))-(defn update-http-action!- "Apply `changes` to the HTTPAction with `action-id`."- [action-id changes]+(mu/defn update-http-action!+ "Apply `changes` to the HTTPAction with `action-id`, returning the number updated."+ [action-id :- ::lib.schema.id/action+ changes :- ::actions.schema/httpaction.update](t2/update! :model/HTTPAction action-id changes))-(defn update-implicit-action!- "Apply `changes` to the ImplicitAction with `action-id`."- [action-id changes]+(mu/defn update-implicit-action!+ "Apply `changes` to the ImplicitAction with `action-id`, returning the number updated."+ [action-id :- ::lib.schema.id/action+ changes :- ::actions.schema/implicit-action.update](t2/update! :model/ImplicitAction action-id changes))-(defn delete-query-action!- "Delete the QueryAction of the Action with `action-id`."- [action-id]+(mu/defn delete-query-action!+ "Delete the QueryAction of the Action with `action-id`, returning the number deleted."+ [action-id :- ::lib.schema.id/action](t2/delete! :model/QueryAction :action_id action-id))-(defn delete-http-action!- "Delete the HTTPAction of the Action with `action-id`."- [action-id]+(mu/defn delete-http-action!+ "Delete the HTTPAction of the Action with `action-id`, returning the number deleted."+ [action-id :- ::lib.schema.id/action](t2/delete! :model/HTTPAction :action_id action-id))-(defn delete-implicit-action!- "Delete the ImplicitAction of the Action with `action-id`."- [action-id]+(mu/defn delete-implicit-action!+ "Delete the ImplicitAction of the Action with `action-id`, returning the number deleted."+ [action-id :- ::lib.schema.id/action](t2/delete! :model/ImplicitAction :action_id action-id))-(defn query-actions+(mu/defn query-actions"The QueryActions of the Actions with `action-ids`."- [action-ids]+ [action-ids :- [:sequential ::lib.schema.id/action]](t2/select :model/QueryAction :action_id [:in action-ids]))-(defn query-action+(mu/defn query-action"The QueryAction of the Action with `action-id`, or nil."- [action-id]+ [action-id :- ::lib.schema.id/action](t2/select-one :model/QueryAction :action_id action-id))-(defn http-actions+(mu/defn http-actions"The HTTPActions of the Actions with `action-ids`."- [action-ids]+ [action-ids :- [:sequential ::lib.schema.id/action]](t2/select :model/HTTPAction :action_id [:in action-ids]))-(defn implicit-actions+(mu/defn implicit-actions"The ImplicitActions of the Actions with `action-ids`."- [action-ids]+ [action-ids :- [:sequential ::lib.schema.id/action]](t2/select :model/ImplicitAction :action_id [:in action-ids]))-(defn actions-with-id+(mu/defn actions-with-id"The Actions with `action-id` (usually a single Action, since ids are unique)."- [action-id]+ [action-id :- [:maybe ::lib.schema.id/action]](t2/select :model/Action :id action-id))-(defn actions-with-ids+(mu/defn actions-with-ids"The Actions whose `:id` is in `action-ids`."- [action-ids]+ [action-ids :- [:sequential ::lib.schema.id/action]](t2/select :model/Action :id [:in action-ids]))-(defn unarchived-action-with-id+(mu/defn unarchived-action-with-id"The unarchived Actions with `action-id` (usually a single Action, since ids are unique)."- [action-id]+ [action-id :- [:maybe ::lib.schema.id/action]](t2/select :model/Action :id action-id :archived false))-(defn action-with-entity-id+(mu/defn action-with-entity-id"The Actions with `entity-id` (usually a single Action, since entity ids are unique)."- [entity-id]+ [entity-id :- :string](t2/select :model/Action :entity_id entity-id))-(defn actions-of-type+(mu/defn actions-of-type"The Actions of `action-type`."- [action-type]+ [action-type :- :keyword](t2/select :model/Action :type action-type))-(defn unarchived-actions-for-models+(mu/defn unarchived-actions-for-models"The unarchived Actions whose `:model_id` is in `model-ids`."- [model-ids]+ [model-ids :- [:sequential ms/PositiveInt]](t2/select :model/Action :model_id [:in model-ids] :archived false))-(defn unarchived-non-http-actions-for-model+(mu/defn unarchived-non-http-actions-for-model"The unarchived, non-HTTP Actions of the model Card with `model-id`."- [model-id]+ [model-id :- ms/PositiveInt](t2/select :model/Action :model_id model-id :archived false :type [:not= "http"]))-(defn unarchived-non-http-actions-for-models+(mu/defn unarchived-non-http-actions-for-models"The unarchived, non-HTTP Actions whose `:model_id` is in `model-ids`."- [model-ids]+ [model-ids :- [:set ms/PositiveInt]](t2/select :model/Action :model_id [:in model-ids] :archived false :type [:not= "http"]))-(defn fields-for-parameters+(mu/defn fields-for-parameters"The id, base type, display name, and description of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :base_type :display_name :description] :id [:in field-ids]))-(defn action-database-settings+(mu/defn action-database-settings"The id and Database settings of the Actions with `action-ids`."- [action-ids]+ [action-ids :- [:sequential ::lib.schema.id/action]](t2/query {:select [:action.id :db.settings]:from :action:join [[:report_card :card] [:= :card.id :action.model_id][:metabase_database :db] [:= :db.id :card.database_id]]:where [:in :action.id action-ids]}))-(defn card-scope-columns+(mu/defn card-scope-columns"The query, Collection id, Database id, and display of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :dataset_query :collection_id :database_id :display] card-id))-(defn dashboard-collection-id+(mu/defn dashboard-collection-id"The Collection id of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :collection_id [:model/Dashboard :collection_id] dashboard-id))-(defn destination-database-exists-for-router?+(mu/defn destination-database-exists-for-router?"Whether the Database with `database-id` has routing destinations."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :router_database_id database-id))-(defn writable-table-exists?+(mu/defn writable-table-exists?"Whether the Database with `database-id` has a writable Table."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Table :db_id database-id :is_writable true))-(defn table-with-unknown-writability-exists?+(mu/defn table-with-unknown-writability-exists?"Whether the Database with `database-id` has a Table whose writability is unknown."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Table :db_id database-id :is_writable nil))diff --git a/src/metabase/actions/schema.clj b/src/metabase/actions/schema.cljindex 95225f09068..ce203b98cc9 100644--- a/src/metabase/actions/schema.clj+++ b/src/metabase/actions/schema.clj@@ -177,3 +177,51 @@(mr/def ::action.for-update"Schema for updating an Action (REST API or internally)."(action-schema :update))++(mr/def ::httpaction+ "A HTTPAction as selected from the app DB: every column of `:http_action`."+ [:map {:closed true}+ [:action_id ::lib.schema.id/action]+ [:template ::http-action.template]+ [:response_handle [:maybe :string]]+ [:error_handle [:maybe :string]]])++(mr/def ::httpaction.update+ "What an update (or insert) of a HTTPAction accepts: every column of `:http_action` except `id`, all optional."+ [:map {:closed true}+ [:action_id {:optional true} [:maybe ::lib.schema.id/action]]+ [:template {:optional true} [:maybe ::http-action.template]]+ [:response_handle {:optional true} [:maybe :string]]+ [:error_handle {:optional true} [:maybe :string]]])++(mr/def ::implicit-action.row+ "A ImplicitAction as selected from the app DB: every column of `:implicit_action`."+ [:map {:closed true}+ [:action_id ::lib.schema.id/action]+ [:kind [:or :keyword :string]]])++(mr/def ::implicit-action.update+ "What an update (or insert) of a ImplicitAction accepts: every column of `:implicit_action` except `id`, all optional."+ [:map {:closed true}+ [:action_id {:optional true} [:maybe ::lib.schema.id/action]]+ [:kind {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::query-action.dataset-query+ "The `:dataset_query` column of a QueryAction, decoded."+ :map)++(mr/def ::query-action.row+ "A QueryAction as selected from the app DB: every column of `:query_action`."+ [:map {:closed true}+ [:action_id ::lib.schema.id/action]+ [:database_id ::lib.schema.id/database]+ [:dataset_query ::query-action.dataset-query]+ [:legacy_query [:maybe :string]]])++(mr/def ::query-action.update+ "What an update (or insert) of a QueryAction accepts: every column of `:query_action` except `id`, all optional."+ [:map {:closed true}+ [:action_id {:optional true} [:maybe ::lib.schema.id/action]]+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:dataset_query {:optional true} [:maybe ::query-action.dataset-query]]+ [:legacy_query {:optional true} [:maybe :string]]])diff --git a/src/metabase/actions_rest/db.clj b/src/metabase/actions_rest/db.cljindex ebb70b5d018..c2726b03b72 100644--- a/src/metabase/actions_rest/db.clj+++ b/src/metabase/actions_rest/db.clj@@ -3,9 +3,11 @@additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require[metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn unarchived-models-visible-to-user+(mu/defn unarchived-models-visible-to-user"The unarchived model Cards in Collections the current user can read."[](t2/select :model/Card {:where [:and@@ -13,22 +15,24 @@[:= :archived false](collection/visible-collection-filter-clause)]}))-(defn public-actions+(mu/defn public-actions"The name, id, public uuid, and model id of the unarchived Actions that are publicly shared."[](t2/select [:model/Action :name :id :public_uuid :model_id], :public_uuid [:not= nil], :archived false))-(defn delete-action!+(mu/defn delete-action!"Delete the Action with `action-id`."- [action-id]+ [action-id :- ::lib.schema.id/action](t2/delete! :model/Action :id action-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn set-action-public-uuid!+(mu/defn set-action-public-uuid!"Set the public uuid of the Action with `action-id` and the User who made it public."- [action-id public-uuid made-public-by-id]+ [action-id :- ::lib.schema.id/action+ public-uuid :- [:maybe :string]+ made-public-by-id :- [:maybe ::lib.schema.id/user]](t2/update! :model/Action action-id {:public_uuid public-uuid, :made_public_by_id made-public-by-id}))diff --git a/src/metabase/activity_feed/db.clj b/src/metabase/activity_feed/db.cljindex e93735c5550..478f412c9b8 100644--- a/src/metabase/activity_feed/db.clj+++ b/src/metabase/activity_feed/db.clj@@ -2,13 +2,17 @@"Application database queries for the activity feed module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.activity-feed.schema :as activity-feed.schema][metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn recent-cards+(mu/defn recent-cards"The recently viewed Cards with `ids`, with their Collection and Dashboard names."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Card:id :name :collection_id :description :display:dataset_query :type :archived :card_schema@@ -18,9 +22,9 @@:left-join [:collection [:= :collection.id :report_card.collection_id][:report_dashboard :dashboard] [:= :dashboard.id :report_card.dashboard_id]]}))-(defn recent-dashboards+(mu/defn recent-dashboards"The recently viewed Dashboards with `ids`, with their Collection names."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Dashboard:id :name :collection_id :description:archived@@ -28,9 +32,9 @@{:where [:in :report_dashboard.id ids]:left-join [:collection [:= :collection.id :report_dashboard.collection_id]]}))-(defn recent-tables+(mu/defn recent-tables"The recently viewed Tables with `ids`, with their Database names and sync status."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Table:id :name :db_id :active:display_name [:metabase_database.initial_sync_status :initial-sync-status]@@ -39,10 +43,10 @@{:where [:in :metabase_table.id ids]:left-join [:metabase_database [:= :metabase_database.id :metabase_table.db_id]]}))-(defn recent-dashboard-and-table-views+(mu/defn recent-dashboard-and-table-views"Up to `limit` most recently viewed unarchived, active Dashboards and Tables with their view counts and lastviewer."- [limit]+ [limit :- ms/PositiveInt](t2/select [:model/RecentViews[[:min :recent_views.user_id] :user_id]:model@@ -66,9 +70,9 @@[:= :model "table"][:= :t.id :model_id]]]}))-(defn recent-card-runs+(mu/defn recent-card-runs"Up to `limit` most recently run question Cards with their run counts and last runner."- [limit]+ [limit :- ms/PositiveInt](t2/select [:model/QueryExecution[:%min.executor_id :user_id][:query_execution.card_id :model_id]@@ -80,70 +84,75 @@:order-by [[:max_ts :desc]]:limit limit}))-(defn card-exists?+(mu/defn card-exists?"Whether a Card with `id` exists."- [id]+ [id :- ::lib.schema.id/card](t2/exists? :model/Card :id id))-(defn dashboard-exists?+(mu/defn dashboard-exists?"Whether a Dashboard with `id` exists."- [id]+ [id :- ::lib.schema.id/dashboard](t2/exists? :model/Dashboard :id id))-(defn table-exists?+(mu/defn table-exists?"Whether a Table with `id` exists."- [id]+ [id :- ::lib.schema.id/table](t2/exists? :model/Table :id id))-(defn collection-exists?+(mu/defn collection-exists?"Whether a Collection with `id` exists."- [id]+ [id :- ::lib.schema.id/collection](t2/exists? :model/Collection :id id))-(defn document-exists?+(mu/defn document-exists?"Whether a Document with `id` exists."- [id]+ [id :- ms/PositiveInt](t2/exists? :model/Document :id id))-(defn card+(mu/defn card"The Card with `id`, or nil."- [id]+ [id :- ::lib.schema.id/card](t2/select-one :model/Card :id id))-(defn table+(mu/defn table"The Table with `id`, or nil."- [id]+ [id :- ::lib.schema.id/table](t2/select-one :model/Table :id id))-(defn collection+(mu/defn collection"The Collection with `id`, or nil."- [id]+ [id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id id))-(defn document+(mu/defn document"The Document with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Document :id id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn card-document-id+(mu/defn card-document-id"The Document id of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :document_id :model/Card :id card-id))-(defn recent-views-for-user-context+(mu/defn recent-views-for-user-context"The RecentViews of the User with `user-id` in `context`, newest first."- [user-id context]+ [user-id :- ::lib.schema.id/user+ context :- [:or :keyword :string]](t2/select :model/RecentViews :user_id user-id :context context {:order-by [[:timestamp :desc]]}))-(defn recent-view-ids-to-prune+(mu/defn recent-view-ids-to-prune"The ids of the RecentViews of the User with `user-id` for `db-model` in `context` beyond the newest `keep` ofthem, restricted to the Cards of `card-type` when non-nil."- [db-model user-id context card-type keep]+ [db-model :- :string+ user-id :- ::lib.schema.id/user+ context :- :string+ card-type :- [:maybe :string]+ keep :- ms/IntGreaterThanOrEqualToZero](t2/select-fn-set :id:model/RecentViews{:select [:rv.id]@@ -163,19 +172,20 @@:limit 100000:offset keep}))-(defn insert-recent-views!+(mu/defn insert-recent-views!"Insert the RecentViews `rows`."- [rows]+ [rows :- [:sequential ::activity-feed.schema/recent-views.update]](t2/insert! :model/RecentViews rows))-(defn delete-recent-views!+(mu/defn delete-recent-views!"Delete the RecentViews with `ids`."- [ids]+ [ids :- [:set ms/PositiveInt]](t2/delete! :model/RecentViews :id [:in ids]))-(defn most-recently-viewed-dashboard-id+(mu/defn most-recently-viewed-dashboard-id"The id of the unarchived Dashboard the User with `user-id` viewed most recently after `since`, or nil."- [user-id since]+ [user-id :- ::lib.schema.id/user+ since :- ms/TemporalInstant](t2/select-one-fn:model_id:model/RecentViews@@ -188,9 +198,9 @@:left-join [[:report_dashboard :d][:= :recent_views.model_id :d.id]]}))-(defn cards-for-recent-views+(mu/defn cards-for-recent-views"The Cards with `card-ids` with their Dashboard, Collection, and moderation status."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select :model/Card{:select [:card.name:card.description@@ -224,9 +234,9 @@[:report_dashboard :dashboard][:= :dashboard.id :card.dashboard_id]]}))-(defn dashboards-for-recent-views+(mu/defn dashboards-for-recent-views"The Dashboards with `dashboard-ids` with their Collection and moderation status."- [dashboard-ids]+ [dashboard-ids :- [:sequential ::lib.schema.id/dashboard]](t2/select :model/Dashboard{:select [:dash.id:dash.name@@ -249,9 +259,9 @@[:= :c.id :dash.collection_id][:= :c.archived false]]]}))-(defn unarchived-collections-with-details+(mu/defn unarchived-collections-with-details"The unarchived Collections with `collection-ids`, with their location, type, and authority level."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select :model/Collection{:select [:id :name :description :authority_level:archived :location :type]@@ -259,9 +269,9 @@[:in :id collection-ids][:= :archived false]]}))-(defn visible-tables-for-recent-views+(mu/defn visible-tables-for-recent-views"The non-hidden Tables with `table-ids` with their Database name and sync status."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select :model/Table{:select [:t.id :t.name :t.description:t.display_name :t.active :t.visibility_type :t.schema@@ -277,11 +287,15 @@:left-join [[:metabase_database :db][:= :db.id :t.db_id]]}))-(defn recent-views-with-card-type+(mu/defn recent-views-with-card-type"The RecentViews of the User with `user-id` in `contexts`, newest first, with the type of the viewed Card. Narrowedto `db-models` and to the Cards of `card-types` when given; excludes trashed and namespaced Collections, explorationDocuments, and, when `selections?`, the instance analytics Collection."- [user-id contexts db-models card-types selections?]+ [user-id :- ::lib.schema.id/user+ contexts :- [:sequential :string]+ db-models :- [:maybe [:sequential :string]]+ card-types :- [:sequential :string]+ selections? :- :boolean](t2/select :model/RecentViews{:select [:rv.* [:rc.type :card_type]]:from [[:recent_views :rv]]@@ -325,9 +339,9 @@[:= :doc.id :rv.model_id]]]:order-by [[:rv.timestamp :desc]]}))-(defn documents-for-recent-views+(mu/defn documents-for-recent-views"The Documents with `document-ids` with their Collection."- [document-ids]+ [document-ids :- [:sequential ms/PositiveInt]](t2/select :model/Document{:select [:d.id:d.namediff --git a/src/metabase/activity_feed/schema.clj b/src/metabase/activity_feed/schema.cljnew file mode 100644index 00000000000..56fe2425183--- /dev/null+++ b/src/metabase/activity_feed/schema.clj@@ -0,0 +1,25 @@+(ns metabase.activity-feed.schema+ "Malli schemas for the activity-feed module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::recent-views+ "A RecentViews as selected from the app DB: every column of `:recent_views`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:model [:or :keyword :string]]+ [:model_id [:maybe :int]]+ [:timestamp ms/TemporalInstant]+ [:context [:or :keyword :string]]])++(mr/def ::recent-views.update+ "What an update (or insert) of a RecentViews accepts: every column of `:recent_views` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:context {:optional true} [:maybe [:or :keyword :string]]]])diff --git a/src/metabase/agent_api/db.clj b/src/metabase/agent_api/db.cljindex a574f482673..da7bc14d7ab 100644--- a/src/metabase/agent_api/db.clj+++ b/src/metabase/agent_api/db.clj@@ -2,71 +2,78 @@"Application database queries for the agent API module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [malli.util :as mut]+ [metabase.dashboards.schema :as dashboards.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn collection-breadcrumb-columns+(mu/defn collection-breadcrumb-columns"The id, name, location, owner, namespace, and archival of the Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one [:model/Collection :id :name :location :personal_owner_id :namespace :archived_directly]collection-id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn dashboard-tab-names+(mu/defn dashboard-tab-names"The id and name of the DashboardTabs of the Dashboard with `dashboard-id`, in position order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select [:model/DashboardTab :id :name] :dashboard_id dashboard-id {:order-by [[:position :asc] [:id :asc]]}))-(defn dashboard-tab-ids+(mu/defn dashboard-tab-ids"The ids of the DashboardTabs of the Dashboard with `dashboard-id`, in position order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-pks-vec :model/DashboardTab :dashboard_id dashboard-id {:order-by [[:position :asc] [:id :asc]]}))-(defn insert-dashboard!+(mu/defn insert-dashboard!"Insert the Dashboard `row` and return the inserted instance."- [row]+ [row :- ::dashboards.schema/dashboard.update](t2/insert-returning-instance! :model/Dashboard row))-(defn insert-dashcard!+(mu/defn insert-dashcard!"Insert the DashboardCard `row` and return the inserted instance."- [row]+ [row :- ::dashboards.schema/dashboard-card.update](t2/insert-returning-instance! :model/DashboardCard row))-(defn dashcard-ids-in-layout-order+(mu/defn dashcard-ids-in-layout-order"The ids of the DashboardCards of the Dashboard with `dashboard-id`, in row then column order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :id :model/DashboardCard :dashboard_id dashboard-id {:order-by [[:row :asc] [:col :asc]]}))-(defn dashcards+(mu/defn dashcards"The DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardCard :dashboard_id dashboard-id))-(defn dashcard-in-dashboard+(mu/defn dashcard-in-dashboard"The DashboardCard with `dashcard-id` on the Dashboard with `dashboard-id`, or nil."- [dashcard-id dashboard-id]+ [dashcard-id :- ::lib.schema.id/dashcard+ dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/DashboardCard :id dashcard-id :dashboard_id dashboard-id))-(defn update-dashcard!- "Apply `changes` to the DashboardCard with `dashcard-id`."- [dashcard-id changes]+(mu/defn update-dashcard!+ "Apply `changes` to the DashboardCard with `dashcard-id`, returning the number updated."+ [dashcard-id :- ::lib.schema.id/dashcard+ changes :- (mut/select-keys ::dashboards.schema/dashboard-card.update [:visualization_settings :row :col])](t2/update! :model/DashboardCard dashcard-id changes))-(defn update-dashboard!- "Apply `changes` to the Dashboard with `dashboard-id`."- [dashboard-id changes]+(mu/defn update-dashboard!+ "Apply `changes` to the Dashboard with `dashboard-id`, returning the number updated."+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- (mut/select-keys ::dashboards.schema/dashboard.update [:name :description :collection_id :archived :archived_directly])](t2/update! :model/Dashboard dashboard-id changes))-(defn active-user-by-email+(mu/defn active-user-by-email"The active User with `email`, compared case-insensitively, or nil."- [email]+ [email :- :string](t2/select-one :model/User :%lower.email (u/lower-case-en email) :is_active true))diff --git a/src/metabase/agent_api/schema.clj b/src/metabase/agent_api/schema.cljnew file mode 100644index 00000000000..9fea8cc95cb--- /dev/null+++ b/src/metabase/agent_api/schema.clj@@ -0,0 +1,33 @@+(ns metabase.agent-api.schema+ "Malli schemas for the agent-api module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::agent-api-call-log+ "A AgentApiCallLog as selected from the app DB: every column of `:agent_api_call_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:client_name [:maybe :string]]+ [:operation [:maybe :string]]+ [:status [:maybe [:or :keyword :string]]]+ [:duration_ms [:maybe :int]]+ [:ip_address [:maybe :string]]+ [:error_message [:maybe :string]]])++(mr/def ::agent-api-call-log.update+ "What an update (or insert) of a AgentApiCallLog accepts: every column of `:agent_api_call_log` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:client_name {:optional true} [:maybe :string]]+ [:operation {:optional true} [:maybe :string]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:duration_ms {:optional true} [:maybe :int]]+ [:ip_address {:optional true} [:maybe :string]]+ [:error_message {:optional true} [:maybe :string]]])diff --git a/src/metabase/analytics/db.clj b/src/metabase/analytics/db.cljindex 63815914b27..31ca075ecdc 100644--- a/src/metabase/analytics/db.clj+++ b/src/metabase/analytics/db.clj@@ -4,72 +4,75 @@(:require[clojure.string :as str][metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn first-user-date-joined+(mu/defn first-user-date-joined"The earliest join date among all Users, or nil."[](t2/select-one-fn :min [:model/User [:%min.date_joined :min]]))-(defn sample-database-exists?+(mu/defn sample-database-exists?"Whether a sample Database exists."[](t2/exists? :model/Database, :is_sample true))-(defn sample-database-id+(mu/defn sample-database-id"The id of the sample Database, or nil."[](t2/select-one-pk :model/Database :is_sample true))-(defn personal-user-stats-columns+(mu/defn personal-user-stats-columns"The active, superuser, last login, and SSO source of every personal User."[](t2/select [:model/User :is_active :is_superuser :last_login :sso_source] :type :personal))-(defn document-archived-flags+(mu/defn document-archived-flags"The archived flag of every Document."[](t2/select [:model/Document :archived]))-(defn collection-by-type+(mu/defn collection-by-type"The id and location of a Collection of `collection-type`, or nil."- [collection-type]+ [collection-type :- :string](t2/select-one [:model/Collection :id :location] :type collection-type))-(defn descendant-collection-ids+(mu/defn descendant-collection-ids"The ids of the Collections whose location starts with `location-prefix`, or nil."- [location-prefix]+ [location-prefix :- :string](t2/select-pks-set :model/Collection :location [:like (str location-prefix "%")]))-(defn published-table-count-in-collections+(mu/defn published-table-count-in-collections"The number of published Tables in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/count :model/Table {:where [:and[:= :is_published true][:in :collection_id collection-ids]]}))-(defn unarchived-metric-count-in-collections+(mu/defn unarchived-metric-count-in-collections"The number of unarchived metric Cards in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/count :model/Card {:where [:and[:= :type "metric"][:= :archived false][:in :collection_id collection-ids]]}))-(defn permissions-group-count+(mu/defn permissions-group-count"The number of PermissionsGroups."[](t2/count :model/PermissionsGroup))-(defn dashboard-stats-columns+(mu/defn dashboard-stats-columns"The creator, public uuid, parameters, and embedding columns of the non-internal Dashboards."[](t2/select [:model/Dashboard :creator_id :public_uuid :parameters :enable_embedding :embedding_params]{:where (mi/exclude-internal-content-hsql :model/Dashboard)}))-(defn dashcards-of-dashboards+(mu/defn dashcards-of-dashboards"The DashboardCards of the non-internal Dashboards."[](t2/query {:select :dc.*@@ -84,33 +87,36 @@:where [(if alerts? :not= :=) :pulse.alert_condition nil]}left-join? (assoc :left-join [:pulse [:= :pulse.id :pulse_id]]))))-(defn pulse-channel-frequencies-by-column+(mu/defn pulse-channel-frequencies-by-column"The distinct `column` values (as `:k`) and their `:count` among the PulseChannels of alerts when `alerts?`, or ofpulses otherwise."- [column alerts?]+ [column :- :keyword+ alerts? :- :boolean](notification-frequencies-by-column* :model/PulseChannel column alerts? true))-(defn pulse-frequencies-by-column+(mu/defn pulse-frequencies-by-column"The distinct `column` values (as `:k`) and their `:count` among the Pulses of alerts when `alerts?`, or of pulsesotherwise."- [column alerts?]+ [column :- :keyword+ alerts? :- :boolean](notification-frequencies-by-column* :model/Pulse column alerts? false))-(defn pulse-card-frequencies-by-column+(mu/defn pulse-card-frequencies-by-column"The distinct `column` values (as `:k`) and their `:count` among the PulseCards of alerts when `alerts?`, or ofpulses otherwise."- [column alerts?]+ [column :- :keyword+ alerts? :- :boolean](notification-frequencies-by-column* :model/PulseCard column alerts? true))-(defn pulse-count+(mu/defn pulse-count"The number of Pulses that are not alerts."[](t2/count :model/Pulse :alert_condition nil))-(defn notification-xls-or-csv-card-count+(mu/defn notification-xls-or-csv-card-count"The `:count` of Notifications with at least one PulseCard with `:include_xls` or `:include_csv`, of alerts when`alerts?`, or of pulses otherwise."- [alerts?]+ [alerts? :- :boolean](t2/query-one {:select [[[::h2x/distinct-count :pulse.id] :count]]:from [:pulse]:left-join [:pulse_card [:= :pulse.id :pulse_card.pulse_id]]@@ -120,38 +126,38 @@[:= :pulse_card.include_xls true]][(if alerts? :not= :=) :alert_condition nil]]}))-(defn alert-count+(mu/defn alert-count"The number of Pulses that are alerts."[](t2/count :model/Pulse :alert_condition [:not= nil]))-(defn first-time-only-alert-count+(mu/defn first-time-only-alert-count"The number of alert Pulses that fire only once."[](t2/count :model/Pulse :alert_condition [:not= nil], :alert_first_only true))-(defn above-goal-alert-count+(mu/defn above-goal-alert-count"The number of alert Pulses that fire above their goal."[](t2/count :model/Pulse :alert_condition [:not= nil], :alert_above_goal true))-(defn collection-count+(mu/defn collection-count"The number of non-internal Collections."[](t2/count :model/Collection {:where (mi/exclude-internal-content-hsql :model/Collection)}))-(defn card-collection-ids+(mu/defn card-collection-ids"The Collection id and schema of the non-internal Cards."[](t2/select [:model/Card :collection_id :card_schema] {:where [:and (mi/exclude-internal-content-hsql :model/Card)]}))-(defn database-stats-columns+(mu/defn database-stats-columns"The sync, engine, and DBMS version of the non-internal Databases."[](t2/select [:model/Database :is_full_sync :engine :dbms_version]{:where (mi/exclude-internal-content-hsql :model/Database)}))-(defn table-database-and-schema+(mu/defn table-database-and-schema"The Database id and schema of the Tables of the non-internal Databases."[](t2/query {:select [:t.db_id :t.schema]@@ -159,7 +165,7 @@:join [[(t2/table-name :model/Database) :d] [:= :d.id :t.db_id]]:where (mi/exclude-internal-content-hsql :model/Database :table-alias :d)}))-(defn field-table-ids+(mu/defn field-table-ids"The Table id of the Fields of the non-internal Databases."[](t2/query {:select [:f.table_id]@@ -168,12 +174,12 @@[(t2/table-name :model/Database) :d] [:= :d.id :t.db_id]]:where (mi/exclude-internal-content-hsql :model/Database :table-alias :d)}))-(defn segment-count+(mu/defn segment-count"The number of Segments."[](t2/count :model/Segment))-(defn unarchived-metric-card-count+(mu/defn unarchived-metric-card-count"The number of unarchived metric Cards."[](t2/count :model/Card :type :metric :archived false))@@ -230,149 +236,151 @@")""SELECT q1.*, q2.* FROM query_stats_1 q1, query_stats_2 q2;"])))-(defn execution-metrics+(mu/defn execution-metrics"The execution statistics over the last 30 days of QueryExecutions."[](first (t2/query (execution-metrics-sql))))-(defn query-cache-stats+(mu/defn query-cache-stats"The average result `:length` and `:count` of the QueryCache entries."[](t2/select-one [:model/QueryCache [[:avg [:length :results]] :length] [:%count.* :count]]))-(defn user-count-joined-before+(mu/defn user-count-joined-before"The number (up to `limit`) of non-internal Users who joined at or before `joined-before`."- [joined-before limit]+ [joined-before :- ms/TemporalInstant+ limit :- ms/PositiveInt](t2/count :model/User {:where [:and[:<= :date_joined joined-before](mi/exclude-internal-content-hsql :model/User)]:limit limit}))-(defn query-execution-ids-excluding-database+(mu/defn query-execution-ids-excluding-database"Up to `limit` ids of QueryExecutions not run against the Database with `database-id`."- [database-id limit]+ [database-id :- [:maybe ::lib.schema.id/database]+ limit :- ms/PositiveInt](t2/select-fn-set :id :model/QueryExecution{:where [:or[:not= :database_id database-id][:= :database_id nil]]:limit limit}))-(defn transform-count+(mu/defn transform-count"The number of Transforms."[](t2/count :model/Transform))-(defn transform-run-count-since+(mu/defn transform-run-count-since"The number of TransformRuns started at or after `since`."- [since]+ [since :- ms/TemporalInstant](t2/count :model/TransformRun :start_time [:>= since]))-(defn unarchived-model-count+(mu/defn unarchived-model-count"The number of unarchived model Cards."[](t2/count :model/Card :type :model :archived false))-(defn new-embedded-dashboard-count-since+(mu/defn new-embedded-dashboard-count-since"The number of unarchived Dashboards with embedding enabled created at or after `since`."- [since]+ [since :- ms/TemporalInstant](t2/count :model/Dashboard :enable_embedding true :archived false :created_at [:>= since]))-(defn new-active-user-count-since+(mu/defn new-active-user-count-since"The number of active Users who joined at or after `since`."- [since]+ [since :- ms/TemporalInstant](t2/count :model/User :is_active true :date_joined [:>= since]))-(defn unarchived-pivot-table-count+(mu/defn unarchived-pivot-table-count"The number of unarchived pivot table Cards."[](t2/count :model/Card :display :pivot :archived false))-(defn query-execution-count-since+(mu/defn query-execution-count-since"The number of QueryExecutions started at or after `since`."- [since]+ [since :- ms/TemporalInstant](t2/count :model/QueryExecution :started_at [:>= since]))-(defn new-scim-user-count-since+(mu/defn new-scim-user-count-since"The number of active SCIM-provisioned Users who joined at or after `since`."- [since]+ [since :- ms/TemporalInstant](t2/count :model/User :sso_source :scim :is_active true :date_joined [:>= since]))-(defn database-engines-among+(mu/defn database-engines-among"The set of engines of the Databases whose engine is one of `engine-names`."- [engine-names]+ [engine-names :- [:sequential :string]](t2/select-fn-set :engine :model/Database {:where [:in :engine engine-names]}))-(defn embedded-dashboard-exists?+(mu/defn embedded-dashboard-exists?"Whether a Dashboard with embedding enabled exists."[](t2/exists? :model/Dashboard :enable_embedding true))-(defn embedded-card-exists?+(mu/defn embedded-card-exists?"Whether a Card with embedding enabled exists."[](t2/exists? :model/Card :enable_embedding true))-(defn public-dashboard-exists?+(mu/defn public-dashboard-exists?"Whether a publicly shared Dashboard exists."[](t2/exists? :model/Dashboard :public_uuid [:not= nil]))-(defn public-card-exists?+(mu/defn public-card-exists?"Whether a publicly shared Card exists."[](t2/exists? :model/Card :public_uuid [:not= nil]))-(defn custom-viz-plugin-exists?+(mu/defn custom-viz-plugin-exists?"Whether a CustomVizPlugin exists."[](t2/exists? :model/CustomVizPlugin))-(defn uploads-database-exists?+(mu/defn uploads-database-exists?"Whether a Database with uploads enabled exists."[](t2/exists? :model/Database :uploads_enabled true))-(defn official-collection-exists?+(mu/defn official-collection-exists?"Whether an official Collection exists."[](t2/exists? :model/Collection :authority_level "official"))-(defn cache-config-exists?+(mu/defn cache-config-exists?"Whether any CacheConfig exists."[](t2/exists? :model/CacheConfig))-(defn preemptive-cache-config-exists?+(mu/defn preemptive-cache-config-exists?"Whether a CacheConfig that refreshes automatically exists."[](t2/exists? :model/CacheConfig :refresh_automatically true))-(defn database-router-exists?+(mu/defn database-router-exists?"Whether a DatabaseRouter exists."[](t2/exists? :model/DatabaseRouter))-(defn moderation-review-exists?+(mu/defn moderation-review-exists?"Whether a ModerationReview exists."[](t2/exists? :model/ModerationReview))-(defn filtered-pulse-exists?+(mu/defn filtered-pulse-exists?"Whether a Pulse with parameters exists."[](t2/exists? :model/Pulse {:where [:not= :parameters "[]"]}))-(defn upload-table-exists?+(mu/defn upload-table-exists?"Whether an uploaded Table exists."[](t2/exists? :model/Table :is_upload true))-(defn snippet-collection-exists?+(mu/defn snippet-collection-exists?"Whether a snippet Collection exists."[](t2/exists? :model/Collection :namespace "snippets"))-(defn starburst-database-details+(mu/defn starburst-database-details"The connection details of the Starburst Databases."[](t2/select-fn-set :details :model/Database :engine "starburst"))diff --git a/src/metabase/api/db.clj b/src/metabase/api/db.cljindex 63d8252b712..89deb15be12 100644--- a/src/metabase/api/db.clj+++ b/src/metabase/api/db.clj@@ -2,16 +2,19 @@"Application database queries for the API module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn entity-exists?+(mu/defn entity-exists?"Whether a row of `entity` matching the key-value `conditions` exists."- [entity & conditions]+ [entity :- :keyword & conditions :- [:* :some]](apply t2/exists? entity conditions))-(defn entity-by-id+(mu/defn entity-by-id"The `entity` row with `id` also matching the key-value `conditions`, or nil."- [entity id & conditions]+ [entity :- :keyword id :- [:or ms/PositiveInt :string] & conditions :- [:* :some]](apply t2/select-one entity :id id conditions))(defn- shift-positions-after!@@ -32,74 +35,102 @@:collection_position [:between lower upper]}{:collection_position [plus-or-minus :collection_position 1]}))-(defn shift-card-positions-after!+(mu/defn shift-card-positions-after!"Add or subtract (`plus-or-minus`) one from the collection position of the Cards in the Collection with`collection-id` positioned after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-after! 'Card collection-id position plus-or-minus))-(defn shift-dashboard-positions-after!+(mu/defn shift-dashboard-positions-after!"Add or subtract (`plus-or-minus`) one from the collection position of the Dashboards in the Collection with`collection-id` positioned after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-after! 'Dashboard collection-id position plus-or-minus))-(defn shift-pulse-positions-after!+(mu/defn shift-pulse-positions-after!"Add or subtract (`plus-or-minus`) one from the collection position of the Pulses in the Collection with`collection-id` positioned after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-after! 'Pulse collection-id position plus-or-minus))-(defn shift-document-positions-after!+(mu/defn shift-document-positions-after!"Add or subtract (`plus-or-minus`) one from the collection position of the Documents in the Collection with`collection-id` positioned after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-after! 'Document collection-id position plus-or-minus))-(defn shift-card-positions-from!+(mu/defn shift-card-positions-from!"Add or subtract (`plus-or-minus`) one from the collection position of the Cards in the Collection with`collection-id` positioned at or after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-from! 'Card collection-id position plus-or-minus))-(defn shift-dashboard-positions-from!+(mu/defn shift-dashboard-positions-from!"Add or subtract (`plus-or-minus`) one from the collection position of the Dashboards in the Collection with`collection-id` positioned at or after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-from! 'Dashboard collection-id position plus-or-minus))-(defn shift-pulse-positions-from!+(mu/defn shift-pulse-positions-from!"Add or subtract (`plus-or-minus`) one from the collection position of the Pulses in the Collection with`collection-id` positioned at or after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-from! 'Pulse collection-id position plus-or-minus))-(defn shift-document-positions-from!+(mu/defn shift-document-positions-from!"Add or subtract (`plus-or-minus`) one from the collection position of the Documents in the Collection with`collection-id` positioned at or after `position`."- [collection-id position plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ position :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-from! 'Document collection-id position plus-or-minus))-(defn shift-card-positions-between!+(mu/defn shift-card-positions-between!"Add or subtract (`plus-or-minus`) one from the collection position of the Cards in the Collection with`collection-id` positioned between `lower` and `upper` inclusive."- [collection-id lower upper plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ lower :- ms/PositiveInt+ upper :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-between! 'Card collection-id lower upper plus-or-minus))-(defn shift-dashboard-positions-between!+(mu/defn shift-dashboard-positions-between!"Add or subtract (`plus-or-minus`) one from the collection position of the Dashboards in the Collection with`collection-id` positioned between `lower` and `upper` inclusive."- [collection-id lower upper plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ lower :- ms/PositiveInt+ upper :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-between! 'Dashboard collection-id lower upper plus-or-minus))-(defn shift-pulse-positions-between!+(mu/defn shift-pulse-positions-between!"Add or subtract (`plus-or-minus`) one from the collection position of the Pulses in the Collection with`collection-id` positioned between `lower` and `upper` inclusive."- [collection-id lower upper plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ lower :- ms/PositiveInt+ upper :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-between! 'Pulse collection-id lower upper plus-or-minus))-(defn shift-document-positions-between!+(mu/defn shift-document-positions-between!"Add or subtract (`plus-or-minus`) one from the collection position of the Documents in the Collection with`collection-id` positioned between `lower` and `upper` inclusive."- [collection-id lower upper plus-or-minus]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ lower :- ms/PositiveInt+ upper :- ms/PositiveInt+ plus-or-minus :- [:enum :+ :-]](shift-positions-between! 'Document collection-id lower upper plus-or-minus))diff --git a/src/metabase/api_keys/db.clj b/src/metabase/api_keys/db.cljindex d1fa611140f..b651de0c3ea 100644--- a/src/metabase/api_keys/db.clj+++ b/src/metabase/api_keys/db.clj@@ -2,41 +2,53 @@"Application database queries for the API keys module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.api-keys.schema :as api-keys.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.users.schema :as users.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn unscoped-api-key-count+(mu/defn unscoped-api-key-count"The number of ApiKeys without a scope."[](t2/count :model/ApiKey :scope nil))-(defn unscoped-api-keys+(mu/defn unscoped-api-keys"The ApiKeys without a scope."[](t2/select :model/ApiKey :scope nil))-(defn api-key+(mu/defn api-key"The ApiKey with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/ApiKey id))-(defn save-api-key!- "Save the changes made to the ApiKey instance `api-key`."- [api-key]+(def ^:private ApiKeyWithGroupId+ "An ApiKey instance possibly carrying the `::api-keys/group-id` the before-update hook consumes."+ (mut/merge ::api-keys.schema/api-key+ [:map+ [:metabase.api-keys.core/group-id {:optional true} [:maybe ms/PositiveInt]]]))++(mu/defn save-api-key!+ "Save the changes made to the ApiKey instance `api-key` and return it."+ [api-key :- ApiKeyWithGroupId](t2/save! api-key))-(defn api-key-exists?+(mu/defn api-key-exists?"Whether an ApiKey with `id` exists."- [id]+ [id :- ms/PositiveInt](t2/exists? :model/ApiKey id))-(defn delete-api-key!+(mu/defn delete-api-key!"Delete the ApiKey with `id`."- [id]+ [id :- ms/PositiveInt](t2/delete! :model/ApiKey id))-(defn api-key-groups+(mu/defn api-key-groups"The group name, group id, and api key id of the PermissionsGroups of the ApiKeys with `api-key-ids`."- [api-key-ids]+ [api-key-ids :- [:sequential ms/PositiveInt]](t2/query {:select [[:pg.name :group-name][:pg.id :group-id][:api_key.id :api-key-id]]@@ -45,42 +57,44 @@:api_key [:= :api_key.user_id :pgm.user_id]]:where [:in :api_key.id api-key-ids]}))-(defn rename-api-key-user!+(mu/defn rename-api-key-user!"Set the first name (and clear the last name) of the api-key User with `user-id`."- [user-id first-name]+ [user-id :- ::lib.schema.id/user+ first-name :- :string](t2/update! :model/User :id user-id, :type :api-key, {:first_name first-name, :last_name ""}))-(defn user-type+(mu/defn user-type"The `:type` of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :type :model/User :id user-id))-(defn deactivate-api-key-user!- "Deactivate the api-key User with `user-id`."- [user-id]+(mu/defn deactivate-api-key-user!+ "Deactivate the api-key User with `user-id` (nil for keys without a user, e.g. SCIM keys, which updates nothing)."+ [user-id :- [:maybe ::lib.schema.id/user]](t2/update! :model/User user-id, :type :api-key, {:is_active false}))-(defn api-key-prefix-exists?+(mu/defn api-key-prefix-exists?"Whether an ApiKey with `prefix` exists."- [prefix]+ [prefix :- :string](t2/exists? :model/ApiKey :key_prefix prefix))-(defn api-key-name-exists?+(mu/defn api-key-name-exists?"Whether an ApiKey named `key-name` exists."- [key-name]+ [key-name :- :string](t2/exists? :model/ApiKey :name key-name))-(defn insert-user!+(mu/defn insert-user!"Insert the User `row` and return its id."- [row]+ [row :- ::users.schema/user.update](t2/insert-returning-pk! :model/User row))-(defn insert-api-key!+(mu/defn insert-api-key!"Insert the ApiKey `row` and return the inserted instance."- [row]+ [row :- ::api-keys.schema/api-key.create](t2/insert-returning-instance! :model/ApiKey row))-(defn update-api-key!+(mu/defn update-api-key!"Apply `changes` to the ApiKey with `id`."- [id changes]+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::api-keys.schema/api-key.update [:key :key_prefix :updated_by_id])](t2/update! :model/ApiKey :id id changes))diff --git a/src/metabase/api_keys/schema.clj b/src/metabase/api_keys/schema.cljindex 908ca9bd345..0d37de6ca86 100644--- a/src/metabase/api_keys/schema.clj+++ b/src/metabase/api_keys/schema.clj@@ -3,6 +3,9 @@[clojure.math :as math][clojure.string :as str][malli.core :as mc]+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.users.schema :as users.schema][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms][metabase.util.secret]))@@ -102,7 +105,9 @@[:name [:ref ::name]][:updated_by_id pos-int?][:scope {:optional true} [:maybe [:ref ::scope]]]- [:single_collection_id {:optional true} [:maybe pos-int?]]])+ [:single_collection_id {:optional true} [:maybe pos-int?]]+ ;; added by the model's after-select hook+ [:masked_key {:optional true} :string]])(defn- insert-schema [map-schema](into [:map]@@ -128,3 +133,24 @@(mr/def ::api-key.update(update-schema ::api-key))++(mr/def ::api-key.create+ "What an insert of a ApiKey accepts: every column of `:api_key` except `id`, all optional, plus `:metabase.api-keys.core/unhashed-key` consumed by the model's hooks."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:key {:optional true} [:maybe :string]]+ [:key_prefix {:optional true} [:maybe :string]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:updated_by_id {:optional true} [:maybe ms/PositiveInt]]+ [:scope {:optional true} [:maybe [:or :keyword :string]]]+ [:metabase.api-keys.core/unhashed-key {:optional true} ::key.unhashed-or-secret]])++(mr/def ::api-key.inserted+ "What inserting an ApiKey returns: the selected row plus the `:group` and `:updated_by` hydrated by the after-insert hook."+ (mut/merge ::api-key+ [:map+ [:group {:optional true} [:map {:closed true} [:id [:maybe pos-int?]] [:name [:maybe :string]]]]+ [:updated_by {:optional true} ::users.schema/user]]))diff --git a/src/metabase/app_db/db.clj b/src/metabase/app_db/db.cljindex 27a6966cd4d..86fbcb24bdb 100644--- a/src/metabase/app_db/db.clj+++ b/src/metabase/app_db/db.clj@@ -8,29 +8,34 @@(:require[honey.sql :as sql][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn current-timestamp-string+(mu/defn current-timestamp-string"The application DB's own current timestamp, as a string, for app DB type `db-type`."- ^String [db-type]+ ^String [db-type :- [:enum :h2 :mysql :postgres]];; for MySQL, cast(current_timestamp AS char); for H2 & Postgres, cast(current_timestamp AS text)(let [cast-form (h2x/cast (if (= db-type :mysql) :char :text) (h2x/current-datetime-honeysql-form db-type))](:timestamp (t2/query-one {:select [[cast-form :timestamp]]}))));;; ------------------------------------------------ Liquibase -------------------------------------------------(defn changelog-by-id+(mu/defn changelog-by-id"The Liquibase changelog row with `changelog-id` in the app DB of type `db-type`, or nil."- [db-type changelog-id]+ [db-type :- [:enum :h2 :mysql :postgres]+ changelog-id :- :string](let [table-name (case db-type(:postgres :h2) "databasechangelog":mysql "DATABASECHANGELOG")](t2/query-one [(format "select * from %s where id = ?" table-name) changelog-id])))-(defn changelog-ids+(mu/defn changelog-ids"The ids among `changelog-ids` still present in the Liquibase changelog table `changelog-table-name`, read on`conn`."- [conn changelog-table-name changelog-ids]+ [conn :- (ms/InstanceOfClass java.sql.Connection)+ changelog-table-name :- :string+ changelog-ids :- [:set :string]](map :id (t2/query conn (sql/format {:select [:id]:from [(keyword changelog-table-name)]:where [:in :id changelog-ids]}))))@@ -40,61 +45,75 @@(def ^:private unmigrated-settings-where[:and [:= :value_with_aad nil] [:not= :value nil] [:not= :value ""]])-(defn setting-value+(mu/defn setting-value"The legacy `value` of the setting row with `setting-key`, or nil."- [setting-key]+ [setting-key :- :string](t2/select-one-fn :value :setting :key setting-key))-(defn settings+(mu/defn settings"Every setting row, raw."[](t2/select :setting))-(defn reducible-setting-values-with-aad+(mu/defn reducible-setting-values-with-aad"A reducible of the key and `value_with_aad` of every setting row that has one."[](t2/reducible-select [:setting :key :value_with_aad] {:where [:!= :value_with_aad nil]}))-(defn unmigrated-settings?+(mu/defn unmigrated-settings?"Whether any setting row has a non-blank `value` but no `value_with_aad`."[](t2/exists? :setting {:where unmigrated-settings-where}))-(defn unmigrated-settings+(mu/defn unmigrated-settings"Every setting row with a non-blank `value` but no `value_with_aad`, locked for update."[](t2/select :setting {:where unmigrated-settings-where, :for :update}))-(defn insert-setting!- "Insert a setting row for `setting-key` holding `value` and `value-with-aad`."- [setting-key value value-with-aad]+(mu/defn insert-setting!+ "Insert a setting row for `setting-key` holding `value` and `value-with-aad`, returning the number inserted (as a+ one-element sequence -- `t2/query` on an INSERT, unlike `t2/insert!`, does not unwrap it)."+ [setting-key :- :string+ value :- [:maybe :string]+ value-with-aad :- [:maybe :string]](t2/query {:insert-into :setting:values [{:key setting-key, :value value, :value_with_aad value-with-aad}]}))-(defn update-setting-values!+(mu/defn update-setting-values!"Set the columns in `changes` -- `:value` and/or `:value_with_aad`, nil writing NULL -- of the setting row with- `setting-key`."- [setting-key changes]+ `setting-key`, returning the number updated (as a one-element sequence -- `t2/query` on an UPDATE, unlike+ `t2/update!`, does not unwrap it)."+ [setting-key :- :string+ changes :- [:map {:closed true}+ [:value {:optional true} [:maybe :string]]+ [:value_with_aad {:optional true} [:maybe :string]]]](t2/query {:update :setting:set (select-keys changes [:value :value_with_aad]):where [:= :key setting-key]}))-(defn delete-setting!- "Delete the setting row with `setting-key`."- [setting-key]+(mu/defn delete-setting!+ "Delete the setting row with `setting-key`, returning the number deleted (as a one-element sequence -- `t2/query`+ on a DELETE, unlike `t2/delete!`, does not unwrap it)."+ [setting-key :- :string](t2/query {:delete-from :setting, :where [:= :key setting-key]}))-(defn reducible-column-values+(mu/defn reducible-column-values"A reducible of the id and `column` value of every row of `table`."- [table column]+ [table :- :keyword+ column :- :keyword](t2/reducible-select [table :id [column :value]]))-(defn update-column-value!- "Set `column` of the row of `table` with `id` to `value`."- [table column id value]+(mu/defn update-column-value!+ "Set `column` of the row of `table` with `id` to `value`, returning the number updated (as a one-element+ sequence -- `t2/query` on an UPDATE, unlike `t2/update!`, does not unwrap it)."+ [table :- :keyword+ column :- :keyword+ id :- ms/PositiveInt+ value :- [:maybe [:or :string bytes?]]](t2/query {:update table, :set {column value}, :where [:= :id id]}))-(defn delete-query-cache!- "Delete every cached query result."+(mu/defn delete-query-cache!+ "Delete every cached query result, returning the number deleted (as a one-element sequence -- `t2/query` on a+ DELETE, unlike `t2/delete!`, does not unwrap it)."[](t2/query {:delete-from :query_cache}))diff --git a/src/metabase/appearance/db.clj b/src/metabase/appearance/db.cljindex e6fdcd99ac4..1ff969e9951 100644--- a/src/metabase/appearance/db.clj+++ b/src/metabase/appearance/db.clj@@ -2,9 +2,11 @@"Application database queries for the appearance module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn unarchived-dashboard-exists?+(mu/defn unarchived-dashboard-exists?"Whether an unarchived Dashboard with `dashboard-id` exists."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/exists? :model/Dashboard :id dashboard-id :archived false))diff --git a/src/metabase/audit_app/db.clj b/src/metabase/audit_app/db.cljindex da1cf1dd0c6..b040178a869 100644--- a/src/metabase/audit_app/db.clj+++ b/src/metabase/audit_app/db.clj@@ -2,41 +2,49 @@"Application database queries for the audit app module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.audit-app.schema :as audit-app.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn cards- "The Cards with `card-ids`."- [card-ids]+(mu/defn cards+ "The Cards with `card-ids` (nil entries, e.g. from virtual dashcards, are ignored)."+ [card-ids :- [:sequential [:maybe ::lib.schema.id/card]]](t2/select :model/Card :id [:in card-ids]))-(defn audit-log-topic-exists?+(mu/defn audit-log-topic-exists?"Whether an AuditLog entry with `topic` exists."- [topic]+ [topic :- [:or :keyword :string]](t2/exists? :model/AuditLog :topic topic))-(defn collection-with-entity-id+(mu/defn collection-with-entity-id"The Collection with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one :model/Collection :entity_id entity-id))-(defn dashboard-with-entity-id+(mu/defn dashboard-with-entity-id"The Dashboard with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one :model/Dashboard :entity_id entity-id))-(defn card-name-and-description+(mu/defn card-name-and-description"The name and description of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :name :description :card_schema], :id card-id))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table, :id table-id))-(defn insert-audit-log!+(mu/defn insert-audit-log!"Insert an AuditLog entry."- [topic details model-name model-id user-id]+ [topic :- :keyword+ details :- [:maybe ::audit-app.schema/audit-log.details]+ model-name :- [:maybe :string]+ model-id :- [:maybe ms/PositiveInt]+ user-id :- [:maybe ::lib.schema.id/user]](t2/insert! :model/AuditLog:topic topic:details details@@ -44,9 +52,12 @@:model_id model-id:user_id user-id))-(defn delete-oldest-by-id-subquery!+(mu/defn delete-oldest-by-id-subquery!"Delete up to `batch-size` of the `table` rows whose `time-column` is at or before `cutoff`, lowest ids first."- [table time-column cutoff batch-size]+ [table :- :keyword+ time-column :- :keyword+ cutoff :- ms/TemporalInstant+ batch-size :- ms/PositiveInt](t2/query-one {:delete-from table:where [:in:id@@ -56,9 +67,12 @@:order-by [[:id :asc]]:limit batch-size}]}))-(defn delete-oldest-with-limit!+(mu/defn delete-oldest-with-limit!"Delete up to `batch-size` of the `table` rows whose `time-column` is at or before `cutoff`."- [table time-column cutoff batch-size]+ [table :- :keyword+ time-column :- :keyword+ cutoff :- ms/TemporalInstant+ batch-size :- ms/PositiveInt](t2/query-one {:delete-from table:where [:<= time-column cutoff]:limit batch-size}))diff --git a/src/metabase/audit_app/schema.clj b/src/metabase/audit_app/schema.cljnew file mode 100644index 00000000000..5206d34bb62--- /dev/null+++ b/src/metabase/audit_app/schema.clj@@ -0,0 +1,33 @@+(ns metabase.audit-app.schema+ "Malli schemas for the audit-app module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::audit-log.details+ "The `:details` column of a AuditLog, decoded."+ :map)++(mr/def ::audit-log+ "A AuditLog as selected from the app DB: every column of `:audit_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:topic [:or :keyword :string]]+ [:timestamp ms/TemporalInstant]+ [:end_timestamp [:maybe ms/TemporalInstant]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:model [:maybe [:or :keyword :string]]]+ [:model_id [:maybe :int]]+ [:details ::audit-log.details]])++(mr/def ::audit-log.update+ "What an update (or insert) of a AuditLog accepts: every column of `:audit_log` except `id`, all optional."+ [:map {:closed true}+ [:topic {:optional true} [:maybe [:or :keyword :string]]]+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:end_timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:details {:optional true} [:maybe ::audit-log.details]]])diff --git a/src/metabase/auth_identity/db.clj b/src/metabase/auth_identity/db.cljindex f5dd76b0770..92c7e4f64b1 100644--- a/src/metabase/auth_identity/db.clj+++ b/src/metabase/auth_identity/db.clj@@ -2,100 +2,128 @@"Application database queries for the auth identity module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.users.schema :as users.schema][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn auth-identity+(def ^:private AuthIdentityRow+ "The writable columns of an AuthIdentity row (excluding `:id`, `:created_at`, `:updated_at`)."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:provider {:optional true} [:maybe [:or :keyword :string]]]+ [:credentials {:optional true} [:maybe :map]]+ [:metadata {:optional true} [:maybe :map]]+ [:provider_id {:optional true} [:maybe :string]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:confirmed_at {:optional true} [:maybe ms/TemporalInstant]]])++(mu/defn auth-identity"The AuthIdentity of the User with `user-id` at `provider`, or nil."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/select-one :model/AuthIdentity :user_id user-id :provider provider))-(defn auth-identity-id+(mu/defn auth-identity-id"The id of the AuthIdentity of the User with `user-id` at `provider`, or nil."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/select-one-pk :model/AuthIdentity :user_id user-id :provider provider))-(defn auth-identity-expiry+(mu/defn auth-identity-expiry"The id and expiry of the AuthIdentity of the User with `user-id` at `provider`, or nil."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/select-one [:model/AuthIdentity :id :expires_at] :user_id user-id :provider provider))-(defn auth-identity-exists?+(mu/defn auth-identity-exists?"Whether the User with `user-id` has an AuthIdentity at `provider`."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/exists? :model/AuthIdentity :user_id user-id :provider provider))-(defn insert-auth-identity!- "Insert the AuthIdentity `row`."- [row]+(mu/defn insert-auth-identity!+ "Insert the AuthIdentity `row`, returning the number inserted."+ [row :- AuthIdentityRow](t2/insert! :model/AuthIdentity row))-(defn update-auth-identity!- "Apply `changes` to the AuthIdentity with `id`."- [id changes]+(mu/defn update-auth-identity!+ "Apply `changes` to the AuthIdentity with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- AuthIdentityRow](t2/update! :model/AuthIdentity id changes))-(defn touch-auth-identity!- "Set `last_used_at` of the AuthIdentity with `id` to now."- [id]+(mu/defn touch-auth-identity!+ "Set `last_used_at` of the AuthIdentity with `id` to now, returning the number updated. `id` may be nil (no+ matching AuthIdentity), which updates nothing."+ [id :- [:maybe ms/PositiveInt]](t2/update! :model/AuthIdentity id {:last_used_at :%now}))-(defn delete-auth-identities!- "Delete the AuthIdentities of the User with `user-id` at `provider`."- [user-id provider]+(mu/defn delete-auth-identities!+ "Delete the AuthIdentities of the User with `user-id` at `provider`, returning the number deleted."+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/delete! :model/AuthIdentity :user_id user-id :provider provider))-(defn delete-sessions-for-user!- "Delete every Session of the User with `user-id`. Duplicates `metabase.session.db/delete-sessions-for-user!`;- can't delegate to it because the `session` module already depends on `auth-identity`, so the reverse dependency- would be a module cycle."- [user-id]+(mu/defn delete-sessions-for-user!+ "Delete every Session of the User with `user-id`, returning the number deleted. Duplicates+ `metabase.session.db/delete-sessions-for-user!`; can't delegate to it because the `session` module already depends+ on `auth-identity`, so the reverse dependency would be a module cycle."+ [user-id :- ::lib.schema.id/user](t2/delete! :model/Session :user_id user-id))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User user-id))-(defn user-by-email+(mu/defn user-by-email"The User whose email matches `email` case-insensitively, or nil."- [email]+ [email :- :string](t2/select-one :model/User :%lower.email (u/lower-case-en email)))-(defn user-login-columns+(mu/defn user-login-columns"The id, active flag, last login, and tenant id of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :is_active :last_login :tenant_id] :id user-id))-(defn user-login-columns-by-email+(mu/defn user-login-columns-by-email"The id, active flag, last login, and tenant id of the User whose email matches `email` case-insensitively, ornil."- [email]+ [email :- :string](t2/select-one [:model/User :id :is_active :last_login :tenant_id] :%lower.email (u/lower-case-en email)))-(defn user-login-status+(mu/defn user-login-status"The id, active flag, and last login of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :is_active :last_login] user-id))-(defn user-active?+(mu/defn user-active?"Whether the User with `user-id` is active."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :is_active :model/User :id user-id))-(defn update-user!- "Apply `changes` to the User with `user-id`."- [user-id changes]+(mu/defn update-user!+ "Apply `changes` to the User with `user-id`, returning the number updated."+ [user-id :- ::lib.schema.id/user+ changes :- ::users.schema/user.update](t2/update! :model/User user-id changes))-(defn insert-user-returning-login-columns!+(mu/defn insert-user-returning-login-columns!"Insert the User `row` and return its id, last login, active flag, and tenant id."- [row]+ [row :- ::users.schema/user.update](t2/insert-returning-instance! [:model/User :id :last_login :is_active :tenant_id] row))-(defn insert-session!+(mu/defn insert-session!"Insert a Session and return the inserted instance."- [session-id user-id auth-identity-id session-key expires-at]+ [session-id :- :string+ user-id :- ::lib.schema.id/user+ auth-identity-id :- [:maybe ms/PositiveInt]+ session-key :- :string+ expires-at :- [:maybe ms/TemporalInstant]](t2/insert-returning-instance! :model/Session;; Without setting the ID here we can't return an instance on MySQL:id session-iddiff --git a/src/metabase/auth_identity/schema.clj b/src/metabase/auth_identity/schema.cljnew file mode 100644index 00000000000..62d60fdade7--- /dev/null+++ b/src/metabase/auth_identity/schema.clj@@ -0,0 +1,43 @@+(ns metabase.auth-identity.schema+ "Malli schemas for the auth-identity module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::auth-identity.credentials+ "The `:credentials` column of a AuthIdentity, decoded."+ :map)++(mr/def ::auth-identity.metadata+ "The `:metadata` column of a AuthIdentity, decoded."+ :map)++(mr/def ::auth-identity+ "A AuthIdentity as selected from the app DB: every column of `:auth_identity`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:provider [:or :keyword :string]]+ [:credentials [:maybe ::auth-identity.credentials]]+ [:metadata [:maybe ::auth-identity.metadata]]+ [:provider_id [:maybe :string]]+ [:last_used_at [:maybe ms/TemporalInstant]]+ [:expires_at [:maybe ms/TemporalInstant]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:confirmed_at [:maybe ms/TemporalInstant]]])++(mr/def ::auth-identity.update+ "What an update (or insert) of a AuthIdentity accepts: every column of `:auth_identity` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:provider {:optional true} [:maybe [:or :keyword :string]]]+ [:credentials {:optional true} [:maybe ::auth-identity.credentials]]+ [:metadata {:optional true} [:maybe ::auth-identity.metadata]]+ [:provider_id {:optional true} [:maybe :string]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:confirmed_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/bookmarks/db.clj b/src/metabase/bookmarks/db.cljindex 8e0d1a400a1..12b1a71bac4 100644--- a/src/metabase/bookmarks/db.clj+++ b/src/metabase/bookmarks/db.clj@@ -2,98 +2,118 @@"Application database queries for the bookmarks module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut][metabase.app-db.core :as mdb]+ [metabase.bookmarks.schema :as bookmarks.schema][metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn card-bookmark-exists?+(mu/defn card-bookmark-exists?"Whether the User with `user-id` has a CardBookmark for the Card with `card-id`."- [card-id user-id]+ [card-id :- ::lib.schema.id/card+ user-id :- ::lib.schema.id/user](t2/exists? :model/CardBookmark :card_id card-id :user_id user-id))-(defn dashboard-bookmark-exists?+(mu/defn dashboard-bookmark-exists?"Whether the User with `user-id` has a DashboardBookmark for the Dashboard with `dashboard-id`."- [dashboard-id user-id]+ [dashboard-id :- ::lib.schema.id/dashboard+ user-id :- ::lib.schema.id/user](t2/exists? :model/DashboardBookmark :dashboard_id dashboard-id :user_id user-id))-(defn collection-bookmark-exists?+(mu/defn collection-bookmark-exists?"Whether the User with `user-id` has a CollectionBookmark for the Collection with `collection-id`."- [collection-id user-id]+ [collection-id :- ::lib.schema.id/collection+ user-id :- ::lib.schema.id/user](t2/exists? :model/CollectionBookmark :collection_id collection-id :user_id user-id))-(defn document-bookmark-exists?+(mu/defn document-bookmark-exists?"Whether the User with `user-id` has a DocumentBookmark for the Document with `document-id`."- [document-id user-id]+ [document-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/exists? :model/DocumentBookmark :document_id document-id :user_id user-id))-(defn exploration-bookmark-exists?+(mu/defn exploration-bookmark-exists?"Whether the User with `user-id` has an ExplorationBookmark for the Exploration with `exploration-id`."- [exploration-id user-id]+ [exploration-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/exists? :model/ExplorationBookmark :exploration_id exploration-id :user_id user-id))-(defn insert-card-bookmark!+(mu/defn insert-card-bookmark!"Insert a CardBookmark for the Card with `card-id` and the User with `user-id`, returning the inserted instance."- [card-id user-id]+ [card-id :- ::lib.schema.id/card+ user-id :- ::lib.schema.id/user](t2/insert-returning-instance! :model/CardBookmark {:card_id card-id :user_id user-id}))-(defn insert-dashboard-bookmark!+(mu/defn insert-dashboard-bookmark!"Insert a DashboardBookmark for the Dashboard with `dashboard-id` and the User with `user-id`, returning theinserted instance."- [dashboard-id user-id]+ [dashboard-id :- ::lib.schema.id/dashboard+ user-id :- ::lib.schema.id/user](t2/insert-returning-instance! :model/DashboardBookmark {:dashboard_id dashboard-id :user_id user-id}))-(defn insert-collection-bookmark!+(mu/defn insert-collection-bookmark!"Insert a CollectionBookmark for the Collection with `collection-id` and the User with `user-id`, returning theinserted instance."- [collection-id user-id]+ [collection-id :- ::lib.schema.id/collection+ user-id :- ::lib.schema.id/user](t2/insert-returning-instance! :model/CollectionBookmark {:collection_id collection-id :user_id user-id}))-(defn insert-document-bookmark!+(mu/defn insert-document-bookmark!"Insert a DocumentBookmark for the Document with `document-id` and the User with `user-id`, returning the insertedinstance."- [document-id user-id]+ [document-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/insert-returning-instance! :model/DocumentBookmark {:document_id document-id :user_id user-id}))-(defn insert-exploration-bookmark!+(mu/defn insert-exploration-bookmark!"Insert an ExplorationBookmark for the Exploration with `exploration-id` and the User with `user-id`, returning theinserted instance."- [exploration-id user-id]+ [exploration-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/insert-returning-instance! :model/ExplorationBookmark {:exploration_id exploration-id :user_id user-id}))-(defn delete-card-bookmark!+(mu/defn delete-card-bookmark!"Delete the CardBookmark of the User with `user-id` for the Card with `card-id`."- [card-id user-id]+ [card-id :- ::lib.schema.id/card+ user-id :- ::lib.schema.id/user](t2/delete! :model/CardBookmark :card_id card-id :user_id user-id))-(defn delete-dashboard-bookmark!+(mu/defn delete-dashboard-bookmark!"Delete the DashboardBookmark of the User with `user-id` for the Dashboard with `dashboard-id`."- [dashboard-id user-id]+ [dashboard-id :- ::lib.schema.id/dashboard+ user-id :- ::lib.schema.id/user](t2/delete! :model/DashboardBookmark :dashboard_id dashboard-id :user_id user-id))-(defn delete-collection-bookmark!+(mu/defn delete-collection-bookmark!"Delete the CollectionBookmark of the User with `user-id` for the Collection with `collection-id`."- [collection-id user-id]+ [collection-id :- ::lib.schema.id/collection+ user-id :- ::lib.schema.id/user](t2/delete! :model/CollectionBookmark :collection_id collection-id :user_id user-id))-(defn delete-document-bookmark!+(mu/defn delete-document-bookmark!"Delete the DocumentBookmark of the User with `user-id` for the Document with `document-id`."- [document-id user-id]+ [document-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/delete! :model/DocumentBookmark :document_id document-id :user_id user-id))-(defn delete-exploration-bookmark!+(mu/defn delete-exploration-bookmark!"Delete the ExplorationBookmark of the User with `user-id` for the Exploration with `exploration-id`."- [exploration-id user-id]+ [exploration-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/delete! :model/ExplorationBookmark :exploration_id exploration-id :user_id user-id))-(defn delete-bookmark-orderings-for-user!+(mu/defn delete-bookmark-orderings-for-user!"Delete the BookmarkOrderings of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/BookmarkOrdering :user_id user-id))-(defn insert-bookmark-orderings!+(mu/defn insert-bookmark-orderings!"Insert the BookmarkOrdering `rows`."- [rows]+ [rows :- [:sequential (mut/select-keys ::bookmarks.schema/bookmark-ordering.update [:user_id :type :item_id :ordering])]](t2/insert! :model/BookmarkOrdering rows))(defn- bookmarks-union-query@@ -151,13 +171,16 @@:from [:exploration_bookmark]:where [:= :user_id user-id]})}))-(defn bookmark-rows-for-user+(mu/defn bookmark-rows-for-user"The bookmarks of the User with `user-id`, joined against the Card, Dashboard, Collection, Document, and Explorationtables, excluding archived items, and filtered to items the target `user-scope` (a map of `:current-user-id` and`:is-superuser?`) can still read (re-checked at read time rather than trusted from when the bookmark was created,see SEC-669). The `collection_id` join uses [[h2x/identifier]] to work aroundhttps://github.com/seancorfield/honeysql/issues/450."- [user-id user-scope]+ [user-id :- ::lib.schema.id/user+ user-scope :- [:map {:closed true}+ [:current-user-id ::lib.schema.id/user]+ [:is-superuser? :boolean]]](let [select-fields [[:bookmark.created_at :created_at][:bookmark.type :type][:bookmark.item_id :item_id]diff --git a/src/metabase/bookmarks/models/bookmark.clj b/src/metabase/bookmarks/models/bookmark.cljindex 3bfef73dc6f..fff8bb643aa 100644--- a/src/metabase/bookmarks/models/bookmark.clj+++ b/src/metabase/bookmarks/models/bookmark.clj@@ -39,7 +39,7 @@[:name ms/NonBlankString][:authority_level {:optional true} [:maybe :string]][:is_remote_synced {:optional true} :boolean]- [:card_type {:optional true} [:maybe ::queries.schema/card-type]]+ [:card_type {:optional true} [:maybe ::queries.schema/card.type]][:description {:optional true} [:maybe :string]][:display {:optional true} [:maybe :string]]])diff --git a/src/metabase/bookmarks/schema.clj b/src/metabase/bookmarks/schema.cljnew file mode 100644index 00000000000..2116fc181e5--- /dev/null+++ b/src/metabase/bookmarks/schema.clj@@ -0,0 +1,98 @@+(ns metabase.bookmarks.schema+ "Malli schemas for the bookmarks module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::bookmark-ordering+ "A BookmarkOrdering as selected from the app DB: every column of `:bookmark_ordering`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:type [:or :keyword :string]]+ [:item_id ms/PositiveInt]+ [:ordering :int]])++(mr/def ::bookmark-ordering.update+ "What an update (or insert) of a BookmarkOrdering accepts: every column of `:bookmark_ordering` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:item_id {:optional true} [:maybe ms/PositiveInt]]+ [:ordering {:optional true} [:maybe :int]]])++(mr/def ::card-bookmark+ "A CardBookmark as selected from the app DB: every column of `:card_bookmark`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:card_id ::lib.schema.id/card]+ [:created_at ms/TemporalInstant]])++(mr/def ::card-bookmark.update+ "What an update (or insert) of a CardBookmark accepts: every column of `:card_bookmark` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::collection-bookmark+ "A CollectionBookmark as selected from the app DB: every column of `:collection_bookmark`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:collection_id ::lib.schema.id/collection]+ [:created_at ms/TemporalInstant]])++(mr/def ::collection-bookmark.update+ "What an update (or insert) of a CollectionBookmark accepts: every column of `:collection_bookmark` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::dashboard-bookmark+ "A DashboardBookmark as selected from the app DB: every column of `:dashboard_bookmark`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:dashboard_id ::lib.schema.id/dashboard]+ [:created_at ms/TemporalInstant]])++(mr/def ::dashboard-bookmark.update+ "What an update (or insert) of a DashboardBookmark accepts: every column of `:dashboard_bookmark` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::document-bookmark+ "A DocumentBookmark as selected from the app DB: every column of `:document_bookmark`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:document_id ms/PositiveInt]+ [:created_at ms/TemporalInstant]])++(mr/def ::document-bookmark.update+ "What an update (or insert) of a DocumentBookmark accepts: every column of `:document_bookmark` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:document_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::exploration-bookmark+ "A ExplorationBookmark as selected from the app DB: every column of `:exploration_bookmark`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:exploration_id ms/PositiveInt]+ [:created_at ms/TemporalInstant]])++(mr/def ::exploration-bookmark.update+ "What an update (or insert) of a ExplorationBookmark accepts: every column of `:exploration_bookmark` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:exploration_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/bug_reporting/db.clj b/src/metabase/bug_reporting/db.cljindex 7eafaa29df0..87e92c1036a 100644--- a/src/metabase/bug_reporting/db.clj+++ b/src/metabase/bug_reporting/db.clj@@ -2,9 +2,10 @@"Application database queries for the bug reporting module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn database-engines+(mu/defn database-engines"The set of engines of all Databases."[](t2/select-fn-set :engine :model/Database))diff --git a/src/metabase/cache/db.clj b/src/metabase/cache/db.cljindex 310c211d47d..64c6df86256 100644--- a/src/metabase/cache/db.clj+++ b/src/metabase/cache/db.clj@@ -2,40 +2,45 @@"Application database queries for the cache module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.cache.schema :as cache.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn database-with-ids+(mu/defn database-with-ids"A Database whose id is in `ids`, or nil."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-one :model/Database :id [:in ids]))-(defn dashboard-with-ids+(mu/defn dashboard-with-ids"A Dashboard whose id is in `ids`, or nil."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-one :model/Dashboard :id [:in ids]))-(defn card-with-ids+(mu/defn card-with-ids"A Card whose id is in `ids`, or nil."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-one :model/Card :id [:in ids]))-(defn dashboard-collection-id+(mu/defn dashboard-collection-id"The `:collection_id` of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one [:model/Dashboard :collection_id] :id dashboard-id))-(defn card-collection-id+(mu/defn card-collection-id"The `:collection_id` of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :collection_id] :id card-id))-(defn cache-config+(mu/defn cache-config"The CacheConfig with primary key `pk`, or nil."- [pk]+ [pk :- ms/PositiveInt](t2/select-one :model/CacheConfig pk))-(defn root-ttl-cache-config+(mu/defn root-ttl-cache-config"The root TTL CacheConfig, or nil."[](t2/select-one :model/CacheConfig :model "root" :model_id 0 :strategy :ttl))@@ -82,68 +87,84 @@[:= :model "dashboard"] [:!= :report_dashboard.id nil]:else true]]}))-(defn cache-configs-page+(mu/defn cache-configs-page"The CacheConfigs of `models` in `collection` (or of the entity with `id`), with the name and Collection of theconfigured entity, sorted by `sort-column` in `sort-direction` when given and paged by `limit` and `offset`."- [models collection id sort-column sort-direction limit offset]+ [models :- [:sequential :string]+ collection :- [:maybe ms/PositiveInt]+ id :- [:maybe ms/IntGreaterThanOrEqualToZero]+ sort-column :- [:maybe [:enum :name :collection :policy]]+ sort-direction :- [:maybe [:enum :asc :desc]]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/CacheConfig(cond-> (base-query models collection id)sort-column (assoc :order-by [[(sort-column->order-by sort-column) sort-direction]])limit (assoc :limit limit)offset (assoc :offset offset))))-(defn cache-config-count-row+(mu/defn cache-config-count-row"The `:count` row of the CacheConfigs [[cache-configs-page]] pages through."- [models collection id]+ [models :- [:sequential :string]+ collection :- [:maybe ms/PositiveInt]+ id :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/query-one (-> (base-query models collection id)(dissoc :select)(assoc :select [[[:count :*] :count]]))))-(defn lock-cache-config+(mu/defn lock-cache-config"The CacheConfig for `model` and `model-id` locked for update, or nil."- [model model-id]+ [model :- :string+ model-id :- ms/IntGreaterThanOrEqualToZero](t2/select-one :model/CacheConfig :model model :model_id model-id {:for :update}))-(defn upsert-cache-config!- "Insert or replace the CacheConfig for `model` and `model-id` with `data`."- [model model-id data]+(mu/defn upsert-cache-config!+ "Insert or replace the CacheConfig for `model` and `model-id` with `data`, returning its ID."+ [model :- :string+ model-id :- ms/IntGreaterThanOrEqualToZero+ data :- (mut/select-keys ::cache.schema/cache-config.update [:model :model_id :strategy :config :refresh_automatically])](app-db/update-or-insert! :model/CacheConfig {:model model :model_id model-id}(constantly data)))-(defn cache-configs-for+(mu/defn cache-configs-for"The CacheConfigs for `model` and `model-ids`."- [model model-ids]+ [model :- :string+ model-ids :- [:sequential ms/IntGreaterThanOrEqualToZero]](t2/select :model/CacheConfig :model model :model_id [:in model-ids]))-(defn delete-cache-configs!- "Delete the CacheConfigs for `model` and `model-ids`."- [model model-ids]+(mu/defn delete-cache-configs!+ "Delete the CacheConfigs for `model` and `model-ids`, returning the number deleted."+ [model :- :string+ model-ids :- [:sequential ms/IntGreaterThanOrEqualToZero]](t2/delete! :model/CacheConfig :model model :model_id [:in model-ids]))-(defn card-ids-for-databases+(mu/defn card-ids-for-databases"The ids of the Cards of the Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select-fn-vec :id [:model/Card :id] :database_id [:in database-ids]))-(defn dashboard-card-ids- "The Card ids of the DashboardCards of the Dashboards with `dashboard-ids`."- [dashboard-ids]+(mu/defn dashboard-card-ids+ "The Card ids of the DashboardCards of the Dashboards with `dashboard-ids` (nil for DashboardCards without a Card)."+ [dashboard-ids :- [:sequential ::lib.schema.id/dashboard]](t2/select-fn-vec :card_id [:model/DashboardCard :card_id] :dashboard_id [:in dashboard-ids]))-(defn invalidate-cards!+(mu/defn invalidate-cards!"Set `cache_invalidated_at` of the Cards with `card-ids` to `invalidated-at`, returning the number updated."- [card-ids invalidated-at]+ [card-ids :- [:sequential ::lib.schema.id/card]+ invalidated-at :- ms/TemporalInstant](t2/update! :model/Card :id [:in card-ids] {:cache_invalidated_at invalidated-at}))-(defn invalidate-cache-configs!- "Set `invalidated_at` of the CacheConfigs identified by the `[model model-id]` pairs to `invalidated-at`."- [model+ids invalidated-at]+(mu/defn invalidate-cache-configs!+ "Set `invalidated_at` of the CacheConfigs identified by the `[model model-id]` pairs to `invalidated-at`, returning+ the number updated."+ [model+ids :- [:sequential [:tuple :string ms/IntGreaterThanOrEqualToZero]]+ invalidated-at :- ms/TemporalInstant](t2/query-one {:update (t2/table-name :model/CacheConfig):set {:invalidated_at invalidated-at}:where (into [:or] (for [[model model-id] model+ids][:and [:= :model model] [:= :model_id model-id]]))}))-(defn cache-config-exists?+(mu/defn cache-config-exists?"Whether any CacheConfig exists."[](t2/exists? :model/CacheConfig))diff --git a/src/metabase/cache/schema.clj b/src/metabase/cache/schema.cljnew file mode 100644index 00000000000..35c1cdbcc21--- /dev/null+++ b/src/metabase/cache/schema.clj@@ -0,0 +1,58 @@+(ns metabase.cache.schema+ "Malli schemas for the cache module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::cache-config.config+ "The `:config` column of a CacheConfig, decoded."+ :map)++(mr/def ::cache-config.state+ "The `:state` column of a CacheConfig, decoded."+ :map)++(mr/def ::cache-config+ "A CacheConfig as selected from the app DB: every column of `:cache_config`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model [:or :keyword :string]]+ [:model_id [:maybe :int]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:strategy [:or :keyword :string]]+ [:config ::cache-config.config]+ [:state [:maybe ::cache-config.state]]+ [:invalidated_at [:maybe ms/TemporalInstant]]+ [:next_run_at [:maybe ms/TemporalInstant]]+ [:refresh_automatically [:maybe :boolean]]])++(mr/def ::cache-config.update+ "What an update (or insert) of a CacheConfig accepts: every column of `:cache_config` except `id`, all optional."+ [:map {:closed true}+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:strategy {:optional true} [:maybe [:or :keyword :string]]]+ [:config {:optional true} [:maybe ::cache-config.config]]+ [:state {:optional true} [:maybe ::cache-config.state]]+ [:invalidated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:next_run_at {:optional true} [:maybe ms/TemporalInstant]]+ [:refresh_automatically {:optional true} [:maybe :boolean]]])++(mr/def ::query-cache+ "A QueryCache as selected from the app DB: every column of `:query_cache`."+ [:map {:closed true}+ [:query_hash [:or bytes? :string]]+ [:updated_at ms/TemporalInstant]+ [:results [:or bytes? :string]]+ [:refresh_started_at [:maybe ms/TemporalInstant]]])++(mr/def ::query-cache.update+ "What an update (or insert) of a QueryCache accepts: every column of `:query_cache` except `id`, all optional."+ [:map {:closed true}+ [:query_hash {:optional true} [:maybe [:or bytes? :string]]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:results {:optional true} [:maybe [:or bytes? :string]]]+ [:refresh_started_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/channel/db.clj b/src/metabase/channel/db.cljindex 6ab6a68e83b..14f2e628561 100644--- a/src/metabase/channel/db.clj+++ b/src/metabase/channel/db.clj@@ -3,39 +3,52 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn channels+(def ^:private ChannelRow+ "A whole Channel row for insert or update."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:details {:optional true} [:maybe :map]]+ [:active {:optional true} [:maybe :boolean]]])++(mu/defn channels"Every Channel."[](t2/select :model/Channel))-(defn active-channels+(mu/defn active-channels"The active Channels."[](t2/select :model/Channel :active true))-(defn channel-name-exists?+(mu/defn channel-name-exists?"Whether a Channel named `channel-name` exists."- [channel-name]+ [channel-name :- :string](t2/exists? :model/Channel :name channel-name))-(defn insert-channel!+(mu/defn insert-channel!"Insert the Channel `row` and return the inserted instance."- [row]+ [row :- ChannelRow](t2/insert-returning-instance! :model/Channel row))-(defn channel+(mu/defn channel"The Channel with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Channel id))-(defn update-channel!- "Apply `changes` to the Channel with `id`."- [id changes]+(mu/defn update-channel!+ "Apply `changes` to the Channel with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- ChannelRow](t2/update! :model/Channel id changes))-(defn accepted-admin-emails+(mu/defn accepted-admin-emails"The emails of the active personal superusers who have logged in at least once, in id order."[](t2/select-fn-set :email :model/User@@ -45,21 +58,21 @@:type "personal"{:order-by [[:id :asc]]}))-(defn user-contact-info- "The name, email, and locale of the User with `user-id`, or nil."- [user-id]+(mu/defn user-contact-info+ "The name, email, locale, and derived common name of the User with `user-id`, or nil."+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :last_name :first_name :email :locale] :id user-id))-(defn active-user-emails+(mu/defn active-user-emails"The emails of the active Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-fn-set :email :model/User {:where [:and[:= :is_active true][:in :id user-ids]]}))-(defn user-ids-with-permission+(mu/defn user-ids-with-permission"The ids of the Users belonging to a PermissionsGroup that holds `permission-path`."- [permission-path]+ [permission-path :- :string](app-db/query {:select [:pgm.user_id]:from [[:permissions_group_membership :pgm]]:join [[:permissions_group :pg] [:= :pgm.group_id :pg.id]]@@ -71,37 +84,37 @@[:= :p.object permission-path]]}]:group-by [:pgm.user_id]}))-(defn delete-pulse-channels-for-channel!- "Delete the PulseChannels of the Channel with `channel-id`."- [channel-id]+(mu/defn delete-pulse-channels-for-channel!+ "Delete the PulseChannels of the Channel with `channel-id`, returning the number deleted."+ [channel-id :- ms/PositiveInt](t2/delete! :model/PulseChannel :channel_id channel-id))-(defn channel-by-name+(mu/defn channel-by-name"The Channel named `channel-name`, or nil."- [channel-name]+ [channel-name :- :string](t2/select-one :model/Channel :name channel-name))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn dashboard-tabs+(mu/defn dashboard-tabs"The DashboardTabs of the Dashboard with `dashboard-id`, in position order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardTab :dashboard_id dashboard-id {:order-by [[:position :asc]]}))-(defn dashcards+(mu/defn dashcards"The DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardCard :dashboard_id dashboard-id))-(defn any-user+(mu/defn any-user"Some User, or nil."[](t2/select-one :model/User))diff --git a/src/metabase/channel/schema.clj b/src/metabase/channel/schema.cljnew file mode 100644index 00000000000..d02338fb86f--- /dev/null+++ b/src/metabase/channel/schema.clj@@ -0,0 +1,55 @@+(ns metabase.channel.schema+ "Malli schemas for the channel module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::channel.details+ "The `:details` column of a Channel, decoded."+ :map)++(mr/def ::channel+ "A Channel as selected from the app DB: every column of `:channel`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:description [:maybe :string]]+ [:type [:or :keyword :string]]+ [:details ::channel.details]+ [:active :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::channel.update+ "What an update (or insert) of a Channel accepts: every column of `:channel` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:details {:optional true} [:maybe ::channel.details]]+ [:active {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::channel-template.details+ "The `:details` column of a ChannelTemplate, decoded."+ :map)++(mr/def ::channel-template+ "A ChannelTemplate as selected from the app DB: every column of `:channel_template`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:channel_type [:or :keyword :string]]+ [:details [:maybe ::channel-template.details]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::channel-template.update+ "What an update (or insert) of a ChannelTemplate accepts: every column of `:channel_template` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:channel_type {:optional true} [:maybe [:or :keyword :string]]]+ [:details {:optional true} [:maybe ::channel-template.details]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/cloud_migration/db.clj b/src/metabase/cloud_migration/db.cljindex 498bbf023d4..f61249a932c 100644--- a/src/metabase/cloud_migration/db.clj+++ b/src/metabase/cloud_migration/db.clj@@ -3,42 +3,55 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as mdb]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn cloud-migration-not-in-states+(mu/defn cloud-migration-not-in-states"A CloudMigration whose state is not one of `states`, or nil."- [states]+ [states :- [:set [:or :keyword :string]]](t2/select-one :model/CloudMigration :state [:not-in states]))-(defn insert-cloud-migration!+(mu/defn insert-cloud-migration!"Insert the CloudMigration `row` and return the inserted instance."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:external_id {:optional true} [:maybe [:or :string :int]]]+ [:upload_url {:optional true} [:maybe :string]]+ [:state {:optional true} [:or :keyword :string]]+ [:progress {:optional true} [:maybe :int]]+ [:created_at {:optional true} ms/TemporalInstant]+ [:updated_at {:optional true} ms/TemporalInstant]]](t2/insert-returning-instance! :model/CloudMigration row))-(defn latest-cloud-migration+(mu/defn latest-cloud-migration"The most recently created CloudMigration, or nil."[](t2/select-one :model/CloudMigration {:order-by [[:created_at :desc]]}))-(defn cancel-cloud-migrations-not-in-states!+(mu/defn cancel-cloud-migrations-not-in-states!"Cancel every CloudMigration whose state is not one of `states`."- [states]+ [states :- [:set [:or :keyword :string]]](t2/update! :model/CloudMigration {:state [:not-in states]} {:state :cancelled}))-(defn quartz-node-count+(mu/defn quartz-node-count"The number of Quartz scheduler nodes recorded in the app DB."[](t2/count (if (= (mdb/db-type) :postgres)"qrtz_scheduler_state""QRTZ_SCHEDULER_STATE")))-(defn set-cloud-migration-progress-if-not-in-states!+(mu/defn set-cloud-migration-progress-if-not-in-states!"Set the state and progress of the CloudMigration with `id` unless its state is one of `states`, returning thenumber of rows updated."- [id states state progress]+ [id :- ms/PositiveInt+ states :- [:set [:or :keyword :string]]+ state :- [:or :keyword :string]+ progress :- :int](t2/update! :model/CloudMigration :id id :state [:not-in states] {:state state :progress progress}))-(defn set-cloud-migration-state!+(mu/defn set-cloud-migration-state!"Set the state of the CloudMigration with `id`."- [id state]+ [id :- ms/PositiveInt+ state :- [:or :keyword :string]](t2/update! :model/CloudMigration :id id {:state state}))diff --git a/src/metabase/cloud_migration/schema.clj b/src/metabase/cloud_migration/schema.cljnew file mode 100644index 00000000000..4779150888f--- /dev/null+++ b/src/metabase/cloud_migration/schema.clj@@ -0,0 +1,26 @@+(ns metabase.cloud-migration.schema+ "Malli schemas for the cloud-migration module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::cloud-migration+ "A CloudMigration as selected from the app DB: every column of `:cloud_migration`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:external_id :string]+ [:upload_url :string]+ [:state [:or :keyword :string]]+ [:progress :int]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::cloud-migration.update+ "What an update (or insert) of a CloudMigration accepts: every column of `:cloud_migration` except `id`, all optional."+ [:map {:closed true}+ [:external_id {:optional true} [:maybe :string]]+ [:upload_url {:optional true} [:maybe :string]]+ [:state {:optional true} [:maybe [:or :keyword :string]]]+ [:progress {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/cmd/db.clj b/src/metabase/cmd/db.cljindex bc56ba95672..b746ffa4553 100644--- a/src/metabase/cmd/db.clj+++ b/src/metabase/cmd/db.clj@@ -3,9 +3,10 @@additional logic, so the rest of the module never talks to `toucan2.core` itself."(:require[metabase.util :as u]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn user-id-and-active-by-email+(mu/defn user-id-and-active-by-email"The `:id` and `:is_active` of the User whose email matches `email` case-insensitively, or nil."- [email]+ [email :- :string](t2/select-one [:model/User :id :is_active] :%lower.email (u/lower-case-en email)))diff --git a/src/metabase/collections/db.clj b/src/metabase/collections/db.cljindex 031301eef72..c12d568a1c6 100644--- a/src/metabase/collections/db.clj+++ b/src/metabase/collections/db.clj@@ -3,117 +3,138 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods,and transactions."(:require+ [malli.util :as mut]+ [metabase.collections.schema :as collections.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.serialization :as serdes]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))+(def ^:private PermissionsRow+ "The writable columns of a Permissions row (excluding `:id`)."+ [:map {:closed true}+ [:object {:optional true} [:maybe :string]]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:perm_value {:optional true} [:maybe [:or :keyword :string]]]+ [:perm_type {:optional true} [:maybe [:or :keyword :string]]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]])+;;; ---------------------------------------------- Single Collections -----------------------------------------------(defn collection- "The Collection with `collection-id`, or nil."- [collection-id]+(mu/defn collection+ "The ::collections.schema/collection with `collection-id`, or nil."+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id collection-id))-(defn collection-id-and-namespace- "The ID and namespace of the Collection with `collection-id`, or nil."- [collection-id]+(mu/defn collection-id-and-namespace+ "The ID and namespace of the ::collections.schema/collection with `collection-id`, or nil."+ [collection-id :- ::lib.schema.id/collection](t2/select-one [:model/Collection :id :namespace] :id collection-id))-(defn collection-of-type- "The Collection of `type`, or nil."- [type]+(mu/defn collection-of-type+ "The ::collections.schema/collection of `type`, or nil."+ [type :- :string](t2/select-one :model/Collection :type type))-(defn root-remote-synced-collection- "The top-level remote-synced Collection, or nil."+(mu/defn root-remote-synced-collection+ "The top-level remote-synced ::collections.schema/collection, or nil."[](t2/select-one :model/Collection :is_remote_synced true :location "/"))-(defn personal-collection-of-user- "The personal Collection of the User with `user-id`, or nil."- [user-id]+(mu/defn personal-collection-of-user+ "The personal ::collections.schema/collection of the User with `user-id`, or nil."+ [user-id :- ::lib.schema.id/user](t2/select-one :model/Collection :personal_owner_id user-id))-(defn collection-exists?- "Whether a Collection with `collection-id` exists."- [collection-id]+(mu/defn collection-exists?+ "Whether a ::collections.schema/collection with `collection-id` exists. `collection-id` may be nil for an item in the root+ ::collections.schema/collection, which always answers false."+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/exists? :model/Collection :id collection-id))-(defn unarchived-collection-exists?- "Whether an unarchived Collection with `collection-id` exists."- [collection-id]+(mu/defn unarchived-collection-exists?+ "Whether an unarchived ::collections.schema/collection with `collection-id` exists."+ [collection-id :- ::lib.schema.id/collection](t2/exists? :model/Collection :id collection-id :archived false))-(defn remote-synced-collection-exists?- "Whether a remote-synced Collection with `collection-id` exists."- [collection-id]+(mu/defn remote-synced-collection-exists?+ "Whether a remote-synced ::collections.schema/collection with `collection-id` exists."+ [collection-id :- ::lib.schema.id/collection](t2/exists? :model/Collection :id collection-id :is_remote_synced true))-(defn personal-collection?- "Whether the Collection with `collection-id` is a personal Collection."- [collection-id]+(mu/defn personal-collection?+ "Whether the ::collections.schema/collection with `collection-id` is a personal ::collections.schema/collection."+ [collection-id :- ::lib.schema.id/collection](t2/exists? :model/Collection :id collection-id :personal_owner_id [:not= nil]))-(defn collection-remote-synced?- "Whether the Collection with `collection-id` is remote-synced."- [collection-id]+(mu/defn collection-remote-synced?+ "Whether the ::collections.schema/collection with `collection-id` is remote-synced, or nil if `collection-id` is nil (the root+ ::collections.schema/collection) or the ::collections.schema/collection does not exist."+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-one-fn :is_remote_synced :model/Collection :id collection-id))-(defn collection-namespace- "The namespace of the Collection with `collection-id`."- [collection-id]+(mu/defn collection-namespace+ "The namespace of the ::collections.schema/collection with `collection-id`."+ [collection-id :- ::lib.schema.id/collection](t2/select-one-fn :namespace :model/Collection :id collection-id))-(defn collection-location- "The location of the Collection with `collection-id`."- [collection-id]+(mu/defn collection-location+ "The location of the ::collections.schema/collection with `collection-id`."+ [collection-id :- ::lib.schema.id/collection](t2/select-one-fn :location :model/Collection :id collection-id))-(defn root-collection-type-by-id- "The type of the top-level Collection with `collection-id`, or nil if it is not top-level."- [collection-id]+(mu/defn root-collection-type-by-id+ "The type of the top-level ::collections.schema/collection with `collection-id`, or nil if it is not top-level."+ [collection-id :- ::lib.schema.id/collection](t2/select-one-fn :type :model/Collection :id collection-id :location "/"))-;;; ---------------------------------------------- Collection sets ----------------------------------------------+;;; ---------------------------------------------- ::collections.schema/collection sets -----------------------------------------------(defn collections-by-id- "A map of ID to Collection for `collection-ids`."- [collection-ids]+(mu/defn collections-by-id+ "A map of ID to ::collections.schema/collection for `collection-ids`."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pk->fn identity :model/Collection :id [:in collection-ids]))-(defn collection-columns-by-id+(mu/defn collection-columns-by-id"A map of ID to the `columns` of the Collections with `collection-ids`."- [columns collection-ids]+ [columns :- [:sequential :keyword]+ collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pk->fn identity (into [:model/Collection] columns) :id [:in collection-ids]))-(defn collection-archived-flags+(mu/defn collection-archived-flags"A map of ID to `:archived` for `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pk->fn :archived :model/Collection :id [:in collection-ids]))-(defn collections-in-namespace+(mu/defn collections-in-namespace"The Collections in the namespace named `namespace-name`."- [namespace-name]+ [namespace-name :- :string](t2/select :model/Collection :namespace namespace-name))-(defn archived-collections-in-operations+(mu/defn archived-collections-in-operations"The archived Collections belonging to the archive operations with `archive-operation-ids`."- [archive-operation-ids]+ [archive-operation-ids :- [:sequential :string]](t2/select :model/Collection :archive_operation_id [:in archive-operation-ids] :archived true))-(defn ancestor-summaries+(mu/defn ancestor-summaries"The name, ID, and owner of the Collections with `collection-ids`, ordered by location."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select [:model/Collection :name :id :personal_owner_id] :id [:in collection-ids] {:order-by [:location]}))-(defn descendant-summaries- "The name, ID, location, and description of the descendant Collections of the Collection at+(mu/defn descendant-summaries+ "The name, ID, location, and description of the descendant Collections of the ::collections.schema/collection at`children-location-prefix` (see `metabase.collections.models.collection/children-location`), excluding otherusers' Personal Collections (Personal Collections owned by `current-user-id` are still included). When`archived?` is given (true or false, not nil), further restricted to that archived status.- `additional-where-clauses` are ANDed in as-is; used by callers that need a permission-filter builder like- `visible-collection-filter-clause`, which lives in `metabase.collections.models.collection` and so can't be- called from here without a require cycle (that namespace already requires this one)."- [children-location-prefix current-user-id archived? additional-where-clauses]+ `additional-where-clauses` are ANDed in as-is (nil, from a caller with none to add, is treated as empty); used+ by callers that need a permission-filter builder like `visible-collection-filter-clause`, which lives in+ `metabase.collections.models.collection` and so can't be called from here without a require cycle (that+ namespace already requires this one)."+ [children-location-prefix :- :string+ current-user-id :- [:maybe ::lib.schema.id/user]+ archived? :- [:maybe :boolean]+ additional-where-clauses :- [:maybe [:sequential vector?]]](t2/select [:model/Collection :name :id :location :description]{:where (into [:and[:like :location (str children-location-prefix "%")]@@ -124,72 +145,78 @@[:= :archived archived?])]additional-where-clauses)}))-(defn descendant-summaries-with-type+(mu/defn descendant-summaries-with-type"The name, ID, location, description, and type of the Collections directly under any of `location-prefixes`(compared with SQL `LIKE`), excluding personal Collections that don't belong to `current-user-id`."- [location-prefixes current-user-id]+ [location-prefixes :- [:sequential :string]+ current-user-id :- [:maybe ::lib.schema.id/user]](t2/select [:model/Collection :name :id :location :description :type]{:where [:and(into [:or] (map (fn [prefix] [:like :location prefix])) location-prefixes)[:or [:= :personal_owner_id nil] [:= :personal_owner_id current-user-id]]]}))-(defn effective-children-where+(mu/defn effective-children-where"The ID, name, description, and type of the Collections matching the Honey SQL `where`."- [where]+ [where :- [:maybe vector?]](t2/select [:model/Collection :id :name :description :type] {:where where}))-(defn collections-for-serdes-reducible+(mu/defn collections-for-serdes-reducible"Reducible Collections matching the Honey SQL `where` in stable storage order."- [where]+ [where :- [:maybe vector?]](t2/reducible-select :model/Collection {:where where, :order-by serdes/stable-storage-order}))-(defn collection-count-by-ids+(mu/defn collection-count-by-ids"The number of Collections among `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/count :model/Collection :id [:in collection-ids]))-(defn collection-count-of-types+(mu/defn collection-count-of-types"The number of Collections with `collection-id` whose type is one of `types`."- [collection-id types]+ [collection-id :- ::lib.schema.id/collection+ types :- [:sequential :string]](t2/count :model/Collection :id collection-id :type [:in types]))-(defn remote-synced-collection-count+(mu/defn remote-synced-collection-count"The number of remote-synced Collections."[](t2/count :model/Collection :is_remote_synced true))-(defn collection-ids-with-location-like+(mu/defn collection-ids-with-location-like"The IDs of the Collections whose location matches the SQL `pattern`."- [pattern]+ [pattern :- :string](t2/select-pks-set :model/Collection :location [:like pattern]))-(defn unarchived-collection-ids-with-location-like+(mu/defn unarchived-collection-ids-with-location-like"The IDs of the unarchived Collections whose location matches the SQL `pattern`."- [pattern]+ [pattern :- :string](t2/select-pks-set :model/Collection :location [:like pattern] :archived false))-(defn not-yet-archived-collection-ids-with-location-like+(mu/defn not-yet-archived-collection-ids-with-location-like"The IDs of the Collections whose location matches the SQL `pattern` and that are not marked archived."- [pattern]+ [pattern :- :string](t2/select-pks-set :model/Collection :location [:like pattern] :archived [:not= true]))-(defn archived-collection-ids-in-operation-with-location-like+(mu/defn archived-collection-ids-in-operation-with-location-like"The IDs of the archived Collections of the archive operation with `archive-operation-id` whose location matchesthe SQL `pattern`."- [pattern archive-operation-id]+ [pattern :- :string+ archive-operation-id :- :string](t2/select-pks-set :model/Collection:location [:like pattern]:archive_operation_id [:= archive-operation-id]:archived [:= true]))-(defn collection-ids-of-type+(mu/defn collection-ids-of-type"The IDs of the Collections among `collection-ids` of `type`."- [collection-ids type]+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ type :- :string](t2/select-pks-set :model/Collection :id [:in collection-ids] :type type))-(defn child-collection-ids+(mu/defn child-collection-ids"The IDs of the non-trash Collections directly at `location`, excluding archived ones when `skip-archived?`."- [location trash-type skip-archived?]+ [location :- :string+ trash-type :- :string+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Collection{:where [:and[:= :location location]@@ -198,44 +225,46 @@[:not= :type trash-type][:= :type nil]]]}))-(defn remote-synced-root-collection-ids+(mu/defn remote-synced-root-collection-ids"The IDs of the top-level remote-synced Collections."[](t2/select-pks-set :model/Collection {:where [:and[:= :is_remote_synced true][:= :location "/"]]}))-(defn personal-collection-ids- "The IDs of every personal Collection."+(mu/defn personal-collection-ids+ "The IDs of every personal ::collections.schema/collection."[](t2/select-pks-set :model/Collection :personal_owner_id [:not= nil]))-(defn personal-collection-ids-by-owner- "A map of owner User ID to personal Collection ID for `user-ids`."- [user-ids]+(mu/defn personal-collection-ids-by-owner+ "A map of owner User ID to personal ::collections.schema/collection ID for `user-ids`."+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-fn->pk :personal_owner_id :model/Collection :personal_owner_id [:in user-ids]))-;;; ---------------------------------------------- Collection writes ----------------------------------------------+;;; ---------------------------------------------- ::collections.schema/collection writes -----------------------------------------------(defn insert-collection!+(mu/defn insert-collection!"Insert `collection` and return the new instance."- [collection]+ [collection :- (mut/select-keys ::collections.schema/collection.update [:name :description :archived :location :personal_owner_id :slug :namespace :authority_level :entity_id :created_at :type :is_sample :archive_operation_id :archived_directly :is_remote_synced])](t2/insert-returning-instance! :model/Collection collection))-(defn update-collection!- "Apply `changes` to the Collection with `collection-id`."- [collection-id changes]+(mu/defn update-collection!+ "Apply `changes` to the ::collections.schema/collection with `collection-id`, returning the number updated."+ [collection-id :- ::lib.schema.id/collection+ changes :- (mut/select-keys ::collections.schema/collection.update [:name :description :archived :location :personal_owner_id :slug :namespace :authority_level :entity_id :created_at :type :is_sample :archive_operation_id :archived_directly :is_remote_synced])](t2/update! :model/Collection collection-id changes))-(defn clear-remote-synced-flags!- "Mark every remote-synced Collection as not remote-synced."+(mu/defn clear-remote-synced-flags!+ "Mark every remote-synced ::collections.schema/collection as not remote-synced, returning the number updated."[](t2/update! :model/Collection :is_remote_synced true {:is_remote_synced false}))-(defn archive-descendant-collections!+(mu/defn archive-descendant-collections!"Archive, as part of the archive operation with `archive-operation-id`, the unarchived Collections whose location- matches the SQL `pattern`."- [pattern archive-operation-id]+ matches the SQL `pattern`, returning the number updated."+ [pattern :- :string+ archive-operation-id :- :string](t2/query-one {:update :collection:set {:archive_operation_id archive-operation-id:archived_directly false@@ -244,10 +273,14 @@[:like :location pattern][:not :archived]]}))-(defn unarchive-descendant-collections!+(mu/defn unarchive-descendant-collections!"Unarchive the Collections of the archive operation with `archive-operation-id` that were not archived directly,- moving them from `orig-children-location` to `new-children-location` and setting `remote-synced?`."- [orig-children-location new-children-location remote-synced? archive-operation-id]+ moving them from `orig-children-location` to `new-children-location` and setting `remote-synced?`, returning the+ number updated."+ [orig-children-location :- :string+ new-children-location :- :string+ remote-synced? :- :boolean+ archive-operation-id :- :string](t2/query-one {:update :collection:set {:location [:replace :location orig-children-location new-children-location]:is_remote_synced remote-synced?@@ -259,108 +292,129 @@[:= :archive_operation_id archive-operation-id][:not= :archived_directly true]]}))-(defn move-descendant-collections!- "Move the Collections under `orig-children-location` to `new-children-location` and set `remote-synced?`."- [orig-children-location new-children-location remote-synced?]+(mu/defn move-descendant-collections!+ "Move the Collections under `orig-children-location` to `new-children-location` and set `remote-synced?`,+ returning the number updated."+ [orig-children-location :- :string+ new-children-location :- :string+ remote-synced? :- :boolean](t2/query-one {:update :collection:set {:location [:replace :location orig-children-location new-children-location]:is_remote_synced remote-synced?}:where [:like :location (str orig-children-location "%")]}))-(defn delete-collections-at-location!- "Delete the Collections directly at `location`."- [location]+(mu/defn delete-collections-at-location!+ "Delete the Collections directly at `location`, returning the number deleted."+ [location :- :string](t2/delete! :model/Collection :location location))-;;; ---------------------------------------------- Collection contents ----------------------------------------------+;;; ---------------------------------------------- ::collections.schema/collection contents -----------------------------------------------(defn instances-with-columns+(mu/defn instances-with-columns"The `columns` of the instances with `ids`."- [columns ids]+ [columns :- [:sequential :keyword]+ ids :- [:set ms/PositiveInt]](t2/select columns :id [:in ids]))-(defn instance-by-id+(mu/defn instance-by-id"The instance of `model` with `id`, or nil."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one model :id id))-(defn collection-namespaces-of- "A map of ID to the namespace of the Collection holding each instance of `model` with `ids`."- [model ids]+(mu/defn collection-namespaces-of+ "A map of ID to the namespace of the ::collections.schema/collection holding each instance of `model` with `ids`."+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :namespace [model :id [:c.namespace :namespace]]{:where [:in (keyword (str (name (t2/table-name model)) ".id")) ids]:join [[:collection :c] [:= :collection_id :c.id]]}))-(defn set-pulse-archived-in-collections!- "Set `archived?` on the Pulses in the Collections with `collection-ids`."- [collection-ids archived?]+(mu/defn set-pulse-archived-in-collections!+ "Set `archived?` on the Pulses in the Collections with `collection-ids`, returning the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Pulse {:collection_id [:in collection-ids]} {:archived archived?}))-(defn set-native-query-snippet-archived-in-collections!- "Set `archived?` on the NativeQuerySnippets in the Collections with `collection-ids`."- [collection-ids archived?]+(mu/defn set-native-query-snippet-archived-in-collections!+ "Set `archived?` on the NativeQuerySnippets in the Collections with `collection-ids`, returning the number+ updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/NativeQuerySnippet {:collection_id [:in collection-ids]} {:archived archived?}))-(defn set-timeline-archived-in-collections!- "Set `archived?` on the Timelines in the Collections with `collection-ids`."- [collection-ids archived?]+(mu/defn set-timeline-archived-in-collections!+ "Set `archived?` on the Timelines in the Collections with `collection-ids`, returning the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Timeline {:collection_id [:in collection-ids]} {:archived archived?}))-(defn set-card-archived-in-collections-not-directly!- "Set `archived?` on the Cards in the Collections with `collection-ids` that were not archived directly."- [collection-ids archived?]+(mu/defn set-card-archived-in-collections-not-directly!+ "Set `archived?` on the Cards in the Collections with `collection-ids` that were not archived directly, returning+ the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Card {:collection_id [:in collection-ids], :archived_directly false} {:archived archived?}))-(defn set-dashboard-archived-in-collections-not-directly!- "Set `archived?` on the Dashboards in the Collections with `collection-ids` that were not archived directly."- [collection-ids archived?]+(mu/defn set-dashboard-archived-in-collections-not-directly!+ "Set `archived?` on the Dashboards in the Collections with `collection-ids` that were not archived directly,+ returning the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Dashboard {:collection_id [:in collection-ids], :archived_directly false} {:archived archived?}))-(defn set-document-archived-in-collections-not-directly!- "Set `archived?` on the Documents in the Collections with `collection-ids` that were not archived directly."- [collection-ids archived?]+(mu/defn set-document-archived-in-collections-not-directly!+ "Set `archived?` on the Documents in the Collections with `collection-ids` that were not archived directly,+ returning the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Document {:collection_id [:in collection-ids], :archived_directly false} {:archived archived?}))-(defn set-exploration-archived-in-collections-not-directly!- "Set `archived?` on the Explorations in the Collections with `collection-ids` that were not archived directly."- [collection-ids archived?]+(mu/defn set-exploration-archived-in-collections-not-directly!+ "Set `archived?` on the Explorations in the Collections with `collection-ids` that were not archived directly,+ returning the number updated."+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ archived? :- :boolean](t2/update! :model/Exploration {:collection_id [:in collection-ids], :archived_directly false} {:archived archived?}))-(defn delete-cards-in-collections!- "Delete the Cards in the Collections with `collection-ids`."- [collection-ids]+(mu/defn delete-cards-in-collections!+ "Delete the Cards in the Collections with `collection-ids`, returning the number deleted."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/delete! :model/Card :collection_id [:in collection-ids]))-(defn delete-dashboards-in-collections!- "Delete the Dashboards in the Collections with `collection-ids`."- [collection-ids]+(mu/defn delete-dashboards-in-collections!+ "Delete the Dashboards in the Collections with `collection-ids`, returning the number deleted."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/delete! :model/Dashboard :collection_id [:in collection-ids]))-(defn delete-native-query-snippets-in-collections!- "Delete the NativeQuerySnippets in the Collections with `collection-ids`."- [collection-ids]+(mu/defn delete-native-query-snippets-in-collections!+ "Delete the NativeQuerySnippets in the Collections with `collection-ids`, returning the number deleted."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/delete! :model/NativeQuerySnippet :collection_id [:in collection-ids]))-(defn delete-pulses-in-collections!- "Delete the Pulses in the Collections with `collection-ids`."- [collection-ids]+(mu/defn delete-pulses-in-collections!+ "Delete the Pulses in the Collections with `collection-ids`, returning the number deleted."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/delete! :model/Pulse :collection_id [:in collection-ids]))-(defn delete-timelines-in-collections!- "Delete the Timelines in the Collections with `collection-ids`."- [collection-ids]+(mu/defn delete-timelines-in-collections!+ "Delete the Timelines in the Collections with `collection-ids`, returning the number deleted."+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/delete! :model/Timeline :collection_id [:in collection-ids]))-(defn dashboard-ids-in-collection- "The IDs of the Dashboards in the Collection with `collection-id`, excluding archived ones when `skip-archived?`."- [collection-id skip-archived?]+(mu/defn dashboard-ids-in-collection+ "The IDs of the Dashboards in the ::collections.schema/collection with `collection-id` (nil for the root ::collections.schema/collection), excluding archived+ ones when `skip-archived?`."+ [collection-id :- [:maybe ::lib.schema.id/collection]+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Dashboard{:where [:and [:= :collection_id collection-id] (when skip-archived? [:not :archived])]}))-(defn card-ids-in-collection- "The IDs of the Cards in the Collection with `collection-id`, excluding archived ones when `skip-archived?` and+(mu/defn card-ids-in-collection+ "The IDs of the Cards in the ::collections.schema/collection with `collection-id`, excluding archived ones when `skip-archived?` andexcluding Cards materialized by an exploration Summary."- [collection-id skip-archived?]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Card {:where [:and[:= :collection_id collection-id](when skip-archived? [:not :archived])@@ -371,48 +425,52 @@:from [:document]:where [:= :exploration_id nil]}]]]}))-(defn document-ids-in-collection- "The IDs of the non-exploration Documents in the Collection with `collection-id`, excluding archived ones when+(mu/defn document-ids-in-collection+ "The IDs of the non-exploration Documents in the ::collections.schema/collection with `collection-id`, excluding archived ones when`skip-archived?`."- [collection-id skip-archived?]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Document {:where [:and[:= :collection_id collection-id][:= :exploration_id nil](when skip-archived? [:not :archived])]}))-(defn timeline-ids-in-collection- "The IDs of the Timelines in the Collection with `collection-id`, excluding archived ones when `skip-archived?`."- [collection-id skip-archived?]+(mu/defn timeline-ids-in-collection+ "The IDs of the Timelines in the ::collections.schema/collection with `collection-id`, excluding archived ones when `skip-archived?`."+ [collection-id :- [:maybe ::lib.schema.id/collection]+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Timeline{:where [:and [:= :collection_id collection-id] (when skip-archived? [:not :archived])]}))-(defn published-table-ids-in-collection- "The IDs of the published Tables in the Collection with `collection-id`, excluding archived ones when+(mu/defn published-table-ids-in-collection+ "The IDs of the published Tables in the ::collections.schema/collection with `collection-id`, excluding archived ones when`skip-archived?`."- [collection-id skip-archived?]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Table {:where [:and[:= :collection_id collection-id][:= :is_published true](when skip-archived? [:= :archived_at nil])]}))-(defn transform-ids-in-collection- "The IDs of the Transforms in the Collection with `collection-id`."- [collection-id]+(mu/defn transform-ids-in-collection+ "The IDs of the Transforms in the ::collections.schema/collection with `collection-id`."+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-pks-set :model/Transform {:where [:= :collection_id collection-id]}))-(defn published-table-ids-in-collections+(mu/defn published-table-ids-in-collections"The IDs of the published Tables in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]](t2/select-pks-set :model/Table :collection_id [:in collection-ids] :is_published true))-(defn unpublish-tables-in-collections!- "Unpublish the Tables in the Collections with `collection-ids` and detach them from their Collection."- [collection-ids]+(mu/defn unpublish-tables-in-collections!+ "Unpublish the Tables in the Collections with `collection-ids` and detach them from their ::collections.schema/collection, returning+ the number updated."+ [collection-ids :- [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]](t2/update! :model/Table {:collection_id [:in collection-ids]} {:collection_id nil, :is_published false}))-(defn dashboard-ids-with-cards+(mu/defn dashboard-ids-with-cards"The `:dashboard_id` rows of the Dashboards among `dashboard-ids` holding an unarchived dashboard question."- [dashboard-ids]+ [dashboard-ids :- [:set ::lib.schema.id/dashboard]](t2/query {:select-distinct [:dashboard_id]:from :report_card:where [:and@@ -426,10 +484,11 @@;;; ------------------------------------------------ Permissions -------------------------------------------------(defn user-has-root-collection-permission?+(mu/defn user-has-root-collection-permission?"Whether the User with `user-id` belongs to a group holding a Permissions row for one of the root collection`objects`."- [user-id objects]+ [user-id :- ::lib.schema.id/user+ objects :- [:sequential :string]](t2/exists? :model/Permissions {:select [:p.*]:from [[:permissions :p]]:join [[:permissions_group :pg] [:= :pg.id :p.group_id]@@ -438,42 +497,42 @@[:= :pgm.user_id user-id][:in :p.object objects]]}))-(defn group-ids-with-permission-object+(mu/defn group-ids-with-permission-object"The set of group IDs holding a Permissions row for `object`."- [object]+ [object :- :string](t2/select-fn-set :group_id :model/Permissions :object object))-(defn insert-permissions!- "Insert the Permissions `rows`."- [rows]+(mu/defn insert-permissions!+ "Insert the Permissions `rows`, returning the number inserted."+ [rows :- [:sequential PermissionsRow]](t2/insert! :model/Permissions rows))-(defn delete-permissions-for-collection!- "Delete the Permissions rows attached to the Collection with `collection-id`."- [collection-id]+(mu/defn delete-permissions-for-collection!+ "Delete the Permissions rows attached to the ::collections.schema/collection with `collection-id`, returning the number deleted."+ [collection-id :- ::lib.schema.id/collection](t2/delete! :model/Permissions :collection_id collection-id))-(defn delete-permissions-with-objects!- "Delete the Permissions rows for `objects`."- [objects]+(mu/defn delete-permissions-with-objects!+ "Delete the Permissions rows for `objects`, returning the number deleted."+ [objects :- [:sequential :string]](t2/query-one {:delete-from :permissions:where [:in :object objects]}));;; ---------------------------------------------------- Users -----------------------------------------------------(defn user-type+(mu/defn user-type"The type of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :type :model/User user-id))-(defn user-name-parts-by-id+(mu/defn user-name-parts-by-id"A map of ID to the first name, last name, and email of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :first_name :last_name :email :id] :id [:in user-ids]))-(defn non-api-key-user-ids+(mu/defn non-api-key-user-ids"The IDs of the Users among `user-ids` that are not API key users."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pks-set :model/User :id [:in user-ids] :type [:not= :api-key]));;; -------------------------------------------------- Hydration --------------------------------------------------diff --git a/src/metabase/collections/schema.clj b/src/metabase/collections/schema.cljindex 5d962f27db4..71525758310 100644--- a/src/metabase/collections/schema.clj+++ b/src/metabase/collections/schema.clj@@ -1,5 +1,6 @@(ns metabase.collections.schema(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]))@@ -92,3 +93,42 @@[:effective_location {:optional true} :string][:authority_level {:optional true} [:maybe :string]][:dashboard_count {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::collection+ "A Collection as selected from the app DB: every column of `:collection`."+ [:map {:closed true}+ [:id ::lib.schema.id/collection]+ [:name :string]+ [:description [:maybe :string]]+ [:archived :boolean]+ [:location :string]+ [:personal_owner_id [:maybe ::lib.schema.id/user]]+ [:slug :string]+ [:namespace [:maybe [:or :keyword :string]]]+ [:authority_level [:maybe [:or :keyword :string]]]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:type [:maybe [:or :keyword :string]]]+ [:is_sample :boolean]+ [:archive_operation_id [:maybe :string]]+ [:archived_directly [:maybe :boolean]]+ [:is_remote_synced [:maybe :boolean]]])++(mr/def ::collection.update+ "What an update (or insert) of a Collection accepts: every column of `:collection` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:archived {:optional true} [:maybe :boolean]]+ [:location {:optional true} [:maybe :string]]+ [:personal_owner_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:slug {:optional true} [:maybe :string]]+ [:namespace {:optional true} [:maybe [:or :keyword :string]]]+ [:authority_level {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:is_sample {:optional true} [:maybe :boolean]]+ [:archive_operation_id {:optional true} [:maybe :string]]+ [:archived_directly {:optional true} [:maybe :boolean]]+ [:is_remote_synced {:optional true} [:maybe :boolean]]])diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.cljindex ca53d356797..5de8599972c 100644--- a/src/metabase/collections_rest/api.clj+++ b/src/metabase/collections_rest/api.clj@@ -4,15 +4,13 @@`:snippet` namespace, ('Snippet folders' in the UI). These namespaces are independent hierarchies. To use theseendpoints for other Collections namespaces, you can pass the `?namespace=` parameter (e.g., `?namespace=snippet`)."(:require- [clojure.string :as str]- [honey.sql.helpers :as sql.helpers][malli.core :as mc][malli.transform :as mtx][malli.util][medley.core :as m][metabase.api.common :as api][metabase.api.macros :as api.macros]- [metabase.app-db.core :as mdb]+ [metabase.collections-rest.children-query :as children-query][metabase.collections-rest.db :as collections-rest.db][metabase.collections.core :as collections][metabase.collections.models.collection :as collection]@@ -24,16 +22,14 @@[metabase.models.interface :as mi][metabase.notification.core :as notification][metabase.permissions.core :as perms]- [metabase.premium-features.core :as premium-features :refer [defenterprise]]+ [metabase.premium-features.core :as premium-features][metabase.queries.core :as queries][metabase.request.core :as request][metabase.revisions.core :as revisions][metabase.tracing.core :as tracing][metabase.transforms.feature-gating :as transforms.gating]- [metabase.transforms.util :as transforms.u][metabase.upload.core :as upload][metabase.util :as u]- [metabase.util.honey-sql-2 :as h2x][metabase.util.i18n :refer [tru]][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -238,125 +234,13 @@;;; --------------------------------- Fetching a single Collection & its 'children' -----------------------------------(def ^:private valid-model-param-values- "Valid values for the `?model=` param accepted by endpoints in this namespace.- `no_models` is for nilling out the set because a nil model set is actually the total model set"- #{"card" ; SavedQuestion- "dataset" ; Model. TODO : update this- "document"- "exploration"- "metric"- "collection"- "dashboard"- "pulse" ; I think the only kinds of Pulses we still have are Alerts?- "snippet"- "no_models"- "timeline"- "table"- "transform"- "measure"})-(def ^:private ModelString- (into [:enum] valid-model-param-values))+ (into [:enum] children-query/valid-model-param-values))(def ^:private Models"This is basically a union type. [[api.macros/defendpoint]] splits the string if it only gets one."[:vector {:decode/string (fn [x] (cond (vector? x) x x [x]))} ModelString])-(def ^:private valid-pinned-state-values- "Valid values for the `?pinned_state` param accepted by endpoints in this namespace."- #{"all" "is_pinned" "is_not_pinned"})--(def ^:private valid-sort-columns #{"name" "last_edited_at" "last_edited_by" "model" "description"})-(def ^:private valid-sort-directions #{"asc" "desc"})-(defn- normalize-sort-choice [w] (when w (keyword (str/replace w #"_" "-"))))--(def ^:private CollectionType- "Collection types that the root/items endpoint can filter on"- [:enum "remote-synced"])--(def ^:private CollectionChildrenOptions- [:map- [:show-dashboard-questions? :boolean]- [:show-exploration-documents? :boolean]- [:collection-type {:optional true} [:maybe CollectionType]]- [:archived? :boolean]- [:include-library? {:optional true} [:maybe :boolean]]- [:pinned-state {:optional true} [:maybe (into [:enum] (map keyword) valid-pinned-state-values)]]- ;; when specified, only return results of this type.- [:models {:optional true} [:maybe [:set (into [:enum] (map keyword) valid-model-param-values)]]]- [:search-text {:optional true} [:maybe :string]]- [:sort-info {:optional true} [:maybe [:map- [:sort-column (into [:enum {:error/message "sort-columns"}]- (map normalize-sort-choice)- valid-sort-columns)]- [:sort-direction (into [:enum {:error/message "sort-direction"}]- (map normalize-sort-choice)- valid-sort-directions)]- [:official-collections-first? {:optional true} :boolean]]]]])--(defmulti ^:private collection-children-query- "Query that will fetch the 'children' of a `collection`, for different types of objects. Possible options are listed- in the [[CollectionChildrenOptions]] schema above.-- NOTES:-- * `collection` will be either a CollectionInstance, or the Root Collection special placeholder object, so do not use- `u/the-id` on it! Use `:id`, which will return `nil` for the Root Collection, which is exactly what we want.-- * These queries will be combined into a union-all query. You do not need to put all of the columns into the query,- any you don't select will be added in the correct position so the union will work (see `all-select-columns` for more- details)."- {:arglists '([model collection options])}- (fn [model _ _] (keyword model)))--;;; TODO -- in Postgres and H2 at least I think we could just do `true` or `false` here... not sure about MySQL.--(def ^:private always-true-hsql-expr- "A Honey SQL expression that is always true.-- 1 = 1"- [:= [:inline 1] [:inline 1]])--(def ^:private always-false-hsql-expr- "A Honey SQL expression that is never true.-- 1 = 2"- [:= [:inline 1] [:inline 2]])--(defn- search-text-clause- "Match every token in `search-text` against an item's name or last editor's first or last name."- [search-text]- (when-not (str/blank? search-text)- (when-let [tokens (->> (str/split (u/lower-case-en (str/trim search-text)) #"\s+")- (filter seq)- not-empty)]- (into [:and]- (for [token tokens- :let [pattern (h2x/like-substring token)]]- [:or- [:like [:lower :name] pattern]- [:like [:lower :last_edit_first_name] pattern]- [:like [:lower :last_edit_last_name] pattern]])))))--(defn- pinned-state->clause- ([pinned-state]- (pinned-state->clause pinned-state :collection_position))- ([pinned-state col]- (case pinned-state- :all always-true-hsql-expr- :is_pinned [:<> col nil]- :is_not_pinned [:= col nil]- always-true-hsql-expr)))--(defn- poison-when-pinned-clause- "Poison a query to return no results when filtering to pinned items. Use for items that do not have a notion of- pinning so that no results return when asking for pinned items."- [pinned-state]- (if (= pinned-state :is_pinned)- always-false-hsql-expr- always-true-hsql-expr))-(defmulti ^:private post-process-collection-children{:arglists '([model options collection rows])}(fn [model _ _ _]@@ -379,42 +263,6 @@(update :archived api/bit->boolean)(update :archived_directly api/bit->boolean))) :can_write :can_restore :can_delete :is_remote_synced :collection_namespace))))-(defmethod collection-children-query :document- [_ collection {:keys [archived? pinned-state show-exploration-documents?]}]- (-> {:select [:document.id- :document.name- :document.collection_id- :document.collection_position- :document.archived- :document.archived_directly- :document.exploration_id- [:u.id :last_edit_user]- [:u.email :last_edit_email]- [:u.first_name :last_edit_first_name]- [:u.last_name :last_edit_last_name]- [:r.timestamp :last_edit_timestamp]- [(h2x/literal "document") :model]]- :from [[:document :document]]- :left-join [[:revision :r] [:and- [:= :r.model_id :document.id]- [:= :r.most_recent true]- [:= :r.model (h2x/literal "Document")]]- [:core_user :u] [:= :u.id :r.user_id]]- :where [:and- (collection/visible-collection-filter-clause :document.collection_id {:cte-name :visible_collection_ids})- (if (collection/is-trash? collection)- [:= :document.archived_directly true]- [:and- [:= :document.collection_id (:id collection)]- [:= :document.archived_directly false]])- [:= :document.archived (boolean archived?)]- ;; Hide exploration-attached documents - similar to Dashboard Questions, they're not visible in the- ;; collection, but only through the Exploration they're in. Callers that want them (e.g. the- ;; Trash view, embedding SDK) pass show-exploration-documents? to opt in.- (when-not show-exploration-documents?- [:= :document.exploration_id nil])]}- (sql.helpers/where (pinned-state->clause pinned-state :document.collection_position))))-(defmethod ^:private post-process-collection-children :exploration[_ _ collection rows](t2/hydrate (for [exploration rows]@@ -425,89 +273,6 @@(update :archived api/bit->boolean)(update :archived_directly api/bit->boolean))) :can_write :can_restore :can_delete))-(def ^:private exploration-recent-edits-subquery- ;; Per-exploration latest edit, unioning the Exploration's own metadata revisions with- ;; revisions of its Summary Document. `rn = 1` picks the winner.- ;; The Exploration row is mostly inert post-creation; the meat of editing happens in- ;; the attached Document, so "Last edited" must reflect both sources.- ^:allow-subquery- {:select [:exploration_id- :timestamp- :user_id- [[:over [[:row_number] ^:allow-subquery {:partition-by [:exploration_id]- :order-by [[:timestamp :desc]]}]] :rn]]- :from [[^:allow-subquery {:union-all- [^:allow-subquery- {:select [[:r.model_id :exploration_id]- [:r.timestamp :timestamp]- [:r.user_id :user_id]]- :from [[:revision :r]]- :where [:and- [:= :r.model (h2x/literal "Exploration")]- [:= :r.most_recent true]]}- ^:allow-subquery- {:select [[:d.exploration_id :exploration_id]- [:r.timestamp :timestamp]- [:r.user_id :user_id]]- :from [[:revision :r]]- :join [[:document :d] [:= :d.id :r.model_id]]- :where [:and- [:= :r.model (h2x/literal "Document")]- [:= :r.most_recent true]- [:not= :d.exploration_id nil]]}]}- :all_edits]]})--(defmethod collection-children-query :exploration- [_ collection {:keys [archived? pinned-state]}]- (-> {:select [:exploration.id- :exploration.name- :exploration.description- :exploration.entity_id- :exploration.collection_id- :exploration.collection_position- :exploration.archived- :exploration.archived_directly- [:u.id :last_edit_user]- [:u.email :last_edit_email]- [:u.first_name :last_edit_first_name]- [:u.last_name :last_edit_last_name]- [:ere.timestamp :last_edit_timestamp]- [(h2x/literal "exploration") :model]]- :from [[:exploration :exploration]]- :left-join [[exploration-recent-edits-subquery :ere]- [:and- [:= :ere.exploration_id :exploration.id]- [:= :ere.rn [:inline 1]]]- [:core_user :u] [:= :u.id :ere.user_id]]- :where [:and- (collection/visible-collection-filter-clause :exploration.collection_id {:cte-name :visible_collection_ids})- (if (collection/is-trash? collection)- [:= :exploration.archived_directly true]- [:and- [:= :exploration.collection_id (:id collection)]- [:= :exploration.archived_directly false]])- [:= :exploration.archived (boolean archived?)]]}- (sql.helpers/where (pinned-state->clause pinned-state :exploration.collection_position))))--(defmethod collection-children-query :pulse- [_ collection {:keys [archived? pinned-state]}]- (-> {:select-distinct [:p.id- :p.name- :p.entity_id- :p.collection_position- :p.collection_id- [(h2x/literal "pulse") :model]]- :from [[:pulse :p]]- :left-join [[:pulse_card :pc] [:= :p.id :pc.pulse_id]]- :where [:and- [:= :p.collection_id (:id collection)]- [:= :p.archived (boolean archived?)]- ;; exclude alerts- [:= :p.alert_condition nil]- ;; exclude dashboard subscriptions- [:= :p.dashboard_id nil]]}- (sql.helpers/where (pinned-state->clause pinned-state :p.collection_position))))-(defmethod post-process-collection-children :pulse[_ _ _ rows](for [row rows]@@ -515,42 +280,6 @@:description :display :authority_level :moderated_status :icon :personal_owner_id :namespace:collection_preview :dataset_query :table_id :query_type :is_upload :collection_namespace)))-(defenterprise snippets-collection-children-query- "Collection children query for snippets on OSS. Returns all snippets regardless of collection, because snippet- collections are an EE feature."- metabase-enterprise.snippet-collections.api.native-query-snippet- [_collection {:keys [archived?]}]- {:select [:id :name :entity_id [(h2x/literal "snippet") :model]]- :from [[:native_query_snippet :nqs]]- :where [:= :archived (boolean archived?)]})--(defmethod collection-children-query :snippet- [_model collection options]- (snippets-collection-children-query collection options))--(defmethod collection-children-query :timeline- [_ collection {:keys [archived? pinned-state]}]- {:select [:id :collection_id :name [(h2x/literal "timeline") :model] :description :entity_id :icon]- :from [[:timeline :timeline]]- :where [:and- (poison-when-pinned-clause pinned-state)- [:= :collection_id (:id collection)]- [:= :archived (boolean archived?)]]})--(defmethod collection-children-query :transform- [_model collection {:keys [pinned-state]}]- (let [enabled-types (transforms.u/enabled-source-types-for-user)]- {:select [:id :collection_id :name [(h2x/literal "transform") :model] :description :entity_id]- :from [[:transform :transform]]- :where [:and- (poison-when-pinned-clause pinned-state)- [:= :collection_id (:id collection)]- (if (seq enabled-types)- [:in :source_type enabled-types]- [:=- [:inline 0]- [:inline 1]])]}))-(defmethod post-process-collection-children :timeline[_ _options _collection rows](for [row rows]@@ -567,74 +296,6 @@:dataset_query :table_id :query_type :is_upload :namespace)(assoc :collection_namespace "snippets"))))-(defn- card-query [card-type collection {:keys [archived? pinned-state show-dashboard-questions?]}]- (-> {:select (cond->- [:c.id :c.name :c.description :c.entity_id :c.collection_position :c.display :c.collection_preview- :dashboard_id- :last_used_at- :c.collection_id- :c.archived_directly- :c.archived- :c.dataset_query- [(h2x/literal (case card-type- :model "dataset"- :metric "metric"- "card"))- :model]- [:u.id :last_edit_user]- [:u.email :last_edit_email]- [:u.first_name :last_edit_first_name]- [:u.last_name :last_edit_last_name]- [:r.timestamp :last_edit_timestamp]- [:mr.status :moderated_status]]- (#{:question :model} card-type)- (conj :c.database_id))- :from [[:report_card :c]]- :left-join [[:revision :r] [:and- [:= :r.model_id :c.id]- [:= :r.most_recent true]- [:= :r.model (h2x/literal "Card")]]- [:moderation_review :mr] [:and- [:= :mr.moderated_item_id :c.id]- [:= :mr.most_recent true]- [:= :mr.moderated_item_type (h2x/literal "card")]]- [:core_user :u] [:= :u.id :r.user_id]]- :where [:and- (collection/visible-collection-filter-clause :c.collection_id {:cte-name :visible_collection_ids})- (if (collection/is-trash? collection)- [:= :c.archived_directly true]- [:and- [:= :c.collection_id (:id collection)]- [:= :c.archived_directly false]])- (when-not show-dashboard-questions?- [:= :c.dashboard_id nil])- [:= :c.document_id nil]- [:= :c.archived (boolean archived?)]- (case card-type- :model- [:= :c.type (h2x/literal "model")]-- :metric- [:= :c.type (h2x/literal "metric")]-- [:= :c.type (h2x/literal "question")])]}- (cond-> (= :model card-type)- (-> (sql.helpers/select :c.table_id :t.is_upload :c.query_type)- (sql.helpers/left-join [:metabase_table :t] [:= :t.id :c.table_id])))- (sql.helpers/where (pinned-state->clause pinned-state))))--(defmethod collection-children-query :dataset- [_ collection options]- (card-query :model collection options))--(defmethod collection-children-query :metric- [_ collection options]- (card-query :metric collection options))--(defmethod collection-children-query :card- [_ collection options]- (card-query :question collection options))-(defn- post-process-card-row [row](-> (t2/instance :model/Card row);; `card-query` filters `[:= :c.document_id nil]` unconditionally, so every row here is known@@ -684,43 +345,6 @@[_ options _ rows](post-process-card-like (assoc options :hydrate-based-on-upload true) rows))-(defn- dashboard-query [collection {:keys [archived? pinned-state]}]- (-> {:select [:d.id :d.name :d.description :d.entity_id :d.collection_position- [:last_viewed_at :last_used_at]- :d.collection_id- :d.archived_directly- [(h2x/literal "dashboard") :model]- [:u.id :last_edit_user]- :d.archived- [:u.email :last_edit_email]- [:u.first_name :last_edit_first_name]- [:u.last_name :last_edit_last_name]- [:r.timestamp :last_edit_timestamp]- [:mr.status :moderated_status]]- :from [[:report_dashboard :d]]- :left-join [[:moderation_review :mr] [:and- [:= :mr.moderated_item_id :d.id]- [:= :mr.most_recent true]- [:= :mr.moderated_item_type (h2x/literal "dashboard")]]- [:revision :r] [:and- [:= :r.model_id :d.id]- [:= :r.most_recent true]- [:= :r.model (h2x/literal "Dashboard")]]- [:core_user :u] [:= :u.id :r.user_id]]- :where [:and- (collection/visible-collection-filter-clause :d.collection_id {:cte-name :visible_collection_ids})- (if (collection/is-trash? collection)- [:= :d.archived_directly true]- [:and- [:= :d.collection_id (:id collection)]- [:not= :d.archived_directly true]])- [:= :d.archived (boolean archived?)]]}- (sql.helpers/where (pinned-state->clause pinned-state))))--(defmethod collection-children-query :dashboard- [_ collection options]- (dashboard-query collection options))-(defn- post-process-dashboard [parent-collection dashboard](-> (t2/instance :model/Dashboard dashboard)(assoc :location (or (when parent-collection@@ -739,104 +363,6 @@collections/annotate-dashboards(map (partial post-process-dashboard parent-collection))))-(defenterprise snippets-collection-filter-clause- "Clause to filter out snippet collections from the collection query on OSS instances, and instances without the- snippet-collections. EE implementation returns `nil`, so as to not filter out snippet collections."- metabase-enterprise.snippet-collections.api.native-query-snippet- []- [:or- [:= :namespace nil]- [:not= :namespace (u/qualified-name "snippets")]])--(defn- collection-query- [collection {:keys [archived? collection-namespace pinned-state collection-type include-library?]}]- (-> (assoc- (collection/effective-children-query- collection- {:cte-name :visible_collection_ids}- [:and- (when collection-type- (if (= collection-type "remote-synced")- [:= :is_remote_synced true]- [:= :type collection-type]))- (when-not include-library?- [:or [:= nil :type]- [:not [:in :type [collection/library-collection-type- collection/library-metrics-collection-type- collection/library-data-collection-type]]]])- (if archived?- [:or- [:= :archived true]- [:= :id (collection/trash-collection-id)]]- [:and [:= :archived false] [:not= :id (collection/trash-collection-id)]])]- (perms/namespace-clause :namespace (u/qualified-name collection-namespace) (collection/is-trash? collection))- ;; never show tenant-specific root collections as children of another collection- [:or- [:= :type nil]- [:not= :type collection/tenant-specific-root-collection-type]]- (snippets-collection-filter-clause))- ;; We get from the effective-children-query a normal set of columns selected:- ;; want to make it fit the others to make UNION ALL work- :select [:id- [:id :collection_id]- :archived- :name- :description- :entity_id- :personal_owner_id- :location- :archived_directly- :namespace- ;; selected as `type` for compatibility with collection fns that expect it- :type- [[:case [:= :is_remote_synced nil] [:inline false] :else :is_remote_synced] :is_remote_synced]- ;; selected as `collection_type` for fast sorting on "when it's a collection, type"- [:type :collection_type]- [(h2x/literal "collection") :model]- :authority_level])- ;; the nil indicates that collections are never pinned.- (sql.helpers/where (pinned-state->clause pinned-state nil))))--(defmethod collection-children-query :collection- [_ collection options]- (collection-query collection options))--(defmethod collection-children-query :table- [_ collection {:keys [archived? pinned-state]}]- (let [user-info {:user-id api/*current-user-id*- :is-superuser? api/*is-superuser?*}- published-clause (perms/published-table-visible-clause :t.id user-info)- queryable-clause (cond-> [:or- [:in :t.id (perms/visible-table-filter-select- :id- user-info- {:perms/view-data :unrestricted- :perms/create-queries :query-builder})]]- published-clause (conj [:and- [:in :t.id (perms/visible-table-filter-select- :id- user-info- {:perms/view-data :unrestricted})]- published-clause]))]- {:select [:t.id- [:t.id :table_id]- [:t.display_name :name]- :t.description- :t.collection_id- [:t.db_id :database_id]- [[:!= :t.archived_at nil] :archived]- [(h2x/literal "table") :model]]- :from [[:metabase_table :t]]- :where [:and- [:= :t.is_published true]- (poison-when-pinned-clause pinned-state)- (collection/visible-collection-filter-clause :t.collection_id {:cte-name :visible_collection_ids})- queryable-clause- [:= :t.collection_id (:id collection)]- (if archived?- [:!= :t.archived_at nil]- [:= :t.archived_at nil])]}))-(defn- annotate-collections[parent-coll colls {:keys [show-dashboard-questions?]}](let [descendant-collections (collection/descendants-flat parent-coll nil@@ -989,184 +515,41 @@(map #(assoc % :collection_id (:id collection)))(sort-by (comp ::index meta))))-(defn- select-name- "Takes a honeysql select column and returns a keyword of which column it is.-- eg:- (select-name :id) -> :id- (select-name [(literal \"card\") :model]) -> :model- (select-name :p.id) -> :id"- [x]- (if (vector? x)- (recur (second x))- (-> x name (str/split #"\.") peek keyword)))--(def ^:private all-select-columns- "All columns that need to be present for the union-all. Generated with the comment form below. Non-text columns that- are optional (not id, but last_edit_user for example) must have a type so that the union-all can unify the nil with- the correct column type."- [:id :name :description :entity_id :display [:collection_preview :boolean] :dataset_query- :collection_id- [:dashboard_id :integer]- [:archived_directly :boolean]- :model :collection_position :authority_level [:personal_owner_id :integer] :location- :last_edit_email :last_edit_first_name :last_edit_last_name :moderated_status :icon- [:last_edit_user :integer] [:last_edit_timestamp :timestamp] [:database_id :integer]- :collection_type :type [:archived :boolean] [:last_used_at :timestamp] [:is_remote_synced :boolean] :namespace- ;; for determining whether a model is based on a csv-uploaded table- [:table_id :integer] [:is_upload :boolean] :query_type])--(defn- add-missing-columns- "Ensures that all necessary columns are in the select-columns collection, adding `[nil :column]` as necessary."- [select-columns necessary-columns]- (let [columns (m/index-by select-name select-columns)]- (map (fn [col]- (let [[col-name type'] (u/one-or-many col)]- (get columns col-name (if (and type' (= (mdb/db-type) :postgres))- [(h2x/cast type' nil) col-name]- [nil col-name]))))- necessary-columns)))--(defn- add-model-ranking- [select-clause model]- (let [rankings {:dashboard 1- :pulse 2- :dataset 3- :metric 4- :card 5- :snippet 6- :collection 7- :timeline 8}]- (conj select-clause [[:inline (get rankings model 100)]- :model_ranking])))--(comment- ;; generate the set of columns across all child queries. Remember to add type info if not a text column- (into []- (comp cat (map select-name) (distinct))- (for [model [:card :metric :dataset :dashboard :snippet :pulse :collection :timeline]]- (:select (collection-children-query model {:id 1 :location "/"} nil)))))--(defn- official-collections-first-sort-clause [{:keys [official-collections-first?]}]- (when official-collections-first?- [:authority_level :asc :nulls-last]))--(def ^:private normal-collections-first-sort-clause- [:type :asc :nulls-first])--(defn children-sort-clause- "Given the client side sort-info, return sort clause to effect this. `db-type` is necessary due to complications from- treatment of nulls in the different app db types."- [sort-info db-type]- (into []- (comp cat- (remove nil?))- [[(official-collections-first-sort-clause sort-info)]- [normal-collections-first-sort-clause]- (case ((juxt :sort-column :sort-direction) sort-info)- [nil nil] [[:%lower.name :asc]]- [:name :asc] [[:%lower.name :asc]]- [:name :desc] [[:%lower.name :desc]]- [:last-edited-at :asc] [(if (= db-type :mysql)- [:%isnull.last_edit_timestamp]- [:last_edit_timestamp :nulls-last])- [:last_edit_timestamp :asc]- [:%lower.name :asc]]- [:last-edited-at :desc] [(case db-type- :mysql [:%isnull.last_edit_timestamp]- :postgres [:last_edit_timestamp :desc-nulls-last]- :h2 nil)- [:last_edit_timestamp :desc]- [:%lower.name :asc]]- [:last-edited-by :asc] [(if (= db-type :mysql)- [:%isnull.last_edit_last_name]- [:last_edit_last_name :nulls-last])- [:last_edit_last_name :asc]- (if (= db-type :mysql)- [:%isnull.last_edit_first_name]- [:last_edit_first_name :nulls-last])- [:last_edit_first_name :asc]- [:%lower.name :asc]]- [:last-edited-by :desc] [(case db-type- :mysql [:%isnull.last_edit_last_name]- :postgres [:last_edit_last_name :desc-nulls-last]- :h2 nil)- [:last_edit_last_name :desc]- (case db-type- :mysql [:%isnull.last_edit_first_name]- :postgres [:last_edit_last_name :desc-nulls-last]- :h2 nil)- [:last_edit_first_name :desc]- [:%lower.name :asc]]- [:model :asc] [[:model_ranking :asc] [:%lower.name :asc]]- [:model :desc] [[:model_ranking :desc] [:%lower.name :asc]]- [:description :asc] [[:%lower.description :asc :nulls-last] [:%lower.name :asc]]- [:description :desc] [[:%lower.description :desc :nulls-last] [:%lower.name :asc]])- ;; add a fallback sort order so paging is still deterministic even if collection have the same name or- ;; whatever- [[:id :asc]]]))-(defn- total-count"The size of the whole result set `rows` is a page of, read off the `total_count` window column.A page past the end of the result set comes back empty and so carries no window column; in that case read the countoff the first row of the same query without its pagination."- [rows rows-query offset]+ [rows collection models options offset](or (some-> rows first :total_count)(when (pos? (or offset 0))- (some-> (collections-rest.db/collection-children-rows (assoc rows-query :limit 1)) first :total_count))+ (some-> (collections-rest.db/collection-children-rows collection models options {:limit 1}) first :total_count))0))(defn- collection-children*- [collection models {:keys [sort-info archived? search-text] :as options}]- (let [sql-order (children-sort-clause sort-info (mdb/db-type))- models (sort (map keyword models))- queries (for [model models- :let [query (collection-children-query model collection options)- select-clause-type (some- (fn [k]- (when (get query k)- k))- [:select :select-distinct])]]- (-> query- (update select-clause-type add-missing-columns all-select-columns)- (update select-clause-type add-model-ranking model)))- viz-config {:include-archived-items :all- :archive-operation-id nil- :permission-level (if archived? :write :read)- :include-trash-collection? archived?}- search-clause (search-text-clause search-text)- rows-query (cond-> {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]- :select [:* [[:over [[:count :*] ^:allow-subquery {} :total_count]]]]- :from [[^:allow-subquery {:union-all queries} :dummy_alias]]- :order-by sql-order}- search-clause- (sql.helpers/where search-clause))- limit (request/limit)- offset (request/offset)+ [collection models options]+ (let [models (sort (map keyword models))+ limit (request/limit)+ offset (request/offset);; We didn't implement collection pagination for snippets namespace for root/items;; Rip out the limit for now and put it back in when we want it- limit-query (if (or- (nil? limit)- (nil? offset)- (= (:collection-namespace options) "snippets"))- rows-query- (assoc rows-query- ;; If limit is 0, we still execute the query with a limit of 1 so that we fetch a- ;; :total_count- :limit (if (zero? limit) 1 limit)- :offset offset))- rows (tracing/with-span :db-app "db-app.collection-items-query" {:collection/id (:id collection)}- (collections-rest.db/collection-children-rows limit-query))- res {:total (total-count rows rows-query offset)- :data (if (= limit 0)- []- (tracing/with-span :db-app "db-app.collection-items-post-process" {:collection/id (:id collection)}- (post-process-rows options collection rows)))- :models models}- limit-res (assoc res- :limit (request/limit)- :offset (request/offset))]+ page (when-not (or (nil? limit)+ (nil? offset)+ (= (:collection-namespace options) "snippets"))+ ;; If limit is 0, we still execute the query with a limit of 1 so that we fetch a :total_count+ {:limit (if (zero? limit) 1 limit)+ :offset offset})+ rows (tracing/with-span :db-app "db-app.collection-items-query" {:collection/id (:id collection)}+ (collections-rest.db/collection-children-rows collection models options page))+ res {:total (total-count rows collection models options offset)+ :data (if (= limit 0)+ []+ (tracing/with-span :db-app "db-app.collection-items-post-process" {:collection/id (:id collection)}+ (post-process-rows options collection rows)))+ :models models}+ limit-res (assoc res+ :limit (request/limit)+ :offset (request/offset))](if (= (:collection-namespace options) "snippets")reslimit-res)))@@ -1186,7 +569,7 @@(mu/defn collection-children"Fetch a sequence of 'child' objects belonging to a Collection, filtered using `options`."[{collection-namespace :namespace, :as collection} :- collection/CollectionWithLocationAndIDOrRoot- {:keys [models], :as options} :- CollectionChildrenOptions]+ {:keys [models], :as options} :- children-query/CollectionChildrenOptions](let [valid-models (cond->> (valid-collection-models collection-namespace)(seq models) (filter models))](if (seq valid-models)@@ -1202,9 +585,9 @@"Return the models that have at least one visible item in `collection`. Respect the requested scope and visibility,but ignore model and search filters. When present, `restrict-models` limits the candidate models. Snippets are neverreported: they are not a filterable type."- [collection :- collection/CollectionWithLocationAndIDOrRoot- restrict-models :- [:maybe [:set :keyword]]- {:keys [archived?] :as options} :- CollectionChildrenOptions]+ [collection :- collection/CollectionWithLocationAndIDOrRoot+ restrict-models :- [:maybe [:set :keyword]]+ options :- children-query/CollectionChildrenOptions](let [candidates (cond->> (remove #{:snippet} (valid-collection-models (:namespace collection)))(seq restrict-models) (filter restrict-models))options (-> options@@ -1214,16 +597,7 @@;; repeats the same EXISTS probes; a separately cached request could avoid that work.(if (empty? candidates){:available_models []}- (let [viz-config {:include-archived-items :all- :archive-operation-id nil- :permission-level (if archived? :write :read)- :include-trash-collection? archived?}- row (first- (collections-rest.db/collection-filter-metadata-rows- {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]- :select (vec- (for [model candidates]- [[:exists (collection-children-query model collection options)] model]))}))]+ (let [row (first (collections-rest.db/collection-filter-metadata-rows collection (vec candidates) options))]{:available_models(->> candidates(keep (fn [model]@@ -1426,13 +800,13 @@include_library collection_type show_dashboard_questionsq include_available_models show_exploration_documents]} :- [:map[:models {:optional true} [:maybe Models]]- [:collection_type {:optional true} CollectionType]+ [:collection_type {:optional true} children-query/CollectionType][:archived {:default false} [:maybe ms/BooleanValue]][:namespace {:optional true} [:maybe ms/NonBlankString]][:include_library {:default false} [:maybe ms/BooleanValue]]- [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]- [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]- [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]+ [:pinned_state {:optional true} [:maybe (into [:enum] children-query/valid-pinned-state-values)]]+ [:sort_column {:optional true} [:maybe (into [:enum] children-query/valid-sort-columns)]]+ [:sort_direction {:optional true} [:maybe (into [:enum] children-query/valid-sort-directions)]][:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]][:show_dashboard_questions {:optional true} [:maybe ms/MaybeBooleanValue]][:q {:optional true} [:maybe :string]]@@ -1456,8 +830,8 @@model-kwds):pinned-state (keyword pinned_state):search-text q- :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)- :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)+ :sort-info {:sort-column (or (some-> sort_column children-query/normalize-sort-choice) :name)+ :sort-direction (or (some-> sort_direction children-query/normalize-sort-choice) :asc);; default to sorting official collections first, but provide the option not to:official-collections-first? (or (nil? official_collections_first)(boolean official_collections_first))}}]@@ -1737,9 +1111,9 @@show_exploration_documents]} :- [:map[:models {:optional true} [:maybe Models]][:archived {:default false} [:maybe ms/BooleanValue]]- [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]- [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]- [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]+ [:pinned_state {:optional true} [:maybe (into [:enum] children-query/valid-pinned-state-values)]]+ [:sort_column {:optional true} [:maybe (into [:enum] children-query/valid-sort-columns)]]+ [:sort_direction {:optional true} [:maybe (into [:enum] children-query/valid-sort-directions)]][:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]][:show_dashboard_questions {:default false} [:maybe ms/BooleanValue]][:show_exploration_documents {:default false} [:maybe ms/BooleanValue]]@@ -1755,8 +1129,8 @@:archived? (or archived (:archived collection) (collection/is-trash? collection)):pinned-state (keyword pinned_state):search-text q- :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)- :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)+ :sort-info {:sort-column (or (some-> sort_column children-query/normalize-sort-choice) :name)+ :sort-direction (or (some-> sort_direction children-query/normalize-sort-choice) :asc);; default to sorting official collections first, except for the trash.:official-collections-first? (if (and (nil? official_collections_first)(not (collection/is-trash? collection)))diff --git a/src/metabase/collections_rest/children_query.clj b/src/metabase/collections_rest/children_query.cljnew file mode 100644index 00000000000..7cfb7020814--- /dev/null+++ b/src/metabase/collections_rest/children_query.clj@@ -0,0 +1,656 @@+(ns metabase.collections-rest.children-query+ "Builds the Honey SQL that lists the children of a Collection (`/api/collection/:id/items` and `/root/items`): one+ query per item model, combined with `UNION ALL` and paginated with a `total_count` window. `metabase.collections-rest.db`+ runs the queries built here, so this namespace must not require it."+ (:require+ [clojure.string :as str]+ [honey.sql.helpers :as sql.helpers]+ [medley.core :as m]+ [metabase.api.common :as api]+ [metabase.app-db.core :as mdb]+ [metabase.collections.models.collection :as collection]+ [metabase.permissions.core :as perms]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.transforms.util :as transforms.u]+ [metabase.util :as u]+ [metabase.util.honey-sql-2 :as h2x]))++(set! *warn-on-reflection* true)++(def valid-model-param-values+ "Valid values for the `?model=` param accepted by endpoints in this namespace.+ `no_models` is for nilling out the set because a nil model set is actually the total model set"+ #{"card" ; SavedQuestion+ "dataset" ; Model. TODO : update this+ "document"+ "exploration"+ "metric"+ "collection"+ "dashboard"+ "pulse" ; I think the only kinds of Pulses we still have are Alerts?+ "snippet"+ "no_models"+ "timeline"+ "table"+ "transform"+ "measure"})++(def valid-pinned-state-values+ "Valid values for the `?pinned_state` param accepted by endpoints in this namespace."+ #{"all" "is_pinned" "is_not_pinned"})++(def valid-sort-columns+ "Valid values for the `?sort_column` param accepted by endpoints in this module."+ #{"name" "last_edited_at" "last_edited_by" "model" "description"})+(def valid-sort-directions+ "Valid values for the `?sort_direction` param accepted by endpoints in this module."+ #{"asc" "desc"})+(defn normalize-sort-choice+ "The keyword for a `?sort_column`/`?sort_direction` param value, e.g. `\"last_edited_at\"` -> `:last-edited-at`."+ [w]+ (when w (keyword (str/replace w #"_" "-"))))++(def CollectionType+ "Collection types that the root/items endpoint can filter on"+ [:enum "remote-synced"])++(def CollectionChildrenOptions+ "The options that select and order the children of a Collection (see [[children-rows-query]])."+ [:map+ [:show-dashboard-questions? :boolean]+ [:show-exploration-documents? :boolean]+ [:collection-type {:optional true} [:maybe CollectionType]]+ [:archived? :boolean]+ [:include-library? {:optional true} [:maybe :boolean]]+ [:pinned-state {:optional true} [:maybe (into [:enum] (map keyword) valid-pinned-state-values)]]+ ;; when specified, only return results of this type.+ [:models {:optional true} [:maybe [:set (into [:enum] (map keyword) valid-model-param-values)]]]+ [:search-text {:optional true} [:maybe :string]]+ [:sort-info {:optional true} [:maybe [:map+ [:sort-column (into [:enum {:error/message "sort-columns"}]+ (map normalize-sort-choice)+ valid-sort-columns)]+ [:sort-direction (into [:enum {:error/message "sort-direction"}]+ (map normalize-sort-choice)+ valid-sort-directions)]+ [:official-collections-first? {:optional true} :boolean]]]]])++(defmulti ^:private collection-children-query+ "Query that will fetch the 'children' of a `collection`, for different types of objects. Possible options are listed+ in the [[CollectionChildrenOptions]] schema above.++ NOTES:++ * `collection` will be either a CollectionInstance, or the Root Collection special placeholder object, so do not use+ `u/the-id` on it! Use `:id`, which will return `nil` for the Root Collection, which is exactly what we want.++ * These queries will be combined into a union-all query. You do not need to put all of the columns into the query,+ any you don't select will be added in the correct position so the union will work (see `all-select-columns` for more+ details)."+ {:arglists '([model collection options])}+ (fn [model _ _] (keyword model)))++;;; TODO -- in Postgres and H2 at least I think we could just do `true` or `false` here... not sure about MySQL.++(def ^:private always-true-hsql-expr+ "A Honey SQL expression that is always true.++ 1 = 1"+ [:= [:inline 1] [:inline 1]])++(def ^:private always-false-hsql-expr+ "A Honey SQL expression that is never true.++ 1 = 2"+ [:= [:inline 1] [:inline 2]])++(defn- search-text-clause+ "Match every token in `search-text` against an item's name or last editor's first or last name."+ [search-text]+ (when-not (str/blank? search-text)+ (when-let [tokens (->> (str/split (u/lower-case-en (str/trim search-text)) #"\s+")+ (filter seq)+ not-empty)]+ (into [:and]+ (for [token tokens+ :let [pattern (h2x/like-substring token)]]+ [:or+ [:like [:lower :name] pattern]+ [:like [:lower :last_edit_first_name] pattern]+ [:like [:lower :last_edit_last_name] pattern]])))))++(defn- pinned-state->clause+ ([pinned-state]+ (pinned-state->clause pinned-state :collection_position))+ ([pinned-state col]+ (case pinned-state+ :all always-true-hsql-expr+ :is_pinned [:<> col nil]+ :is_not_pinned [:= col nil]+ always-true-hsql-expr)))++(defn- poison-when-pinned-clause+ "Poison a query to return no results when filtering to pinned items. Use for items that do not have a notion of+ pinning so that no results return when asking for pinned items."+ [pinned-state]+ (if (= pinned-state :is_pinned)+ always-false-hsql-expr+ always-true-hsql-expr))++(defmethod collection-children-query :document+ [_ collection {:keys [archived? pinned-state show-exploration-documents?]}]+ (-> {:select [:document.id+ :document.name+ :document.collection_id+ :document.collection_position+ :document.archived+ :document.archived_directly+ :document.exploration_id+ [:u.id :last_edit_user]+ [:u.email :last_edit_email]+ [:u.first_name :last_edit_first_name]+ [:u.last_name :last_edit_last_name]+ [:r.timestamp :last_edit_timestamp]+ [(h2x/literal "document") :model]]+ :from [[:document :document]]+ :left-join [[:revision :r] [:and+ [:= :r.model_id :document.id]+ [:= :r.most_recent true]+ [:= :r.model (h2x/literal "Document")]]+ [:core_user :u] [:= :u.id :r.user_id]]+ :where [:and+ (collection/visible-collection-filter-clause :document.collection_id {:cte-name :visible_collection_ids})+ (if (collection/is-trash? collection)+ [:= :document.archived_directly true]+ [:and+ [:= :document.collection_id (:id collection)]+ [:= :document.archived_directly false]])+ [:= :document.archived (boolean archived?)]+ ;; Hide exploration-attached documents - similar to Dashboard Questions, they're not visible in the+ ;; collection, but only through the Exploration they're in. Callers that want them (e.g. the+ ;; Trash view, embedding SDK) pass show-exploration-documents? to opt in.+ (when-not show-exploration-documents?+ [:= :document.exploration_id nil])]}+ (sql.helpers/where (pinned-state->clause pinned-state :document.collection_position))))++(def ^:private exploration-recent-edits-subquery+ ;; Per-exploration latest edit, unioning the Exploration's own metadata revisions with+ ;; revisions of its Summary Document. `rn = 1` picks the winner.+ ;; The Exploration row is mostly inert post-creation; the meat of editing happens in+ ;; the attached Document, so "Last edited" must reflect both sources.+ ^:allow-subquery+ {:select [:exploration_id+ :timestamp+ :user_id+ [[:over [[:row_number] ^:allow-subquery {:partition-by [:exploration_id]+ :order-by [[:timestamp :desc]]}]] :rn]]+ :from [[^:allow-subquery {:union-all+ [^:allow-subquery+ {:select [[:r.model_id :exploration_id]+ [:r.timestamp :timestamp]+ [:r.user_id :user_id]]+ :from [[:revision :r]]+ :where [:and+ [:= :r.model (h2x/literal "Exploration")]+ [:= :r.most_recent true]]}+ ^:allow-subquery+ {:select [[:d.exploration_id :exploration_id]+ [:r.timestamp :timestamp]+ [:r.user_id :user_id]]+ :from [[:revision :r]]+ :join [[:document :d] [:= :d.id :r.model_id]]+ :where [:and+ [:= :r.model (h2x/literal "Document")]+ [:= :r.most_recent true]+ [:not= :d.exploration_id nil]]}]}+ :all_edits]]})++(defmethod collection-children-query :exploration+ [_ collection {:keys [archived? pinned-state]}]+ (-> {:select [:exploration.id+ :exploration.name+ :exploration.description+ :exploration.entity_id+ :exploration.collection_id+ :exploration.collection_position+ :exploration.archived+ :exploration.archived_directly+ [:u.id :last_edit_user]+ [:u.email :last_edit_email]+ [:u.first_name :last_edit_first_name]+ [:u.last_name :last_edit_last_name]+ [:ere.timestamp :last_edit_timestamp]+ [(h2x/literal "exploration") :model]]+ :from [[:exploration :exploration]]+ :left-join [[exploration-recent-edits-subquery :ere]+ [:and+ [:= :ere.exploration_id :exploration.id]+ [:= :ere.rn [:inline 1]]]+ [:core_user :u] [:= :u.id :ere.user_id]]+ :where [:and+ (collection/visible-collection-filter-clause :exploration.collection_id {:cte-name :visible_collection_ids})+ (if (collection/is-trash? collection)+ [:= :exploration.archived_directly true]+ [:and+ [:= :exploration.collection_id (:id collection)]+ [:= :exploration.archived_directly false]])+ [:= :exploration.archived (boolean archived?)]]}+ (sql.helpers/where (pinned-state->clause pinned-state :exploration.collection_position))))++(defmethod collection-children-query :pulse+ [_ collection {:keys [archived? pinned-state]}]+ (-> {:select-distinct [:p.id+ :p.name+ :p.entity_id+ :p.collection_position+ :p.collection_id+ [(h2x/literal "pulse") :model]]+ :from [[:pulse :p]]+ :left-join [[:pulse_card :pc] [:= :p.id :pc.pulse_id]]+ :where [:and+ [:= :p.collection_id (:id collection)]+ [:= :p.archived (boolean archived?)]+ ;; exclude alerts+ [:= :p.alert_condition nil]+ ;; exclude dashboard subscriptions+ [:= :p.dashboard_id nil]]}+ (sql.helpers/where (pinned-state->clause pinned-state :p.collection_position))))++(defenterprise snippets-collection-children-query+ "Collection children query for snippets on OSS. Returns all snippets regardless of collection, because snippet+ collections are an EE feature."+ metabase-enterprise.snippet-collections.api.native-query-snippet+ [_collection {:keys [archived?]}]+ {:select [:id :name :entity_id [(h2x/literal "snippet") :model]]+ :from [[:native_query_snippet :nqs]]+ :where [:= :archived (boolean archived?)]})++(defmethod collection-children-query :snippet+ [_model collection options]+ (snippets-collection-children-query collection options))++(defmethod collection-children-query :timeline+ [_ collection {:keys [archived? pinned-state]}]+ {:select [:id :collection_id :name [(h2x/literal "timeline") :model] :description :entity_id :icon]+ :from [[:timeline :timeline]]+ :where [:and+ (poison-when-pinned-clause pinned-state)+ [:= :collection_id (:id collection)]+ [:= :archived (boolean archived?)]]})++(defmethod collection-children-query :transform+ [_model collection {:keys [pinned-state]}]+ (let [enabled-types (transforms.u/enabled-source-types-for-user)]+ {:select [:id :collection_id :name [(h2x/literal "transform") :model] :description :entity_id]+ :from [[:transform :transform]]+ :where [:and+ (poison-when-pinned-clause pinned-state)+ [:= :collection_id (:id collection)]+ (if (seq enabled-types)+ [:in :source_type enabled-types]+ [:=+ [:inline 0]+ [:inline 1]])]}))++(defn- card-query [card-type collection {:keys [archived? pinned-state show-dashboard-questions?]}]+ (-> {:select (cond->+ [:c.id :c.name :c.description :c.entity_id :c.collection_position :c.display :c.collection_preview+ :dashboard_id+ :last_used_at+ :c.collection_id+ :c.archived_directly+ :c.archived+ :c.dataset_query+ [(h2x/literal (case card-type+ :model "dataset"+ :metric "metric"+ "card"))+ :model]+ [:u.id :last_edit_user]+ [:u.email :last_edit_email]+ [:u.first_name :last_edit_first_name]+ [:u.last_name :last_edit_last_name]+ [:r.timestamp :last_edit_timestamp]+ [:mr.status :moderated_status]]+ (#{:question :model} card-type)+ (conj :c.database_id))+ :from [[:report_card :c]]+ :left-join [[:revision :r] [:and+ [:= :r.model_id :c.id]+ [:= :r.most_recent true]+ [:= :r.model (h2x/literal "Card")]]+ [:moderation_review :mr] [:and+ [:= :mr.moderated_item_id :c.id]+ [:= :mr.most_recent true]+ [:= :mr.moderated_item_type (h2x/literal "card")]]+ [:core_user :u] [:= :u.id :r.user_id]]+ :where [:and+ (collection/visible-collection-filter-clause :c.collection_id {:cte-name :visible_collection_ids})+ (if (collection/is-trash? collection)+ [:= :c.archived_directly true]+ [:and+ [:= :c.collection_id (:id collection)]+ [:= :c.archived_directly false]])+ (when-not show-dashboard-questions?+ [:= :c.dashboard_id nil])+ [:= :c.document_id nil]+ [:= :c.archived (boolean archived?)]+ (case card-type+ :model+ [:= :c.type (h2x/literal "model")]++ :metric+ [:= :c.type (h2x/literal "metric")]++ [:= :c.type (h2x/literal "question")])]}+ (cond-> (= :model card-type)+ (-> (sql.helpers/select :c.table_id :t.is_upload :c.query_type)+ (sql.helpers/left-join [:metabase_table :t] [:= :t.id :c.table_id])))+ (sql.helpers/where (pinned-state->clause pinned-state))))++(defmethod collection-children-query :dataset+ [_ collection options]+ (card-query :model collection options))++(defmethod collection-children-query :metric+ [_ collection options]+ (card-query :metric collection options))++(defmethod collection-children-query :card+ [_ collection options]+ (card-query :question collection options))++(defn- dashboard-query [collection {:keys [archived? pinned-state]}]+ (-> {:select [:d.id :d.name :d.description :d.entity_id :d.collection_position+ [:last_viewed_at :last_used_at]+ :d.collection_id+ :d.archived_directly+ [(h2x/literal "dashboard") :model]+ [:u.id :last_edit_user]+ :d.archived+ [:u.email :last_edit_email]+ [:u.first_name :last_edit_first_name]+ [:u.last_name :last_edit_last_name]+ [:r.timestamp :last_edit_timestamp]+ [:mr.status :moderated_status]]+ :from [[:report_dashboard :d]]+ :left-join [[:moderation_review :mr] [:and+ [:= :mr.moderated_item_id :d.id]+ [:= :mr.most_recent true]+ [:= :mr.moderated_item_type (h2x/literal "dashboard")]]+ [:revision :r] [:and+ [:= :r.model_id :d.id]+ [:= :r.most_recent true]+ [:= :r.model (h2x/literal "Dashboard")]]+ [:core_user :u] [:= :u.id :r.user_id]]+ :where [:and+ (collection/visible-collection-filter-clause :d.collection_id {:cte-name :visible_collection_ids})+ (if (collection/is-trash? collection)+ [:= :d.archived_directly true]+ [:and+ [:= :d.collection_id (:id collection)]+ [:not= :d.archived_directly true]])+ [:= :d.archived (boolean archived?)]]}+ (sql.helpers/where (pinned-state->clause pinned-state))))++(defmethod collection-children-query :dashboard+ [_ collection options]+ (dashboard-query collection options))++(defenterprise snippets-collection-filter-clause+ "Clause to filter out snippet collections from the collection query on OSS instances, and instances without the+ snippet-collections. EE implementation returns `nil`, so as to not filter out snippet collections."+ metabase-enterprise.snippet-collections.api.native-query-snippet+ []+ [:or+ [:= :namespace nil]+ [:not= :namespace (u/qualified-name "snippets")]])++(defn- collection-query+ [collection {:keys [archived? collection-namespace pinned-state collection-type include-library?]}]+ (-> (assoc+ (collection/effective-children-query+ collection+ {:cte-name :visible_collection_ids}+ [:and+ (when collection-type+ (if (= collection-type "remote-synced")+ [:= :is_remote_synced true]+ [:= :type collection-type]))+ (when-not include-library?+ [:or [:= nil :type]+ [:not [:in :type [collection/library-collection-type+ collection/library-metrics-collection-type+ collection/library-data-collection-type]]]])+ (if archived?+ [:or+ [:= :archived true]+ [:= :id (collection/trash-collection-id)]]+ [:and [:= :archived false] [:not= :id (collection/trash-collection-id)]])]+ (perms/namespace-clause :namespace (u/qualified-name collection-namespace) (collection/is-trash? collection))+ ;; never show tenant-specific root collections as children of another collection+ [:or+ [:= :type nil]+ [:not= :type collection/tenant-specific-root-collection-type]]+ (snippets-collection-filter-clause))+ ;; We get from the effective-children-query a normal set of columns selected:+ ;; want to make it fit the others to make UNION ALL work+ :select [:id+ [:id :collection_id]+ :archived+ :name+ :description+ :entity_id+ :personal_owner_id+ :location+ :archived_directly+ :namespace+ ;; selected as `type` for compatibility with collection fns that expect it+ :type+ [[:case [:= :is_remote_synced nil] [:inline false] :else :is_remote_synced] :is_remote_synced]+ ;; selected as `collection_type` for fast sorting on "when it's a collection, type"+ [:type :collection_type]+ [(h2x/literal "collection") :model]+ :authority_level])+ ;; the nil indicates that collections are never pinned.+ (sql.helpers/where (pinned-state->clause pinned-state nil))))++(defmethod collection-children-query :collection+ [_ collection options]+ (collection-query collection options))++(defmethod collection-children-query :table+ [_ collection {:keys [archived? pinned-state]}]+ (let [user-info {:user-id api/*current-user-id*+ :is-superuser? api/*is-superuser?*}+ published-clause (perms/published-table-visible-clause :t.id user-info)+ queryable-clause (cond-> [:or+ [:in :t.id (perms/visible-table-filter-select+ :id+ user-info+ {:perms/view-data :unrestricted+ :perms/create-queries :query-builder})]]+ published-clause (conj [:and+ [:in :t.id (perms/visible-table-filter-select+ :id+ user-info+ {:perms/view-data :unrestricted})]+ published-clause]))]+ {:select [:t.id+ [:t.id :table_id]+ [:t.display_name :name]+ :t.description+ :t.collection_id+ [:t.db_id :database_id]+ [[:!= :t.archived_at nil] :archived]+ [(h2x/literal "table") :model]]+ :from [[:metabase_table :t]]+ :where [:and+ [:= :t.is_published true]+ (poison-when-pinned-clause pinned-state)+ (collection/visible-collection-filter-clause :t.collection_id {:cte-name :visible_collection_ids})+ queryable-clause+ [:= :t.collection_id (:id collection)]+ (if archived?+ [:!= :t.archived_at nil]+ [:= :t.archived_at nil])]}))++(defn- select-name+ "Takes a honeysql select column and returns a keyword of which column it is.++ eg:+ (select-name :id) -> :id+ (select-name [(literal \"card\") :model]) -> :model+ (select-name :p.id) -> :id"+ [x]+ (if (vector? x)+ (recur (second x))+ (-> x name (str/split #"\.") peek keyword)))++(def ^:private all-select-columns+ "All columns that need to be present for the union-all. Generated with the comment form below. Non-text columns that+ are optional (not id, but last_edit_user for example) must have a type so that the union-all can unify the nil with+ the correct column type."+ [:id :name :description :entity_id :display [:collection_preview :boolean] :dataset_query+ :collection_id+ [:dashboard_id :integer]+ [:archived_directly :boolean]+ :model :collection_position :authority_level [:personal_owner_id :integer] :location+ :last_edit_email :last_edit_first_name :last_edit_last_name :moderated_status :icon+ [:last_edit_user :integer] [:last_edit_timestamp :timestamp] [:database_id :integer]+ :collection_type :type [:archived :boolean] [:last_used_at :timestamp] [:is_remote_synced :boolean] :namespace+ ;; for determining whether a model is based on a csv-uploaded table+ [:table_id :integer] [:is_upload :boolean] :query_type])++(defn- add-missing-columns+ "Ensures that all necessary columns are in the select-columns collection, adding `[nil :column]` as necessary."+ [select-columns necessary-columns]+ (let [columns (m/index-by select-name select-columns)]+ (map (fn [col]+ (let [[col-name type'] (u/one-or-many col)]+ (get columns col-name (if (and type' (= (mdb/db-type) :postgres))+ [(h2x/cast type' nil) col-name]+ [nil col-name]))))+ necessary-columns)))++(defn- add-model-ranking+ [select-clause model]+ (let [rankings {:dashboard 1+ :pulse 2+ :dataset 3+ :metric 4+ :card 5+ :snippet 6+ :collection 7+ :timeline 8}]+ (conj select-clause [[:inline (get rankings model 100)]+ :model_ranking])))++(comment+ ;; generate the set of columns across all child queries. Remember to add type info if not a text column+ (into []+ (comp cat (map select-name) (distinct))+ (for [model [:card :metric :dataset :dashboard :snippet :pulse :collection :timeline]]+ (:select (collection-children-query model {:id 1 :location "/"} nil)))))++(defn- official-collections-first-sort-clause [{:keys [official-collections-first?]}]+ (when official-collections-first?+ [:authority_level :asc :nulls-last]))++(def ^:private normal-collections-first-sort-clause+ [:type :asc :nulls-first])++(defn children-sort-clause+ "Given the client side sort-info, return sort clause to effect this. `db-type` is necessary due to complications from+ treatment of nulls in the different app db types."+ [sort-info db-type]+ (into []+ (comp cat+ (remove nil?))+ [[(official-collections-first-sort-clause sort-info)]+ [normal-collections-first-sort-clause]+ (case ((juxt :sort-column :sort-direction) sort-info)+ [nil nil] [[:%lower.name :asc]]+ [:name :asc] [[:%lower.name :asc]]+ [:name :desc] [[:%lower.name :desc]]+ [:last-edited-at :asc] [(if (= db-type :mysql)+ [:%isnull.last_edit_timestamp]+ [:last_edit_timestamp :nulls-last])+ [:last_edit_timestamp :asc]+ [:%lower.name :asc]]+ [:last-edited-at :desc] [(case db-type+ :mysql [:%isnull.last_edit_timestamp]+ :postgres [:last_edit_timestamp :desc-nulls-last]+ :h2 nil)+ [:last_edit_timestamp :desc]+ [:%lower.name :asc]]+ [:last-edited-by :asc] [(if (= db-type :mysql)+ [:%isnull.last_edit_last_name]+ [:last_edit_last_name :nulls-last])+ [:last_edit_last_name :asc]+ (if (= db-type :mysql)+ [:%isnull.last_edit_first_name]+ [:last_edit_first_name :nulls-last])+ [:last_edit_first_name :asc]+ [:%lower.name :asc]]+ [:last-edited-by :desc] [(case db-type+ :mysql [:%isnull.last_edit_last_name]+ :postgres [:last_edit_last_name :desc-nulls-last]+ :h2 nil)+ [:last_edit_last_name :desc]+ (case db-type+ :mysql [:%isnull.last_edit_first_name]+ :postgres [:last_edit_last_name :desc-nulls-last]+ :h2 nil)+ [:last_edit_first_name :desc]+ [:%lower.name :asc]]+ [:model :asc] [[:model_ranking :asc] [:%lower.name :asc]]+ [:model :desc] [[:model_ranking :desc] [:%lower.name :asc]]+ [:description :asc] [[:%lower.description :asc :nulls-last] [:%lower.name :asc]]+ [:description :desc] [[:%lower.description :desc :nulls-last] [:%lower.name :asc]])+ ;; add a fallback sort order so paging is still deterministic even if collection have the same name or+ ;; whatever+ [[:id :asc]]]))++(defn- visible-collections-config+ "The visibility config of the `visible_collection_ids` CTE: archived listings need write access to the items."+ [{:keys [archived?]}]+ {:include-archived-items :all+ :archive-operation-id nil+ :permission-level (if archived? :write :read)+ :include-trash-collection? archived?})++(defn children-rows-query+ "The query listing the children of `collection` for the item `models` (keywords, in the order to union them),+ filtered and ordered by `options` (see [[CollectionChildrenOptions]]). Every row carries the size of the whole+ result set as `total_count`. `page` is `nil` for the whole result set, or a map with `:limit` and optionally+ `:offset`."+ [collection models {:keys [sort-info search-text] :as options} page]+ (let [sql-order (children-sort-clause sort-info (mdb/db-type))+ queries (for [model models+ :let [query (collection-children-query model collection options)+ select-clause-type (some+ (fn [k]+ (when (get query k)+ k))+ [:select :select-distinct])]]+ (-> query+ (update select-clause-type add-missing-columns all-select-columns)+ (update select-clause-type add-model-ranking model)))+ search-clause (search-text-clause search-text)]+ (cond-> {:with [[:visible_collection_ids (collection/visible-collection-query (visible-collections-config options))]]+ :select [:* [[:over [[:count :*] ^:allow-subquery {} :total_count]]]]+ :from [[^:allow-subquery {:union-all queries} :dummy_alias]]+ :order-by sql-order}+ search-clause (sql.helpers/where search-clause)+ (:limit page) (assoc :limit (:limit page))+ (:offset page) (assoc :offset (:offset page)))))++(defn filter-metadata-query+ "The single-row query whose columns say, for each of the item `models`, whether `collection` has at least one visible+ child of that model under `options`."+ [collection models options]+ {:with [[:visible_collection_ids (collection/visible-collection-query (visible-collections-config options))]]+ :select (vec+ (for [model models]+ [[:exists (collection-children-query model collection options)] model]))})diff --git a/src/metabase/collections_rest/db.clj b/src/metabase/collections_rest/db.cljindex a5a294c4345..9fc21b087ef 100644--- a/src/metabase/collections_rest/db.clj+++ b/src/metabase/collections_rest/db.clj@@ -2,13 +2,19 @@"Application database queries for the collections REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.collections-rest.children-query :as children-query][metabase.collections.models.collection :as collection]+ [metabase.collections.schema :as collections.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn other-users-personal-collection-ids+(mu/defn other-users-personal-collection-ids"The ids of the personal Collections owned by Users other than `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-fn-set :id :model/Collection{:where [:and [:!= :personal_owner_id nil] [:!= :personal_owner_id user-id]]}))@@ -23,13 +29,22 @@[:not [:like :location (str "%/" collection-id "/%/%/%")]]][:not [:like :location "/%/%/"]]))-(defn collections-for-listing+(mu/defn collections-for-listing"The Collections the User with `current-user-id` can read, for the listing endpoint: archived or unarchived ones(`archived`), only those around `collection-id` when `shallow`, only personal ones when `personal-only`, only theuser's own personal ones when `exclude-other-user-collections`, library ones only when `include-library?`, andthose in `namespaces`; official and non-trash Collections first, then by name."[{:keys [archived exclude-other-user-collections namespaces shallow collection-id personal-only include-library?]}- current-user-id]+ :- [:map {:closed true}+ [:archived {:optional true} [:maybe :boolean]]+ [:exclude-other-user-collections {:optional true} [:maybe :boolean]]+ [:namespaces {:optional true} [:maybe [:or [:set [:maybe :string]] [:sequential [:maybe :string]]]]]+ [:shallow {:optional true} [:maybe :boolean]]+ [:collection-id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:personal-only {:optional true} [:maybe :boolean]]+ [:include-library? {:optional true} [:maybe :boolean]]+ [:permissions-set {:optional true} [:maybe [:set :string]]]]+ current-user-id :- ::lib.schema.id/user](t2/select :model/Collection{:where [:and(case archived@@ -72,42 +87,44 @@:else 2]] :asc][:%lower.name :asc]]}))-(defn collection+(mu/defn collection"The Collection with `id`, or nil."- [id]+ [id :- [:maybe ::lib.schema.id/collection]](t2/select-one :model/Collection :id id))-(defn collection-location-columns+(mu/defn collection-location-columns"The location, id, and type of the Collection with `id`, or nil."- [id]+ [id :- ::lib.schema.id/collection](t2/select-one [:model/Collection :location :id :type] :id id))-(defn directly-archived-descendant-collections+(mu/defn directly-archived-descendant-collections"The directly archived Collections whose location starts with `location-prefix`."- [location-prefix]+ [location-prefix :- :string](t2/select :model/Collection :location [:like (str location-prefix "%")] :archived_directly true))-(defn update-collection!+(mu/defn update-collection!"Apply `changes` to the Collection with `id`."- [id changes]+ [id :- ::lib.schema.id/collection+ changes :- (mut/select-keys ::collections.schema/collection.update [:name :description :authority_level])](t2/update! :model/Collection id changes))-(defn delete-collection!+(mu/defn delete-collection!"Delete the Collection with `id`."- [id]+ [id :- ::lib.schema.id/collection](t2/delete! :model/Collection :id id))-(defn unarchived-card-collection-types-reducible+(mu/defn unarchived-card-collection-types-reducible"A reducible of the distinct Collection id and type of the unarchived Cards."[](t2/reducible-query {:select-distinct [:collection_id :type]:from [:report_card]:where [:= :archived false]}))-(defn unarchived-card-collection-types-in-reducible+(mu/defn unarchived-card-collection-types-in-reducible"A reducible of the distinct Collection id and type of the unarchived Cards in the Collections with`collection-ids`, leaving out dashboard questions when `exclude-dashboard-questions?`."- [collection-ids exclude-dashboard-questions?]+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ exclude-dashboard-questions? :- :boolean](t2/reducible-query {:select-distinct [:collection_id :type]:from [:report_card]:where [:and@@ -116,7 +133,7 @@[:= :archived false][:in :collection_id collection-ids]]}))-(defn published-table-collection-ids+(mu/defn published-table-collection-ids"The distinct `:collection_id`s of the published, unarchived Tables."[](t2/query {:select-distinct [:collection_id]@@ -125,9 +142,9 @@[:= :is_published true][:= :archived_at nil]]}))-(defn published-table-collection-ids-in+(mu/defn published-table-collection-ids-in"The distinct `:collection_id`s of the published, unarchived Tables in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/query {:select-distinct [:collection_id]:from :metabase_table:where [:and@@ -135,48 +152,55 @@[:= :archived_at nil][:in :collection_id collection-ids]]}))-(defn transform-collection-ids-in+(mu/defn transform-collection-ids-in"The distinct `:collection_id`s of the Transforms with one of `source-types` in the Collections with`collection-ids`."- [collection-ids source-types]+ [collection-ids :- [:sequential ::lib.schema.id/collection]+ source-types :- [:set :string]](t2/query {:select-distinct [:collection_id]:from :transform:where [:and[:in :collection_id collection-ids][:in :source_type source-types]]}))-(defn unarchived-dashboard-collection-ids-in+(mu/defn unarchived-dashboard-collection-ids-in"The distinct `:collection_id`s of the unarchived Dashboards in the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/query {:select-distinct [:collection_id]:from :report_dashboard:where [:and[:= :archived false][:in :collection_id collection-ids]]}))-(defn top-level-cards-in-collection+(mu/defn top-level-cards-in-collection"The Cards in the Collection with `collection-id` that belong to no Dashboard, newest first."- [collection-id]+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select :model/Card {:where [:and[:= :collection_id collection-id][:= :dashboard_id nil]]:order-by [[:id :desc]]}))-(defn cards-in-collection+(mu/defn cards-in-collection"The Cards in the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select :model/Card :collection_id collection-id))-(defn collection-children-rows- "The rows matching the collection-children Honey SQL `query`, built by `metabase.collections-rest.api` from the- per-model item queries for a Collection's paginated child listing. Follows the same exception as- `metabase.search.db` for spec-driven Honey SQL that can't be reduced to plain-data parameters."- [query]- (app-db/query query))--(defn collection-filter-metadata-rows- "The rows matching the collection-filter-metadata Honey SQL `query`, built by `metabase.collections-rest.api` to- probe which item models have at least one visible child in a Collection. Follows the same exception as- `metabase.search.db` for spec-driven Honey SQL that can't be reduced to plain-data parameters."- [query]- (app-db/query query))+(mu/defn collection-children-rows+ "The children of `collection` for the item `models` (keywords), filtered and ordered by `options` (see+ `metabase.collections-rest.children-query/CollectionChildrenOptions`), every row carrying the whole result set's+ size as `total_count`; `page` is nil for every row, or a map with `:limit` and optionally `:offset`."+ [collection :- collection/CollectionWithLocationAndIDOrRoot+ models :- [:sequential :keyword]+ options :- children-query/CollectionChildrenOptions+ page :- [:maybe [:map {:closed true}+ [:limit ms/IntGreaterThanOrEqualToZero]+ [:offset {:optional true} ms/IntGreaterThanOrEqualToZero]]]]+ (app-db/query (children-query/children-rows-query collection models options page)))++(mu/defn collection-filter-metadata-rows+ "The single row saying, for each of the item `models` (keywords), whether `collection` has at least one visible child+ of that model under `options`."+ [collection :- collection/CollectionWithLocationAndIDOrRoot+ models :- [:sequential :keyword]+ options :- children-query/CollectionChildrenOptions]+ (app-db/query (children-query/filter-metadata-query collection models options)))diff --git a/src/metabase/comments/db.clj b/src/metabase/comments/db.cljindex d497aa1d58d..89ec0f8d42c 100644--- a/src/metabase/comments/db.clj+++ b/src/metabase/comments/db.clj@@ -3,45 +3,53 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[honey.sql.helpers :as sql.helpers]+ [malli.util :as mut][metabase.api.common :as api]+ [metabase.comments.schema :as comments.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.users.core :as users][metabase.users.models.user :as user][metabase.users.settings :as users.settings]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn comments-for-target+(mu/defn comments-for-target"The Comments on the target with `target-type` and `target-id`, oldest first."- [target-type target-id]+ [target-type :- :string+ target-id :- ms/PositiveInt](t2/select :model/Comment{:where [:and[:= :target_type target-type][:= :target_id target-id]]:order-by [[:created_at :asc]]}))-(defn active-user-ids+(mu/defn active-user-ids"The ids among `user-ids` of active Users, or nil."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pks-set :model/User :id [:in user-ids] :is_active true))-(defn document+(mu/defn document"The Document with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Document :id id))-(defn exploration+(mu/defn exploration"The Exploration with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Exploration :id id))-(defn comment-by-id+(mu/defn comment-by-id"The Comment with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Comment :id id))-(defn comment-recipient-emails+(mu/defn comment-recipient-emails"The emails of the Users to notify about a comment: the authors of the Comment with `parent-comment-id` and of itsreplies, or the User with `creator-id` for a top-level comment, plus the Users with `mention-ids`."- [creator-id parent-comment-id mention-ids]+ [creator-id :- ::lib.schema.id/user+ parent-comment-id :- [:maybe ms/PositiveInt]+ mention-ids :- [:maybe [:sequential ms/PositiveInt]]](t2/select-fn-set :email [:model/User :email]{:where [:or(if parent-comment-id@@ -54,19 +62,20 @@(when (seq mention-ids)[:in :id mention-ids])]}))-(defn insert-comment!+(mu/defn insert-comment!"Insert the Comment `row` and return the inserted instance."- [row]+ [row :- ::comments.schema/comment.update](t2/insert-returning-instance! :model/Comment row))-(defn update-comment!- "Apply `changes` to the Comment with `id`."- [id changes]+(mu/defn update-comment!+ "Apply `changes` to the Comment with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::comments.schema/comment.update [:content :is_resolved])](t2/update! :model/Comment id changes))-(defn soft-delete-comment!- "Mark the Comment with `id` deleted now."- [id]+(mu/defn soft-delete-comment!+ "Mark the Comment with `id` deleted now, returning the number updated."+ [id :- ms/PositiveInt](t2/update! :model/Comment id {:deleted_at [:now]}))(defn- restrict-to-visible-users@@ -84,10 +93,11 @@(conj api/*current-user-id*))]):none (sql.helpers/where clauses [:= :core_user.id api/*current-user-id*])))))-(defn mentionable-users+(mu/defn mentionable-users"The id, first name, last name, and email of the active Users the current user may @mention, ordered by namethen id, limited to `limit` starting at `offset`."- [limit offset]+ [limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select [:model/User :id :first_name :last_name :email](-> (user/filter-clauses {:limit limit :offset offset})restrict-to-visible-users@@ -95,7 +105,7 @@[:%lower.last_name :asc][:id :asc]))))-(defn mentionable-user-count+(mu/defn mentionable-user-count"The `:count` of the active Users the current user may @mention."[](t2/query-one (merge {:select [[[:count [:distinct :core_user.id]] :count]]@@ -104,29 +114,36 @@restrict-to-visible-usersusers/filter-clauses-without-paging))))-(defn users-by-id+(mu/defn users-by-id"A map of User id to the id, email, and name of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn reaction-exists?+(mu/defn reaction-exists?"Whether the User with `user-id` has reacted to the Comment with `comment-id` with `emoji`."- [comment-id user-id emoji]+ [comment-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user+ emoji :- :string](t2/exists? :model/CommentReaction :comment_id comment-id :user_id user-id :emoji emoji))-(defn insert-reaction!+(mu/defn insert-reaction!"Insert a CommentReaction by the User with `user-id` on the Comment with `comment-id` with `emoji`."- [comment-id user-id emoji]+ [comment-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user+ emoji :- :string](t2/insert! :model/CommentReaction {:comment_id comment-id, :user_id user-id, :emoji emoji}))-(defn delete-reaction!- "Delete the CommentReaction by the User with `user-id` on the Comment with `comment-id` with `emoji`."- [comment-id user-id emoji]+(mu/defn delete-reaction!+ "Delete the CommentReaction by the User with `user-id` on the Comment with `comment-id` with `emoji`, returning+ the number deleted."+ [comment-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user+ emoji :- :string](t2/delete! :model/CommentReaction :comment_id comment-id :user_id user-id :emoji emoji))-(defn reactions-for-comments+(mu/defn reactions-for-comments"The CommentReactions on the Comments with `comment-ids`, ordered by comment, time, and emoji."- [comment-ids]+ [comment-ids :- [:sequential ms/PositiveInt]](t2/select :model/CommentReaction{:where [:in :comment_id comment-ids]:order-by [[:comment_id :asc] [:created_at :asc] [:emoji :asc]]}))diff --git a/src/metabase/comments/schema.clj b/src/metabase/comments/schema.cljnew file mode 100644index 00000000000..446453d000b--- /dev/null+++ b/src/metabase/comments/schema.clj@@ -0,0 +1,64 @@+(ns metabase.comments.schema+ "Malli schemas for the comments module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::comment.content+ "The `:content` column of a Comment, decoded."+ :map)++(mr/def ::comment.context+ "The `:context` column of a Comment, decoded."+ :map)++(mr/def ::comment+ "A Comment as selected from the app DB: every column of `:comment`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:parent_comment_id [:maybe ms/PositiveInt]]+ [:target_type [:or :keyword :string]]+ [:target_id ms/PositiveInt]+ [:child_target_id [:maybe :string]]+ [:creator_id ::lib.schema.id/user]+ [:content ::comment.content]+ [:is_resolved :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:deleted_at [:maybe ms/TemporalInstant]]+ [:content_html [:maybe :string]]+ [:context [:maybe ::comment.context]]])++(mr/def ::comment.update+ "What an update (or insert) of a Comment accepts: every column of `:comment` except `id`, all optional."+ [:map {:closed true}+ [:parent_comment_id {:optional true} [:maybe ms/PositiveInt]]+ [:target_type {:optional true} [:maybe [:or :keyword :string]]]+ [:target_id {:optional true} [:maybe ms/PositiveInt]]+ [:child_target_id {:optional true} [:maybe :string]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:content {:optional true} [:maybe ::comment.content]]+ [:is_resolved {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:deleted_at {:optional true} [:maybe ms/TemporalInstant]]+ [:content_html {:optional true} [:maybe :string]]+ [:context {:optional true} [:maybe ::comment.context]]])++(mr/def ::comment-reaction+ "A CommentReaction as selected from the app DB: every column of `:comment_reaction`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:comment_id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:emoji :string]+ [:created_at ms/TemporalInstant]])++(mr/def ::comment-reaction.update+ "What an update (or insert) of a CommentReaction accepts: every column of `:comment_reaction` except `id`, all optional."+ [:map {:closed true}+ [:comment_id {:optional true} [:maybe ms/PositiveInt]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:emoji {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/content_translation/db.clj b/src/metabase/content_translation/db.cljindex 23145e0227c..a2b8e689a8e 100644--- a/src/metabase/content_translation/db.clj+++ b/src/metabase/content_translation/db.clj@@ -2,14 +2,15 @@"Application database queries for the content translation module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn translations-for-locale+(mu/defn translations-for-locale"The ContentTranslations for `locale`, ordered by message id."- [locale]+ [locale :- :string](t2/select :model/ContentTranslation :locale locale {:order-by [:msgid]}))-(defn all-translations+(mu/defn all-translations"Every ContentTranslation, ordered by locale and message id."[](t2/select :model/ContentTranslation {:order-by [:locale :msgid]}))diff --git a/src/metabase/content_translation/schema.clj b/src/metabase/content_translation/schema.cljnew file mode 100644index 00000000000..edc2455711f--- /dev/null+++ b/src/metabase/content_translation/schema.clj@@ -0,0 +1,20 @@+(ns metabase.content-translation.schema+ "Malli schemas for the content-translation module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::content-translation+ "A ContentTranslation as selected from the app DB: every column of `:content_translation`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:locale :string]+ [:msgid :string]+ [:msgstr :string]])++(mr/def ::content-translation.update+ "What an update (or insert) of a ContentTranslation accepts: every column of `:content_translation` except `id`, all optional."+ [:map {:closed true}+ [:locale {:optional true} [:maybe :string]]+ [:msgid {:optional true} [:maybe :string]]+ [:msgstr {:optional true} [:maybe :string]]])diff --git a/src/metabase/content_verification/db.clj b/src/metabase/content_verification/db.cljindex e7048ba4c4a..fdd62c19f7a 100644--- a/src/metabase/content_verification/db.clj+++ b/src/metabase/content_verification/db.clj@@ -2,25 +2,32 @@"Application database queries for the content verification module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.content-verification.schema :as content-verification.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn moderation-reviews-for-items+(mu/defn moderation-reviews-for-items"The ModerationReviews of the items with `item-types` and `item-ids`, newest first."- [item-types item-ids]+ [item-types :- [:set :keyword]+ item-ids :- [:sequential ms/PositiveInt]](t2/select :model/ModerationReview:moderated_item_type [:in item-types]:moderated_item_id [:in item-ids]{:order-by [[:id :desc]]}))-(defn users+(mu/defn users"The Users with `user-ids`."- [user-ids]+ [user-ids :- [:maybe [:sequential [:maybe ::lib.schema.id/user]]]](t2/select :model/User :id [:in user-ids]))-(defn moderation-review-ids-for-item+(mu/defn moderation-review-ids-for-item"The ids of the ModerationReviews of the item with `item-id` and `item-type`, newest first."- [item-id item-type]+ [item-id :- ms/PositiveInt+ item-type :- [:or :string :keyword]](app-db/query {:select [:id]:from [:moderation_review]:where [:and@@ -28,29 +35,31 @@[:= :moderated_item_type item-type]]:order-by [[:id :desc]]}))-(defn most-recent-moderation-review-statuses+(mu/defn most-recent-moderation-review-statuses"The item id, item type, and status of the most recent ModerationReviews of the items with `item-types` and`item-ids`, newest first."- [item-types item-ids]+ [item-types :- [:set :keyword]+ item-ids :- [:sequential ms/PositiveInt]](t2/select [:model/ModerationReview :moderated_item_id :moderated_item_type :status]:moderated_item_type [:in item-types]:moderated_item_id [:in item-ids]:most_recent true{:order-by [[:id :desc]]}))-(defn delete-moderation-reviews!+(mu/defn delete-moderation-reviews!"Delete the ModerationReviews with `ids`."- [ids]+ [ids :- [:set ms/PositiveInt]](t2/delete! :model/ModerationReview :id [:in ids]))-(defn unmark-most-recent-moderation-reviews!+(mu/defn unmark-most-recent-moderation-reviews!"Clear `most_recent` on the ModerationReviews of the item with `item-id` and `item-type`."- [item-id item-type]+ [item-id :- ms/PositiveInt+ item-type :- [:or :string :keyword]](t2/update! :model/ModerationReview{:moderated_item_id item-id, :moderated_item_type item-type}{:most_recent false}))-(defn insert-moderation-review!+(mu/defn insert-moderation-review!"Insert the ModerationReview `row` and return the inserted instance."- [row]+ [row :- (mut/select-keys ::content-verification.schema/moderation-review.update [:moderated_item_id :moderated_item_type :moderator_id :status :text :most_recent])](t2/insert-returning-instance! :model/ModerationReview row))diff --git a/src/metabase/content_verification/schema.clj b/src/metabase/content_verification/schema.cljnew file mode 100644index 00000000000..5ee67b92433--- /dev/null+++ b/src/metabase/content_verification/schema.clj@@ -0,0 +1,30 @@+(ns metabase.content-verification.schema+ "Malli schemas for the content-verification module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::moderation-review+ "A ModerationReview as selected from the app DB: every column of `:moderation_review`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:updated_at ms/TemporalInstant]+ [:created_at ms/TemporalInstant]+ [:status [:maybe [:or :keyword :string]]]+ [:text [:maybe :string]]+ [:moderated_item_id ms/PositiveInt]+ [:moderated_item_type [:or :keyword :string]]+ [:moderator_id ms/PositiveInt]+ [:most_recent :boolean]])++(mr/def ::moderation-review.update+ "What an update (or insert) of a ModerationReview accepts: every column of `:moderation_review` except `id`, all optional."+ [:map {:closed true}+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:text {:optional true} [:maybe :string]]+ [:moderated_item_id {:optional true} [:maybe ms/PositiveInt]]+ [:moderated_item_type {:optional true} [:maybe [:or :keyword :string]]]+ [:moderator_id {:optional true} [:maybe ms/PositiveInt]]+ [:most_recent {:optional true} [:maybe :boolean]]])diff --git a/src/metabase/dashboards/db.clj b/src/metabase/dashboards/db.cljindex 7b0c24afcaf..b14e15f9726 100644--- a/src/metabase/dashboards/db.clj+++ b/src/metabase/dashboards/db.clj@@ -2,56 +2,63 @@"Application database queries for the dashboards module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut][medley.core :as m][metabase.app-db.core :as mdb]+ [metabase.dashboards.schema :as dashboards.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.serialization :as serdes]+ [metabase.queries.schema :as queries.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn insert-dashboard!+(mu/defn insert-dashboard!"Insert the Dashboard `row` and return the inserted instance."- [row]+ [row :- ::dashboards.schema/dashboard.update](t2/insert-returning-instance! :model/Dashboard row))-(defn update-dashboard!- "Apply `changes` to the Dashboard with `dashboard-id`."- [dashboard-id changes]+(mu/defn update-dashboard!+ "Apply `changes` to the Dashboard with `dashboard-id`, returning the number updated."+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- (mut/select-keys ::dashboards.schema/dashboard.update [:parameters])](t2/update! :model/Dashboard dashboard-id changes))-(defn delete-dashboard-revisions!- "Delete the Revisions of the Dashboard with `dashboard-id`."- [dashboard-id]+(mu/defn delete-dashboard-revisions!+ "Delete the Revisions of the Dashboard with `dashboard-id`, returning the number deleted."+ [dashboard-id :- ::lib.schema.id/dashboard](t2/delete! :model/Revision :model "Dashboard" :model_id dashboard-id))-(defn delete-pulses-for-dashboard!- "Delete the Pulses of the Dashboard with `dashboard-id`."- [dashboard-id]+(mu/defn delete-pulses-for-dashboard!+ "Delete the Pulses of the Dashboard with `dashboard-id`, returning the number deleted."+ [dashboard-id :- ::lib.schema.id/dashboard](t2/delete! :model/Pulse :dashboard_id dashboard-id))-(defn dashboard-tabs-for-dashboards+(mu/defn dashboard-tabs-for-dashboards"The DashboardTabs of the Dashboards with `dashboard-ids`, in dashboard then position order."- [dashboard-ids]+ [dashboard-ids :- [:sequential ::lib.schema.id/dashboard]](t2/select :model/DashboardTab:dashboard_id [:in dashboard-ids]{:order-by [[:dashboard_id :asc] [:position :asc] [:id :asc]]}))-(defn dashboard-collection-authority-levels+(mu/defn dashboard-collection-authority-levels"The id and Collection `authority_level` of the Dashboards with `dashboard-ids`."- [dashboard-ids]+ [dashboard-ids :- [:set ::lib.schema.id/dashboard]](mdb/query {:select [:dashboard.id :collection.authority_level]:from [[:report_dashboard :dashboard]]:left-join [[:collection :collection] [:= :collection.id :dashboard.collection_id]]:where [:in :dashboard.id dashboard-ids]}))-(defn dashcards-with-visible-cards-for-dashboards+(mu/defn dashcards-with-visible-cards-for-dashboards"The DashboardCards of the Dashboards with `dashboard-ids` whose Card is visible (unarchived, a dashboard questionnot archived by itself, or absent), with their Card's Collection authority level, in dashboard then creation order."- [dashboard-ids]+ [dashboard-ids :- [:sequential ::lib.schema.id/dashboard]](t2/select :model/DashboardCard{:select [:dashcard.* [:collection.authority_level :collection_authority_level]]:from [[:report_dashboardcard :dashcard]]@@ -70,85 +77,90 @@[:= :card.archived nil]]] ; e.g. DashCards with no corresponding Card, e.g. text Cards:order-by [[:dashcard.dashboard_id] [:dashcard.created_at :asc]]}))-(defn internal-dashboard-question-ids+(mu/defn internal-dashboard-question-ids"The ids of the Cards internal to the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-pks-set :model/Card :dashboard_id dashboard-id))-(defn set-cards-archived!- "Set the archived flags of the Cards with `card-ids`."- [card-ids archived?]+(mu/defn set-cards-archived!+ "Set the archived flags of the Cards with `card-ids`, returning the number updated."+ [card-ids :- [:sequential ::lib.schema.id/card]+ archived? :- :boolean](t2/update! :model/Card :id [:in card-ids] {:archived archived? :archived_directly archived?}))-(defn archive-dashboard-questions!- "Archive the unarchived Cards internal to the Dashboard with `dashboard-id`, not directly."- [dashboard-id]+(mu/defn archive-dashboard-questions!+ "Archive the unarchived Cards internal to the Dashboard with `dashboard-id`, not directly, returning the number+ updated."+ [dashboard-id :- ::lib.schema.id/dashboard](t2/update! :model/Card :dashboard_id dashboard-id :archived false {:archived true :archived_directly false}))-(defn unarchive-dashboard-questions!- "Unarchive the Cards internal to the Dashboard with `dashboard-id` that were not archived directly."- [dashboard-id]+(mu/defn unarchive-dashboard-questions!+ "Unarchive the Cards internal to the Dashboard with `dashboard-id` that were not archived directly, returning the+ number updated."+ [dashboard-id :- ::lib.schema.id/dashboard](t2/update! :model/Card :dashboard_id dashboard-id :archived true :archived_directly false {:archived false}))-(defn move-dashboard-questions!- "Move the Cards internal to the Dashboard with `dashboard-id` to the Collection with `collection-id`."- [dashboard-id collection-id]+(mu/defn move-dashboard-questions!+ "Move the Cards internal to the Dashboard with `dashboard-id` to the Collection with `collection-id`, returning+ the number updated."+ [dashboard-id :- ::lib.schema.id/dashboard+ collection-id :- [:maybe ::lib.schema.id/collection]](t2/update! :model/Card :dashboard_id dashboard-id {:collection_id collection-id}))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-query-columns+(mu/defn card-query-columns"The query and schema of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :dataset_query :card_schema] :id card-id))-(defn card-queries+(mu/defn card-queries"A map of Card id to query for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn :dataset_query :model/Card :id [:in card-ids]))-(defn document-cards-among+(mu/defn document-cards-among"The Cards among `card-ids` that belong to a Document."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select :model/Card :id [:in card-ids] :document_id [:<> nil]))-(defn insert-card!+(mu/defn insert-card!"Insert the Card `row` and return the inserted instance."- [row]+ [row :- ::queries.schema/card.update](t2/insert-returning-instance! :model/Card row))-(defn dashcard-serdes-columns+(mu/defn dashcard-serdes-columns"The id, Card, Action, parameter mappings, and visualization settings of the DashboardCards of the Dashboard with`dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select [:model/DashboardCard :id :card_id :action_id :parameter_mappings :visualization_settings]:dashboard_id dashboard-id))-(defn dashcard-series-columns+(mu/defn dashcard-series-columns"The id, Card id, and DashboardCard id of the DashboardCardSeries of the DashboardCards with `dashcard-ids`."- [dashcard-ids]+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/select [:model/DashboardCardSeries :id :card_id :dashboardcard_id] :dashboardcard_id [:in dashcard-ids]))-(defn series-cards-for-dashcards+(mu/defn series-cards-for-dashcards"The series Cards of the DashboardCards with `dashcard-ids`, each with its `:dashboardcard_id`, in series order."- [dashcard-ids]+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/select [:model/Card :id :name :description :display :dataset_query :type :database_id:visualization_settings :collection_id :card_schema :series.dashboardcard_id]{:left-join [[:dashboardcard_series :series] [:= :report_card.id :series.card_id]]:where [:in :series.dashboardcard_id dashcard-ids]:order-by [[:series.position :asc]]}))-(defn dashcard+(mu/defn dashcard"The DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one :model/DashboardCard :id dashcard-id))-(defn multi-cards-for-dashcard+(mu/defn multi-cards-for-dashcard"The unarchived Cards added to the DashboardCard with `dashcard-id` as series (the 'add series' feature)."- [dashcard-id]+ [dashcard-id :- [:maybe ::lib.schema.id/dashcard]](mdb/query {:select [:newcard.*]:from [[:report_dashboardcard :dashcard]]:left-join [[:dashboardcard_series :dashcardseries]@@ -159,51 +171,55 @@[:= :newcard.archived false][:= :dashcard.id dashcard-id]]}))-(defn dashcards-by-ids+(mu/defn dashcards-by-ids"The DashboardCards with `dashcard-ids`."- [dashcard-ids]+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/select :model/DashboardCard :id [:in dashcard-ids]))-(defn dashcards-in-tabs+(mu/defn dashcards-in-tabs"The DashboardCards of the Dashboard with `dashboard-id` on the DashboardTabs with `tab-ids`."- [dashboard-id tab-ids]+ [dashboard-id :- ::lib.schema.id/dashboard+ tab-ids :- [:sequential ms/PositiveInt]](t2/select :model/DashboardCard :dashboard_id dashboard-id :dashboard_tab_id [:in tab-ids]))-(defn insert-dashcards!+(mu/defn insert-dashcards!"Insert the DashboardCard `rows` and return their ids."- [rows]+ [rows :- [:sequential+ ::dashboards.schema/dashboard-card.update]](t2/insert-returning-pks! :model/DashboardCard rows))-(defn update-dashcard!- "Apply `changes` to the DashboardCard with `dashcard-id`."- [dashcard-id changes]+(mu/defn update-dashcard!+ "Apply `changes` to the DashboardCard with `dashcard-id`, returning the number updated."+ [dashcard-id :- ::lib.schema.id/dashcard+ changes :- ::dashboards.schema/dashboard-card.update](t2/update! :model/DashboardCard dashcard-id changes))-(defn delete-dashcards!- "Delete the DashboardCards with `dashcard-ids`."- [dashcard-ids]+(mu/defn delete-dashcards!+ "Delete the DashboardCards with `dashcard-ids`, returning the number deleted."+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/delete! :model/DashboardCard :id [:in dashcard-ids]))-(defn delete-pulse-cards-for-dashcards!- "Delete the PulseCards of the DashboardCards with `dashcard-ids`."- [dashcard-ids]+(mu/defn delete-pulse-cards-for-dashcards!+ "Delete the PulseCards of the DashboardCards with `dashcard-ids`, returning the number deleted."+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/delete! :model/PulseCard :dashboard_card_id [:in dashcard-ids]))-(defn delete-series-for-dashcards!- "Delete the DashboardCardSeries of the DashboardCards with `dashcard-ids`."- [dashcard-ids]+(mu/defn delete-series-for-dashcards!+ "Delete the DashboardCardSeries of the DashboardCards with `dashcard-ids`, returning the number deleted."+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/delete! :model/DashboardCardSeries :dashboardcard_id [:in dashcard-ids]))-(defn insert-dashcard-series!- "Insert the DashboardCardSeries `rows`."- [rows]+(mu/defn insert-dashcard-series!+ "Insert the DashboardCardSeries `rows`, returning the number inserted."+ [rows :- [:sequential+ (mut/select-keys ::dashboards.schema/dashboard-card-series.update [:dashboardcard_id :card_id :position])]](t2/insert! :model/DashboardCardSeries rows));;; ----------------------------------------------- Link cards -----------------------------------------------------(defn ensure-integer-link-card-id+(mu/defn ensure-integer-link-card-id"Return `id` if it is an integer, else throw a 400."- [id]+ [id :- :some](when-not (integer? id)(throw (ex-info "Link card entity id must be an integer"{:status-code 400, :id id})))@@ -274,10 +290,11 @@[:in :id (mapv ensure-integer-link-card-id id-or-ids)][:= :id (ensure-integer-link-card-id id-or-ids)])})-(defn link-card-entity+(mu/defn link-card-entity"The instance of the link-card `model` (a string like \"card\" or \"table\") with `id`, projected the same way as[[link-card-info-rows]], or nil."- [model id]+ [model :- :string+ id :- [:or :int :string]](t2/select-one (serdes/link-card-model->toucan-model model) (link-card-info-query-for-model model id)))(defn- link-card-info-query@@ -288,28 +305,30 @@:from [[^:allow-subquery {:union-all (map #(apply link-card-info-query-for-model %) link-card-model->ids)}:alias_is_required_by_sql_but_not_needed_here]]}))-(defn link-card-info-rows+(mu/defn link-card-info-rows"The name, description, and related columns of the entities the link cards `link-card-model->ids`(`[[model #{ids}] ...]`) point at."- [link-card-model->ids]+ [link-card-model->ids :- [:sequential [:tuple :string [:or [:set [:or :int :string]] [:sequential [:or :int :string]]]]]](t2/query (link-card-info-query link-card-model->ids)))-(defn dashboard-tab+(mu/defn dashboard-tab"The DashboardTab with `tab-id`, or nil."- [tab-id]+ [tab-id :- ms/PositiveInt](t2/select-one :model/DashboardTab :id tab-id))-(defn insert-dashboard-tabs!+(mu/defn insert-dashboard-tabs!"Insert the DashboardTab `rows` and return their ids."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::dashboards.schema/dashboard-tab.update [:dashboard_id :name :position :entity_id])]](t2/insert-returning-pks! :model/DashboardTab rows))-(defn update-dashboard-tab!- "Apply `changes` to the DashboardTab with `tab-id`."- [tab-id changes]+(mu/defn update-dashboard-tab!+ "Apply `changes` to the DashboardTab with `tab-id`, returning the number updated."+ [tab-id :- ms/PositiveInt+ changes :- (mut/select-keys ::dashboards.schema/dashboard-tab.update [:name :position])](t2/update! :model/DashboardTab tab-id changes))-(defn delete-dashboard-tabs!- "Delete the DashboardTabs with `tab-ids`."- [tab-ids]+(mu/defn delete-dashboard-tabs!+ "Delete the DashboardTabs with `tab-ids`, returning the number deleted."+ [tab-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/DashboardTab :id [:in tab-ids]))diff --git a/src/metabase/dashboards/schema.clj b/src/metabase/dashboards/schema.cljindex 126cbf4ce83..5db2505585b 100644--- a/src/metabase/dashboards/schema.clj+++ b/src/metabase/dashboards/schema.clj@@ -4,7 +4,8 @@[metabase.lib.schema.parameter :as lib.schema.parameter][metabase.parameters.schema :as parameters.schema][metabase.queries.schema :as queries.schema]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::dashcard[:map@@ -24,3 +25,119 @@[:id {:optional true} ::lib.schema.id/dashboard][:parameters {:optional true} [:maybe ::parameters]][:dashcards {:optional true} [:maybe [:sequential ::dashcard]]]])++(mr/def ::dashboard.parameter+ "One entry of the `:parameters` column of a Dashboard, decoded."+ :map)++(mr/def ::dashboard.update+ "What an update (or insert) of a Dashboard accepts: every column of `:report_dashboard` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:parameters {:optional true} [:maybe [:sequential ::dashboard.parameter]]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:caveats {:optional true} [:maybe :string]]+ [:show_in_getting_started {:optional true} [:maybe :boolean]]+ [:public_uuid {:optional true} [:maybe :string]]+ [:made_public_by_id {:optional true} [:maybe ms/PositiveInt]]+ [:enable_embedding {:optional true} [:maybe :boolean]]+ [:embedding_params {:optional true} [:maybe ms/EmbeddingParams]]+ [:archived {:optional true} [:maybe :boolean]]+ [:position {:optional true} [:maybe :int]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:collection_position {:optional true} [:maybe :int]]+ [:cache_ttl {:optional true} [:maybe :int]]+ [:entity_id {:optional true} [:maybe :string]]+ [:auto_apply_filters {:optional true} [:maybe :boolean]]+ [:width {:optional true} [:maybe :string]]+ [:initially_published_at {:optional true} [:maybe ms/TemporalInstant]]+ [:view_count {:optional true} [:maybe :int]]+ [:archived_directly {:optional true} [:maybe :boolean]]+ [:last_viewed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:embedding_type {:optional true} [:maybe [:or :keyword :string]]]+ [:public_uuid_prefix {:optional true} [:maybe :string]]])++(mr/def ::dashboard-card.parameter-mapping+ "One entry of the `:parameter_mappings` column of a DashboardCard, decoded."+ :map)++(mr/def ::dashboard-card.visualization-settings+ "The `:visualization_settings` column of a DashboardCard, decoded."+ :map)++(mr/def ::dashboard-card+ "A DashboardCard as selected from the app DB: every column of `:report_dashboardcard`."+ [:map {:closed true}+ [:id ::lib.schema.id/dashcard]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:size_x :int]+ [:size_y :int]+ [:row :int]+ [:col :int]+ [:card_id [:maybe ::lib.schema.id/card]]+ [:dashboard_id ::lib.schema.id/dashboard]+ [:parameter_mappings [:sequential ::dashboard-card.parameter-mapping]]+ [:visualization_settings ::dashboard-card.visualization-settings]+ [:entity_id :string]+ [:action_id [:maybe ::lib.schema.id/action]]+ [:dashboard_tab_id [:maybe ms/PositiveInt]]+ [:inline_parameters [:maybe [:sequential :string]]]])++(mr/def ::dashboard-card.update+ "What an update (or insert) of a DashboardCard accepts: every column of `:report_dashboardcard` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:size_x {:optional true} [:maybe :int]]+ [:size_y {:optional true} [:maybe :int]]+ [:row {:optional true} [:maybe :int]]+ [:col {:optional true} [:maybe :int]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:parameter_mappings {:optional true} [:maybe [:sequential ::dashboard-card.parameter-mapping]]]+ [:visualization_settings {:optional true} [:maybe ::dashboard-card.visualization-settings]]+ [:entity_id {:optional true} [:maybe :string]]+ [:action_id {:optional true} [:maybe ::lib.schema.id/action]]+ [:dashboard_tab_id {:optional true} [:maybe ms/PositiveInt]]+ [:inline_parameters {:optional true} [:maybe [:sequential :string]]]])++(mr/def ::dashboard-card-series+ "A DashboardCardSeries as selected from the app DB: every column of `:dashboardcard_series`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:dashboardcard_id ::lib.schema.id/dashcard]+ [:card_id ::lib.schema.id/card]+ [:position :int]])++(mr/def ::dashboard-card-series.update+ "What an update (or insert) of a DashboardCardSeries accepts: every column of `:dashboardcard_series` except `id`, all optional."+ [:map {:closed true}+ [:dashboardcard_id {:optional true} [:maybe ::lib.schema.id/dashcard]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:position {:optional true} [:maybe :int]]])++(mr/def ::dashboard-tab+ "A DashboardTab as selected from the app DB: every column of `:dashboard_tab`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:dashboard_id ::lib.schema.id/dashboard]+ [:name :string]+ [:position :int]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::dashboard-tab.update+ "What an update (or insert) of a DashboardTab accepts: every column of `:dashboard_tab` except `id`, all optional."+ [:map {:closed true}+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:name {:optional true} [:maybe :string]]+ [:position {:optional true} [:maybe :int]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/dashboards_rest/db.clj b/src/metabase/dashboards_rest/db.cljindex 2bc7b035766..95cb07bafa1 100644--- a/src/metabase/dashboards_rest/db.clj+++ b/src/metabase/dashboards_rest/db.clj@@ -2,81 +2,92 @@"Application database queries for the dashboards REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.dashboards.schema :as dashboards.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn dashboards+(mu/defn dashboards"The archived or unarchived (`archived?`) Dashboards, restricted to those created by `creator-id` when given, incase-insensitive name order."- [archived? creator-id]+ [archived? :- :boolean+ creator-id :- [:maybe ::lib.schema.id/user]](t2/select :model/Dashboard {:where [:and(when creator-id [:= :creator_id creator-id])[:= :archived archived?]]:order-by [:%lower.name]}))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn dashboard-parameters+(mu/defn dashboard-parameters"The id and parameters of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one [:model/Dashboard :id :parameters] dashboard-id))-(defn dashboard-name-columns+(mu/defn dashboard-name-columns"The name, description, and creator of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one [:model/Dashboard :name :description :creator_id] dashboard-id))-(defn dashboard-public-uuid+(mu/defn dashboard-public-uuid"The public uuid of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :public_uuid :model/Dashboard :id dashboard-id))-(defn public-dashboards+(mu/defn public-dashboards"The name, id, and public uuid of the unarchived Dashboards that are publicly shared."[](t2/select [:model/Dashboard :name :id :public_uuid], :public_uuid [:not= nil], :archived false))-(defn embeddable-dashboards+(mu/defn embeddable-dashboards"The name and id of the unarchived Dashboards with embedding enabled."[](t2/select [:model/Dashboard :name :id], :enable_embedding true, :archived false))-(defn insert-dashboard!+(mu/defn insert-dashboard!"Insert the Dashboard `row` and return the inserted instance."- [row]+ [row :- ::dashboards.schema/dashboard.update](t2/insert-returning-instance! :model/Dashboard row))-(defn update-dashboard!+(mu/defn update-dashboard!"Apply `changes` to the Dashboard with `dashboard-id`."- [dashboard-id changes]+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- ::dashboards.schema/dashboard.update](t2/update! :model/Dashboard dashboard-id changes))-(defn delete-dashboard!+(mu/defn delete-dashboard!"Delete the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/delete! :model/Dashboard :id dashboard-id))-(defn parameter-card-ids+(mu/defn parameter-card-ids"The Card ids of the ParameterCards of the `parameterized-object-type` with `parameterized-object-id`."- [parameterized-object-type parameterized-object-id]+ [parameterized-object-type :- [:or :keyword :string]+ parameterized-object-id :- ms/PositiveInt](t2/select-fn-vec :card_id :model/ParameterCard:parameterized_object_type parameterized-object-type:parameterized_object_id parameterized-object-id))-(defn dashboard-card-ids+(mu/defn dashboard-card-ids"The Card ids of the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :card_id :model/DashboardCard :dashboard_id dashboard-id))-(defn dashboard-item-cards+(mu/defn dashboard-item-cards"The card items of the Dashboard with `dashboard-id`, for the `/:id/items` endpoint: id, name, description, entityid, collection position, display, collection preview, last-used-at, collection id, archived flags, database id,and moderated status. Paged by `limit`/`offset` when `paged?`."- [dashboard-id paged? limit offset]+ [dashboard-id :- ::lib.schema.id/dashboard+ paged? :- :boolean+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](app-db/query(cond-> {:select [:c.id :c.name :c.description :c.entity_id :c.collection_position :c.display :c.collection_preview:last_used_at :c.collection_id :c.archived_directly :c.archived :c.database_id@@ -103,116 +114,119 @@[:= :c.archived false]]}paged? (merge {:limit limit :offset offset}))))-(defn dashboard-series-card-ids+(mu/defn dashboard-series-card-ids"The Card ids of the DashboardCardSeries of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :card_id :model/DashboardCardSeries{:where [:in :dashboardcard_id^:allow-subquery {:select [:id]:from [(t2/table-name :model/DashboardCard)]:where [:= :dashboard_id dashboard-id]}]}))-(defn dashboard-action-ids+(mu/defn dashboard-action-ids"The Action ids of the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :action_id :model/DashboardCard :dashboard_id dashboard-id))-(defn query-average-execution-times+(mu/defn query-average-execution-times"A map of query hash to average execution time for the Queries with `query-hashes`."- [query-hashes]+ [query-hashes :- [:sequential bytes?]](t2/select-fn->fn :query_hash :average_execution_time :model/Query :query_hash [:in query-hashes]))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-query- "The query of the Card with `card-id`, or nil."- [card-id]+(mu/defn card-query+ "The query of the Card with `card-id`, or nil (also nil for virtual dashcards, which have no Card id)."+ [card-id :- [:maybe ::lib.schema.id/card]](t2/select-one-fn :dataset_query :model/Card :id card-id))-(defn card-queries+(mu/defn card-queries"A map of Card id to query for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn :dataset_query :model/Card :id [:in card-ids]))-(defn unarchived-dashboard-question-exists?+(mu/defn unarchived-dashboard-question-exists?"Whether an unarchived Card internal to the Dashboard with `dashboard-id` exists."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/exists? :model/Card :dashboard_id dashboard-id :archived false))-(defn card-internal-to-other-dashboard-exists?+(mu/defn card-internal-to-other-dashboard-exists?"Whether any of the Cards with `card-ids` is internal to a Dashboard other than `dashboard-id`."- [dashboard-id card-ids]+ [dashboard-id :- ::lib.schema.id/dashboard+ card-ids :- [:set ::lib.schema.id/card]](t2/exists? :model/Card{:where [:and[:not= :dashboard_id dashboard-id][:not= :dashboard_id nil][:in :id card-ids]]}))-(defn insert-dashboard-tabs!+(mu/defn insert-dashboard-tabs!"Insert the DashboardTab `rows` and return their ids."- [rows]+ [rows :- [:sequential (mut/select-keys ::dashboards.schema/dashboard-tab.update [:dashboard_id :name :position])]](t2/insert-returning-pks! :model/DashboardTab rows))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-one :model/Collection :id collection-id))-(defn personal-collection-for-user+(mu/defn personal-collection-for-user"The personal Collection of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/Collection :personal_owner_id user-id))-(defn dashcard+(mu/defn dashcard"The DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one :model/DashboardCard dashcard-id))-(defn dashcard-in-dashboard+(mu/defn dashcard-in-dashboard"The DashboardCard with `dashcard-id` on the Dashboard with `dashboard-id`, or nil."- [dashcard-id dashboard-id]+ [dashcard-id :- ::lib.schema.id/dashcard+ dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/DashboardCard :id dashcard-id :dashboard_id dashboard-id))-(defn dashcards-by-ids+(mu/defn dashcards-by-ids"The DashboardCards with `dashcard-ids`."- [dashcard-ids]+ [dashcard-ids :- [:sequential ::lib.schema.id/dashcard]](t2/select :model/DashboardCard :id [:in dashcard-ids]))-(defn dashcard-parameter-mappings+(mu/defn dashcard-parameter-mappings"A map of DashboardCard id to parameter mappings for the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-pk->fn :parameter_mappings :model/DashboardCard :dashboard_id dashboard-id))-(defn dashcard-card-ids-by-id+(mu/defn dashcard-card-ids-by-id"A map of DashboardCard id to Card id for the DashboardCards with `dashcard-ids` of the Dashboard with`dashboard-id`."- [dashboard-id dashcard-ids]+ [dashboard-id :- ::lib.schema.id/dashboard+ dashcard-ids :- [:set ::lib.schema.id/dashcard]](t2/select-pk->fn :card_id :model/DashboardCard :dashboard_id dashboard-id :id [:in dashcard-ids]))-(defn user-name-and-email+(mu/defn user-name-and-email"The name and email of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :first_name :last_name :email] user-id))-(defn user-names-and-emails+(mu/defn user-names-and-emails"The names and emails of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select [:model/User :first_name :last_name :email] :id [:in user-ids]))-(defn pulse-channels-for-pulse+(mu/defn pulse-channels-for-pulse"The id, type, and details of the PulseChannels of the Pulse with `pulse-id`."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select [:model/PulseChannel :id :channel_type :details] :pulse_id [:= pulse-id]))-(defn pulse-channel-recipients+(mu/defn pulse-channel-recipients"The PulseChannelRecipients of the PulseChannel with `pulse-channel-id`."- [pulse-channel-id]+ [pulse-channel-id :- ms/PositiveInt](t2/select :model/PulseChannelRecipient :pulse_channel_id pulse-channel-id))-(defn unarchived-pulses-for-dashboard+(mu/defn unarchived-pulses-for-dashboard"The unarchived Pulses of the Dashboard with `dashboard-id`, in id order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/Pulse :dashboard_id dashboard-id :archived false {:order-by [[:id :asc]]}))diff --git a/src/metabase/data_studio/db.clj b/src/metabase/data_studio/db.cljindex 7a08b5993f4..362a935f2de 100644--- a/src/metabase/data_studio/db.clj+++ b/src/metabase/data_studio/db.clj@@ -3,8 +3,20 @@additional logic, so the rest of the module never talks to `toucan2.core` itself."(:require[clojure.string :as str]+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema][toucan2.core :as t2]))+(def ^:private TableSelectors+ "A map of `:database_ids`, `:schema_ids`, and/or `:table_ids` picking out a set of Tables."+ [:map {:closed true}+ [:database_ids {:optional true} [:or [:set ::lib.schema.id/database] [:sequential ::lib.schema.id/database]]]+ [:schema_ids {:optional true} [:or [:set :string] [:sequential :string]]]+ [:table_ids {:optional true} [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]])+(defn- table-selectors-where"The `:where` clause for the Tables picked out by `selectors`, a map of `:database_ids`, `:schema_ids`,and/or `:table_ids`."@@ -17,11 +29,14 @@(seq table_ids) (conj [:in :id (sort table_ids)])(seq schema_ids) (conj (into [:or] (map schema-expr) (sort schema_ids))))))-(defn fk-remapped-table-ids-for-tables+(mu/defn fk-remapped-table-ids-for-tables"The `:table_id`s reached through external Dimensions from the Tables with `table-ids`, reading the Table idof the `input-table-id` column and returning that of the `output-table-id` column, excluding ids already in`exclude-table-ids`."- [input-table-id output-table-id table-ids exclude-table-ids]+ [input-table-id :- :keyword+ output-table-id :- :keyword+ table-ids :- [:set ::lib.schema.id/table]+ exclude-table-ids :- [:set ::lib.schema.id/table]](t2/reducible-query {:select [[output-table-id :table_id]]:from [[(t2/table-name :model/Dimension) :dim]]:join [[(t2/table-name :model/Field) :source_field]@@ -33,11 +48,13 @@[:in input-table-id table-ids][:not [:in output-table-id exclude-table-ids]]]}))-(defn fk-remapped-table-ids-for-selectors+(mu/defn fk-remapped-table-ids-for-selectors"The `:table_id`s reached through external Dimensions from the Tables picked out by `selectors`, reading theTable id of the `input-table-id` column and returning that of the `output-table-id` column, excluding Tablesalso picked out by `selectors`."- [input-table-id output-table-id selectors]+ [input-table-id :- :keyword+ output-table-id :- :keyword+ selectors :- TableSelectors](let [where (table-selectors-where selectors)](t2/reducible-query {:select [[output-table-id :table_id]]:from [[(t2/table-name :model/Dimension) :dim]]@@ -54,50 +71,52 @@:from [(t2/table-name :model/Table)]:where [:and where [:= :id output-table-id]]}]]]})))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn databases+(mu/defn databases"The Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select :model/Database :id [:in database-ids]))-(defn tables-matching-selectors+(mu/defn tables-matching-selectors"The Tables picked out by `selectors`, a map of `:database_ids`, `:schema_ids`, and/or `:table_ids`."- [selectors]+ [selectors :- TableSelectors](t2/select :model/Table {:where (table-selectors-where selectors)}))-(defn tables-matching-selectors-in-id-order+(mu/defn tables-matching-selectors-in-id-order"The Tables picked out by `selectors`, in id order."- [selectors]+ [selectors :- TableSelectors](t2/select :model/Table {:where (table-selectors-where selectors), :order-by [[:id]]}))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn update-tables!- "Apply `changes` to the Tables with `table-ids`."- [table-ids changes]+(mu/defn update-tables!+ "Apply `changes` to the Tables with `table-ids`, returning the number updated."+ [table-ids :- [:set ::lib.schema.id/table]+ changes :- (mut/select-keys ::warehouse-schema.schema/table.update [:data_authority :data_source :data_layer :entity_type :owner_email :owner_user_id])](t2/update! :model/Table [:in table-ids] changes))-(defn selection-columns-for-selectors+(mu/defn selection-columns-for-selectors"Up to `limit` id, database, name, schema, and published flag of the Tables picked out by `selectors`."- [selectors limit]+ [selectors :- TableSelectors+ limit :- ms/PositiveInt](t2/select [:model/Table :id :db_id :name :display_name :schema :is_published]{:where (table-selectors-where selectors), :limit limit}))-(defn selection-columns-for-tables+(mu/defn selection-columns-for-tables"The id, database, name, schema, and published flag of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Table :id :db_id :name :display_name :schema :is_published] :id [:in table-ids]))-(defn delete-field-values-for-tables!- "Delete the FieldValues of the Fields of the Tables with `table-ids`."- [table-ids]+(mu/defn delete-field-values-for-tables!+ "Delete the FieldValues of the Fields of the Tables with `table-ids`, returning the number deleted."+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/delete! (t2/table-name :model/FieldValues):field_id [:in ^:allow-subquery {:select [:id]:from [(t2/table-name :model/Field)]diff --git a/src/metabase/documents/api/document.clj b/src/metabase/documents/api/document.cljindex 84643d82e26..52ed825a580 100644--- a/src/metabase/documents/api/document.clj+++ b/src/metabase/documents/api/document.clj@@ -411,7 +411,7 @@{}cards-to-copy)))-(api.macros/defendpoint :post "/:from-document-id/copy" :- ::documents.schema/document+(api.macros/defendpoint :post "/:from-document-id/copy" :- [:map [:id ::documents.schema/document.id]]"Copy a Document."[{:keys [from-document-id]} :- [:map[:from-document-id ms/PositiveInt]]diff --git a/src/metabase/documents/db.clj b/src/metabase/documents/db.cljindex df07505559c..450191487d7 100644--- a/src/metabase/documents/db.clj+++ b/src/metabase/documents/db.clj@@ -3,20 +3,26 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[java-time.api :as t]+ [malli.util :as mut][metabase.collections.models.collection :as collection]+ [metabase.documents.schema :as documents.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn document+(mu/defn document"The Document with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Document :id id))-(defn unarchived-document+(mu/defn unarchived-document"The Document with `id` if it is not archived, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Document :id id :archived false))-(defn visible-unarchived-documents+(mu/defn visible-unarchived-documents"The unarchived Documents not attached to an Exploration, in a Collection visible to the current user."[](t2/select :model/Document {:where [:and@@ -24,97 +30,103 @@[:= :archived false][:= :exploration_id nil]]}))-(defn insert-document!+(mu/defn insert-document!"Insert the Document `row` and return its id."- [row]+ [row :- ::documents.schema/document.update](t2/insert-returning-pk! :model/Document row))-(defn update-document!- "Apply `changes` to the Document with `id`."- [id changes]+(mu/defn update-document!+ "Apply `changes` to the Document with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- ::documents.schema/document.update](t2/update! :model/Document id changes))-(defn delete-document!- "Delete the Document with `id`."- [id]+(mu/defn delete-document!+ "Delete the Document with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/Document :id id))-(defn document-public-uuid+(mu/defn document-public-uuid"The public uuid of the Document with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one-fn :public_uuid :model/Document :id id))-(defn document-exploration-id+(mu/defn document-exploration-id"The Exploration id of the Document with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one-fn :exploration_id :model/Document :id id))-(defn public-documents+(mu/defn public-documents"The name, id, and public uuid of the unarchived Documents that are publicly shared."[](t2/select [:model/Document :name :id :public_uuid], :public_uuid [:not= nil], :archived false))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card card-id))-(defn cards-not-in-document+(mu/defn cards-not-in-document"The Cards with `card-ids` that do not belong to the Document with `document-id`."- [card-ids document-id]+ [card-ids :- [:sequential ::lib.schema.id/card]+ document-id :- ms/PositiveInt](t2/select :model/Card {:where [:and [:in :id card-ids][:or [:<> :document_id document-id][:= :document_id nil]]]}))-(defn cards-for-document+(mu/defn cards-for-document"The Cards of the Document with `document-id`."- [document-id]+ [document-id :- ms/PositiveInt](t2/select :model/Card :document_id document-id))-(defn unarchived-cards-for-documents+(mu/defn unarchived-cards-for-documents"The unarchived Cards of the Documents with `document-ids`."- [document-ids]+ [document-ids :- [:sequential ms/PositiveInt]](t2/select :model/Card :document_id [:in document-ids] :archived false))-(defn unarchived-card-in-document-exists?+(mu/defn unarchived-card-in-document-exists?"Whether the unarchived Card with `card-id` belongs to the Document with `document-id`."- [card-id document-id]+ [card-id :- ::lib.schema.id/card+ document-id :- ms/PositiveInt](t2/exists? :model/Card :id card-id :document_id document-id :archived false))-(defn update-card!- "Apply `changes` to the Card with `card-id`."- [card-id changes]+(mu/defn update-card!+ "Apply `changes` to the Card with `card-id`, returning the number updated."+ [card-id :- ::lib.schema.id/card+ changes :- (mut/select-keys ::queries.schema/card.update [:archived :archived_directly])](t2/update! :model/Card card-id changes))-(defn update-cards-for-document!- "Apply `changes` to the Cards of the Document with `document-id`."- [document-id changes]+(mu/defn update-cards-for-document!+ "Apply `changes` to the Cards of the Document with `document-id`, returning the number updated."+ [document-id :- ms/PositiveInt+ changes :- (mut/select-keys ::queries.schema/card.update [:collection_id :archived :archived_directly])](t2/update! :model/Card :document_id document-id changes))-(defn unarchived-collection-exists?+(mu/defn unarchived-collection-exists?"Whether an unarchived Collection with `collection-id` exists."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/exists? :model/Collection :id collection-id :archived false))-(defn user-columns+(mu/defn user-columns"The id, email, and name of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn table+(mu/defn table"The Table with `id`, or nil."- [id]+ [id :- ::lib.schema.id/table](t2/select-one :model/Table :id id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `id`, or nil."- [id]+ [id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id id))-(defn update-documents-last-viewed-at!+(mu/defn update-documents-last-viewed-at!"Move `last_viewed_at` of each Document in `document-id->timestamp` forward to its timestamp, without touching- `updated_at`."- [document-id->timestamp]+ `updated_at`, returning the number updated (as a one-element sequence -- `t2/query` on an UPDATE, unlike+ `t2/update!`, does not unwrap it)."+ [document-id->timestamp :- [:map-of ms/PositiveInt ms/TemporalInstant]];; A raw update rather than `t2/update!` so Toucan 2 model hooks don't fire: the :model/Document after-update;; publishes :event/document-update and syncs card collections, side effects that must not re-run on retry.(t2/query {:update (t2/table-name :model/Document)diff --git a/src/metabase/documents/schema.clj b/src/metabase/documents/schema.cljindex 0e91c611e57..45826535fb7 100644--- a/src/metabase/documents/schema.clj+++ b/src/metabase/documents/schema.clj@@ -1,12 +1,58 @@(ns metabase.documents.schema(:require- [metabase.util.malli.registry :as mr]))+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::document.id"Valid Document ID"pos-int?)+(mr/def ::document.document+ "The `:document` column of a Document, decoded."+ :map)+(mr/def ::document- "Schema for an instance of a `:model/Document`."- [:map- [:id ::document.id]])+ "A Document as selected from the app DB: every column of `:document`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:created_at ms/TemporalInstant]+ [:document [:maybe ::document.document]]+ [:content_type [:or :keyword :string]]+ [:creator_id ::lib.schema.id/user]+ [:updated_at ms/TemporalInstant]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:archived :boolean]+ [:archived_directly [:maybe :boolean]]+ [:entity_id :string]+ [:last_viewed_at ms/TemporalInstant]+ [:view_count :int]+ [:collection_position [:maybe :int]]+ [:public_uuid [:maybe :string]]+ [:made_public_by_id [:maybe ms/PositiveInt]]+ [:public_uuid_prefix [:maybe :string]]+ [:exploration_id [:maybe ms/PositiveInt]]+ [:is_placeholder :boolean]])++(mr/def ::document.update+ "What an update (or insert) of a Document accepts: every column of `:document` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:document {:optional true} [:maybe ::document.document]]+ [:content_type {:optional true} [:maybe [:or :keyword :string]]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:archived {:optional true} [:maybe :boolean]]+ [:archived_directly {:optional true} [:maybe :boolean]]+ [:entity_id {:optional true} [:maybe :string]]+ [:last_viewed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:view_count {:optional true} [:maybe :int]]+ [:collection_position {:optional true} [:maybe :int]]+ [:public_uuid {:optional true} [:maybe :string]]+ [:made_public_by_id {:optional true} [:maybe ms/PositiveInt]]+ [:public_uuid_prefix {:optional true} [:maybe :string]]+ [:exploration_id {:optional true} [:maybe ms/PositiveInt]]+ [:is_placeholder {:optional true} [:maybe :boolean]]])diff --git a/src/metabase/driver/db.clj b/src/metabase/driver/db.cljindex 5693525fc67..57e899abcdc 100644--- a/src/metabase/driver/db.clj+++ b/src/metabase/driver/db.clj@@ -2,27 +2,29 @@"Application database queries for the driver module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2]))-(defn databases+(mu/defn databases"Every Database."[](t2/select :model/Database))-(defn table-field-names+(mu/defn table-field-names"The names of the Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn-vec :name [:model/Field :name] :table_id table-id))-(defn database-connection-details+(mu/defn database-connection-details"The engine and connection details of the Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one [:model/Database :id :engine :details :write_data_details :admin_details] :id database-id))-(defn json-field-names-with-unfolding-disabled+(mu/defn json-field-names-with-unfolding-disabled"The names of the JSON Fields of the Table with `table-id` that have JSON unfolding disabled."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn-set :name [:model/Field :name]:table_id table-id:base_type :type/JSONdiff --git a/src/metabase/eid_translation/db.clj b/src/metabase/eid_translation/db.cljindex 58694630bc5..b2661915965 100644--- a/src/metabase/eid_translation/db.clj+++ b/src/metabase/eid_translation/db.clj@@ -2,103 +2,104 @@"Application database queries for the entity id translation module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))(defn- ids-by-entity-ids[model entity-ids](t2/select-fn->fn :entity_id :id [model :id :entity_id] :entity_id [:in entity-ids]))-(defn action-ids-by-entity-ids+(mu/defn action-ids-by-entity-ids"A map of entity id to id for the Actions whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Action entity-ids))-(defn card-ids-by-entity-ids+(mu/defn card-ids-by-entity-ids"A map of entity id to id for the Cards whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Card entity-ids))-(defn collection-ids-by-entity-ids+(mu/defn collection-ids-by-entity-ids"A map of entity id to id for the Collections whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Collection entity-ids))-(defn dashboard-ids-by-entity-ids+(mu/defn dashboard-ids-by-entity-ids"A map of entity id to id for the Dashboards whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Dashboard entity-ids))-(defn dashboard-card-ids-by-entity-ids+(mu/defn dashboard-card-ids-by-entity-ids"A map of entity id to id for the DashboardCards whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/DashboardCard entity-ids))-(defn dashboard-tab-ids-by-entity-ids+(mu/defn dashboard-tab-ids-by-entity-ids"A map of entity id to id for the DashboardTabs whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/DashboardTab entity-ids))-(defn dimension-ids-by-entity-ids+(mu/defn dimension-ids-by-entity-ids"A map of entity id to id for the Dimensions whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Dimension entity-ids))-(defn document-ids-by-entity-ids+(mu/defn document-ids-by-entity-ids"A map of entity id to id for the Documents whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Document entity-ids))-(defn exploration-ids-by-entity-ids+(mu/defn exploration-ids-by-entity-ids"A map of entity id to id for the Explorations whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Exploration entity-ids))-(defn measure-ids-by-entity-ids+(mu/defn measure-ids-by-entity-ids"A map of entity id to id for the Measures whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Measure entity-ids))-(defn permissions-group-ids-by-entity-ids+(mu/defn permissions-group-ids-by-entity-ids"A map of entity id to id for the PermissionsGroups whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/PermissionsGroup entity-ids))-(defn pulse-ids-by-entity-ids+(mu/defn pulse-ids-by-entity-ids"A map of entity id to id for the Pulses whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Pulse entity-ids))-(defn pulse-card-ids-by-entity-ids+(mu/defn pulse-card-ids-by-entity-ids"A map of entity id to id for the PulseCards whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/PulseCard entity-ids))-(defn pulse-channel-ids-by-entity-ids+(mu/defn pulse-channel-ids-by-entity-ids"A map of entity id to id for the PulseChannels whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/PulseChannel entity-ids))-(defn segment-ids-by-entity-ids+(mu/defn segment-ids-by-entity-ids"A map of entity id to id for the Segments whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Segment entity-ids))-(defn native-query-snippet-ids-by-entity-ids+(mu/defn native-query-snippet-ids-by-entity-ids"A map of entity id to id for the NativeQuerySnippets whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/NativeQuerySnippet entity-ids))-(defn timeline-ids-by-entity-ids+(mu/defn timeline-ids-by-entity-ids"A map of entity id to id for the Timelines whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Timeline entity-ids))-(defn transform-ids-by-entity-ids+(mu/defn transform-ids-by-entity-ids"A map of entity id to id for the Transforms whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/Transform entity-ids))-(defn user-ids-by-entity-ids+(mu/defn user-ids-by-entity-ids"A map of entity id to id for the Users whose entity id is in `entity-ids`."- [entity-ids]+ [entity-ids :- [:sequential :string]](ids-by-entity-ids :model/User entity-ids))diff --git a/src/metabase/embedding/db.clj b/src/metabase/embedding/db.cljindex f2956ab5931..a050e5eb26b 100644--- a/src/metabase/embedding/db.clj+++ b/src/metabase/embedding/db.clj@@ -2,14 +2,15 @@"Application database queries for the embedding module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn count-embedded-cards+(mu/defn count-embedded-cards"Number of Cards that have embedding enabled."[](t2/count :model/Card :enable_embedding true))-(defn count-embedded-dashboards+(mu/defn count-embedded-dashboards"Number of Dashboards that have embedding enabled."[](t2/count :model/Dashboard :enable_embedding true))diff --git a/src/metabase/embedding/schema.clj b/src/metabase/embedding/schema.cljnew file mode 100644index 00000000000..48c94f796fb--- /dev/null+++ b/src/metabase/embedding/schema.clj@@ -0,0 +1,28 @@+(ns metabase.embedding.schema+ "Malli schemas for the embedding module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::embedding-theme.settings+ "The `:settings` column of a EmbeddingTheme, decoded."+ :map)++(mr/def ::embedding-theme+ "A EmbeddingTheme as selected from the app DB: every column of `:embedding_theme`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:entity_id :string]+ [:name :string]+ [:settings ::embedding-theme.settings]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::embedding-theme.update+ "What an update (or insert) of a EmbeddingTheme accepts: every column of `:embedding_theme` except `id`, all optional."+ [:map {:closed true}+ [:entity_id {:optional true} [:maybe :string]]+ [:name {:optional true} [:maybe :string]]+ [:settings {:optional true} [:maybe ::embedding-theme.settings]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/embedding_rest/db.clj b/src/metabase/embedding_rest/db.cljindex 4daf1083054..968c5313791 100644--- a/src/metabase/embedding_rest/db.clj+++ b/src/metabase/embedding_rest/db.clj@@ -2,75 +2,81 @@"Application database queries for the embedding REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [malli.util :as mut]+ [metabase.embedding.schema :as embedding.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card card-id))-(defn card-embedding-params+(mu/defn card-embedding-params"The embedding parameters of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :embedding_params :model/Card :id card-id))-(defn card-embedding-flags+(mu/defn card-embedding-flags"The embedding-enabled and archived flags of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :enable_embedding :archived] :id card-id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard dashboard-id))-(defn dashboard-embedding-params+(mu/defn dashboard-embedding-params"The embedding parameters of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :embedding_params :model/Dashboard, :id dashboard-id))-(defn dashboard-embedding-flags+(mu/defn dashboard-embedding-flags"The embedding-enabled and archived flags of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one [:model/Dashboard :enable_embedding :archived] :id dashboard-id))-(defn dashcard+(mu/defn dashcard"The DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one :model/DashboardCard dashcard-id))-(defn embedding-themes+(mu/defn embedding-themes"The id, entity id, name, settings, and timestamps of every EmbeddingTheme, oldest first."[](t2/select :model/EmbeddingTheme {:order-by [[:created_at :asc]]:select [:id :entity_id :name :settings :created_at :updated_at]}))-(defn embedding-theme-exists?+(mu/defn embedding-theme-exists?"Whether an EmbeddingTheme with `id` exists."- [id]+ [id :- ms/PositiveInt](t2/exists? :model/EmbeddingTheme :id id))-(defn embedding-theme+(mu/defn embedding-theme"The EmbeddingTheme with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/EmbeddingTheme :id id))-(defn insert-embedding-theme!+(mu/defn insert-embedding-theme!"Insert the EmbeddingTheme `row` and return the inserted instance."- [row]+ [row :- (mut/select-keys ::embedding.schema/embedding-theme.update [:name :settings])](t2/insert-returning-instance! :model/EmbeddingTheme row))-(defn insert-embedding-themes!+(mu/defn insert-embedding-themes!"Insert the EmbeddingTheme `rows`."- [rows]+ [rows :- [:sequential (mut/select-keys ::embedding.schema/embedding-theme.update [:name :settings])]](t2/insert! :model/EmbeddingTheme rows))-(defn update-embedding-theme!+(mu/defn update-embedding-theme!"Apply `changes` to the EmbeddingTheme with `id`."- [id changes]+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::embedding.schema/embedding-theme.update [:name :settings])](t2/update! :model/EmbeddingTheme id changes))-(defn delete-embedding-theme!+(mu/defn delete-embedding-theme!"Delete the EmbeddingTheme with `id`."- [id]+ [id :- ms/PositiveInt](t2/delete! :model/EmbeddingTheme :id id))diff --git a/src/metabase/entity_retrieval/db.clj b/src/metabase/entity_retrieval/db.cljindex 3435a08db17..892837760a3 100644--- a/src/metabase/entity_retrieval/db.clj+++ b/src/metabase/entity_retrieval/db.clj@@ -2,11 +2,13 @@"Application database queries for the entity retrieval module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn ai-contexts-for-entities+(mu/defn ai-contexts-for-entities"The OsiAiContext rows for the given `[entity-type entity-local-id]` pairs."- [entity-type+ids]+ [entity-type+ids :- [:sequential [:tuple :string ms/PositiveInt]]];; row-value IN isn't portable across our app DBs, so match the wanted (type, id) pairs with OR-of-ANDs.(t2/select :model/OsiAiContext{:where (into [:or]diff --git a/src/metabase/explorations/db.clj b/src/metabase/explorations/db.cljindex 180867b20b8..0188a9194eb 100644--- a/src/metabase/explorations/db.clj+++ b/src/metabase/explorations/db.clj@@ -3,16 +3,23 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods,and transactions."(:require+ [malli.util :as mut][metabase.app-db.core :as mdb][metabase.collections.models.collection :as collection]+ [metabase.documents.schema :as documents.schema]+ [metabase.explorations.schema :as explorations.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.queries.core :as queries]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]));;; ----------------------------------------------- Exploration ------------------------------------------------(defn exploration+(mu/defn exploration"The Exploration with `exploration-id`, or nil."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/select-one :model/Exploration :id exploration-id))(defn- my-explorations-query@@ -68,139 +75,146 @@limit (assoc :limit limit)offset (assoc :offset offset))))-(defn my-explorations+(mu/defn my-explorations"The Explorations `user-id` created or edited, most-recently-touched first, each carrying`:current_user_last_touched_at` and a `:total_count` window column; paginated by `limit`/`offset`."- [user-id limit offset]+ [user-id :- ::lib.schema.id/user+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/Exploration (my-explorations-query user-id limit offset)))-(defn exploration-creator-id-for-thread+(mu/defn exploration-creator-id-for-thread"The creator of the Exploration owning the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one-fn :creator_id :model/Exploration{:join [:exploration_thread [:= :exploration_thread.exploration_id :exploration.id]]:where [:= :exploration_thread.id thread-id]}))-(defn insert-exploration!+(mu/defn insert-exploration!"Insert `exploration` and return the new instance."- [exploration]+ [exploration :- ::explorations.schema/exploration.update](first (t2/insert-returning-instances! :model/Exploration exploration)))-(defn update-exploration!+(mu/defn update-exploration!"Apply `changes` to the Exploration with `exploration-id`."- [exploration-id changes]+ [exploration-id :- ms/PositiveInt+ changes :- (mut/select-keys ::explorations.schema/exploration.update [:name :description :archived :archived_directly :collection_id :collection_position])](t2/update! :model/Exploration exploration-id changes))-(defn delete-exploration!+(mu/defn delete-exploration!"Delete the Exploration with `exploration-id`."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/delete! :model/Exploration :id exploration-id));;; ------------------------------------------------- Threads --------------------------------------------------(defn thread+(mu/defn thread"The ExplorationThread with `thread-id`, or nil."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one :model/ExplorationThread :id thread-id))-(defn thread-exploration-id-row+(mu/defn thread-exploration-id-row"The `:exploration_id` row of the ExplorationThread with `thread-id`, or nil."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one [:model/ExplorationThread :exploration_id] :id thread-id))-(defn thread-terminal-state+(mu/defn thread-terminal-state"The ID, cancel time, and completion time of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one [:model/ExplorationThread :id :canceled_at :completed_at] :id thread-id))-(defn thread-planning-state+(mu/defn thread-planning-state"The ID, cancel time, and analysis start time of the ExplorationThread with `thread-id`, or nil."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one [:model/ExplorationThread :id :canceled_at :analysis_started_at] :id thread-id))-(defn thread-exploration-id+(mu/defn thread-exploration-id"The Exploration ID of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one-fn :exploration_id :model/ExplorationThread :id thread-id))-(defn thread-prompt+(mu/defn thread-prompt"The prompt of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one-fn :prompt :model/ExplorationThread :id thread-id))-(defn thread-transcript+(mu/defn thread-transcript"The query-plan transcript of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-one-fn :query_plan_transcript :model/ExplorationThread :id thread-id))-(defn thread-in-exploration?+(mu/defn thread-in-exploration?"Whether the ExplorationThread with `thread-id` belongs to the Exploration with `exploration-id`."- [thread-id exploration-id]+ [thread-id :- ms/PositiveInt+ exploration-id :- ms/PositiveInt](t2/exists? :model/ExplorationThread :id thread-id :exploration_id exploration-id))-(defn thread-canceled?+(mu/defn thread-canceled?"Whether the ExplorationThread with `thread-id` has been canceled."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/exists? :model/ExplorationThread :id thread-id :canceled_at [:not= nil]))-(defn threads-for-explorations+(mu/defn threads-for-explorations"The ExplorationThreads of the Explorations with `exploration-ids`, in position order."- [exploration-ids]+ [exploration-ids :- [:sequential ms/PositiveInt]](t2/select :model/ExplorationThread:exploration_id [:in exploration-ids]{:order-by [[:position :asc] [:id :asc]]}))-(defn thread-ids-for-exploration+(mu/defn thread-ids-for-exploration"The IDs of the ExplorationThreads of the Exploration with `exploration-id`."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/select-pks-set :model/ExplorationThread :exploration_id exploration-id))-(defn lens-stamped-threads+(mu/defn lens-stamped-threads"The ID and data-access token of the ExplorationThreads among `thread-ids` that carry a token."- [thread-ids]+ [thread-ids :- [:set ms/PositiveInt]](t2/select [:model/ExplorationThread :id :data_access_token]:id [:in thread-ids]:data_access_token [:not= nil]))-(defn last-thread-position+(mu/defn last-thread-position"The highest position among the ExplorationThreads of the Exploration with `exploration-id`."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/select-one-fn :position :model/ExplorationThread:exploration_id exploration-id{:order-by [[:position :desc] [:id :desc]]}))-(defn lock-thread+(mu/defn lock-thread"The `:id` row of the ExplorationThread with `thread-id`, locked for update."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/query {:select [:id]:from [:exploration_thread]:where [:= :id thread-id]:for [:update]}))-(defn insert-thread!+(mu/defn insert-thread!"Insert `thread` and return the new instance."- [thread]+ [thread :- ::explorations.schema/exploration-thread.update](first (t2/insert-returning-instances! :model/ExplorationThread thread)))-(defn update-thread!+(mu/defn update-thread!"Apply `changes` to the ExplorationThread with `thread-id`."- [thread-id changes]+ [thread-id :- ms/PositiveInt+ changes :- (mut/select-keys ::explorations.schema/exploration-thread.update [:started_at :analysis_started_at :completed_at :query_plan_transcript])](t2/update! :model/ExplorationThread thread-id changes))-(defn cancel-thread!+(mu/defn cancel-thread!"Mark the uncompleted ExplorationThread with `thread-id` canceled and completed at `now`, returning the number ofrows updated."- [thread-id now]+ [thread-id :- ms/PositiveInt+ now :- ms/TemporalInstant](t2/update! :model/ExplorationThread:id thread-id:completed_at nil{:canceled_at now:completed_at now}))-(defn reset-terminal-thread!+(mu/defn reset-terminal-thread!"Reset the terminal ExplorationThread with `thread-id` to freshly started at `started-at`, if no query of it isstill running. Returns the number of rows updated."- [thread-id started-at]+ [thread-id :- ms/PositiveInt+ started-at :- ms/TemporalInstant](t2/query-one {:update :exploration_thread:set {:started_at started-at:query_plan_started_at nil@@ -217,10 +231,11 @@[:= :exploration_thread_id thread-id][:= :status "running"]]}]]}))-(defn claim-thread-analysis!+(mu/defn claim-thread-analysis!"Stamp the analysis start and completion of the ExplorationThread with `thread-id` at `now` if it has not beenclaimed, canceled, or left with pending queries. Returns the number of rows updated."- [thread-id now]+ [thread-id :- ms/PositiveInt+ now :- ms/TemporalInstant](t2/query-one {:update :exploration_thread:set {:analysis_started_at now:completed_at now}@@ -236,153 +251,167 @@;;; ------------------------------------------------- Blocks --------------------------------------------------(defn block+(mu/defn block"The ExplorationBlock with `block-id`, or nil."- [block-id]+ [block-id :- ms/PositiveInt](t2/select-one :model/ExplorationBlock :id block-id))-(defn block-thread-id-row+(mu/defn block-thread-id-row"The `:exploration_thread_id` row of the ExplorationBlock with `block-id`, or nil."- [block-id]+ [block-id :- ms/PositiveInt](t2/select-one [:model/ExplorationBlock :exploration_thread_id] :id block-id))-(defn block-metrics+(mu/defn block-metrics"The metric selections of the ExplorationBlock with `block-id`."- [block-id]+ [block-id :- ms/PositiveInt](t2/select-one-fn :metrics :model/ExplorationBlock :id block-id))-(defn block-for-page+(mu/defn block-for-page"The ExplorationBlock owning the ExplorationPage with `page-id`, or nil."- [page-id]+ [page-id :- ms/PositiveInt](t2/select-one :model/ExplorationBlock- {:join [[:exploration_page :p] [:= :p.exploration_block_id :exploration_block.id]]- :where [:= :p.id page-id]}))+ {:select [:exploration_block.*]+ :join [[:exploration_page :p] [:= :p.exploration_block_id :exploration_block.id]]+ :where [:= :p.id page-id]}))-(defn blocks-for-thread+(mu/defn blocks-for-thread"The ExplorationBlocks of the ExplorationThread with `thread-id`, in position order."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select :model/ExplorationBlock :exploration_thread_id thread-id {:order-by [[:position :asc] [:id :asc]]}))-(defn blocks-for-threads+(mu/defn blocks-for-threads"The ExplorationBlocks of the ExplorationThreads with `thread-ids`, in position order."- [thread-ids]+ [thread-ids :- [:sequential ms/PositiveInt]](t2/select :model/ExplorationBlock:exploration_thread_id [:in thread-ids]{:order-by [[:position :asc] [:id :asc]]}))-(defn block-metrics-for-threads-newest-first+(mu/defn block-metrics-for-threads-newest-first"The thread ID and metric selections of the ExplorationBlocks of the ExplorationThreads with `thread-ids`, inreverse position order."- [thread-ids]+ [thread-ids :- [:sequential ms/PositiveInt]](t2/select [:model/ExplorationBlock :exploration_thread_id :metrics]:exploration_thread_id [:in thread-ids]{:order-by [[:position :desc] [:id :desc]]}))-(defn block-ids-for-thread+(mu/defn block-ids-for-thread"The IDs of the ExplorationBlocks of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-pks-vec :model/ExplorationBlock :exploration_thread_id thread-id))-(defn insert-blocks!+(mu/defn insert-blocks!"Insert one ExplorationBlock map or a sequence of them."- [blocks]+ [blocks :- (let [row [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:exploration_thread_id {:optional true} [:maybe ms/PositiveInt]]+ [:metrics {:optional true} [:maybe [:sequential :map]]]+ [:dimensions {:optional true} [:maybe [:sequential :map]]]+ [:position {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]]]+ [:or row [:sequential row]])](t2/insert! :model/ExplorationBlock blocks));;; -------------------------------------------------- Pages ---------------------------------------------------(defn page+(mu/defn page"The ExplorationPage with `page-id`, or nil."- [page-id]+ [page-id :- ms/PositiveInt](t2/select-one :model/ExplorationPage :id page-id))-(defn page-block-id-row+(mu/defn page-block-id-row"The `:exploration_block_id` row of the ExplorationPage with `page-id`, or nil."- [page-id]+ [page-id :- ms/PositiveInt](t2/select-one [:model/ExplorationPage :exploration_block_id] :id page-id))-(defn page-block-id+(mu/defn page-block-id"The block ID of the ExplorationPage with `page-id`."- [page-id]+ [page-id :- ms/PositiveInt](t2/select-one-fn :exploration_block_id :model/ExplorationPage :id page-id))-(defn page-id-for-key+(mu/defn page-id-for-key"The ID of the ExplorationPage of the given block, Card, dimension, and query type, or nil."- [block-id card-id dimension-id query-type]+ [block-id :- ms/PositiveInt+ card-id :- [:maybe ::lib.schema.id/card]+ dimension-id :- [:or :string :int]+ query-type :- [:maybe :string]](t2/select-one-pk :model/ExplorationPage:exploration_block_id block-id:card_id card-id:dimension_id dimension-id:query_type query-type))-(defn pages-for-blocks+(mu/defn pages-for-blocks"The ExplorationPages of the ExplorationBlocks with `block-ids`."- [block-ids]+ [block-ids :- [:sequential ms/PositiveInt]](t2/select :model/ExplorationPage :exploration_block_id [:in block-ids]))-(defn page-ids-for-blocks+(mu/defn page-ids-for-blocks"The IDs of the ExplorationPages of the ExplorationBlocks with `block-ids`."- [block-ids]+ [block-ids :- [:sequential ms/PositiveInt]](t2/select-pks-vec :model/ExplorationPage :exploration_block_id [:in block-ids]))-(defn starred-page-ids+(mu/defn starred-page-ids"The IDs of the starred ExplorationPages among `page-ids`."- [page-ids]+ [page-ids :- [:sequential ms/PositiveInt]](t2/select-pks-set :model/ExplorationPage :id [:in page-ids] :starred true))-(defn page-thread-ids+(mu/defn page-thread-ids"Rows of ExplorationPage `:id` and the `:thread_id` of its block, for the ExplorationThreads with `thread-ids`."- [thread-ids]+ [thread-ids :- [:sequential ms/PositiveInt]](t2/query {:select [[:p.id :id] [:b.exploration_thread_id :thread_id]]:from [[:exploration_page :p]]:join [[:exploration_block :b] [:= :b.id :p.exploration_block_id]]:where [:in :b.exploration_thread_id thread-ids]}))-(defn insert-page!+(mu/defn insert-page!"Insert `page` and return its ID."- [page]+ [page :- ::explorations.schema/exploration-page.update](t2/insert-returning-pk! :model/ExplorationPage page))-(defn update-page!+(mu/defn update-page!"Apply `changes` to the ExplorationPage with `page-id`."- [page-id changes]+ [page-id :- ms/PositiveInt+ changes :- (mut/select-keys ::explorations.schema/exploration-page.update [:starred])](t2/update! :model/ExplorationPage page-id changes))-(defn update-pages!+(mu/defn update-pages!"Apply `changes` to the ExplorationPages with `page-ids`."- [page-ids changes]+ [page-ids :- [:sequential ms/PositiveInt]+ changes :- (mut/select-keys ::explorations.schema/exploration-page.update [:hidden])](t2/update! :model/ExplorationPage :id [:in page-ids] changes))-(defn delete-pages!+(mu/defn delete-pages!"Delete the ExplorationPages with `page-ids`."- [page-ids]+ [page-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/ExplorationPage :id [:in page-ids]));;; ------------------------------------------------- Queries --------------------------------------------------(defn query+(mu/defn query"The ExplorationQuery with `query-id`, or nil."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one :model/ExplorationQuery :id query-id))-(defn query-thread-id-row+(mu/defn query-thread-id-row"The `:exploration_thread_id` row of the ExplorationQuery with `query-id`, or nil."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one [:model/ExplorationQuery :exploration_thread_id] :id query-id))-(defn query-thread-id+(mu/defn query-thread-id"The thread ID of the ExplorationQuery with `query-id`."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one-fn :exploration_thread_id :model/ExplorationQuery :id query-id))-(defn finished-query-thread-id+(mu/defn finished-query-thread-id"The thread ID of the ExplorationQuery with `query-id` if it has finished, or nil."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one-fn :exploration_thread_id :model/ExplorationQuery:id query-id :status [:in ["done" "error" "canceled"]]))-(defn runnable-query+(mu/defn runnable-query"The pending ExplorationQuery with `query-id` on an uncanceled thread, or nil."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one :model/ExplorationQuery{:select [:eq.*]:from [[:exploration_query :eq]]@@ -392,30 +421,31 @@[:= :eq.status "pending"][:= :et.canceled_at nil]]}))-(defn queries-for-threads+(mu/defn queries-for-threads"The ExplorationQueries of the ExplorationThreads with `thread-ids`, in position order."- [thread-ids]+ [thread-ids :- [:sequential ms/PositiveInt]](t2/select :model/ExplorationQuery:exploration_thread_id [:in thread-ids]{:order-by [[:position :asc] [:id :asc]]}))-(defn lens-stamped-queries+(mu/defn lens-stamped-queries"The ID, thread, Database, query, and data-access token of the ExplorationQueries with a query on theExplorationThreads with `thread-ids`, in ID order."- [thread-ids]+ [thread-ids :- [:set ms/PositiveInt]](t2/select [:model/ExplorationQuery :id :exploration_thread_id :database_id :dataset_query :data_access_token]:exploration_thread_id [:in thread-ids]:dataset_query [:not= nil]{:order-by [[:id :asc]]}))-(defn thread-has-queries?+(mu/defn thread-has-queries?"Whether the ExplorationThread with `thread-id` has any ExplorationQuery."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/exists? :model/ExplorationQuery :exploration_thread_id thread-id))-(defn query-count-in-exploration+(mu/defn query-count-in-exploration"The number of ExplorationQueries among `query-ids` belonging to the Exploration with `exploration-id`."- [exploration-id query-ids]+ [exploration-id :- ms/PositiveInt+ query-ids :- [:sequential ms/PositiveInt]](t2/count :model/ExplorationQuery{:where [:and[:in :id query-ids]@@ -424,12 +454,12 @@:from [:exploration_thread]:where [:= :exploration_id exploration-id]}]]}))-(defn pending-query-count+(mu/defn pending-query-count"The number of pending ExplorationQueries."[](t2/count :model/ExplorationQuery :status "pending"))-(defn oldest-pending-query-created-at+(mu/defn oldest-pending-query-created-at"The creation time of the oldest pending ExplorationQuery, or nil."[](t2/select-one-fn :created_at :model/ExplorationQuery@@ -437,15 +467,15 @@:order-by [[:created_at :asc]]:limit 1}))-(defn pending-query-ids-for-thread+(mu/defn pending-query-ids-for-thread"The IDs of the pending ExplorationQueries of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-pks-vec :model/ExplorationQuery :exploration_thread_id thread-id :status "pending"))-(defn lock-pending-query-ids-for-thread+(mu/defn lock-pending-query-ids-for-thread"The ids of the pending ExplorationQueries of the ExplorationThread with `thread-id`, locked forupdate (`SKIP LOCKED` outside H2, whose single-worker model neither needs nor supports it)."- [thread-id]+ [thread-id :- ms/PositiveInt](map :id (t2/query (cond-> {:select [:id]:from [:exploration_query]:where [:and@@ -453,98 +483,113 @@[:= :status "pending"]]}(not= :h2 (mdb/db-type)) (assoc :for [:update :skip-locked])))))-(defn page-ids-with-queries+(mu/defn page-ids-with-queries"The subset of `page-ids` that some ExplorationQuery points at."- [page-ids]+ [page-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :page_id :model/ExplorationQuery :page_id [:in page-ids]))-(defn insert-queries!+(mu/defn insert-queries!"Insert the ExplorationQuery `rows`."- [rows]+ [rows :- [:sequential+ ::explorations.schema/exploration-query.update]](t2/insert! :model/ExplorationQuery rows))-(defn update-query!+(mu/defn update-query!"Apply `changes` to the ExplorationQuery with `query-id`."- [query-id changes]+ [query-id :- ms/PositiveInt+ changes :- (mut/select-keys ::explorations.schema/exploration-query.update [:dataset_query :name :data_access_token :status :started_at :finished_at])](t2/update! :model/ExplorationQuery query-id changes))-(defn fail-pending-query!+(mu/defn fail-pending-query!"Mark the pending ExplorationQuery with `query-id` as errored with `message` at `finished-at`, returning the numberof rows updated."- [query-id message finished-at]+ [query-id :- ms/PositiveInt+ message :- :string+ finished-at :- ms/TemporalInstant](t2/update! :model/ExplorationQuery{:id query-id, :status "pending"}{:status "error":error_message message:finished_at finished-at}))-(defn cancel-pending-queries-for-thread!+(mu/defn cancel-pending-queries-for-thread!"Mark the pending ExplorationQueries of the ExplorationThread with `thread-id` canceled."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/update! :model/ExplorationQuery{:exploration_thread_id thread-id, :status "pending"}{:status "canceled"}))-(defn cancel-queries!- "Mark the ExplorationQueries with `query-ids` canceled."- [query-ids]+(mu/defn cancel-queries!+ "Mark the ExplorationQueries with `query-ids` canceled, returning the affected-row count in a one-element vector."+ [query-ids :- [:sequential ms/PositiveInt]](t2/query {:update (t2/table-name :model/ExplorationQuery):set {:status "canceled"}:where [:in :id query-ids]}))-(defn delete-queries-for-thread!+(mu/defn delete-queries-for-thread!"Delete the ExplorationQueries of the ExplorationThread with `thread-id`."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/delete! :model/ExplorationQuery :exploration_thread_id thread-id));;; ---------------------------------------------- Query results -----------------------------------------------(defn query-result+(mu/defn query-result"The ExplorationQueryResult of the ExplorationQuery with `query-id`, or nil."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one :model/ExplorationQueryResult :exploration_query_id query-id))-(defn query-result-stored-result-id+(mu/defn query-result-stored-result-id"The stored result ID of the ExplorationQueryResult of the ExplorationQuery with `query-id`."- [query-id]+ [query-id :- ms/PositiveInt](t2/select-one-fn :stored_result_id :model/ExplorationQueryResult :exploration_query_id query-id))-(defn query-result-exists?+(mu/defn query-result-exists?"Whether the ExplorationQuery with `query-id` has an ExplorationQueryResult."- [query-id]+ [query-id :- ms/PositiveInt](t2/exists? :model/ExplorationQueryResult :exploration_query_id query-id))-(defn query-result-scores+(mu/defn query-result-scores"The query ID and `score-column` of the ExplorationQueryResults of the ExplorationQueries with `query-ids`."- [score-column query-ids]+ [score-column :- :keyword+ query-ids :- [:sequential ms/PositiveInt]](t2/select [:model/ExplorationQueryResult :exploration_query_id score-column]:exploration_query_id [:in query-ids]))-(defn query-result-row-counts+(mu/defn query-result-row-counts"The query ID and stored row count of the ExplorationQueryResults of the ExplorationQueries with `query-ids`."- [query-ids]+ [query-ids :- [:sequential ms/PositiveInt]](t2/select [:model/ExplorationQueryResult:exploration_query_result.exploration_query_id[:stored_result.row_count :row_count]]{:join [:stored_result [:= :stored_result.id :exploration_query_result.stored_result_id]]:where [:in :exploration_query_result.exploration_query_id query-ids]}))-(defn insert-query-result!+(mu/defn insert-query-result!"Insert `query-result`."- [query-result]+ [query-result :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:exploration_query_id {:optional true} [:maybe ms/PositiveInt]]+ [:stored_result_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:interestingness_score {:optional true} [:maybe number?]]+ [:contextual_interestingness_score {:optional true} [:maybe number?]]+ [:chart_stats {:optional true} [:maybe :map]]+ [:metric_description {:optional true} [:maybe :string]]+ [:chart_description {:optional true} [:maybe :string]]]](t2/insert! :model/ExplorationQueryResult query-result));;; ---------------------------------------------- Stored results -----------------------------------------------(defn stored-result+(mu/defn stored-result"The StoredResult with `stored-result-id`, or nil."- [stored-result-id]+ [stored-result-id :- ms/PositiveInt](t2/select-one :model/StoredResult :id stored-result-id))-(defn orphaned-stored-result-ids+(mu/defn orphaned-stored-result-ids"Up to `limit` `:id` rows of the StoredResults created before `created-before` that no ExplorationQueryResult orCard embed reaches, in ID order."- [created-before limit]+ [created-before :- ms/TemporalInstant+ limit :- ms/PositiveInt](t2/query {:select [:sr.id]:from [[:stored_result :sr]]:where [:and@@ -560,55 +605,61 @@:order-by [[:sr.id :asc]]:limit limit}))-(defn insert-stored-result!+(mu/defn insert-stored-result!"Insert `stored-result` and return its ID."- [stored-result]+ [stored-result :- ::queries.schema/stored-result.update](first (t2/insert-returning-pks! :model/StoredResult stored-result)))-(defn insert-stored-result-use!+(mu/defn insert-stored-result-use!"Insert `stored-result-use`."- [stored-result-use]+ [stored-result-use :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:stored_result_id {:optional true} [:maybe ms/PositiveInt]]+ [:exploration_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]]](t2/insert! :model/StoredResultUse stored-result-use))-(defn delete-stored-results!+(mu/defn delete-stored-results!"Delete the StoredResults with `stored-result-ids`, returning the number deleted."- [stored-result-ids]+ [stored-result-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/StoredResult :id [:in stored-result-ids]));;; ------------------------------------------------ Timelines -------------------------------------------------(defn timelines+(mu/defn timelines"The Timelines with `timeline-ids`."- [timeline-ids]+ [timeline-ids :- [:set ms/PositiveInt]](t2/select :model/Timeline :id [:in timeline-ids]))-(defn thread-timelines-for-threads+(mu/defn thread-timelines-for-threads"The ExplorationThreadTimelines of the ExplorationThreads with `thread-ids`, in position order."- [thread-ids]+ [thread-ids :- [:sequential ms/PositiveInt]](t2/select :model/ExplorationThreadTimeline:exploration_thread_id [:in thread-ids]{:order-by [[:position :asc] [:id :asc]]}))-(defn thread-timeline-ids+(mu/defn thread-timeline-ids"The Timeline IDs selected on the ExplorationThread with `thread-id`, in position order."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/select-fn-vec :timeline_id :model/ExplorationThreadTimeline:exploration_thread_id thread-id{:order-by [[:position :asc] [:id :asc]]}))-(defn thread-timeline-names+(mu/defn thread-timeline-names"The `:name` rows of the Timelines selected on the ExplorationThread with `thread-id`, in position order."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/query {:select [[:t.name :name]]:from [[:exploration_thread_timeline :ett]]:left-join [[:timeline :t] [:= :t.id :ett.timeline_id]]:where [:= :ett.exploration_thread_id thread-id]:order-by [[:ett.position :asc]]}))-(defn thread-timeline-event-rows+(mu/defn thread-timeline-event-rows"The Timelines selected on the ExplorationThread with `thread-id` joined to their unarchived events, ordered byposition and event timestamp."- [thread-id]+ [thread-id :- ms/PositiveInt](t2/query {:select [[:t.id :timeline_id][:t.name :timeline_name][:t.description :timeline_description]@@ -626,61 +677,69 @@:where [:= :ett.exploration_thread_id thread-id]:order-by [[:ett.position :asc] [:te.timestamp :asc]]}))-(defn insert-thread-timelines!+(mu/defn insert-thread-timelines!"Insert the ExplorationThreadTimeline `rows`."- [rows]+ [rows :- [:sequential+ [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:exploration_thread_id {:optional true} [:maybe ms/PositiveInt]]+ [:timeline_id {:optional true} [:maybe ms/PositiveInt]]+ [:position {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]]]](t2/insert! :model/ExplorationThreadTimeline rows));;; ------------------------------------------------ Documents -------------------------------------------------(defn summary-document-columns+(mu/defn summary-document-columns"The wire-shape columns of the Document with `document-id`, or nil."- [document-id]+ [document-id :- ms/PositiveInt](t2/select-one [:model/Document:id :name :exploration_id :creator_id :content_type:created_at :updated_at :archived :is_placeholder]:id document-id))-(defn summary-documents-for-explorations+(mu/defn summary-documents-for-explorations"The wire-shape columns of the Summary Documents of the Explorations with `exploration-ids`, oldest first."- [exploration-ids]+ [exploration-ids :- [:sequential ms/PositiveInt]](t2/select [:model/Document:id :name :exploration_id :creator_id :content_type:created_at :updated_at :archived :is_placeholder]:exploration_id [:in exploration-ids]{:order-by [[:created_at :asc] [:id :asc]]}))-(defn unarchived-summary-document+(mu/defn unarchived-summary-document"The unarchived Summary Document of the Exploration with `exploration-id`, or nil."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/select-one :model/Document :exploration_id exploration-id :archived false))-(defn document-exploration-id+(mu/defn document-exploration-id"The Exploration ID of the Document with `document-id`."- [document-id]+ [document-id :- ms/PositiveInt](t2/select-one-fn :exploration_id :model/Document :id document-id))-(defn insert-document!+(mu/defn insert-document!"Insert `document`."- [document]+ [document :- ::documents.schema/document.update](t2/insert! :model/Document document))-(defn move-summary-documents!+(mu/defn move-summary-documents!"Move the Summary Documents of the Exploration with `exploration-id` to the Collection with `collection-id`."- [exploration-id collection-id]+ [exploration-id :- ms/PositiveInt+ collection-id :- [:maybe ::lib.schema.id/collection]](t2/update! :model/Document :exploration_id exploration-id {:collection_id collection-id}))-(defn archive-summary-documents!+(mu/defn archive-summary-documents!"Archive the unarchived Summary Documents of the Exploration with `exploration-id` as not archived directly."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/update! :model/Document:exploration_id exploration-id:archived false{:archived true, :archived_directly false}))-(defn unarchive-summary-documents!+(mu/defn unarchive-summary-documents!"Unarchive the Summary Documents of the Exploration with `exploration-id` that were archived with it."- [exploration-id]+ [exploration-id :- ms/PositiveInt](t2/update! :model/Document:exploration_id exploration-id:archived true@@ -689,43 +748,44 @@;;; ------------------------------------------------ Other models -------------------------------------------------(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-names+(mu/defn card-names"The ID and name of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :name] :id [:in card-ids]))-(defn card-description+(mu/defn card-description"The description of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :description :model/Card :id card-id))-(defn card-presentation+(mu/defn card-presentation"The name, description, display, and visualization settings of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :name :description :display :visualization_settings] :id card-id))-(defn card-queries+(mu/defn card-queries"The ID, schema, Database, and query of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :card_schema :database_id :dataset_query] :id [:in card-ids]))-(defn metric-cards-by-id+(mu/defn metric-cards-by-id"A map of ID to the planner columns of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-pk->fn identity[:model/Card :id :name :description :database_id :dataset_query :card_schema :dimensions:dimension_mappings]:id [:in card-ids]))-(defn metric-card-ids+(mu/defn metric-card-ids"The `:id`s of the Cards visible to the current user as metrics, restricted to `metric-ids` whennon-empty, with those in `library-collection-ids` sorted first and then alphabetically by name."- [metric-ids library-collection-ids]+ [metric-ids :- [:maybe [:sequential ::lib.schema.id/metric]]+ library-collection-ids :- [:maybe [:set ::lib.schema.id/collection]]](t2/select [:model/Card :id]{:where (cond-> (queries/visible-metric-cards-where-clause)(seq metric-ids) (as-> where [:and where [:in :id (vec metric-ids)]]))@@ -741,39 +801,39 @@[:id :name :description :collection_id :database_id :table_id :type :entity_id:card_schema :dataset_query :dimensions :dimension_mappings])-(defn metric-cards-for-explorations+(mu/defn metric-cards-for-explorations"The exploration-relevant columns of the metric Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select (into [:model/Card] exploration-card-columns) :id [:in card-ids] :type "metric"))-(defn cards-for-explorations+(mu/defn cards-for-explorations"The exploration-relevant columns of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select (into [:model/Card] exploration-card-columns) :id [:in card-ids]))-(defn library-metrics-root-collection+(mu/defn library-metrics-root-collection"The ID and location of the library metrics Collection of `type`, or nil."- [type]+ [type :- :string](t2/select-one [:model/Collection :id :location] :type type))-(defn segment-names+(mu/defn segment-names"A map of ID to name for the Segments with `segment-ids`."- [segment-ids]+ [segment-ids :- [:set ::lib.schema.id/segment]](t2/select-pk->fn :name [:model/Segment :id :name] :id [:in segment-ids]))-(defn segment-name+(mu/defn segment-name"The name of the Segment with `segment-id`."- [segment-id]+ [segment-id :- ::lib.schema.id/segment](t2/select-one-fn :name :model/Segment :id segment-id))-(defn user-summaries-by-id+(mu/defn user-summaries-by-id"A map of ID to the ID, email, and names of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn live-comment-targets+(mu/defn live-comment-targets"The child target IDs of the live exploration Comments anchored to one of `child-target-ids`."- [child-target-ids]+ [child-target-ids :- [:sequential :string]](t2/select-fn-set :child_target_id :model/Comment:target_type "exploration":child_target_id [:in child-target-ids]diff --git a/src/metabase/explorations/schema.clj b/src/metabase/explorations/schema.cljnew file mode 100644index 00000000000..c24563cd223--- /dev/null+++ b/src/metabase/explorations/schema.clj@@ -0,0 +1,255 @@+(ns metabase.explorations.schema+ "Malli schemas for the explorations module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::exploration+ "A Exploration as selected from the app DB: every column of `:exploration`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:description [:maybe :string]]+ [:creator_id ::lib.schema.id/user]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:archived :boolean]+ [:archived_directly :boolean]+ [:collection_position [:maybe :int]]+ [:entity_id [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::exploration.update+ "What an update (or insert) of a Exploration accepts: every column of `:exploration` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:archived {:optional true} [:maybe :boolean]]+ [:archived_directly {:optional true} [:maybe :boolean]]+ [:collection_position {:optional true} [:maybe :int]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::exploration-block.metric+ "One entry of the `:metrics` column of a ExplorationBlock, decoded."+ :map)++(mr/def ::exploration-block.dimension+ "One entry of the `:dimensions` column of a ExplorationBlock, decoded."+ :map)++(mr/def ::exploration-block+ "A ExplorationBlock as selected from the app DB: every column of `:exploration_block`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:exploration_thread_id ms/PositiveInt]+ [:metrics [:maybe [:sequential ::exploration-block.metric]]]+ [:dimensions [:maybe [:sequential ::exploration-block.dimension]]]+ [:position :int]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::exploration-block.update+ "What an update (or insert) of a ExplorationBlock accepts: every column of `:exploration_block` except `id`, all optional."+ [:map {:closed true}+ [:exploration_thread_id {:optional true} [:maybe ms/PositiveInt]]+ [:metrics {:optional true} [:maybe [:sequential ::exploration-block.metric]]]+ [:dimensions {:optional true} [:maybe [:sequential ::exploration-block.dimension]]]+ [:position {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::exploration-page+ "A ExplorationPage as selected from the app DB: every column of `:exploration_page`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:entity_id [:maybe :string]]+ [:exploration_block_id ms/PositiveInt]+ [:card_id ::lib.schema.id/card]+ [:dimension_id :string]+ [:query_type [:or :keyword :string]]+ [:position :int]+ [:starred :boolean]+ [:hidden :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::exploration-page.update+ "What an update (or insert) of a ExplorationPage accepts: every column of `:exploration_page` except `id`, all optional."+ [:map {:closed true}+ [:entity_id {:optional true} [:maybe :string]]+ [:exploration_block_id {:optional true} [:maybe ms/PositiveInt]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:dimension_id {:optional true} [:maybe :string]]+ [:query_type {:optional true} [:maybe [:or :keyword :string]]]+ [:position {:optional true} [:maybe :int]]+ [:starred {:optional true} [:maybe :boolean]]+ [:hidden {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::exploration-query.visualization-settings+ "The `:visualization_settings` column of a ExplorationQuery, decoded."+ :map)++(mr/def ::exploration-query.dataset-query+ "The `:dataset_query` column of a ExplorationQuery, decoded."+ :map)++(mr/def ::exploration-query.params+ "The `:params` column of a ExplorationQuery, decoded."+ :map)++(mr/def ::exploration-query.data-access-token+ "The `:data_access_token` column of a ExplorationQuery, decoded."+ :map)++(mr/def ::exploration-query+ "A ExplorationQuery as selected from the app DB: every column of `:exploration_query`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:exploration_thread_id ms/PositiveInt]+ [:page_id ms/PositiveInt]+ [:name [:maybe :string]]+ [:card_id ::lib.schema.id/card]+ [:database_id ::lib.schema.id/database]+ [:segment_id [:maybe ::lib.schema.id/segment]]+ [:dimension_id :string]+ [:query_type [:or :keyword :string]]+ [:display [:maybe [:or :keyword :string]]]+ [:visualization_settings [:maybe ::exploration-query.visualization-settings]]+ [:dataset_query [:maybe ::exploration-query.dataset-query]]+ [:params [:maybe ::exploration-query.params]]+ [:position :int]+ [:status [:or :keyword :string]]+ [:error_message [:maybe :string]]+ [:started_at [:maybe ms/TemporalInstant]]+ [:finished_at [:maybe ms/TemporalInstant]]+ [:entity_id [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:data_access_token [:maybe ::exploration-query.data-access-token]]])++(mr/def ::exploration-query.update+ "What an update (or insert) of a ExplorationQuery accepts: every column of `:exploration_query` except `id`, all optional."+ [:map {:closed true}+ [:exploration_thread_id {:optional true} [:maybe ms/PositiveInt]]+ [:page_id {:optional true} [:maybe ms/PositiveInt]]+ [:name {:optional true} [:maybe :string]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:segment_id {:optional true} [:maybe ::lib.schema.id/segment]]+ [:dimension_id {:optional true} [:maybe :string]]+ [:query_type {:optional true} [:maybe [:or :keyword :string]]]+ [:display {:optional true} [:maybe [:or :keyword :string]]]+ [:visualization_settings {:optional true} [:maybe ::exploration-query.visualization-settings]]+ [:dataset_query {:optional true} [:maybe ::exploration-query.dataset-query]]+ [:params {:optional true} [:maybe ::exploration-query.params]]+ [:position {:optional true} [:maybe :int]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:error_message {:optional true} [:maybe :string]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:finished_at {:optional true} [:maybe ms/TemporalInstant]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:data_access_token {:optional true} [:maybe ::exploration-query.data-access-token]]])++(mr/def ::exploration-query-result.chart-stats+ "The `:chart_stats` column of a ExplorationQueryResult, decoded."+ :map)++(mr/def ::exploration-query-result+ "A ExplorationQueryResult as selected from the app DB: every column of `:exploration_query_result`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:exploration_query_id ms/PositiveInt]+ [:stored_result_id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:interestingness_score [:maybe number?]]+ [:contextual_interestingness_score [:maybe number?]]+ [:chart_stats [:maybe ::exploration-query-result.chart-stats]]+ [:metric_description [:maybe :string]]+ [:chart_description [:maybe :string]]])++(mr/def ::exploration-query-result.update+ "What an update (or insert) of a ExplorationQueryResult accepts: every column of `:exploration_query_result` except `id`, all optional."+ [:map {:closed true}+ [:exploration_query_id {:optional true} [:maybe ms/PositiveInt]]+ [:stored_result_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:interestingness_score {:optional true} [:maybe number?]]+ [:contextual_interestingness_score {:optional true} [:maybe number?]]+ [:chart_stats {:optional true} [:maybe ::exploration-query-result.chart-stats]]+ [:metric_description {:optional true} [:maybe :string]]+ [:chart_description {:optional true} [:maybe :string]]])++(mr/def ::exploration-thread.query-plan-transcript+ "The `:query_plan_transcript` column of a ExplorationThread, decoded."+ :map)++(mr/def ::exploration-thread.data-access-token+ "The `:data_access_token` column of a ExplorationThread, decoded."+ :map)++(mr/def ::exploration-thread+ "A ExplorationThread as selected from the app DB: every column of `:exploration_thread`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:exploration_id ms/PositiveInt]+ [:name [:maybe :string]]+ [:prompt [:maybe :string]]+ [:position :int]+ [:source_page_id [:maybe ms/PositiveInt]]+ [:started_at [:maybe ms/TemporalInstant]]+ [:entity_id [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:completed_at [:maybe ms/TemporalInstant]]+ [:analysis_started_at [:maybe ms/TemporalInstant]]+ [:query_plan_started_at [:maybe ms/TemporalInstant]]+ [:query_plan_transcript [:maybe ::exploration-thread.query-plan-transcript]]+ [:canceled_at [:maybe ms/TemporalInstant]]+ [:data_access_token [:maybe ::exploration-thread.data-access-token]]])++(mr/def ::exploration-thread.update+ "What an update (or insert) of a ExplorationThread accepts: every column of `:exploration_thread` except `id`, all optional."+ [:map {:closed true}+ [:exploration_id {:optional true} [:maybe ms/PositiveInt]]+ [:name {:optional true} [:maybe :string]]+ [:prompt {:optional true} [:maybe :string]]+ [:position {:optional true} [:maybe :int]]+ [:source_page_id {:optional true} [:maybe ms/PositiveInt]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:completed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:analysis_started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:query_plan_started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:query_plan_transcript {:optional true} [:maybe ::exploration-thread.query-plan-transcript]]+ [:canceled_at {:optional true} [:maybe ms/TemporalInstant]]+ [:data_access_token {:optional true} [:maybe ::exploration-thread.data-access-token]]])++(mr/def ::exploration-thread-timeline+ "A ExplorationThreadTimeline as selected from the app DB: every column of `:exploration_thread_timeline`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:exploration_thread_id ms/PositiveInt]+ [:timeline_id ms/PositiveInt]+ [:position :int]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::exploration-thread-timeline.update+ "What an update (or insert) of a ExplorationThreadTimeline accepts: every column of `:exploration_thread_timeline` except `id`, all optional."+ [:map {:closed true}+ [:exploration_thread_id {:optional true} [:maybe ms/PositiveInt]]+ [:timeline_id {:optional true} [:maybe ms/PositiveInt]]+ [:position {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/glossary/db.clj b/src/metabase/glossary/db.cljindex 2fe42508374..6f8295e8762 100644--- a/src/metabase/glossary/db.clj+++ b/src/metabase/glossary/db.clj@@ -2,13 +2,17 @@"Application database queries for the glossary module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.glossary.schema :as glossary.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn glossary-entries+(mu/defn glossary-entries"The Glossary entries whose term or definition contains `search` case-insensitively, or everyentry when `search` is nil, in term order."- [search]+ [search :- [:maybe :string]](t2/select :model/Glossary(cond-> {:order-by [[:term :asc]]}search (assoc :where (let [pattern (h2x/like-substring search)]@@ -16,32 +20,34 @@[:like [:lower :term] pattern][:like [:lower :definition] pattern]])))))-(defn insert-glossary-entry!+(mu/defn insert-glossary-entry!"Insert the Glossary `row` and return the inserted instance."- [row]+ [row :- ::glossary.schema/glossary.update](t2/insert-returning-instance! :model/Glossary row))-(defn glossary-entry+(mu/defn glossary-entry"The Glossary entry with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Glossary :id id))-(defn update-glossary-entry!+(mu/defn update-glossary-entry!"Set the term and definition of the Glossary entry with `id`."- [id term definition]+ [id :- ms/PositiveInt+ term :- :string+ definition :- :string](t2/update! :model/Glossary id {:term term :definition definition}))-(defn delete-glossary-entry!+(mu/defn delete-glossary-entry!"Delete the Glossary entry with `id`."- [id]+ [id :- ms/PositiveInt](t2/delete! :model/Glossary :id id))-(defn users-by-id+(mu/defn users-by-id"A map of User id to the id, email, and name of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn glossary-entry-by-term+(mu/defn glossary-entry-by-term"The Glossary entry for `term`, or nil."- [term]+ [term :- :string](t2/select-one :model/Glossary :term term))diff --git a/src/metabase/glossary/schema.clj b/src/metabase/glossary/schema.cljnew file mode 100644index 00000000000..906f30f9523--- /dev/null+++ b/src/metabase/glossary/schema.clj@@ -0,0 +1,25 @@+(ns metabase.glossary.schema+ "Malli schemas for the glossary module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::glossary+ "A Glossary as selected from the app DB: every column of `:glossary`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:term :string]+ [:definition :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:creator_id ::lib.schema.id/user]])++(mr/def ::glossary.update+ "What an update (or insert) of a Glossary accepts: every column of `:glossary` except `id`, all optional."+ [:map {:closed true}+ [:term {:optional true} [:maybe :string]]+ [:definition {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]])diff --git a/src/metabase/health_inspector/db.clj b/src/metabase/health_inspector/db.cljindex 38e1a8006a4..46c21f33f4a 100644--- a/src/metabase/health_inspector/db.clj+++ b/src/metabase/health_inspector/db.clj@@ -2,32 +2,39 @@"Application database queries for the health inspector module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn unarchived-cards-reducible+(mu/defn unarchived-cards-reducible"A reducible of the unarchived `report_card` rows."[](t2/reducible-select :report_card {:where [:= :archived false]}))-(defn insert-run!+(mu/defn insert-run!"Insert the `health_inspector_runs` `row`."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} :int]+ [:message {:optional true} :string]+ [:check_name {:optional true} :string]+ [:run_at {:optional true} ms/TemporalInstant]+ [:health {:optional true} :int]]](t2/insert! :health_inspector_runs row))-(defn delete-runs-before!+(mu/defn delete-runs-before!"Delete the `health_inspector_runs` rows run before `run-before`."- [run-before]+ [run-before :- ms/TemporalInstant](t2/delete! :health_inspector_runs :run_at [:< run-before]))-(defn latest-run+(mu/defn latest-run"The `:health` and `:message` of the most recent `health_inspector_runs` row for `check-name`, or nil."- [check-name]- ;; Tie-break on id: back-to-back inserts can share a run_at, and run_at alone would then pick a non-deterministic+ [check-name :- :string]+ ;; Ty-break on id: back-to-back inserts can share a run_at, and run_at alone would then pick a non-deterministic;; row (id is a monotonic auto-increment PK).(t2/select-one [:health_inspector_runs :health :message] :check_name check-name{:order-by [[:run_at :desc] [:id :desc]]}))-(defn latest-runs+(mu/defn latest-runs"The `limit` most recent `health_inspector_runs` rows."- [limit]+ [limit :- ms/PositiveInt](t2/select :health_inspector_runs {:limit limit :order-by [[:run_at :desc]]}))diff --git a/src/metabase/indexed_entities/db.clj b/src/metabase/indexed_entities/db.cljindex 41b5d4f6f47..149a33412bc 100644--- a/src/metabase/indexed_entities/db.clj+++ b/src/metabase/indexed_entities/db.clj@@ -2,73 +2,83 @@"Application database queries for the indexed entities module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.indexed-entities.schema :as indexed-entities.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn model-index+(mu/defn model-index"The ModelIndex with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/ModelIndex :id id))-(defn model-indexes-for-model+(mu/defn model-indexes-for-model"The ModelIndexes of the model Card with `model-id`."- [model-id]+ [model-id :- ms/PositiveInt](t2/select :model/ModelIndex :model_id model-id))-(defn all-model-indexes+(mu/defn all-model-indexes"Every ModelIndex."[](t2/select :model/ModelIndex))-(defn model-indexes-except+(mu/defn model-indexes-except"The ModelIndexes whose id is not in `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/ModelIndex :id [:not-in ids]))-(defn delete-model-index!- "Delete the ModelIndex with `id`."- [id]+(mu/defn delete-model-index!+ "Delete the ModelIndex with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/ModelIndex id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn model-index-values+(mu/defn model-index-values"The ModelIndexValues of the ModelIndex with `model-index-id`."- [model-index-id]+ [model-index-id :- ms/PositiveInt](t2/select :model/ModelIndexValue :model_index_id model-index-id))-(defn model-index-values-reducible+(mu/defn model-index-values-reducible"A reducible of the ModelIndexValues of the ModelIndex with `model-index-id`."- [model-index-id]+ [model-index-id :- ms/PositiveInt](t2/reducible-select :model/ModelIndexValue :model_index_id model-index-id))-(defn mark-model-index-error!- "Record `error-message` on the ModelIndex with `id` and stamp its indexing time."- [id error-message]+(mu/defn mark-model-index-error!+ "Record `error-message` on the ModelIndex with `id` and stamp its indexing time, returning the number updated."+ [id :- ms/PositiveInt+ error-message :- :string](t2/update! :model/ModelIndex id {:state "error":error error-message:indexed_at :%now}))-(defn mark-model-index-indexed!- "Set the ModelIndex with `id` to `state`, clear its error, and stamp its indexing time."- [id state]+(mu/defn mark-model-index-indexed!+ "Set the ModelIndex with `id` to `state`, clear its error, and stamp its indexing time, returning the number+ updated."+ [id :- ms/PositiveInt+ state :- :string](t2/update! :model/ModelIndex id {:indexed_at :%now:error nil:state state}))-(defn delete-model-index-values!- "Delete the ModelIndexValues of the ModelIndex with `model-index-id` for the model primary keys `model-pks`."- [model-index-id model-pks]+(mu/defn delete-model-index-values!+ "Delete the ModelIndexValues of the ModelIndex with `model-index-id` for the model primary keys `model-pks`,+ returning the number deleted."+ [model-index-id :- ms/PositiveInt+ model-pks :- [:sequential :int]](t2/delete! :model/ModelIndexValue :model_index_id model-index-id :model_pk [:in model-pks]))-(defn insert-model-index-values!- "Insert the ModelIndexValue `rows`."- [rows]+(mu/defn insert-model-index-values!+ "Insert the ModelIndexValue `rows`, returning the number inserted."+ [rows :- [:sequential (mut/select-keys ::indexed-entities.schema/model-index-value.update [:model_index_id :model_pk :name])]](t2/insert! :model/ModelIndexValue rows))-(defn insert-model-index!+(mu/defn insert-model-index!"Insert the ModelIndex `row` and return the inserted instance."- [row]+ [row :- ::indexed-entities.schema/model-index.update](t2/insert-returning-instance! :model/ModelIndex [row]))diff --git a/src/metabase/indexed_entities/schema.clj b/src/metabase/indexed_entities/schema.cljnew file mode 100644index 00000000000..23162d4336d--- /dev/null+++ b/src/metabase/indexed_entities/schema.clj@@ -0,0 +1,55 @@+(ns metabase.indexed-entities.schema+ "Malli schemas for the indexed-entities module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::model-index.pk-ref+ "The `:pk_ref` column of a ModelIndex, decoded."+ vector?)++(mr/def ::model-index.value-ref+ "The `:value_ref` column of a ModelIndex, decoded."+ vector?)++(mr/def ::model-index+ "A ModelIndex as selected from the app DB: every column of `:model_index`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model_id [:maybe :int]]+ [:pk_ref ::model-index.pk-ref]+ [:value_ref ::model-index.value-ref]+ [:schedule :string]+ [:state :string]+ [:indexed_at [:maybe ms/TemporalInstant]]+ [:error [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:creator_id ::lib.schema.id/user]])++(mr/def ::model-index.update+ "What an update (or insert) of a ModelIndex accepts: every column of `:model_index` except `id`, all optional."+ [:map {:closed true}+ [:model_id {:optional true} [:maybe :int]]+ [:pk_ref {:optional true} [:maybe ::model-index.pk-ref]]+ [:value_ref {:optional true} [:maybe ::model-index.value-ref]]+ [:schedule {:optional true} [:maybe :string]]+ [:state {:optional true} [:maybe :string]]+ [:indexed_at {:optional true} [:maybe ms/TemporalInstant]]+ [:error {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]])++(mr/def ::model-index-value+ "A ModelIndexValue as selected from the app DB: every column of `:model_index_value`."+ [:map {:closed true}+ [:model_index_id [:maybe ms/PositiveInt]]+ [:model_pk :int]+ [:name :string]])++(mr/def ::model-index-value.update+ "What an update (or insert) of a ModelIndexValue accepts: every column of `:model_index_value` except `id`, all optional."+ [:map {:closed true}+ [:model_index_id {:optional true} [:maybe ms/PositiveInt]]+ [:model_pk {:optional true} [:maybe :int]]+ [:name {:optional true} [:maybe :string]]])diff --git a/src/metabase/indexes/db.clj b/src/metabase/indexes/db.cljindex f54564a0503..6e65ae2dc9a 100644--- a/src/metabase/indexes/db.clj+++ b/src/metabase/indexes/db.clj@@ -2,68 +2,81 @@"Application database queries for the indexes module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn table-indexes-for-transform+(def ^:private Status+ "Lifecycle states for a table index request. Mirrors `metabase.indexes.schema/statuses`; duplicated here (rather+ than required) to keep this namespace free of module-internal logic dependencies."+ [:enum :create-pending :update-pending :delete-pending :running :succeeded :failed])++(mu/defn table-indexes-for-transform"The TableIndexes of the Transform with `transform-id`, in id order."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select :model/TableIndex :transform_id transform-id {:order-by [[:id :asc]]}))-(defn applicable-table-indexes-for-transform+(mu/defn applicable-table-indexes-for-transform"The TableIndexes of the Transform with `transform-id` that are not pending deletion, in name order."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select :model/TableIndex:transform_id transform-id:status [:not= :delete-pending]{:order-by [[:index_name :asc]]}))-(defn running-table-indexes+(mu/defn running-table-indexes"The running TableIndexes among `ids` of the Transform with `transform-id`, in id order."- [transform-id ids]+ [transform-id :- ::lib.schema.id/transform+ ids :- [:sequential ms/PositiveInt]](t2/select :model/TableIndex:transform_id transform-id:id [:in ids]:status :running{:order-by [[:id :asc]]}))-(defn delete-pending-table-indexes+(mu/defn delete-pending-table-indexes"The TableIndexes of the Transform with `transform-id` that are pending deletion, in id order."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select :model/TableIndex:transform_id transform-id:status :delete-pending{:order-by [[:id :asc]]}))-(defn applicable-table-index+(mu/defn applicable-table-index"The TableIndex with `id` if it is not pending deletion, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/TableIndex :id id :status [:not= :delete-pending]))-(defn table-index-with-status-exists?+(mu/defn table-index-with-status-exists?"Whether the Transform with `transform-id` has a TableIndex in one of `statuses`."- [transform-id statuses]+ [transform-id :- ::lib.schema.id/transform+ statuses :- [:set Status]](t2/exists? :model/TableIndex :transform_id transform-id :status [:in statuses]))-(defn mark-table-indexes-running!+(mu/defn mark-table-indexes-running!"Set the TableIndexes among `ids` whose status is in `from-statuses` to running."- [ids from-statuses]+ [ids :- [:set ms/PositiveInt]+ from-statuses :- [:set Status]](t2/update! :model/TableIndex {:id [:in ids] :status [:in from-statuses]} {:status :running}))-(defn mark-running-table-indexes-failed!+(mu/defn mark-running-table-indexes-failed!"Set the running TableIndexes among `ids` to failed with `error-message`."- [ids error-message]+ [ids :- [:set ms/PositiveInt]+ error-message :- :string](t2/update! :model/TableIndex{:id [:in ids] :status :running}{:status :failed:error_message error-message:last_executed_at :%now}))-(defn mark-table-indexes-update-pending!+(mu/defn mark-table-indexes-update-pending!"Set the TableIndexes with `ids` to update-pending and clear their error."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/update! :model/TableIndex {:id [:in ids]} {:status :update-pending, :error_message nil}))-(defn table-index-exists-for-transform?+(mu/defn table-index-exists-for-transform?"Whether the Transform with `transform-id` has a TableIndex named `index-name`."- [transform-id index-name]+ [transform-id :- ::lib.schema.id/transform+ index-name :- :string](t2/exists? :model/TableIndex :transform_id transform-id :index_name index-name))diff --git a/src/metabase/indexes/schema.clj b/src/metabase/indexes/schema.cljindex 8de969782c3..017b40c10f1 100644--- a/src/metabase/indexes/schema.clj+++ b/src/metabase/indexes/schema.clj@@ -6,7 +6,9 @@`:direction`) to strings; [[keywordize-structured]] turns them back into keywords so a driver can dispatch on`:kind`."(:require- [metabase.util.malli.registry :as mr]))+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(set! *warn-on-reflection* true)@@ -99,3 +101,34 @@(def statuses"Valid lifecycle states for a table index request."#{:create-pending :update-pending :delete-pending :running :succeeded :failed})++(mr/def ::table-index.structured+ "The `:structured` column of a TableIndex, decoded."+ :map)++(mr/def ::table-index+ "A TableIndex as selected from the app DB: every column of `:metabase_table_indexes`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:transform_id ::lib.schema.id/transform]+ [:index_name :string]+ [:structured ::table-index.structured]+ [:status [:or :keyword :string]]+ [:error_message [:maybe :string]]+ [:created_by [:maybe :int]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:last_executed_at [:maybe ms/TemporalInstant]]])++(mr/def ::table-index.update+ "What an update (or insert) of a TableIndex accepts: every column of `:metabase_table_indexes` except `id`, all optional."+ [:map {:closed true}+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:index_name {:optional true} [:maybe :string]]+ [:structured {:optional true} [:maybe ::table-index.structured]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:error_message {:optional true} [:maybe :string]]+ [:created_by {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_executed_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/indexes_rest/db.clj b/src/metabase/indexes_rest/db.cljindex 874e600cc28..0f772b75ed2 100644--- a/src/metabase/indexes_rest/db.clj+++ b/src/metabase/indexes_rest/db.clj@@ -2,29 +2,35 @@"Application database queries for the indexes REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.indexes.schema :as indexes.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn table-index+(mu/defn table-index"The TableIndex with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/TableIndex :id id))-(defn insert-table-index!+(mu/defn insert-table-index!"Insert the TableIndex `row` and return the inserted instance."- [row]+ [row :- ::indexes.schema/table-index.update](t2/insert-returning-instance! :model/TableIndex row))-(defn set-table-index-structured!+(mu/defn set-table-index-structured!"Set the `structured` definition of the TableIndex with `id`."- [id structured]+ [id :- ms/PositiveInt+ structured :- [:maybe ::indexes.schema/table-index.structured]](t2/update! :model/TableIndex id {:structured structured}))-(defn set-table-index-status!+(mu/defn set-table-index-status!"Set the `status` of the TableIndex with `id`."- [id status]+ [id :- ms/PositiveInt+ status :- [:or :keyword :string]](t2/update! :model/TableIndex id {:status status}))diff --git a/src/metabase/internal_stats/db.clj b/src/metabase/internal_stats/db.cljindex e2c76f79b55..4c1d6c188f5 100644--- a/src/metabase/internal_stats/db.clj+++ b/src/metabase/internal_stats/db.clj@@ -5,44 +5,46 @@[metabase.app-db.core :as mdb][metabase.internal-stats.util :as u][metabase.models.interface :as mi]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn enabled-data-app-count+(mu/defn enabled-data-app-count"The number of enabled data apps without a sync error."[](t2/count :data_app :enabled true :sync_error nil))-(defn embedded-dashboard-count+(mu/defn embedded-dashboard-count"The number of unarchived Dashboards with embedding enabled."[](t2/count :model/Dashboard :enable_embedding true :archived false))-(defn embedded-question-count+(mu/defn embedded-question-count"The number of unarchived question Cards with embedding enabled."[](t2/count :model/Card :enable_embedding true :archived false :type :question))-(defn proxied-ai-usage-tokens-by-model+(mu/defn proxied-ai-usage-tokens-by-model"The model and total tokens of the proxied AiUsageLog rows on `date`, grouped by model."- [date]+ [date :- ms/TemporalInstant](t2/select [:model/AiUsageLog :model [:%sum.total_tokens :tokens]]{:where [:and:ai_proxied[:= [:cast :created_at :date] [:cast date :date]]]:group-by [:model]}))-(defn proxied-ai-usage-tokens-on+(mu/defn proxied-ai-usage-tokens-on"The total tokens of the proxied AiUsageLog rows on `date`, or nil."- [date]+ [date :- ms/TemporalInstant](t2/select-one-fn :sum[:model/AiUsageLog [:%sum.total_tokens :sum]]{:where [:and:ai_proxied[:= [:cast :created_at :date] [:cast date :date]]]}))-(defn proxied-metabot-user-message-count-on+(mu/defn proxied-metabot-user-message-count-on"The number of proxied, unforked user MetabotMessages on `date`."- [date]+ [date :- ms/TemporalInstant](t2/select-one-fn :cnt[:model/MetabotMessage [:%count.id :cnt]]:role "user"@@ -51,9 +53,9 @@:ai_proxied[:= [:cast :created_at :date] [:cast date :date]]]}))-(defn proxied-metabot-user-count-on+(mu/defn proxied-metabot-user-count-on"The `:cnt` of distinct Users with proxied, unforked MetabotMessages on `date`."- [date]+ [date :- ms/TemporalInstant];; New rows stamp `metabot_message.user_id`; legacy rows fall back to the conversation's user.(t2/query-one {:select [[[:count [:distinct [:coalesce :m.user_id :c.user_id]]] :cnt]]:from [[:metabot_message :m]]@@ -105,19 +107,19 @@public-link-condition][:inline false]]]) :internal]])-(defn query-execution-statistics-all-time+(mu/defn query-execution-statistics-all-time"The QueryExecution counts per embedding client over all time."[](t2/select-one query-execution-statistics))-(defn query-execution-statistics-since+(mu/defn query-execution-statistics-since"The QueryExecution counts per embedding client for executions started after `started-after`."- [started-after]+ [started-after :- ms/TemporalInstant](t2/select-one query-execution-statistics {:where [:> :started_at started-after]}))-(defn query-execution-statistics-on+(mu/defn query-execution-statistics-on"The QueryExecution counts per embedding client for executions started on the day of `date`."- [date]+ [date :- ms/TemporalInstant](t2/select-one query-execution-statistics {:where [:= [:cast :started_at :date] [:cast date :date]]}))(defn- and-not-nil@@ -153,7 +155,7 @@(def ^:private embedding-on [:= :enable_embedding [:inline true]])-(defn question-statistics-all-time+(mu/defn question-statistics-all-time"Aggregate counts of unarchived, non-internal Cards over all time: totals, native vs GUI, dashboardquestions, embedded, publicly shared, and (where the app db supports JSON path queries) countsbroken down by template-tag parameters and embedding-parameter locking."@@ -193,7 +195,7 @@:with_disabled_params]))){:where (mi/exclude-internal-content-hsql :model/Card)})))-(defn active-personal-user-email-domain-count+(mu/defn active-personal-user-email-domain-count"The `:count` of distinct email domains of active personal Users."[](let [domain-expr (condp contains? (mdb/db-type)@@ -207,21 +209,21 @@[:= :is_active true][:= :type "personal"]]} :distinct_emails]]})))-(defn active-jwt-user-count+(mu/defn active-jwt-user-count"The number of active personal Users whose SSO source is JWT."[];; Because this count is needed *during* token checks, it uses `t2/table-name` to avoid the `after-select` method on;; users, which calls an EE method that needs ... a token check :|(t2/count (t2/table-name :model/User) :is_active true :sso_source "jwt" :type "personal"))-(defn active-tenant-user-count+(mu/defn active-tenant-user-count"The number of active personal Users belonging to a Tenant."[];; Because this count is needed *during* token checks, it uses `t2/table-name` to avoid the `after-select` method on;; users, which calls an EE method that needs ... a token check :|(t2/count (t2/table-name :model/User) :is_active true :tenant_id [:not= nil] :type "personal"))-(defn tenants-with-active-users-count+(mu/defn tenants-with-active-users-count"The `:count` of Tenants with at least one active personal User."[](t2/query-one {:select [[[:count [:distinct :tenant_id]] :count]]diff --git a/src/metabase/lib_be/db.clj b/src/metabase/lib_be/db.cljindex e6a393397e9..10f95eee526 100644--- a/src/metabase/lib_be/db.clj+++ b/src/metabase/lib_be/db.clj@@ -8,14 +8,14 @@[metabase.util.malli :as mu][toucan2.core :as t2]))-(defn card-database-ids+(mu/defn card-database-ids"The `:id`, `:database_id`, and `:card_schema` of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card :id :database_id :card_schema] :id [:in card-ids]))-(defn database+(mu/defn database"The `:metadata/database` with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :metadata/database database-id));;; ----------------------------------------------- Lib metadata -----------------------------------------------@@ -102,7 +102,7 @@#_elsenil))-(mu/defn- metadata-where :- [:map {:closed true} [:where {:optional true} vector?]]+(mu/defn- metadata-where"The `:where` map picking out the `metadata-type` rows for `database-id`, narrowed by `metadata-spec`'s `:id`,`:name`, `:table-ids`, and `:card-ids` (whichever apply to `metadata-type`), or restricted to active/visiblerows when none of `:id`/`:name` is given (`:include-sensitive?` controlling whether sensitive columns count@@ -125,42 +125,50 @@(and metric? table-ids) (conj [:= :source_card_id nil]))](reduce sql.helpers/where {} where-clauses)))-(defn tables+(mu/defn tables"The `:metadata/table` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/table (metadata-where :metadata/table database-id metadata-spec)))-(defn columns+(mu/defn columns"The `:metadata/column` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/column (metadata-where :metadata/column database-id metadata-spec)))-(defn cards+(mu/defn cards"The `:metadata/card` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/card (metadata-where :metadata/card database-id metadata-spec)))-(defn metrics+(mu/defn metrics"The `:metadata/metric` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/metric (metadata-where :metadata/metric database-id metadata-spec)))-(defn segments+(mu/defn segments"The `:metadata/segment` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/segment (metadata-where :metadata/segment database-id metadata-spec)))-(defn measures+(mu/defn measures"The `:metadata/measure` rows for `database-id` picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/measure (metadata-where :metadata/measure database-id metadata-spec)))-(defn native-query-snippets+(mu/defn native-query-snippets"The `:metadata/native-query-snippet` rows picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/native-query-snippet (metadata-where :metadata/native-query-snippet database-id metadata-spec)))-(defn transforms+(mu/defn transforms"The `:metadata/transform` rows picked out by `metadata-spec`."- [database-id metadata-spec]+ [database-id :- ::lib.schema.id/database+ metadata-spec :- ::lib.metadata.protocols/metadata-spec](t2/select :metadata/transform (metadata-where :metadata/transform database-id metadata-spec)))diff --git a/src/metabase/lib_metric/db.clj b/src/metabase/lib_metric/db.cljindex ad4d9c1b000..1294dc76eb4 100644--- a/src/metabase/lib_metric/db.clj+++ b/src/metabase/lib_metric/db.clj@@ -3,21 +3,24 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[honey.sql.helpers :as sql.helpers]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn card-table-and-database-id+(mu/defn card-table-and-database-id"The `:table_id` and `:database_id` of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :table_id :database_id] card-id))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table table-id))(defn- metric-where@@ -34,9 +37,13 @@card-ids (conj [:in :source_card_id card-ids])active-only? (conj [:= :archived false])))))-(defn metrics+(mu/defn metrics"The metric Cards (`:metadata/metric`) picked out by `metadata-spec`."- [metadata-spec]+ [metadata-spec :- [:map+ [:id {:optional true} [:maybe [:set ms/PositiveInt]]]+ [:name {:optional true} [:maybe [:set :string]]]+ [:table-ids {:optional true} [:maybe [:set ::lib.schema.id/table]]]+ [:card-ids {:optional true} [:maybe [:set ::lib.schema.id/card]]]]](t2/select :metadata/metric (metric-where metadata-spec)))(defn- measure-where@@ -51,7 +58,10 @@table-ids (conj [:in :measure/table_id table-ids])active-only? (conj [:= :measure/archived false])))))-(defn measures+(mu/defn measures"The `:metadata/measure` rows picked out by `metadata-spec`."- [metadata-spec]+ [metadata-spec :- [:map+ [:id {:optional true} [:maybe [:set ms/PositiveInt]]]+ [:name {:optional true} [:maybe [:set :string]]]+ [:table-ids {:optional true} [:maybe [:set ::lib.schema.id/table]]]]](t2/select :metadata/measure (measure-where metadata-spec)))diff --git a/src/metabase/llm/db.clj b/src/metabase/llm/db.cljindex 99592620f6b..0e9b67764fa 100644--- a/src/metabase/llm/db.clj+++ b/src/metabase/llm/db.clj@@ -2,13 +2,15 @@"Application database queries for the LLM module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.util :as u]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn database-engine+(mu/defn database-engine"The engine of the Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :engine :model/Database :id database-id))(defn- table-match-clause@@ -21,21 +23,27 @@[:= [:lower :schema] (u/lower-case-en schema)]][:= [:lower :name] table-lower])))-(defn active-tables-matching+(mu/defn active-tables-matching"The active Tables of the Database with `database-id` matching one of `tables` (each a map of `:table` andoptional `:schema`), by case-insensitive name/schema."- [database-id tables]+ [database-id :- ::lib.schema.id/database+ tables :- [:sequential [:map {:closed true}+ [:table :string]+ [:schema {:optional true} [:maybe :string]]]]](t2/select :model/Table{:where [:and[:= :db_id database-id][:= :active true](into [:or] (map table-match-clause) tables)]}))-(defn visible-tables+(mu/defn visible-tables"The active, visible Tables among `table-ids` of the Database with `database-id` that `user-id` (or asuperuser) can access for querying, requiring unrestricted view-data and query-builder-or-native createpermissions."- [table-ids database-id user-id superuser?]+ [table-ids :- [:set ::lib.schema.id/table]+ database-id :- ::lib.schema.id/database+ user-id :- [:maybe ::lib.schema.id/user]+ superuser? :- :boolean](let [{:keys [clause with]} (mi/visible-filter-clause:model/Table :id{:user-id user-id, :is-superuser? superuser?}@@ -49,22 +57,22 @@(cond-> {:where clause}with (assoc :with with)))))-(defn unarchived-cards+(mu/defn unarchived-cards"The unarchived Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select :model/Card :id [:in card-ids] :archived false))-(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn field-names-and-tables+(mu/defn field-names-and-tables"The id, name, and Table id of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :name :table_id] :id [:in field-ids]))-(defn field-fingerprints+(mu/defn field-fingerprints"A map of Field id to fingerprint for the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select-pk->fn :fingerprint :model/Field :id [:in field-ids]))diff --git a/src/metabase/login_history/db.clj b/src/metabase/login_history/db.cljindex 527b885c633..6dbab0a7eb3 100644--- a/src/metabase/login_history/db.clj+++ b/src/metabase/login_history/db.clj@@ -2,37 +2,46 @@"Application database queries for the login history module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.login-history.schema :as login-history.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn login-history-for-user+(mu/defn login-history-for-user"The timestamp, session id, device description, and IP address of the LoginHistory of the User with `user-id`,newest first."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select [:model/LoginHistory :timestamp :session_id :device_description :ip_address]:user_id user-id{:order-by [[:timestamp :desc]]}))-(defn insert-login-history!- "Insert the LoginHistory `row`."- [row]+(mu/defn insert-login-history!+ "Insert the LoginHistory `row`, returning the number of rows inserted."+ [row :- (mut/select-keys ::login-history.schema/login-history.update [:user_id :session_id :device_id :device_description :ip_address])](t2/insert! :model/LoginHistory row))-(defn login-history-ids-for-user+(mu/defn login-history-ids-for-user"Up to `limit` LoginHistory ids of the User with `user-id`."- [user-id limit]+ [user-id :- ::lib.schema.id/user+ limit :- ms/PositiveInt](t2/select [:model/LoginHistory :id] :user_id user-id {:limit limit}))-(defn login-history-ids-for-user-device+(mu/defn login-history-ids-for-user-device"Up to `limit` LoginHistory ids of the User with `user-id` on the device with `device-id`."- [user-id device-id limit]+ [user-id :- ::lib.schema.id/user+ device-id :- :string+ limit :- ms/PositiveInt](t2/select [:model/LoginHistory :id] :user_id user-id, :device_id device-id, {:limit limit}))-(defn first-device-login-count-since+(mu/defn first-device-login-count-since"The number of LoginHistory rows of the User with `user-id` in the last `window-hours` that are the firstlogin on their device."- [user-id window-hours]+ [user-id :- ::lib.schema.id/user+ window-hours :- ms/PositiveInt](t2/count :model/LoginHistory{:where [:and[:= :user_id user-id]diff --git a/src/metabase/login_history/schema.clj b/src/metabase/login_history/schema.cljnew file mode 100644index 00000000000..ae99336442e--- /dev/null+++ b/src/metabase/login_history/schema.clj@@ -0,0 +1,28 @@+(ns metabase.login-history.schema+ "Malli schemas for the login-history module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::login-history+ "A LoginHistory as selected from the app DB: every column of `:login_history`, plus `:active` added by the model's after-select hook."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:timestamp ms/TemporalInstant]+ [:user_id ::lib.schema.id/user]+ [:session_id {:optional true} [:maybe :string]]+ [:device_id :string]+ [:device_description :string]+ [:ip_address :string]+ [:active {:optional true} :boolean]])++(mr/def ::login-history.update+ "What an update (or insert) of a LoginHistory accepts: every column of `:login_history` except `id`, all optional."+ [:map {:closed true}+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:session_id {:optional true} [:maybe :string]]+ [:device_id {:optional true} [:maybe :string]]+ [:device_description {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]])diff --git a/src/metabase/mcp/db.clj b/src/metabase/mcp/db.cljindex 0a56168750b..3b33c0e4064 100644--- a/src/metabase/mcp/db.clj+++ b/src/metabase/mcp/db.clj@@ -3,24 +3,29 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.mcp.schema :as mcp.schema][metabase.session.core :as session]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn insert-feedback!- "Insert the McpFeedback `row`."- [row]+(mu/defn insert-feedback!+ "Insert the McpFeedback `row`, returning the number inserted."+ [row :- ::mcp.schema/mcp-feedback.update](t2/insert! :model/McpFeedback row))-(defn session-user-id+(mu/defn session-user-id"The id of the User owning the `core_session` with `key-hashed`, or nil."- [key-hashed]+ [key-hashed :- :string](t2/select-one-fn :user_id :core_session :key_hashed key-hashed))-(defn get-or-create-core-session!+(mu/defn get-or-create-core-session!"The `core_session` row for `key-hashed` and `user-id`, creating one with a freshly generated session id if noneexists. Uses the raw `:core_session` table (not `:model/Session`) to bypass the after-insert hook, which wouldotherwise publish a spurious `:event/user-login` event."- [key-hashed user-id]+ [key-hashed :- :string+ user-id :- ::lib.schema.id/user](app-db/select-or-insert!:core_session{:key_hashed key-hashed@@ -30,14 +35,16 @@:anti_csrf_token nil:created_at :%now})))-(defn insert-query-handle!- "Insert the McpQueryHandle `row`."- [row]- (t2/insert! :model/McpQueryHandle row))+(mu/defn insert-query-handle!+ "Insert the McpQueryHandle `row` under the client-generated `handle-id`, returning the number inserted."+ [handle-id :- ms/UUIDString+ row :- ::mcp.schema/mcp-query-handle.update]+ (t2/insert! :model/McpQueryHandle (assoc row :id handle-id)))-(defn query-handle-for-user+(mu/defn query-handle-for-user"The McpQueryHandle with `handle-id` whose session belongs to the User with `user-id`, or nil."- [handle-id user-id]+ [handle-id :- :string+ user-id :- ::lib.schema.id/user](t2/select-one :model/McpQueryHandle{:select [:mqh.*]:from [[:mcp_query_handle :mqh]]@@ -46,15 +53,16 @@[:= :mqh.id handle-id][:= :cs.user_id user-id]]}))-(defn delete-session-for-user!+(mu/defn delete-session-for-user!"Delete the `core_session` with `key-hashed` if it belongs to the User with `user-id`."- [key-hashed user-id]+ [key-hashed :- :string+ user-id :- ::lib.schema.id/user](t2/query {:delete-from :core_session:where [:and[:= :key_hashed key-hashed][:= :user_id user-id]]}))-(defn delete-query-handles-for-mcp-session!- "Delete the McpQueryHandles of the MCP session `mcp-session-id`."- [mcp-session-id]+(mu/defn delete-query-handles-for-mcp-session!+ "Delete the McpQueryHandles of the MCP session `mcp-session-id`, returning the number deleted."+ [mcp-session-id :- :string](t2/delete! :model/McpQueryHandle :mcp_session_id mcp-session-id))diff --git a/src/metabase/mcp/schema.clj b/src/metabase/mcp/schema.cljnew file mode 100644index 00000000000..2d64d3bfa56--- /dev/null+++ b/src/metabase/mcp/schema.clj@@ -0,0 +1,108 @@+(ns metabase.mcp.schema+ "Malli schemas for the mcp module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::mcp-feedback+ "A McpFeedback as selected from the app DB: every column of `:mcp_feedback`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:positive :boolean]+ [:issue_type [:maybe [:or :keyword :string]]]+ [:freeform_feedback [:maybe :string]]+ [:prompt [:maybe :string]]+ [:query [:maybe :string]]+ [:created_at ms/TemporalInstant]])++(mr/def ::mcp-feedback.update+ "What an update (or insert) of a McpFeedback accepts: every column of `:mcp_feedback` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:positive {:optional true} [:maybe :boolean]]+ [:issue_type {:optional true} [:maybe [:or :keyword :string]]]+ [:freeform_feedback {:optional true} [:maybe :string]]+ [:prompt {:optional true} [:maybe :string]]+ [:query {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::mcp-query-handle+ "A McpQueryHandle as selected from the app DB: every column of `:mcp_query_handle`."+ [:map {:closed true}+ [:id :string]+ [:mcp_session_id :string]+ [:core_session_id [:maybe :string]]+ [:encoded_query :string]+ [:created_at ms/TemporalInstant]+ [:prompt [:maybe :string]]])++(mr/def ::mcp-query-handle.update+ "What an update (or insert) of a McpQueryHandle accepts: every column of `:mcp_query_handle` except `id`, all optional."+ [:map {:closed true}+ [:mcp_session_id {:optional true} [:maybe :string]]+ [:core_session_id {:optional true} [:maybe :string]]+ [:encoded_query {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:prompt {:optional true} [:maybe :string]]])++(mr/def ::mcp-session-log+ "A McpSessionLog as selected from the app DB: every column of `:mcp_session_log`."+ [:map {:closed true}+ [:id :string]+ [:created_at ms/TemporalInstant]+ [:ended_at [:maybe ms/TemporalInstant]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:client_name [:maybe :string]]+ [:client_version [:maybe :string]]+ [:ip_address [:maybe :string]]+ [:user_agent [:maybe :string]]])++(mr/def ::mcp-session-log.update+ "What an update (or insert) of a McpSessionLog accepts: every column of `:mcp_session_log` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:ended_at {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:client_name {:optional true} [:maybe :string]]+ [:client_version {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]])++(mr/def ::mcp-tool-call-log+ "A McpToolCallLog as selected from the app DB: every column of `:mcp_tool_call_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:tool_name :string]+ [:status [:maybe [:or :keyword :string]]]+ [:duration_ms [:maybe :int]]+ [:error_code [:maybe :int]]+ [:error_message [:maybe :string]]+ [:client_name [:maybe :string]]+ [:client_version [:maybe :string]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:ip_address [:maybe :string]]+ [:user_agent [:maybe :string]]+ [:sanitized_user_agent [:maybe :string]]])++(mr/def ::mcp-tool-call-log.update+ "What an update (or insert) of a McpToolCallLog accepts: every column of `:mcp_tool_call_log` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tool_name {:optional true} [:maybe :string]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:duration_ms {:optional true} [:maybe :int]]+ [:error_code {:optional true} [:maybe :int]]+ [:error_message {:optional true} [:maybe :string]]+ [:client_name {:optional true} [:maybe :string]]+ [:client_version {:optional true} [:maybe :string]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:ip_address {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]+ [:sanitized_user_agent {:optional true} [:maybe :string]]])diff --git a/src/metabase/mcp/session.clj b/src/metabase/mcp/session.cljindex facbef54544..dc055654d3d 100644--- a/src/metabase/mcp/session.clj+++ b/src/metabase/mcp/session.clj@@ -332,8 +332,8 @@(let [core-session-id (:id (get-or-create-embedding-session! mcp-session-id user-id))handle-id (str (UUID/randomUUID))](mcp.db/insert-query-handle!- (cond-> {:id handle-id- :mcp_session_id mcp-session-id+ handle-id+ (cond-> {:mcp_session_id mcp-session-id:core_session_id core-session-id:encoded_query encoded-query}prompt (assoc :prompt prompt)))diff --git a/src/metabase/measures/db.clj b/src/metabase/measures/db.cljindex de84c9e972c..7edf280d87f 100644--- a/src/metabase/measures/db.clj+++ b/src/metabase/measures/db.clj@@ -2,53 +2,63 @@"Application database queries for the measures module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.measures.schema :as measures.schema]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn insert-measure!+(mu/defn insert-measure!"Insert a Measure and return the inserted instance."- [creator-id measure-name description definition]+ [creator-id :- ::lib.schema.id/user+ measure-name :- :string+ description :- [:maybe :string]+ definition :- [:maybe ::measures.schema/measure.definition]](t2/insert-returning-instance! :model/Measure:creator_id creator-id:name measure-name:description description:definition definition))-(defn measure+(mu/defn measure"The Measure with `id`, or nil."- [id]+ [id :- ::lib.schema.id/measure](t2/select-one :model/Measure :id id))-(defn unarchived-measures+(mu/defn unarchived-measures"The unarchived Measures, in case-insensitive name order."[](t2/select :model/Measure, :archived false, {:order-by [[:%lower.name :asc]]}))-(defn table-database-ids+(mu/defn table-database-ids"The set of Database ids of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-fn-set :db_id :model/Table :id [:in table-ids]))-(defn update-measure!+(mu/defn update-measure!"Apply `changes` to the Measure with `id`."- [id changes]+ [id :- ::lib.schema.id/measure+ changes :- (mut/select-keys ::measures.schema/measure.update [:name :description :archived :definition])](t2/update! :model/Measure id changes))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn table-perms-columns+(mu/defn table-perms-columns"The Database id, schema, and id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/Table :db_id :schema :id] :id table-id))-(defn collections+(mu/defn collections"The Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select :model/Collection :id [:in collection-ids]))-(defn set-measure-dimensions!+(mu/defn set-measure-dimensions!"Set the dimensions and dimension mappings of the Measure with `id`."- [id dimensions dimension-mappings]+ [id :- ::lib.schema.id/measure+ dimensions :- [:maybe sequential?]+ dimension-mappings :- [:maybe [:sequential :map]]](t2/update! :model/Measure id {:dimensions dimensions, :dimension_mappings dimension-mappings}))diff --git a/src/metabase/measures/schema.clj b/src/metabase/measures/schema.cljindex f232d3da611..090f89b1653 100644--- a/src/metabase/measures/schema.clj+++ b/src/metabase/measures/schema.clj@@ -2,8 +2,10 @@(:require[metabase.lib-be.schema :as lib-be.schema][metabase.lib.core :as lib]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.i18n :refer [deferred-tru]]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(set! *warn-on-reflection* true)@@ -16,3 +18,46 @@[:fn{:error/message (deferred-tru "measure definition must have a source table")}#(some? (lib/primary-source-table-id %))]]])++(mr/def ::measure.definition+ "The `:definition` column of a Measure, decoded."+ :map)++(mr/def ::measure.dimension+ "One entry of the `:dimensions` column of a Measure, decoded."+ :map)++(mr/def ::measure.dimension-mapping+ "One entry of the `:dimension_mappings` column of a Measure, decoded."+ :map)++(mr/def ::measure+ "A Measure as selected from the app DB: every column of `:measure`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:table_id ::lib.schema.id/table]+ [:creator_id ::lib.schema.id/user]+ [:name :string]+ [:description [:maybe :string]]+ [:archived :boolean]+ [:definition ::measure.definition]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:entity_id :string]+ [:dimensions [:maybe [:sequential ::measure.dimension]]]+ [:dimension_mappings [:maybe [:sequential ::measure.dimension-mapping]]]])++(mr/def ::measure.update+ "What an update (or insert) of a Measure accepts: every column of `:measure` except `id`, all optional."+ [:map {:closed true}+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:archived {:optional true} [:maybe :boolean]]+ [:definition {:optional true} [:maybe ::measure.definition]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:entity_id {:optional true} [:maybe :string]]+ [:dimensions {:optional true} [:maybe [:sequential ::measure.dimension]]]+ [:dimension_mappings {:optional true} [:maybe [:sequential ::measure.dimension-mapping]]]])diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.cljindex cef75ebc65c..b008e2390bd 100644--- a/src/metabase/metabot/db.clj+++ b/src/metabase/metabot/db.clj@@ -4,54 +4,60 @@and transactions."(:require[clojure.string :as str]+ [malli.util :as mut][metabase.api.common :as api][metabase.app-db.core :as mdb][metabase.audit-app.core :as audit-app][metabase.collections.models.collection :as collection.model]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.metabot.schema :as metabot.schema][metabase.models.interface :as mi][metabase.premium-features.core :as premium-features][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(declare collection metabot-metrics-and-models-query root-collections-of-types);;; ------------------------------------------------- Metabot --------------------------------------------------(defn metabot+(mu/defn metabot"The Metabot with `metabot-id`, or nil."- [metabot-id]+ [metabot-id :- ms/PositiveInt](t2/select-one :model/Metabot :id metabot-id))-(defn metabot-by-entity-id+(mu/defn metabot-by-entity-id"The Metabot with `entity-id`, or nil."- [entity-id]+ [entity-id :- [:maybe :string]](t2/select-one :model/Metabot :entity_id entity-id))-(defn metabot-id-by-entity-id+(mu/defn metabot-id-by-entity-id"The ID of the Metabot with `entity-id`, or nil."- [entity-id]+ [entity-id :- [:maybe :string]](t2/select-one-pk :model/Metabot :entity_id entity-id))-(defn metabots-by-name+(mu/defn metabots-by-name"Every Metabot, ordered by name."[](t2/select :model/Metabot {:order-by [[:name :asc]]}))-(defn metabot-exists?+(mu/defn metabot-exists?"Whether a Metabot with `metabot-id` exists."- [metabot-id]+ [metabot-id :- ms/PositiveInt](t2/exists? :model/Metabot :id metabot-id))-(defn update-metabot!+(mu/defn update-metabot!"Apply `changes` to the Metabot with `metabot-id`."- [metabot-id changes]+ [metabot-id :- ms/PositiveInt+ changes :- (mut/select-keys ::metabot.schema/metabot.update [:use_verified_content :collection_id])](t2/update! :model/Metabot metabot-id changes));;; --------------------------------------------- Metabot prompts ----------------------------------------------(defn prompts-for-metabots+(mu/defn prompts-for-metabots"The MetabotPrompts of the Metabots with `metabot-ids`."- [metabot-ids]+ [metabot-ids :- [:sequential ms/PositiveInt]](t2/select :model/MetabotPrompt {:where [:in :metabot_id metabot-ids]}))(defn- prompts-for-metabot-query@@ -59,7 +65,7 @@scope, optionally further restricted to Cards of `card-type` or the Card with `card-id`."[metabot-id card-type card-id](cond-> {:join [[^:allow-subquery {:select [:id :name :type]- :from [[(metabot-metrics-and-models-query metabot-id) :scope]]}+ :from [[(metabot-metrics-and-models-query metabot-id nil) :scope]]}:card][:and[:= :card.id :metabot_prompt.card_id]]]@@ -75,11 +81,16 @@:postgres :random:rand)]]])-(defn prompts+(mu/defn prompts"The prompt, model, and Card columns of the MetabotPrompts of the Metabot with `metabot-id` whose Card is withinscope, optionally restricted to Cards of `card-type` or the Card with `card-id`, ordered randomly if `sample?` elseby Card name, and limited/offset by `limit`/`offset`."- [metabot-id card-type card-id sample? limit offset]+ [metabot-id :- ms/PositiveInt+ card-type :- [:maybe [:enum "metric" "model"]]+ card-id :- [:maybe ::lib.schema.id/card]+ sample? :- [:maybe :boolean]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select [:model/MetabotPrompt:id:prompt@@ -95,54 +106,57 @@limit (assoc :limit limit)offset (assoc :offset offset))))-(defn prompt-count+(mu/defn prompt-count"The number of MetabotPrompts of the Metabot with `metabot-id` whose Card is within scope, optionally restricted toCards of `card-type` or the Card with `card-id`."- [metabot-id card-type card-id]+ [metabot-id :- ms/PositiveInt+ card-type :- [:maybe [:enum "metric" "model"]]+ card-id :- [:maybe ::lib.schema.id/card]](t2/count :model/MetabotPrompt (prompts-for-metabot-query metabot-id card-type card-id)))-(defn prompt-count-for-metabot+(mu/defn prompt-count-for-metabot"The number of MetabotPrompts of the Metabot with `metabot-id`."- [metabot-id]+ [metabot-id :- ms/PositiveInt](t2/count :model/MetabotPrompt :metabot_id metabot-id))-(defn insert-prompts!+(mu/defn insert-prompts!"Insert the MetabotPrompt `rows`."- [rows]+ [rows :- [:sequential ::metabot.schema/metabot-prompt.update]](t2/insert! :model/MetabotPrompt rows))-(defn delete-metabot-prompt!+(mu/defn delete-metabot-prompt!"Delete the MetabotPrompt with `prompt-id` belonging to the Metabot with `metabot-id`."- [metabot-id prompt-id]+ [metabot-id :- ms/PositiveInt+ prompt-id :- ms/PositiveInt](t2/delete! :model/MetabotPrompt {:where [:and[:= :id prompt-id][:= :metabot_id metabot-id]]}))-(defn delete-prompts-for-metabot!+(mu/defn delete-prompts-for-metabot!"Delete the MetabotPrompts of the Metabot with `metabot-id`."- [metabot-id]+ [metabot-id :- ms/PositiveInt](t2/delete! :model/MetabotPrompt {:where [:= :metabot_id metabot-id]}));;; ----------------------------------------------- Conversations ------------------------------------------------(defn conversation+(mu/defn conversation"The MetabotConversation with `conversation-id`, or nil."- [conversation-id]+ [conversation-id :- :string](t2/select-one :model/MetabotConversation :id conversation-id))-(defn conversation-id-and-user-id+(mu/defn conversation-id-and-user-id"The ID and originator of the MetabotConversation with `conversation-id`, or nil."- [conversation-id]+ [conversation-id :- :string](t2/select-one [:model/MetabotConversation :id :user_id] :id conversation-id))-(defn conversation-title+(mu/defn conversation-title"The title of the MetabotConversation with `conversation-id`."- [conversation-id]+ [conversation-id :- :string](t2/select-one-fn :title :model/MetabotConversation :id conversation-id))-(defn lock-conversation+(mu/defn lock-conversation"The MetabotConversation with `conversation-id`, locked for update."- [conversation-id]+ [conversation-id :- :string](t2/select-one :model/MetabotConversation :id conversation-id {:for :update}))(defn- participation-clause@@ -197,10 +211,13 @@(cond-> [:and (participation-clause user-id)]profile-id (conj [:= (last-live-message-profile-id-subquery) profile-id])))-(defn conversations-page+(mu/defn conversations-page"A page of up to `limit` (offset by `offset`) MetabotConversations visible in the history of the User with`user-id`, most-recent-activity first, optionally narrowed to the last live message's `profile-id`."- [user-id profile-id limit offset]+ [user-id :- ::lib.schema.id/user+ profile-id :- [:maybe :string]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/MetabotConversation{:select [:c.id :c.created_at :c.title :c.user_id :c.forked_from_conversation_id[(live-message-count-subquery) :message_count]@@ -212,76 +229,83 @@:limit limit:offset offset}))-(defn conversation-count+(mu/defn conversation-count"The number of MetabotConversations visible in the history of the User with `user-id`, optionally narrowed to thelast live message's `profile-id`."- [user-id profile-id]+ [user-id :- ::lib.schema.id/user+ profile-id :- [:maybe :string]](:count (t2/query-one {:select [[[:count :*] :count]]:from [[:metabot_conversation :c]]:where (conversations-list-where user-id profile-id)})))-(defn titleless-conversation-ids+(mu/defn titleless-conversation-ids"Up to `limit` IDs of the MetabotConversations without a title, whose ID is greater than `after-id` (or every one,when `after-id` is nil), in ID order."- [after-id limit]+ [after-id :- [:maybe :string]+ limit :- ms/PositiveInt](t2/select-fn-vec :id :model/MetabotConversation{:where [:and [:= :title nil] (when after-id [:> :id after-id])]:order-by [[:id :asc]]:limit limit}))-(defn insert-conversation!- "Insert `conversation`."- [conversation]- (t2/insert! :model/MetabotConversation conversation))+(mu/defn insert-conversation!+ "Insert `conversation` under the client-generated `conversation-id`."+ [conversation-id :- :string+ conversation :- ::metabot.schema/metabot-conversation.update]+ (t2/insert! :model/MetabotConversation (assoc conversation :id conversation-id)))-(defn upsert-conversation!+(mu/defn upsert-conversation!"Insert or update the MetabotConversation with `conversation-id`. `update-fn` receives the existing row (or nil oninsert) and must return the fields to write."- [conversation-id update-fn]+ [conversation-id :- :string+ update-fn :- fn?](mdb/update-or-insert! :model/MetabotConversation {:id conversation-id} update-fn))-(defn set-conversation-title-if-missing!+(mu/defn set-conversation-title-if-missing!"Set the title of the MetabotConversation with `conversation-id` if it has none."- [conversation-id title]+ [conversation-id :- :string+ title :- :string](t2/update! :model/MetabotConversation {:id conversation-id, :title nil} {:title title}))-(defn delete-conversations-created-before!+(mu/defn delete-conversations-created-before!"Delete the MetabotConversations created before `cutoff`, returning the number deleted."- [cutoff]+ [cutoff :- ms/TemporalInstant](t2/delete! :model/MetabotConversation {:where [:< :created_at cutoff]}));;; -------------------------------------------------- Messages ---------------------------------------------------(defn participant?+(mu/defn participant?"Whether the User with `user-id` has sent a message in the MetabotConversation with `conversation-id`."- [conversation-id user-id]+ [conversation-id :- :string+ user-id :- ::lib.schema.id/user](t2/exists? :model/MetabotMessage :conversation_id conversation-id :user_id user-id))-(defn message-by-external-id+(mu/defn message-by-external-id"The ID and conversation of the MetabotMessage with `external-id`, or nil."- [external-id]+ [external-id :- :string](t2/select-one [:model/MetabotMessage :id :conversation_id] :external_id external-id))-(defn live-messages+(mu/defn live-messages"The non-deleted MetabotMessages of the MetabotConversation with `conversation-id`, in reader order."- [conversation-id]+ [conversation-id :- [:maybe :string]](t2/select :model/MetabotMessage:conversation_id conversation-id:deleted_at nil{:order-by [[:created_at :asc] [:id :asc]]}))-(defn opening-messages+(mu/defn opening-messages"The first `limit` non-deleted MetabotMessages of the MetabotConversation with `conversation-id`, in reader order."- [conversation-id limit]+ [conversation-id :- :string+ limit :- ms/PositiveInt](t2/select :model/MetabotMessage:conversation_id conversation-id:deleted_at nil{:order-by [[:created_at :asc] [:id :asc]]:limit limit}))-(defn leaf-assistant-message+(mu/defn leaf-assistant-message"The most recent non-deleted assistant MetabotMessage of the MetabotConversation with `conversation-id`, or nil."- [conversation-id]+ [conversation-id :- :string](t2/select-one :model/MetabotMessage{:where [:and[:= :conversation_id conversation-id]@@ -289,49 +313,71 @@[:= :role "assistant"]]:order-by [[:created_at :desc] [:id :desc]]}))-(defn insert-message-returning-pk!+(mu/defn insert-message-returning-pk!"Insert `message` and return its ID."- [message]+ [message :- ::metabot.schema/metabot-message.update](t2/insert-returning-pk! :model/MetabotMessage message))-(defn insert-messages!+(mu/defn insert-messages!"Insert one MetabotMessage map or a sequence of them."- [messages]+ [messages :- [:or ::metabot.schema/metabot-message.update [:sequential ::metabot.schema/metabot-message.update]]](t2/insert! :model/MetabotMessage messages))-(defn update-message!+(mu/defn update-message!"Apply `changes` to the MetabotMessage with `message-id`."- [message-id changes]+ [message-id :- ms/PositiveInt+ changes :- ::metabot.schema/metabot-message.update](t2/update! :model/MetabotMessage message-id changes))-(defn soft-delete-messages-where!- "Soft-delete the MetabotMessages matching the Toucan `conditions` on behalf of `deleted-by-user-id`, returning the- number of rows updated."- [conditions deleted-by-user-id]- (t2/update! :model/MetabotMessage conditions {:deleted_at [:now]- :deleted_by_user_id deleted-by-user-id}))--(defn insert-used-tables!+(mu/defn soft-delete-messages-where!+ "Soft-delete the MetabotMessages matching `conditions` on behalf of `deleted-by-user-id`, returning the number of+ rows updated. `conditions` must have at least one key -- an empty map would match every message. `:id` matches a+ single MetabotMessage or, given a collection, any of several."+ [conditions :- [:and+ [:map {:closed true}+ [:id {:optional true} [:maybe [:or ms/PositiveInt [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]]+ [:channel_id {:optional true} [:maybe :string]]+ [:slack_msg_id {:optional true} [:maybe :string]]+ [:role {:optional true} [:maybe [:or :keyword :string]]]]+ [:fn {:error/message "must have at least one condition"} seq]]+ deleted-by-user-id :- ::lib.schema.id/user]+ (let [{:keys [id]} conditions+ conditions (cond-> conditions+ (coll? id) (assoc :id [:in id]))]+ (t2/update! :model/MetabotMessage conditions {:deleted_at [:now]+ :deleted_by_user_id deleted-by-user-id})))++(mu/defn insert-used-tables!"Insert the MetabotUsedTable `rows`."- [rows]+ [rows :- [:sequential [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:message_id {:optional true} [:maybe ms/PositiveInt]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]]]](t2/insert! :model/MetabotUsedTable rows));;; --------------------------------------------------- Feedback ----------------------------------------------------(defn upsert-feedback!+(mu/defn upsert-feedback!"Insert or update the MetabotFeedback row for the MetabotMessage with `message-id` and the User with`submitter-user-id`. `update-fn` receives the existing row (or nil on insert) and must return the fields towrite."- [message-id submitter-user-id update-fn]+ [message-id :- ms/PositiveInt+ submitter-user-id :- ::lib.schema.id/user+ update-fn :- fn?](mdb/update-or-insert! :model/MetabotFeedback{:message_id message-id :user_id submitter-user-id}update-fn))-(defn upsert-source-feedback!+(mu/defn upsert-source-feedback!"Insert or update the MetabotSourceFeedback row for the MetabotMessage with `message-id`, the User with`submitter-user-id`, and the source with `source-id`/`source-type`. `update-fn` receives the existing row (or nilon insert) and must return the fields to write."- [message-id submitter-user-id source-id source-type update-fn]+ [message-id :- ms/PositiveInt+ submitter-user-id :- ::lib.schema.id/user+ source-id :- ms/PositiveInt+ source-type :- [:enum "table" "card" "model" "dashboard"]+ update-fn :- fn?](mdb/update-or-insert! :model/MetabotSourceFeedback{:message_id message-id:user_id submitter-user-id@@ -341,37 +387,38 @@;;; ------------------------------------------------ Databases -------------------------------------------------(defn database-summary+(mu/defn database-summary"The ID, name, description, and engine of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one [:model/Database :id :name :description :engine] database-id))-(defn database-with-columns+(mu/defn database-with-columns"The `columns` of the Database with `database-id`."- [columns database-id]+ [columns :- [:sequential :keyword]+ database-id :- ::lib.schema.id/database](t2/select-one columns database-id))-(defn database-exists?+(mu/defn database-exists?"Whether a Database with `database-id` exists."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id))-(defn database-ids-by-name+(mu/defn database-ids-by-name"The IDs of the Databases named `database-name`."- [database-name]+ [database-name :- :string](t2/select-pks-vec :model/Database :name database-name))-(defn database-engines-and-names+(mu/defn database-engines-and-names"A map of ID to the engine and name of the Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select-pk->fn identity [:model/Database :id :engine :name] :id [:in database-ids]))-(defn destination-database-ids+(mu/defn destination-database-ids"The IDs of the routing destination Databases among `database-ids`."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select-fn-set :id :model/Database :id [:in database-ids] :router_database_id [:not= nil]))-(defn non-audit-databases+(mu/defn non-audit-databases"The ID, name, engine, description, and audit flag of every non-audit, non-destination Database, ordered by name."[](t2/select [:model/Database :id :name :engine :description :is_audit]@@ -381,45 +428,47 @@;;; ------------------------------------------------- Tables --------------------------------------------------(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select-one :model/Table :id table-id))-(defn active-table-with-columns+(mu/defn active-table-with-columns"The `columns` of the active Table with `table-id`, or nil."- [columns table-id]+ [columns :- [:sequential :keyword]+ table-id :- ::lib.schema.id/table](t2/select-one columns :id table-id :active true))-(defn table-database-id+(mu/defn table-database-id"The Database ID of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table :id table-id))-(defn tables-by-id+(mu/defn tables-by-id"A map of ID to Table for `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-fn->fn :id identity :model/Table :id [:in table-ids]))-(defn table-summaries+(mu/defn table-summaries"The ID, names, schema, Database ID, and description of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :display_name :schema :db_id :description] :id [:in table-ids]))-(defn table-schema-rows+(mu/defn table-schema-rows"The ID, name, schema, and Database ID of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :schema :db_id] :id [:in table-ids]))-(defn table-curation-rows+(mu/defn table-curation-rows"The ID, published flag, data layer, and data authority of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :is_published :data_layer :data_authority] :id [:in table-ids]))-(defn visible-table-summaries+(mu/defn visible-table-summaries"The ID, name, schema, and description of the active, unhidden Tables among `table-ids` in the Database with`database-id`."- [database-id table-ids]+ [database-id :- ::lib.schema.id/database+ table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :schema :description]:db_id database-id:id [:in table-ids]@@ -440,10 +489,11 @@(cond-> {:where table-where-clause}table-cte (assoc :with table-cte))))-(defn visible-table-summaries-for-current-user+(mu/defn visible-table-summaries-for-current-user"The ID, name, schema, and description of the active, unhidden Tables among `table-ids` in the Database with`database-id` that are visible to the current user for querying."- [database-id table-ids]+ [database-id :- ::lib.schema.id/database+ table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :schema :description]:db_id database-id:id [:in table-ids]@@ -455,10 +505,11 @@"Cap on the number of visible Tables fetched for fuzzy table-name matching."10000)-(defn visible-tables-excluding+(mu/defn visible-tables-excluding"Reducible ID, name, schema, and description of up to [[max-visible-tables-to-consider]] active, unhidden Tables inthe Database with `database-id` that are visible to the current user for querying, excluding `excluded-table-ids`."- [database-id excluded-table-ids]+ [database-id :- ::lib.schema.id/database+ excluded-table-ids :- [:sequential ::lib.schema.id/table]](t2/reducible-select [:model/Table :id :name :schema :description]:db_id database-id:active true@@ -470,45 +521,49 @@[:and where-clause [:not-in :id excluded-table-ids]][:not-in :id excluded-table-ids]))))))-(defn most-viewed-tables-visible-to-current-user+(mu/defn most-viewed-tables-visible-to-current-user"The ID, Database ID, name, schema, and description of up to `limit` active, unhidden Tables in the Database with- `database-id` that are visible to the current user for querying, most viewed first."- [database-id limit]+ `database-id` (which callers may pass as an invalid/nonexistent id to get no results back) that are visible to the+ current user for querying, most viewed first."+ [database-id :- :int+ limit :- ms/PositiveInt](t2/select [:model/Table :id :db_id :name :schema :description]:db_id database-id:active true:visibility_type nil(assoc (current-user-visible-table-clause) :order-by [[:view_count :desc]] :limit limit)))-(defn table-names+(mu/defn table-names"Up to `limit` IDs, names, and schemas of the active, unhidden Tables in the Database with `database-id`."- [database-id limit]+ [database-id :- ::lib.schema.id/database+ limit :- ms/PositiveInt](t2/select [:model/Table :id :name :schema]:db_id database-id:active true:visibility_type nil{:limit limit}))-(defn active-tables-for-database+(mu/defn active-tables-for-database"The presentable columns of the active Tables in the Database with `database-id`, ordered by schema and name."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Table :id :name :display_name :schema :db_id :description]:db_id database-id:active true{:order-by [[:%lower.schema :asc] [:%lower.name :asc]]}))-(defn active-tables-in-schema+(mu/defn active-tables-in-schema"The presentable columns of the active Tables in `schema` of the Database with `database-id`, ordered by name."- [database-id schema]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/select [:model/Table :id :name :display_name :schema :db_id :description]:db_id database-id:schema schema:active true{:order-by [[:%lower.name :asc]]}))-(defn active-schemas-for-database+(mu/defn active-schemas-for-database"The distinct `:schema` rows of the active Tables in the Database with `database-id`, ordered by schema."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/query {:select-distinct [:schema]:from [:metabase_table]:where [:and [:= :db_id database-id] [:= :active true]]@@ -546,10 +601,12 @@(table-part-clause :t.name table)(table-part-clause :t.schema schema)]))-(defn query-table-reference+(mu/defn query-table-reference"The first Table ID, name, and schema in the Database with `db-id` matching `table` (and `schema`, if given), as aquery table reference. Matching is case-insensitive unless `table`/`schema` are quoted with `\"` or `` ` ``."- [db-id schema table]+ [db-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table :- :string](t2/select-one :model/QueryTable{:select [[:t.id :table-id] [:t.name :table] [:t.schema :schema]]:from [[(t2/table-name :model/Table) :t]]@@ -557,11 +614,14 @@[:= :t.db_id db-id](table-match-clause {:schema schema :table table})]}))-(defn query-table-references+(mu/defn query-table-references"The Table IDs, names, and schemas in the Database with `db-id` matching any of `tables` (each a map of `:schema`and `:table`), as query table references. Matching is case-insensitive unless quoted, as in[[query-table-reference]]."- [db-id tables]+ [db-id :- ::lib.schema.id/database+ tables :- [:sequential [:map {:closed true}+ [:schema [:maybe :string]]+ [:table :string]]]](t2/select :model/QueryTable{:select [[:t.id :table-id] [:t.name :table] [:t.schema :schema]]:from [[(t2/table-name :model/Table) :t]]@@ -571,49 +631,50 @@;;; ------------------------------------------------- Fields --------------------------------------------------(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))-(defn field-fingerprint+(mu/defn field-fingerprint"The fingerprint of the Field with `field-id`."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :fingerprint :model/Field :id field-id))-(defn field-table-ids+(mu/defn field-table-ids"A map of Field ID to Table ID for `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select-fn->fn :id :table_id [:model/Field :id :table_id] :id [:in field-ids]));;; -------------------------------------------------- Cards ---------------------------------------------------(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-by-entity-id+(mu/defn card-by-entity-id"The Card with `entity-id`, or nil."- [entity-id]+ [entity-id :- :string](t2/select-one :model/Card :entity_id entity-id))-(defn card-of-type+(mu/defn card-of-type"The Card with `card-id` if it is of `type`, or nil."- [card-id type]+ [card-id :- ::lib.schema.id/card+ type :- [:enum :model :metric :question]](t2/select-one :model/Card :id card-id :type type))-(defn card-type-row+(mu/defn card-type-row"The ID, type, and schema of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :id :type :card_schema] :id card-id))-(defn metabot-metrics-and-models-query+(defn- metabot-metrics-and-models-query"Honey SQL query selecting the metric and model Cards in scope of the Metabot with `metabot-id` that are visible tothe current user, ignoring analytics content. If the Metabot has `:use_verified_content` enabled, restricts toverified-or-curated content (verified, official-collection, or library-published). `limit`, if given, caps thenumber of rows."- [metabot-id & {:keys [limit]}]+ [metabot-id limit](let [metabot-instance (metabot metabot-id)metabot-collection-id (:collection_id metabot-instance)use-verified-content? (:use_verified_content metabot-instance)@@ -673,102 +734,108 @@(integer? limit)(assoc :limit limit))))-(defn cards-where- "The Cards matching the Honey SQL `query`."- [query]- (t2/select :model/Card query))+(mu/defn metabot-metrics-and-models+ "The metric and model Cards in scope of the Metabot with `metabot-id` that are visible to the current user (see+ [[metabot-metrics-and-models-query]]), ordered to prioritize curated content and then by Card id. `limit`, if+ given, caps the number of rows returned."+ [metabot-id :- ms/PositiveInt+ limit :- [:maybe ms/PositiveInt]]+ (t2/select :model/Card (-> (metabot-metrics-and-models-query metabot-id limit)+ (update :order-by (fnil conj []) [:report_card.id]))))-(defn card-entity-ids+(mu/defn card-entity-ids"A map of Card ID to entity ID for `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-pk->fn :entity_id :model/Card :id [:in card-ids]))-(defn card-table-ids+(mu/defn card-table-ids"A map of Card ID to Table ID for `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-pk->fn :table_id :model/Card :id [:in card-ids]))-(defn card-search-rows+(mu/defn card-search-rows"The searchable columns of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :name :description :database_id :collection_id :card_schema :type] :id [:in card-ids]))-(defn unarchived-card-summaries+(mu/defn unarchived-card-summaries"The presentable columns of the unarchived Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card :id :name :type :description :card_schema :collection_id :database_id :table_id]:id [:in card-ids]:archived false))-(defn cards-in-collection+(mu/defn cards-in-collection"The presentable columns of the unarchived Cards in the Collection with `collection-id`, ordered by name."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select [:model/Card :id :name :type :description :card_schema :collection_id :database_id :table_id]{:where [:and [:= :collection_id collection-id] [:= :archived false]]:order-by [[:%lower.name :asc]]}))-(defn cards-for-table+(mu/defn cards-for-table"The presentable columns of the unarchived Cards on the Table with `table-id`, ordered by name."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select [:model/Card :id :name :type :description :card_schema :collection_id :database_id :table_id]:table_id table-id:archived false{:order-by [[:%lower.name :asc]]}))-(defn models-for-database+(mu/defn models-for-database"The presentable columns of the unarchived model Cards on the Database with `database-id`, ordered by name."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Card :id :name :type :description :card_schema :collection_id :database_id :table_id]:type :model:database_id database-id:archived false{:order-by [[:%lower.name :asc]]}))-(defn saved-cards-for-conversation+(mu/defn saved-cards-for-conversation"The ID and chart ID of the unarchived Cards saved from the MetabotConversation with `conversation-id`, in ID order."- [conversation-id]+ [conversation-id :- :string](t2/select [:model/Card :id :metabot_chart_id]:metabot_conversation_id conversation-id:archived false{:order-by [[:id :asc]]}))-(defn link-card-to-conversation!+(mu/defn link-card-to-conversation!"Record that the Card with `card-id` was saved from the MetabotConversation with `conversation-id` as `chart-id`."- [card-id conversation-id chart-id]+ [card-id :- ::lib.schema.id/card+ conversation-id :- :string+ chart-id :- :string](t2/update! (t2/table-name :model/Card) card-id {:metabot_conversation_id conversation-id:metabot_chart_id chart-id}));;; ----------------------------------------------- Collections ------------------------------------------------(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-one :model/Collection :id collection-id))-(defn collection-name+(mu/defn collection-name"The name of the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one-fn :name :model/Collection :id collection-id))-(defn collection-descriptions+(mu/defn collection-descriptions"A map of Collection ID to description for `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select-pk->fn :description :model/Collection :id [:in collection-ids]))-(defn collection-summaries+(mu/defn collection-summaries"The ID, name, and authority level of the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select [:model/Collection :id :name :authority_level] :id [:in collection-ids]))-(defn collection-curation-info-by-id+(mu/defn collection-curation-info-by-id"A map of ID to the ID, authority level, location, and type of the Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select-pk->fn identity [:model/Collection :id :authority_level :location :type] :id [:in collection-ids]))-(defn navigable-collections+(mu/defn navigable-collections"The presentable columns of the non-trash Collections in the default namespace, ordered by location and name.Restricted to top-level Collections unless `include-nested?`."- [include-nested?]+ [include-nested? :- :boolean](t2/select [:model/Collection :id :name :location :authority_level :description :personal_owner_id]{:where (cond-> [:and[:= :archived false]@@ -777,162 +844,165 @@(not include-nested?) (conj [:= :location "/"])):order-by [[:location :asc] [:%lower.name :asc]]}))-(defn unarchived-collections-at-location+(mu/defn unarchived-collections-at-location"The presentable columns of the unarchived Collections directly at `location`, ordered by name."- [location]+ [location :- :string](t2/select [:model/Collection :id :name :location :authority_level :description :personal_owner_id]:location location:archived false{:order-by [[:%lower.name :asc]]}))-(defn root-collections-of-types+(mu/defn root-collections-of-types"The top-level Collections whose type is one of `types`."- [types]+ [types :- [:sequential :string]](t2/select :model/Collection :type [:in types] :location "/"));;; ---------------------------------------------- Other models -----------------------------------------------(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard dashboard-id))-(defn dashboard-summary+(mu/defn dashboard-summary"The ID, description, name, and Collection ID of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one [:model/Dashboard :id :description :name :collection_id] dashboard-id))-(defn dashboard-name+(mu/defn dashboard-name"The name of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :name :model/Dashboard :id dashboard-id))-(defn dashboards-in-collection+(mu/defn dashboards-in-collection"The ID, name, description, and Collection ID of the unarchived Dashboards in the Collection with`collection-id`, ordered by name."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select [:model/Dashboard :id :name :description :collection_id]:collection_id collection-id:archived false{:order-by [[:%lower.name :asc]]}))-(defn dashboard-tabs+(mu/defn dashboard-tabs"The ID and name of the DashboardTabs of the Dashboard with `dashboard-id`, in position order."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select [:model/DashboardTab :id :name] :dashboard_id dashboard-id {:order-by [[:position :asc] [:id :asc]]}))-(defn dashcards+(mu/defn dashcards"The ID, Card, action, tab, and settings of the DashboardCards of the Dashboard with `dashboard-id`, in gridorder."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select [:model/DashboardCard :id :card_id :action_id :dashboard_tab_id :visualization_settings]:dashboard_id dashboard-id{:order-by [[:row :asc] [:col :asc]]}))-(defn documents-in-collection+(mu/defn documents-in-collection"The ID, name, Collection ID, and exploration of the unarchived, non-exploration Documents in the Collection with`collection-id`, ordered by name."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select [:model/Document :id :name :collection_id :exploration_id]:collection_id collection-id:archived false:exploration_id nil{:order-by [[:%lower.name :asc]]}))-(defn unarchived-documents+(mu/defn unarchived-documents"The unarchived Documents with `document-ids`."- [document-ids]+ [document-ids :- [:set ms/PositiveInt]](t2/select :model/Document :id [:in document-ids] :archived false))-(defn transforms+(mu/defn transforms"The Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/select :model/Transform :id [:in transform-ids]))-(defn transforms-for-source-database+(mu/defn transforms-for-source-database"The ID, name, description, source Database, and source of the Transforms reading from the Database with`database-id`, ordered by name."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Transform :id :name :description :source_database_id :source]:source_database_id database-id{:order-by [[:%lower.name :asc]]}))-(defn verified-item-ids+(mu/defn verified-item-ids"The subset of `item-ids` of `item-type` whose most recent moderation review is verified."- [item-ids item-type]+ [item-ids :- [:sequential ms/PositiveInt]+ item-type :- :string](t2/select-fn-set :moderated_item_id :model/ModerationReview:moderated_item_id [:in item-ids]:moderated_item_type item-type:most_recent true:status "verified"))-(defn latest-review-status-row+(mu/defn latest-review-status-row"The `:status` row of the most recent moderation review of the item with `item-id` of `item-type`, or nil."- [item-id item-type]+ [item-id :- ms/PositiveInt+ item-type :- :string](t2/select-one [:model/ModerationReview :status]:moderated_item_id item-id:moderated_item_type item-type:most_recent true{:order-by [[:id :desc]]}))-(defn glossary-count+(mu/defn glossary-count"The number of Glossary entries."[](t2/count :model/Glossary))-(defn glossary-definitions+(mu/defn glossary-definitions"A map of term to definition for the first `limit` Glossary entries ordered by `order-column` descending."- [order-column limit]+ [order-column :- :keyword+ limit :- ms/PositiveInt](t2/select-fn->fn :term :definition :model/Glossary{:order-by [[order-column :desc]]:limit limit}))-(defn user-summary+(mu/defn user-summary"The ID, email, and names of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :email :first_name :last_name] user-id))-(defn user-summaries-by-id+(mu/defn user-summaries-by-id"A map of ID to the ID, email, and names of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn card-collection-ids+(mu/defn card-collection-ids"The ID and Collection ID of the Cards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Card :id :collection_id] :id [:in ids]))-(defn dashboard-collection-ids+(mu/defn dashboard-collection-ids"The ID and Collection ID of the Dashboards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Dashboard :id :collection_id] :id [:in ids]))-(defn measure-table-id+(mu/defn measure-table-id"The Table ID of the Measure with `id`, or nil."- [id]+ [id :- ::lib.schema.id/measure](t2/select-one-fn :table_id :model/Measure :id id))-(defn segment-table-id+(mu/defn segment-table-id"The Table ID of the Segment with `id`, or nil."- [id]+ [id :- ::lib.schema.id/segment](t2/select-one-fn :table_id :model/Segment :id id))-(defn measure-entity-id+(mu/defn measure-entity-id"The entity ID of the Measure with `id`."- [id]+ [id :- ::lib.schema.id/measure](t2/select-one-fn :entity_id :model/Measure :id id))-(defn segment-entity-id+(mu/defn segment-entity-id"The entity ID of the Segment with `id`."- [id]+ [id :- ::lib.schema.id/segment](t2/select-one-fn :entity_id :model/Segment :id id))-(defn measures+(mu/defn measures"The ID, name, description, Table ID, and entity ID of the Measures with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Measure :id :name :description :table_id :entity_id] :id [:in ids]))-(defn segments+(mu/defn segments"The ID, name, description, Table ID, and entity ID of the Segments with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select [:model/Segment :id :name :description :table_id :entity_id] :id [:in ids]))diff --git a/src/metabase/metabot/persistence.clj b/src/metabase/metabot/persistence.cljindex 16ac9be14b0..31edfe9afd8 100644--- a/src/metabase/metabot/persistence.clj+++ b/src/metabase/metabot/persistence.clj@@ -287,7 +287,7 @@;; metabot_message rows for chat-detail rendering must tiebreak on `:id`(t2/with-transaction [_conn](when (seq delete-message-ids)- (soft-delete-messages! {:id [:in delete-message-ids]} originator-id))+ (soft-delete-messages! {:id delete-message-ids} originator-id))(metabot.db/upsert-conversation!conversation-id(fn [existing]@@ -349,7 +349,7 @@{:profile-id (or profile-id "unknown")})(t2/with-transaction [_conn](when (seq delete-message-ids)- (soft-delete-messages! {:id [:in delete-message-ids]} api/*current-user-id*))+ (soft-delete-messages! {:id delete-message-ids} api/*current-user-id*))(let [pk (insert-assistant-placeholder! conversation-id profile-id assistant-external-id ai-proxy?)]{:assistant-msg-id pk:assistant-external-id assistant-external-id@@ -808,8 +808,8 @@(let [to-clone (conj (vec before) target)new-conversation-id (str (random-uuid))](t2/with-transaction [_conn]- (metabot.db/insert-conversation! {:id new-conversation-id- :user_id user-id+ (metabot.db/insert-conversation! new-conversation-id+ {:user_id user-id:forked_from_conversation_id conversation-id})(metabot.db/insert-messages! (mapv #(forked-message-row new-conversation-id user-id %) to-clone)))new-conversation-id))))diff --git a/src/metabase/metabot/schema.clj b/src/metabase/metabot/schema.cljindex 455283103eb..8c34f3d283d 100644--- a/src/metabase/metabot/schema.clj+++ b/src/metabase/metabot/schema.clj@@ -3,8 +3,10 @@[malli.core :as mc][malli.transform :as mtx][metabase.lib.schema.common :as lib.schema.common]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::role[:enum@@ -105,3 +107,241 @@;; how much of the window the conversation occupied once this message was;; produced; absent on all user rows and legacy agent records[:contextTokens {:optional true} :int]])++(mr/def ::ai-usage-log+ "A AiUsageLog as selected from the app DB: every column of `:ai_usage_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:source [:or :keyword :string]]+ [:model [:or :keyword :string]]+ [:prompt_tokens :int]+ [:completion_tokens :int]+ [:total_tokens :int]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:conversation_id [:maybe :string]]+ [:profile_id [:maybe :string]]+ [:request_id [:maybe :string]]+ [:ai_proxied [:maybe :boolean]]+ [:cache_creation_tokens [:maybe :int]]+ [:cache_read_tokens [:maybe :int]]])++(mr/def ::ai-usage-log.update+ "What an update (or insert) of a AiUsageLog accepts: every column of `:ai_usage_log` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:source {:optional true} [:maybe [:or :keyword :string]]]+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:prompt_tokens {:optional true} [:maybe :int]]+ [:completion_tokens {:optional true} [:maybe :int]]+ [:total_tokens {:optional true} [:maybe :int]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:conversation_id {:optional true} [:maybe :string]]+ [:profile_id {:optional true} [:maybe :string]]+ [:request_id {:optional true} [:maybe :string]]+ [:ai_proxied {:optional true} [:maybe :boolean]]+ [:cache_creation_tokens {:optional true} [:maybe :int]]+ [:cache_read_tokens {:optional true} [:maybe :int]]])++(mr/def ::metabot+ "A Metabot as selected from the app DB: every column of `:metabot`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:description [:maybe :string]]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:use_verified_content [:maybe :boolean]]+ [:collection_id [:maybe ::lib.schema.id/collection]]])++(mr/def ::metabot.update+ "What an update (or insert) of a Metabot accepts: every column of `:metabot` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:use_verified_content {:optional true} [:maybe :boolean]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]])++(mr/def ::metabot-conversation+ "A MetabotConversation as selected from the app DB: every column of `:metabot_conversation`."+ [:map {:closed true}+ [:id :string]+ [:created_at ms/TemporalInstant]+ [:user_id ::lib.schema.id/user]+ [:title [:maybe :string]]+ [:ip_address [:maybe :string]]+ [:slack_team_id [:maybe :string]]+ [:slack_channel_id [:maybe :string]]+ [:slack_thread_ts [:maybe :string]]+ [:embedding_hostname [:maybe :string]]+ [:embedding_path [:maybe :string]]+ [:user_agent [:maybe :string]]+ [:sanitized_user_agent [:maybe :string]]+ [:forked_from_conversation_id [:maybe :string]]])++(mr/def ::metabot-conversation.update+ "What an update (or insert) of a MetabotConversation accepts: every column of `:metabot_conversation` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:title {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]+ [:slack_team_id {:optional true} [:maybe :string]]+ [:slack_channel_id {:optional true} [:maybe :string]]+ [:slack_thread_ts {:optional true} [:maybe :string]]+ [:embedding_hostname {:optional true} [:maybe :string]]+ [:embedding_path {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]+ [:sanitized_user_agent {:optional true} [:maybe :string]]+ [:forked_from_conversation_id {:optional true} [:maybe :string]]])++(mr/def ::metabot-feedback+ "A MetabotFeedback as selected from the app DB: every column of `:metabot_feedback`."+ [:map {:closed true}+ [:message_id ms/PositiveInt]+ [:positive :boolean]+ [:issue_type [:maybe [:or :keyword :string]]]+ [:freeform_feedback [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]])++(mr/def ::metabot-feedback.update+ "What an update (or insert) of a MetabotFeedback accepts: every column of `:metabot_feedback` except `id`, all optional."+ [:map {:closed true}+ [:message_id {:optional true} [:maybe ms/PositiveInt]]+ [:positive {:optional true} [:maybe :boolean]]+ [:issue_type {:optional true} [:maybe [:or :keyword :string]]]+ [:freeform_feedback {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]])++(mr/def ::metabot-message.data-part+ "One entry of the `:data` column of a MetabotMessage, decoded."+ :map)++(mr/def ::metabot-message.usage+ "The `:usage` column of a MetabotMessage, decoded."+ :map)++(mr/def ::metabot-message.state+ "The `:state` column of a MetabotMessage, decoded."+ :map)++(mr/def ::metabot-message+ "A MetabotMessage as selected from the app DB: every column of `:metabot_message`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:profile_id :string]+ [:role [:or :keyword :string]]+ [:data [:sequential ::metabot-message.data-part]]+ [:usage [:maybe ::metabot-message.usage]]+ [:total_tokens :int]+ [:conversation_id :string]+ [:slack_msg_id [:maybe :string]]+ [:channel_id [:maybe :string]]+ [:deleted_at [:maybe ms/TemporalInstant]]+ [:deleted_by_user_id [:maybe ::lib.schema.id/user]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:ai_proxied [:maybe :boolean]]+ [:external_id [:maybe :string]]+ [:finished [:maybe :boolean]]+ [:error [:maybe :string]]+ [:data_version :int]+ [:state [:maybe ::metabot-message.state]]+ [:forked_from_message_id [:maybe ms/PositiveInt]]+ [:context_tokens [:maybe :int]]])++(mr/def ::metabot-message.update+ "What an update (or insert) of a MetabotMessage accepts: every column of `:metabot_message` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:profile_id {:optional true} [:maybe :string]]+ [:role {:optional true} [:maybe [:or :keyword :string]]]+ [:data {:optional true} [:maybe [:sequential ::metabot-message.data-part]]]+ [:usage {:optional true} [:maybe ::metabot-message.usage]]+ [:total_tokens {:optional true} [:maybe :int]]+ [:conversation_id {:optional true} [:maybe :string]]+ [:slack_msg_id {:optional true} [:maybe :string]]+ [:channel_id {:optional true} [:maybe :string]]+ [:deleted_at {:optional true} [:maybe ms/TemporalInstant]]+ [:deleted_by_user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:ai_proxied {:optional true} [:maybe :boolean]]+ [:external_id {:optional true} [:maybe :string]]+ [:finished {:optional true} [:maybe :boolean]]+ [:error {:optional true} [:maybe :string]]+ [:data_version {:optional true} [:maybe :int]]+ [:state {:optional true} [:maybe ::metabot-message.state]]+ [:forked_from_message_id {:optional true} [:maybe ms/PositiveInt]]+ [:context_tokens {:optional true} [:maybe :int]]])++(mr/def ::metabot-prompt+ "A MetabotPrompt as selected from the app DB: every column of `:metabot_prompt`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model [:or :keyword :string]]+ [:card_id ::lib.schema.id/card]+ [:entity_id :string]+ [:prompt :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:metabot_id ms/PositiveInt]])++(mr/def ::metabot-prompt.update+ "What an update (or insert) of a MetabotPrompt accepts: every column of `:metabot_prompt` except `id`, all optional."+ [:map {:closed true}+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:entity_id {:optional true} [:maybe :string]]+ [:prompt {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:metabot_id {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::metabot-source-feedback+ "A MetabotSourceFeedback as selected from the app DB: every column of `:metabot_source_feedback`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:message_id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:source_id ms/PositiveInt]+ [:source_type [:or :keyword :string]]+ [:positive :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::metabot-source-feedback.update+ "What an update (or insert) of a MetabotSourceFeedback accepts: every column of `:metabot_source_feedback` except `id`, all optional."+ [:map {:closed true}+ [:message_id {:optional true} [:maybe ms/PositiveInt]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:positive {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::metabot-used-table+ "A MetabotUsedTable as selected from the app DB: every column of `:metabot_used_table`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:message_id ms/PositiveInt]+ [:table_id ::lib.schema.id/table]+ [:created_at ms/TemporalInstant]])++(mr/def ::metabot-used-table.update+ "What an update (or insert) of a MetabotUsedTable accepts: every column of `:metabot_used_table` except `id`, all optional."+ [:map {:closed true}+ [:message_id {:optional true} [:maybe ms/PositiveInt]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.cljindex 12b60bb8f00..1f4e3342f9f 100644--- a/src/metabase/metabot/tools/util.clj+++ b/src/metabase/metabot/tools/util.clj@@ -203,11 +203,8 @@Only cards visible to the current user are returned, excluding those backed by a destination(routed) database (see [[destination-db-ids]])."- [metabot-id & {:as opts}]- (let [cards (metabot.db/cards-where (-> (metabot.db/metabot-metrics-and-models-query metabot-id opts)- ;; qualified: the official-collections branch joins `collection`,- ;; which also has `id`- (update :order-by (fnil conj []) [:report_card.id])))+ [metabot-id & {:keys [limit]}]+ (let [cards (metabot.db/metabot-metrics-and-models metabot-id limit)destination-ids (destination-db-ids (into #{} (keep :database_id) cards))](if (seq destination-ids)(remove #(contains? destination-ids (:database_id %)) cards)diff --git a/src/metabase/metrics/db.clj b/src/metabase/metrics/db.cljindex 808bca803ed..f165acf3988 100644--- a/src/metabase/metrics/db.clj+++ b/src/metabase/metrics/db.clj@@ -2,44 +2,50 @@"Application database queries for the metrics module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn metric-card-count+(mu/defn metric-card-count"The number of Cards matching the Honey SQL `where` clause."- [where]+ [where :- [:maybe vector?]](t2/count :model/Card {:where where}))-(defn metric-cards-page+(mu/defn metric-cards-page"Up to `limit` id, name, description, and Collection id rows from `offset` of the Cards matching the Honey SQL`where` clause, in name order."- [where limit offset]+ [where :- [:maybe vector?]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](t2/select [:model/Card :id :name :description :collection_id]{:where where:order-by [[:name :asc]]:limit limit:offset offset}))-(defn metric-card+(mu/defn metric-card"The metric Card with `id`, or nil."- [id]+ [id :- ::lib.schema.id/card](t2/select-one :model/Card :id id :type "metric"))-(defn measure+(mu/defn measure"The Measure with `id`, or nil."- [id]+ [id :- ::lib.schema.id/measure](t2/select-one :model/Measure :id id))-(defn metric-cards-for-database+(mu/defn metric-cards-for-database"The id and dimensions of the metric Cards of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Card :id :dimensions] :type "metric" :database_id database-id))-(defn fields-with-columns+(mu/defn fields-with-columns"The id and `columns` of the Fields with `field-ids`."- [columns field-ids]+ [columns :- [:sequential :keyword]+ field-ids :- [:set ::lib.schema.id/field]](t2/select (into [:model/Field :id] columns) :id [:in field-ids]))-(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))diff --git a/src/metabase/model_persistence/db.clj b/src/metabase/model_persistence/db.cljindex 01a42f7a673..34645a3dd66 100644--- a/src/metabase/model_persistence/db.clj+++ b/src/metabase/model_persistence/db.clj@@ -3,17 +3,27 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[honey.sql.helpers :as sql.helpers]+ [malli.util :as mut][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.model-persistence.schema :as model-persistence.schema][metabase.tracing.core :as tracing][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn persisted-info-listing+(mu/defn persisted-info-listing"Up to `limit` PersistedInfo listing rows (id, database, definition, active, state, error, refresh window,table name, creator, card name/archived/type, database name, and collection id/name/authority level) forunarchived model Cards, optionally narrowed to `persisted-info-id`, `db-ids`, and/or `card-id`, newestrefresh first, paginated from `offset` by `limit`."- [persisted-info-id db-ids card-id limit offset]+ [persisted-info-id :- [:maybe ms/PositiveInt]+ db-ids :- [:maybe [:sequential ::lib.schema.id/database]]+ card-id :- [:maybe ::lib.schema.id/card]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/PersistedInfo(cond-> {:select [:p.id :p.database_id :p.definition:p.active :p.state :p.error@@ -39,10 +49,10 @@limit (sql.helpers/limit limit)offset (sql.helpers/offset offset))))-(defn deletable-prunable-persisted-infos+(mu/defn deletable-prunable-persisted-infos"The PersistedInfos in one of `states` for over an hour, or attached to an archived question, or whoseCard has been deleted — the records [[metabase.model-persistence.task.persist-refresh]] may unpersist."- [states]+ [states :- [:set :string]](let [hsql {:select [:p.*]:from [[:persisted_info :p]]:left-join [[:report_card :c] [:= :c.id :p.card_id]]@@ -60,10 +70,11 @@(tracing/with-span :tasks "task.persist.find-deletable" {:db/statement (tracing/best-effort-sanitize-sql hsql)}(t2/select :model/PersistedInfo hsql))))-(defn refreshable-persisted-infos+(mu/defn refreshable-persisted-infos"The PersistedInfos of the Database with `database-id` in one of `states` whose Card is an unarchivedmodel, plus the Card's `:type`, `:archived`, and `:name`."- [database-id states]+ [database-id :- ::lib.schema.id/database+ states :- [:set :string]](let [hsql {:select [:p.* :c.type :c.archived :c.name]:from [[:persisted_info :p]]:left-join [[:report_card :c] [:= :c.id :p.card_id]]@@ -76,44 +87,45 @@:db/statement (tracing/best-effort-sanitize-sql hsql)}(t2/select :model/PersistedInfo hsql))))-(defn persisted-infos-by-ids+(mu/defn persisted-infos-by-ids"The PersistedInfos with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select :model/PersistedInfo :id [:in ids]))-(defn persisted-info+(mu/defn persisted-info"The PersistedInfo with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/PersistedInfo :id id))-(defn persisted-info-for-card+(mu/defn persisted-info-for-card"The PersistedInfo of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/PersistedInfo :card_id card-id))-(defn persisted-info-id-for-card+(mu/defn persisted-info-id-for-card"The id of the PersistedInfo of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :id :model/PersistedInfo :card_id card-id))-(defn persisted-info-state+(mu/defn persisted-info-state"The state of the PersistedInfo with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one-fn :state :model/PersistedInfo :id id))-(defn persisted-database-ids+(mu/defn persisted-database-ids"The set of Database ids with PersistedInfos."[](t2/select-fn-set :database_id :model/PersistedInfo))-(defn persisted-card-ids-in-states+(mu/defn persisted-card-ids-in-states"The Card ids among `card-ids` whose PersistedInfo is in one of `states`."- [card-ids states]+ [card-ids :- [:sequential ::lib.schema.id/card]+ states :- [:set :string]](t2/select-fn-set :card_id :model/PersistedInfo :card_id [:in card-ids] :state [:in states]))-(defn persisted-model-count-for-databases+(mu/defn persisted-model-count-for-databases"The number of PersistedInfos of unarchived model Cards of the Databases with `database-ids`."- [database-ids]+ [database-ids :- [:or [:set ::lib.schema.id/database] [:sequential ::lib.schema.id/database]]](t2/count :model/PersistedInfo {:from [[:persisted_info :p]]:join [[:report_card :c] [:= :c.id :p.card_id]]:where [:and@@ -121,51 +133,80 @@[:= :c.type "model"][:not :c.archived]]}))-(defn insert-persisted-info!+(mu/defn insert-persisted-info!"Insert the PersistedInfo `row` and return the inserted instance."- [row]+ [row :- ::model-persistence.schema/persisted-info.update](t2/insert-returning-instance! :model/PersistedInfo row))-(defn insert-persisted-infos!- "Insert the PersistedInfo `rows`."- [rows]+(mu/defn insert-persisted-infos!+ "Insert the PersistedInfo `rows`, returning the number inserted."+ [rows :- [:sequential ::model-persistence.schema/persisted-info.update]](t2/insert! :model/PersistedInfo rows))-(defn update-persisted-info!- "Apply `changes` to the PersistedInfo with `id`."- [id changes]+(mu/defn update-persisted-info!+ "Apply `changes` to the PersistedInfo with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- ::model-persistence.schema/persisted-info.update](t2/update! :model/PersistedInfo id changes))-(defn deactivate-all-persisted-infos!- "Deactivate every PersistedInfo and move it to `state`."- [state]+(mu/defn begin-persisted-info-refresh!+ "Mark the PersistedInfo with `id` as refreshing `definition` (with `query-hash`), starting now, returning the number+ updated."+ [id :- ms/PositiveInt+ definition :- [:maybe ::model-persistence.schema/persisted-info.definition]+ query-hash :- [:maybe :string]]+ (t2/update! :model/PersistedInfo id {:definition definition+ :query_hash query-hash+ :active false+ :refresh_end nil+ :state "refreshing"}))++(mu/defn end-persisted-info-refresh!+ "Record the outcome of the refresh of the PersistedInfo with `id`: `state` (\"persisted\" or \"error\"), whether+ it is now `active?`, and the `error` message, ended now. Returns the number updated."+ [id :- ms/PositiveInt+ active? :- :boolean+ state :- [:enum "persisted" "error"]+ error :- [:maybe :string]]+ (t2/update! :model/PersistedInfo id {:active active?+ :state state+ :error error}))++(mu/defn deactivate-all-persisted-infos!+ "Deactivate every PersistedInfo and move it to `state`, returning the number updated."+ [state :- :string](t2/update! :model/PersistedInfo {} {:active false, :state state, :state_change_at :%now}))-(defn deactivate-persisted-info!- "Deactivate the PersistedInfo with `id` and move it to `state`."- [id state]+(mu/defn deactivate-persisted-info!+ "Deactivate the PersistedInfo with `id` and move it to `state`, returning the number updated."+ [id :- ms/PositiveInt+ state :- :string](t2/update! :model/PersistedInfo id {:active false, :state state, :state_change_at :%now}))-(defn deactivate-persisted-infos-for-database!- "Deactivate the PersistedInfos of the Database with `database-id` and move them to `state`."- [database-id state]+(mu/defn deactivate-persisted-infos-for-database!+ "Deactivate the PersistedInfos of the Database with `database-id` and move them to `state`, returning the+ number updated."+ [database-id :- ::lib.schema.id/database+ state :- :string](t2/update! :model/PersistedInfo :database_id database-id {:active false, :state state, :state_change_at :%now}))-(defn invalidate-persisted-infos-for-cards!- "Deactivate the active PersistedInfos of the Cards with `card-ids` and move them back to creating."- [card-ids]+(mu/defn invalidate-persisted-infos-for-cards!+ "Deactivate the active PersistedInfos of the Cards with `card-ids` and move them back to creating, returning+ the number updated."+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/update! :model/PersistedInfo{:active true, :card_id [:in card-ids]}{:active false, :state "creating", :state_change_at :%now}))-(defn reset-persisted-info-to-creating!- "Deactivate the PersistedInfo with `id` and move it back to creating."- [id]+(mu/defn reset-persisted-info-to-creating!+ "Deactivate the PersistedInfo with `id` and move it back to creating, returning the number updated."+ [id :- ms/PositiveInt](t2/update! :model/PersistedInfo id {:active false, :state "creating", :state_change_at :%now}))-(defn ready-deletable-persisted-infos!- "Move the deletable PersistedInfos of the Database with `database-id` to `state`."- [database-id state]+(mu/defn ready-deletable-persisted-infos!+ "Move the deletable PersistedInfos of the Database with `database-id` to `state`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ state :- :string](t2/query-one{:update [:persisted_info]:where [:and@@ -175,14 +216,14 @@:state state,:state_change_at :%now}}))-(defn delete-persisted-info!- "Delete the PersistedInfo with `id`."- [id]+(mu/defn delete-persisted-info!+ "Delete the PersistedInfo with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/PersistedInfo :id id))-(defn unpersisted-models-for-database+(mu/defn unpersisted-models-for-database"The model Cards of the Database with `database-id` that have no PersistedInfo."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select :model/Card{:where [:and[:= :database_id database-id]@@ -192,32 +233,33 @@:from [:persisted_info]:where [:= :persisted_info.card_id :report_card.id]}]]]}))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn card-archived-and-type+(mu/defn card-archived-and-type"The archived flag and type of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :archived :type :card_schema] :id card-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one :model/Database :id database-id))-(defn databases+(mu/defn databases"The Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select :model/Database :id [:in database-ids]))-(defn all-databases+(mu/defn all-databases"Every Database."[](t2/select :model/Database))-(defn update-database!- "Apply `changes` to the Database with `database-id`."- [database-id changes]+(mu/defn update-database!+ "Apply `changes` to the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ changes :- (mut/select-keys ::warehouses.schema/database.update [:settings])](t2/update! :model/Database database-id changes))diff --git a/src/metabase/model_persistence/models/persisted_info.clj b/src/metabase/model_persistence/models/persisted_info.cljindex f16dae0c303..668f3f6faf1 100644--- a/src/metabase/model_persistence/models/persisted_info.clj+++ b/src/metabase/model_persistence/models/persisted_info.clj@@ -19,6 +19,24 @@(derive :model/PersistedInfo :metabase/model)+(t2/define-before-insert :model/PersistedInfo+ [persisted-info]+ (merge {:refresh_begin (mi/now), :state_change_at (mi/now)} persisted-info))++(t2/define-before-update :model/PersistedInfo+ [persisted-info]+ (let [changes (t2/changes persisted-info)+ state (some-> (:state changes) name)]+ (cond-> persisted-info+ (and state (not (contains? changes :state_change_at)))+ (assoc :state_change_at (mi/now))++ (and (= state "refreshing") (not (contains? changes :refresh_begin)))+ (assoc :refresh_begin (mi/now))++ (and (#{"persisted" "error"} state) (not (contains? changes :refresh_end)))+ (assoc :refresh_end (mi/now)))))+(defn transform-definition-out"Parse the value of `:definition` when it comes out of the application Database."[definition]@@ -152,10 +170,8 @@:question_slug slug:table_name (format "model_%s_%s" card-id slug):active false- :refresh_begin :%now:refresh_end nil:state default-state- :state_change_at :%now:creator_id user-id}))(defn ready-unpersisted-models!diff --git a/src/metabase/model_persistence/schema.clj b/src/metabase/model_persistence/schema.cljnew file mode 100644index 00000000000..b0a0cd845ce--- /dev/null+++ b/src/metabase/model_persistence/schema.clj@@ -0,0 +1,47 @@+(ns metabase.model-persistence.schema+ "Malli schemas for the model-persistence module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::persisted-info.definition+ "The `:definition` column of a PersistedInfo, decoded."+ :map)++(mr/def ::persisted-info+ "A PersistedInfo as selected from the app DB: every column of `:persisted_info`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:database_id ::lib.schema.id/database]+ [:card_id [:maybe ::lib.schema.id/card]]+ [:question_slug :string]+ [:table_name :string]+ [:definition [:maybe ::persisted-info.definition]]+ [:query_hash [:maybe :string]]+ [:active :boolean]+ [:state :string]+ [:refresh_begin ms/TemporalInstant]+ [:refresh_end [:maybe ms/TemporalInstant]]+ [:state_change_at [:maybe ms/TemporalInstant]]+ [:error [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:creator_id [:maybe ::lib.schema.id/user]]])++(mr/def ::persisted-info.update+ "What an update (or insert) of a PersistedInfo accepts: every column of `:persisted_info` except `id`, all optional."+ [:map {:closed true}+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:question_slug {:optional true} [:maybe :string]]+ [:table_name {:optional true} [:maybe :string]]+ [:definition {:optional true} [:maybe ::persisted-info.definition]]+ [:query_hash {:optional true} [:maybe :string]]+ [:active {:optional true} [:maybe :boolean]]+ [:state {:optional true} [:maybe :string]]+ [:refresh_begin {:optional true} [:maybe ms/TemporalInstant]]+ [:refresh_end {:optional true} [:maybe ms/TemporalInstant]]+ [:state_change_at {:optional true} [:maybe ms/TemporalInstant]]+ [:error {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]])diff --git a/src/metabase/model_persistence/task/persist_refresh.clj b/src/metabase/model_persistence/task/persist_refresh.cljindex 5a13661203c..3cfd20ac66b 100644--- a/src/metabase/model_persistence/task/persist_refresh.clj+++ b/src/metabase/model_persistence/task/persist_refresh.clj@@ -61,26 +61,19 @@(let [card (model-persistence.db/card (:card_id persisted-info))definition (persisted-info/metadata->definition (:result_metadata card)(:table_name persisted-info))- _ (model-persistence.db/update-persisted-info! (u/the-id persisted-info)- {:definition definition,- :query_hash (persisted-info/query-hash (:dataset_query card))- :active false,- :refresh_begin :%now,- :refresh_end nil,- :state "refreshing"- :state_change_at :%now})+ _ (model-persistence.db/begin-persisted-info-refresh! (u/the-id persisted-info)+ definition+ (persisted-info/query-hash (:dataset_query card))){:keys [state error]} (try(refresh! refresher database definition card)(catch Exception e(log/infof "Error refreshing persisting model with card-id %s: %s"(:card_id persisted-info) (ex-message e)){:state :error :error (ex-message e)}))]- (model-persistence.db/update-persisted-info! (u/the-id persisted-info)- {:active (= state :success),- :refresh_end :%now,- :state (if (= state :success) "persisted" "error")- :state_change_at :%now- :error (when (= state :error) error)})+ (model-persistence.db/end-persisted-info-refresh! (u/the-id persisted-info)+ (= state :success)+ (if (= state :success) "persisted" "error")+ (when (= state :error) error))(if (= :success state)(update stats :success inc)(-> statsdiff --git a/src/metabase/models/db.clj b/src/metabase/models/db.cljindex 7c5f45ef2aa..a45dd038f62 100644--- a/src/metabase/models/db.clj+++ b/src/metabase/models/db.clj@@ -3,53 +3,74 @@additional logic, so no other namespace in the module runs a query itself (the model machinery still uses`toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2][toucan2.tools.identity-query :as t2.identity-query]))-(defn entity-by-pk+(mu/defn entity-by-pk"The `model` row whose `pk-column` is `id`, or nil."- [model pk-column id]+ [model :- [:or :keyword symbol?]+ pk-column :- :keyword+ id :- [:or :int :string]](t2/select-one model pk-column id))-(defn entity-by-entity-id+(mu/defn entity-by-entity-id"The `model` row with `entity-id`, or nil."- [model entity-id]+ [model :- [:or :keyword symbol?]+ entity-id :- :string](t2/select-one model :entity_id entity-id))-(defn entity-field+(mu/defn entity-field"The `field` of the `model` row with `id`, or nil."- [model id field]+ [model :- [:or :keyword symbol?]+ id :- ms/PositiveInt+ field :- :keyword](t2/select-one-fn field model :id id))-(defn pk-by-entity-id+(mu/defn pk-by-entity-id"The `pk-column` of the `model` row with `entity-id`, or nil."- [model pk-column entity-id]+ [model :- [:or :keyword symbol?]+ pk-column :- :keyword+ entity-id :- :string](t2/select-one-fn pk-column [model pk-column] :entity_id entity-id))-(defn pk-by-field+(mu/defn pk-by-field"The primary key of the `model` row whose `field` is `value`, or nil."- [model field value]+ [model :- [:or :keyword symbol?]+ field :- :keyword+ value :- [:maybe [:or :string :int :boolean :keyword]]](t2/select-one-pk model field value))-(defn after-select-via-identity-query- "`row-map` run through the after-select machinery of `model`."- [model row-map]+(mu/defn after-select-via-identity-query+ "`row-map` run through the after-select machinery of `model`.++ `row-map` is a model instance row and so is generic across models; typed as a plain keyword-keyed map+ rather than a closed schema."+ [model :- [:or :keyword symbol?]+ row-map :- [:map-of :keyword [:maybe :some]]](t2/select-one model (t2.identity-query/identity-query [row-map])))-(defn entities-reducible+(mu/defn entities-reducible"A reducible of the `model` rows additionally matching the Honey SQL `extra-where` when given — the serdesextract-query nested-fetch hook passes a dynamic foreign-key-column + id-list condition here that varies permodel/transform and can't be expressed as fixed data — ordered by `order-by` (or unordered when nil)."- [model extra-where order-by]+ [model :- [:or :keyword symbol?]+ extra-where :- [:maybe vector?]+ order-by :- [:maybe vector?]](t2/reducible-select model (cond-> {}extra-where (assoc :where extra-where)order-by (assoc :order-by order-by))))-(defn entities-in-collections-reducible+(mu/defn entities-in-collections-reducible"A reducible of the `model` rows whose `:collection_id` is in `collection-set` (nil in the set counts as the rootcollection), additionally matching the Honey SQL `extra-where` when given (see [[entities-reducible]] for why thisstays a raw clause), ordered by `order-by` (or unordered when nil)."- [model collection-set extra-where order-by]+ [model :- [:or :keyword symbol?]+ collection-set :- [:or [:set [:maybe ms/PositiveInt]] [:sequential [:maybe ms/PositiveInt]]]+ extra-where :- [:maybe vector?]+ order-by :- [:maybe vector?]](t2/reducible-select model(cond-> {:where [:and[:or@@ -59,84 +80,106 @@extra-where]}order-by (assoc :order-by order-by))))-(defn table-names-reducible+(mu/defn table-names-reducible"A reducible of the id, name, and display name of every Table."[](t2/reducible-select [:model/Table :id :name :display_name]))-(defn field-names-reducible+(mu/defn field-names-reducible"A reducible of the id, name, and display name of every Field."[](t2/reducible-select [:model/Field :id :name :display_name]))-(defn update-entity!- "Apply `changes` to the `model` row with `id`."- [model id changes]+(mu/defn update-entity!+ "Apply `changes` to the `model` row with `id`, returning the number updated.++ Generic across models, so `changes` is typed as a plain keyword-keyed map rather than a closed schema."+ [model :- [:or :keyword symbol?]+ id :- [:or :int :string]+ changes :- [:map-of :keyword [:maybe :some]]](t2/update! model id changes))-(defn set-table-display-name!- "Set the display name of the Table with `id`."- [id display-name]+(mu/defn set-table-display-name!+ "Set the display name of the Table with `id`, returning the number updated."+ [id :- ::lib.schema.id/table+ display-name :- :string](t2/update! :model/Table id {:display_name display-name}))-(defn set-field-display-name!- "Set the display name of the Field with `id`."- [id display-name]+(mu/defn set-field-display-name!+ "Set the display name of the Field with `id`, returning the number updated."+ [id :- ::lib.schema.id/field+ display-name :- :string](t2/update! :model/Field id {:display_name display-name}))-(defn insert-entity!- "Insert the `model` `row` and return the inserted instance."- [model row]+(mu/defn insert-entity!+ "Insert the `model` `row` and return the inserted instance.++ Generic across models, so `row` is typed as a plain keyword-keyed map rather than a closed schema."+ [model :- [:or :keyword symbol?]+ row :- [:map-of :keyword [:maybe :some]]](t2/insert-returning-instance! model row))-(defn insert-entity-returning-pk!- "Insert the `model` `row` and return its primary key."- [model row]+(mu/defn insert-entity-returning-pk!+ "Insert the `model` `row` and return its primary key.++ Generic across models, so `row` is typed as a plain keyword-keyed map rather than a closed schema."+ [model :- [:or :keyword symbol?]+ row :- [:map-of :keyword [:maybe :some]]](t2/insert-returning-pk! model row))-(defn insert-entities!- "Insert the `model` `rows`."- [model rows]+(mu/defn insert-entities!+ "Insert the `model` `rows`, returning the number inserted."+ [model :- [:or :keyword symbol?]+ rows :- [:sequential :map]](t2/insert! model rows))-(defn delete-entity!- "Delete the `model` row with `id`."- [model id]+(mu/defn delete-entity!+ "Delete the `model` row with `id`, returning the number deleted."+ [model :- [:or :keyword symbol?]+ id :- [:maybe [:or :int :string]]](t2/delete! model id))-(defn delete-entities-with-ids!- "Delete the `model` rows whose `id-column` is one of `ids`."- [model id-column ids]+(mu/defn delete-entities-with-ids!+ "Delete the `model` rows whose `id-column` is one of `ids`, returning the number deleted."+ [model :- [:or :keyword symbol?]+ id-column :- :keyword+ ids :- [:sequential [:or :int :string]]](t2/delete! model id-column [:in ids]))-(defn delete-children!- "Delete the `model` rows whose `parent-column` is `parent-id`."- [model parent-column parent-id]+(mu/defn delete-children!+ "Delete the `model` rows whose `parent-column` is `parent-id`, returning the number deleted."+ [model :- [:or :keyword symbol?]+ parent-column :- :keyword+ parent-id :- [:or :int :string]](t2/delete! model parent-column parent-id))-(defn delete-children-except!- "Delete the `model` rows whose `parent-column` is `parent-id` and whose entity id is not one of `entity-ids`."- [model parent-column parent-id entity-ids]+(mu/defn delete-children-except!+ "Delete the `model` rows whose `parent-column` is `parent-id` and whose entity id is not one of `entity-ids`,+ returning the number deleted."+ [model :- [:or :keyword symbol?]+ parent-column :- :keyword+ parent-id :- [:or :int :string]+ entity-ids :- [:sequential :string]](t2/delete! model parent-column parent-id :entity_id [:not-in entity-ids]))-(defn collection-paths-columns+(mu/defn collection-paths-columns"The id, entity id, location, and name of every Collection."[](t2/select [:model/Collection :id :entity_id :location :name]))-(defn dashboard-entity-ids-and-names+(mu/defn dashboard-entity-ids-and-names"The entity id and name of every Dashboard."[](t2/select [:model/Dashboard :entity_id :name]))-(defn document-entity-ids-and-names+(mu/defn document-entity-ids-and-names"The entity id and name of every Document."[](t2/select [:model/Document :entity_id :name]))-(defn field-hierarchy-rows+(mu/defn field-hierarchy-rows"The name and Table id of the Field with `field-id` and each of its ancestors, deepest first."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select :model/Field{:with-recursive [[[:parents ^:allow-subquery {:columns [:id :name :parent_id :table_id]}]^:allow-subquery {:union-all [^:allow-subquery {:from [[:metabase_field :mf]]@@ -148,42 +191,48 @@:from [:parents]:select [:name :table_id]}))-(defn table-ref-columns+(mu/defn table-ref-columns"The id, Database id, name, and schema of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/Table :id :db_id :name :schema] :id table-id))-(defn database-name+(mu/defn database-name"The name of the Database with `database-id`, or nil."- [database-id]+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one-fn :name [:model/Database :id :name] :id database-id))-(defn database-names+(mu/defn database-names"The names of every Database."[](t2/select-fn-vec :name :model/Database))-(defn database-id-by-name+(mu/defn database-id-by-name"The id of the Database named `database-name`, or nil."- [database-name]+ [database-name :- :string](t2/select-one-fn :id :model/Database :name database-name))-(defn table-id-by-name+(mu/defn table-id-by-name"The id of the Table named `table-name` in `schema` of the Database with `database-id`, or nil."- [table-name schema database-id]+ [table-name :- :string+ schema :- [:maybe :string]+ database-id :- ::lib.schema.id/database](t2/select-one-fn :id :model/Table :name table-name :schema schema :db_id database-id))-(defn insert-inactive-table!+(mu/defn insert-inactive-table!"Insert an inactive Table named `table-name` in `schema` of the Database with `database-id` and return its id."- [database-id schema table-name]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/insert-returning-pk! :model/Table {:db_id database-id:schema schema:name table-name:active false}))-(defn field-pk+(mu/defn field-pk"The id of the Field named `field-name` under `parent-id` in the Table with `table-id`, or nil."- [table-id field-name parent-id]+ [table-id :- ::lib.schema.id/table+ field-name :- :string+ parent-id :- [:maybe ms/PositiveInt]](t2/select-one-pk :model/Field :table_id table-id :name field-name :parent_id parent-id))(defn- field-in-path-query@@ -196,24 +245,28 @@[:= :name field][:= :parent_id (field-in-path-query table-id rest)]]}))-(defn field-pk-in-path+(mu/defn field-pk-in-path"The id of the Field named by the last of `field-names` (each nested inside the previous, bottom-most first) under`table-id`, or nil."- [table-id field-names]+ [table-id :- ::lib.schema.id/table+ field-names :- [:sequential :string]](when (seq field-names)(t2/select-one-pk :model/Field (field-in-path-query table-id field-names))))-(defn field-in-path+(mu/defn field-in-path"The Field named by the last of `field-names` (each nested inside the previous, bottom-most first) under`table-id`, or nil."- [table-id field-names]+ [table-id :- [:maybe ::lib.schema.id/table]+ field-names :- [:sequential :string]](when (seq field-names)(t2/select-one :model/Field (field-in-path-query table-id field-names))))-(defn insert-inactive-field!+(mu/defn insert-inactive-field!"Insert an inactive, untyped Field named `field-name` under `parent-id` in the Table with `table-id` and return itsid."- [table-id parent-id field-name]+ [table-id :- ::lib.schema.id/table+ parent-id :- [:maybe ms/PositiveInt]+ field-name :- :string](t2/insert-returning-pk! :model/Field {:table_id table-id:parent_id parent-id:name field-name@@ -221,27 +274,30 @@:base_type :type/*:database_type "NULL"}))-(defn metadata-tables+(mu/defn metadata-tables"The `:metadata/table` rows named `table-name` in `schema` of the Database with `database-id`."- [database-id schema table-name]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/select :metadata/table :db_id database-id :schema schema :name table-name))-(defn card-serdes-columns+(mu/defn card-serdes-columns"The id, entity id, Collection id, Database id, and schema of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :id :entity_id :collection_id :database_id :card_schema] :id card-id))-(defn measure-serdes-columns+(mu/defn measure-serdes-columns"The id, entity id, and Table id of the Measure with `measure-id`, or nil."- [measure-id]+ [measure-id :- ::lib.schema.id/measure](t2/select-one [:model/Measure :id :entity_id :table_id] :id measure-id))-(defn segment-serdes-columns+(mu/defn segment-serdes-columns"The id, entity id, and Table id of the Segment with `segment-id`, or nil."- [segment-id]+ [segment-id :- ::lib.schema.id/segment](t2/select-one [:model/Segment :id :entity_id :table_id] :id segment-id))-(defn entity-by-own-pk+(mu/defn entity-by-own-pk"The `model` row identified by `id`, using whatever column is that model's own primary key."- [model id]+ [model :- [:or :keyword symbol?]+ id :- [:or :int :string]](t2/select-one model (first (t2/primary-keys model)) id))diff --git a/src/metabase/mq/db.clj b/src/metabase/mq/db.cljindex 3ca843ae631..7a3b5245ab7 100644--- a/src/metabase/mq/db.clj+++ b/src/metabase/mq/db.clj@@ -2,32 +2,42 @@"Application database queries for the message queue module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn waiting-queue-triggers-before+(mu/defn waiting-queue-triggers-before"The `{:trigger_name :job_name}` rows of `trigger-group` triggers in `sched-name`'s Quartz store that are `WAITING`and started before `threshold` (epoch ms)."- [sched-name trigger-group threshold]+ [sched-name :- :string+ trigger-group :- :string+ threshold :- :int];; unquoted upper-case `QRTZ_TRIGGERS` resolves on every app DB; see [[metabase.mq.task.queue-reaper]].(t2/query [(str "SELECT trigger_name, job_name FROM QRTZ_TRIGGERS"" WHERE sched_name = ? AND trigger_group = ? AND trigger_state = 'WAITING'"" AND start_time < ?")sched-name trigger-group threshold]))-(defn insert-outbox-row!+(mu/defn insert-outbox-row!"Insert a `queue_message_outbox` row for `queue-name` with `payload` and return its id."- [queue-name payload]+ [queue-name :- :string+ payload :- :string](t2/insert-returning-pk! :queue_message_outbox {:queue_name queue-name, :payload payload}))-(defn delete-outbox-rows!- "Delete the `queue_message_outbox` rows with `ids`."- [ids]+(mu/defn delete-outbox-rows!+ "Delete the `queue_message_outbox` rows with `ids`, returning the number deleted."+ [ids :- [:sequential ms/PositiveInt]](t2/delete! :queue_message_outbox :id [:in ids]))-(defn due-outbox-rows+(mu/defn due-outbox-rows"Up to `limit` `queue_message_outbox` rows after `after-id`, in id order, that are due: never attempted and created- before `created-before`, or scheduled to retry at or before `now`. Locked with the `for` clause `for-clause`."- [after-id now created-before limit for-clause]+ before `created-before`, or scheduled to retry at or before `now`. Locked with the `for` clause `for-clause`.+"+ [after-id :- ms/IntGreaterThanOrEqualToZero+ now :- ms/TemporalInstant+ created-before :- ms/TemporalInstant+ limit :- ms/PositiveInt+ for-clause :- [:sequential :keyword]](t2/query {:select [:id :queue_name :payload :publish_attempts]:from [:queue_message_outbox]:where [:and@@ -39,9 +49,11 @@:limit limit:for for-clause}))-(defn bump-outbox-row!- "Increment the publish attempts of the `queue_message_outbox` row with `id` and schedule its next attempt."- [id next-attempt-at]+(mu/defn bump-outbox-row!+ "Increment the publish attempts of the `queue_message_outbox` row with `id` and schedule its next attempt,+ returning the number updated."+ [id :- ms/PositiveInt+ next-attempt-at :- ms/TemporalInstant](t2/update! :queue_message_outbox :id id{:publish_attempts [:+ :publish_attempts [:inline 1]]:next_attempt_at next-attempt-at}))diff --git a/src/metabase/mq/queue/outbox.clj b/src/metabase/mq/queue/outbox.cljindex dce1d043e28..5f45ec6e338 100644--- a/src/metabase/mq/queue/outbox.clj+++ b/src/metabase/mq/queue/outbox.clj@@ -28,7 +28,6 @@[metabase.util.log :as log][toucan2.core :as t2])(:import- (java.sql Timestamp)(java.time Instant)))(set! *warn-on-reflection* true)@@ -141,7 +140,7 @@(log/warn "Failed to publish queue outbox row during recovery; will retry with backoff"{:queue queue_name :outbox-id id :publish-attempts next-attempts :retry-delay-ms delay-ms :error (ex-message e)})(analytics/inc! :metabase-mq/batches-retried {:channel queue_name :reason "outbox-recovery"})- (update acc :bumps conj {:id id :next-attempt-at (Timestamp/from (.plusMillis now delay-ms))})))+ (update acc :bumps conj {:id id :next-attempt-at (.plusMillis now delay-ms)})))(defn- recover-page!"Runs one transaction of the recovery sweep over up to [[recovery-page-size]] *due* rows, in id order@@ -153,10 +152,10 @@[after-id](t2/with-transaction [_conn](let [now (Instant/now)- now-ts (Timestamp/from now)+ now-ts nowrows (mq.db/due-outbox-rows after-idnow-ts- (Timestamp/from (.minusMillis now recovery-age-ms))+ (.minusMillis now recovery-age-ms)recovery-page-size(for-update-clause)){:keys [recover-ids bumps backend-down?]}diff --git a/src/metabase/native_query_snippets/db.clj b/src/metabase/native_query_snippets/db.cljindex ced861973df..56a9f8e1320 100644--- a/src/metabase/native_query_snippets/db.clj+++ b/src/metabase/native_query_snippets/db.clj@@ -3,55 +3,64 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[honey.sql.helpers :as sql.helpers]+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.serialization :as serdes]+ [metabase.native-query-snippets.schema :as native-query-snippets.schema]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn snippets-by-archived+(mu/defn snippets-by-archived"The NativeQuerySnippets whose archived flag is `archived`, in case-insensitive name order."- [archived]+ [archived :- :boolean](t2/select :model/NativeQuerySnippet :archived archived {:order-by [[:%lower.name :asc]]}))-(defn snippet+(mu/defn snippet"The NativeQuerySnippet with `id`, or nil."- [id]+ [id :- ::lib.schema.id/native-query-snippet](t2/select-one :model/NativeQuerySnippet :id id))-(defn snippet-name-exists?+(mu/defn snippet-name-exists?"Whether a NativeQuerySnippet named `snippet-name` exists."- [snippet-name]+ [snippet-name :- :string](t2/exists? :model/NativeQuerySnippet :name snippet-name))-(defn other-snippet-with-name-exists?+(mu/defn other-snippet-with-name-exists?"Whether a NativeQuerySnippet named `snippet-name` with an entity id other than `entity-id` exists."- [snippet-name entity-id]+ [snippet-name :- :string+ entity-id :- :string](t2/exists? :model/NativeQuerySnippet :name snippet-name :entity_id [:!= entity-id]))-(defn insert-snippet!+(mu/defn insert-snippet!"Insert the NativeQuerySnippet `row` and return the inserted instance."- [row]+ [row :- ::native-query-snippets.schema/native-query-snippet.update](t2/insert-returning-instance! :model/NativeQuerySnippet row))-(defn update-snippet!+(mu/defn update-snippet!"Apply `changes` to the NativeQuerySnippet with `id`."- [id changes]+ [id :- ::lib.schema.id/native-query-snippet+ changes :- (mut/select-keys ::native-query-snippets.schema/native-query-snippet.update [:description :collection_id :archived :content :name])](t2/update! :model/NativeQuerySnippet id changes))-(defn snippet-id-by-name+(mu/defn snippet-id-by-name"The id of the NativeQuerySnippet named `snippet-name`, or nil."- [snippet-name]+ [snippet-name :- :string](t2/select-one-fn :id :model/NativeQuerySnippet :name snippet-name))-(defn snippet-collection-id+(mu/defn snippet-collection-id"The Collection id of the NativeQuerySnippet with `id`, or nil."- [id]+ [id :- ::lib.schema.id/native-query-snippet](t2/select-one-fn :collection_id :model/NativeQuerySnippet :id id))-(defn exportable-snippets+(mu/defn exportable-snippets"A reducible of the NativeQuerySnippets to export via serdes: unarchived when `skip-archived?`, and eitherin one of `collection-ids`, uncollected when `include-root?`, or matching the serdes-supplied`extra-condition` — an additional condition the caller widens the export scope with (e.g. also export asa Card dependency, regardless of collection), or nil — in stable export order."- [collection-ids include-root? skip-archived? extra-condition]+ [collection-ids :- [:maybe [:sequential ::lib.schema.id/collection]]+ include-root? :- :boolean+ skip-archived? :- [:maybe :boolean]+ extra-condition :- [:maybe vector?]](t2/reducible-select :model/NativeQuerySnippet(cond-> {:where [:and(when skip-archived? [:not :archived])diff --git a/src/metabase/native_query_snippets/schema.clj b/src/metabase/native_query_snippets/schema.cljindex ecf3da70479..b95d68d8627 100644--- a/src/metabase/native_query_snippets/schema.clj+++ b/src/metabase/native_query_snippets/schema.clj@@ -2,15 +2,34 @@(:require[metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.template-tag :as lib.schema.template-tag]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::native-query-snippet- "Schema for an instance of a `:model/NativeQuerySnippet`."- [:map- [:id ::lib.schema.id/snippet]- [:name :string]- [:content :string]- [:description {:optional true} [:maybe :string]]+ "A NativeQuerySnippet as selected from the app DB: every column of `:native_query_snippet`."+ [:map {:closed true}+ [:id ::lib.schema.id/native-query-snippet]+ [:name :string]+ [:description [:maybe :string]]+ [:content :string]+ [:creator_id ::lib.schema.id/user]+ [:archived :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:entity_id :string]+ [:template_tags [:maybe ::lib.schema.template-tag/template-tag-map]]])++(mr/def ::native-query-snippet.update+ "What an update (or insert) of a NativeQuerySnippet accepts: every column of `:native_query_snippet` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:content {:optional true} [:maybe :string]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:archived {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]][:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]- ;; TODO (Cam 2026-07-08) Change Native Query Snippets to store template tags as a list like we do in MBQL as of 63.- [:template_tags [:ref ::lib.schema.template-tag/template-tag-map]]])+ [:entity_id {:optional true} [:maybe :string]]+ [:template_tags {:optional true} [:maybe ::lib.schema.template-tag/template-tag-map]]])diff --git a/src/metabase/notification/api/admin.clj b/src/metabase/notification/api/admin.cljindex 63efc22a2d4..93de3ea1fb2 100644--- a/src/metabase/notification/api/admin.clj+++ b/src/metabase/notification/api/admin.clj@@ -333,7 +333,6 @@(let [runs (notification.db/terminal-alert-runs run-type-alertnotification-idterminal-statuses- (notification.db/admin-lookback-cutoff)result-limit)failed-ids (into #{} (keep (fn [{:keys [id status]}](when (#{:failed :abandoned} status) id))@@ -363,8 +362,7 @@:channels channel-entries}))))(notification.db/channel-send-history-reducible run-type-alertnotification-id- task-channel-send- (notification.db/admin-lookback-cutoff))))+ task-channel-send)))(defn- get-notification-detail"Fetch a single card-type notification with `:last_check`, `:last_send`, `:check_history`, anddiff --git a/src/metabase/notification/db.clj b/src/metabase/notification/db.cljindex b1da9795c2f..da99f648dd6 100644--- a/src/metabase/notification/db.clj+++ b/src/metabase/notification/db.clj@@ -5,39 +5,119 @@(:require[clojure.string :as str][honey.sql.helpers :as sql.helpers]+ [malli.util :as mut][metabase.app-db.core :as mdb]- [metabase.models.interface :as mi]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.notification.schema :as notification.schema][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))+(def ^:private NotificationRow+ "A whole Notification row for insert."+ [:map {:closed true}+ [:payload_type {:optional true} [:maybe [:or :keyword :string]]]+ [:active {:optional true} [:maybe :boolean]]+ [:internal_id {:optional true} [:maybe :string]]+ [:payload_id {:optional true} [:maybe ms/PositiveInt]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]])++(def ^:private NotificationCardRow+ "A whole NotificationCard row for insert."+ [:map {:closed true}+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:send_once {:optional true} [:maybe :boolean]]+ [:send_condition {:optional true} [:maybe [:or :keyword :string]]]+ [:disable_links {:optional true} [:maybe :boolean]]])++(def ^:private NotificationHandlerRow+ "A whole NotificationHandler row for insert."+ [:map {:closed true}+ [:channel_type {:optional true} [:maybe [:or :keyword :string]]]+ [:notification_id {:optional true} [:maybe ms/PositiveInt]]+ [:channel_id {:optional true} [:maybe ms/PositiveInt]]+ [:template_id {:optional true} [:maybe ms/PositiveInt]]+ [:active {:optional true} [:maybe :boolean]]])++(def ^:private NotificationRecipientRow+ "A whole NotificationRecipient row for insert."+ [:map {:closed true}+ [:notification_handler_id {:optional true} [:maybe ms/PositiveInt]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:permissions_group_id {:optional true} [:maybe ms/PositiveInt]]+ [:details {:optional true} [:maybe :map]]])++(def ^:private NotificationSubscriptionRow+ "A whole NotificationSubscription row for insert."+ [:map {:closed true}+ [:notification_id {:optional true} [:maybe ms/PositiveInt]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:event_name {:optional true} [:maybe [:or :keyword :string]]]+ [:cron_schedule {:optional true} [:maybe :string]]+ [:ui_display_type {:optional true} [:maybe [:or :keyword :string]]]])++(def ^:private ChannelTemplateRow+ "A whole ChannelTemplate row for insert."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:channel_type {:optional true} [:maybe [:or :keyword :string]]]+ [:details {:optional true} [:maybe :map]]])++(def ^:private AdminFilters+ "The filters accepted by [[admin-notifications-page]] and [[admin-notifications-count]]."+ [:map {:closed true}+ [:active {:optional true} [:maybe :boolean]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:creator_active {:optional true} [:maybe :boolean]]+ [:creatorless {:optional true} [:maybe :boolean]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:recipient_notification_ids {:optional true} [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]]+ [:channel {:optional true} [:maybe [:or :string [:sequential :string]]]]+ [:last_send_status {:optional true} [:maybe [:enum :failing :successful]]]+ [:last_check_status {:optional true} [:maybe [:enum :failing :successful]]]+ [:query {:optional true} [:maybe :string]]+ [:sort_column {:optional true} [:maybe [:enum :id :last_send :last_check :card_name :creator_name :updated_at]]]+ [:sort_direction {:optional true} [:maybe [:enum :asc :desc]]]])+;;; --------------------------------------------- Notification ----------------------------------------------(defn notification+(mu/defn notification"The Notification with `notification-id`, or nil."- [notification-id]+ [notification-id :- ms/PositiveInt](t2/select-one :model/Notification notification-id))-(defn notification-by-internal-id+(mu/defn notification-by-internal-id"The seeded Notification with `internal-id`, or nil."- [internal-id]+ [internal-id :- :string](t2/select-one :model/Notification :internal_id internal-id))-(defn notification-for-handler+(mu/defn notification-for-handler"The Notification owning the NotificationHandler with `handler-id`, or nil."- [handler-id]+ [handler-id :- ms/PositiveInt](t2/select-one :model/Notification:id [:in ^:allow-subquery {:select [:notification_id]:from :notification_handler:where [:= :id handler-id]}]))-(defn notifications-matching+(mu/defn notifications-matching"Reducible Notifications, optionally narrowed to `creator-id`, `creator-or-recipient-id` (a User who is either thecreator or a recipient), `recipient-id`, `card-id`, and `payload-type`; active Notifications only unless`include-inactive?` or `legacy-active` (a boolean, overriding both) is given. `legacy-user-id` narrows to a Userwho is either the creator or a recipient."[{:keys [creator-id creator-or-recipient-id recipient-id card-id payload-type include-inactive? legacy-active- legacy-user-id]}]+ legacy-user-id]}+ :- [:map {:closed true}+ [:creator-id {:optional true} [:maybe ::lib.schema.id/user]]+ [:creator-or-recipient-id {:optional true} [:maybe ms/PositiveInt]]+ [:recipient-id {:optional true} [:maybe ms/PositiveInt]]+ [:card-id {:optional true} [:maybe ::lib.schema.id/card]]+ [:payload-type {:optional true} [:maybe [:or :keyword :string]]]+ [:include-inactive? {:optional true} [:maybe :boolean]]+ [:legacy-active {:optional true} [:maybe :boolean]]+ [:legacy-user-id {:optional true} [:maybe ::lib.schema.id/user]]]](t2/reducible-select:model/Notification(cond-> {:select-distinct [:notification.*]}@@ -85,19 +165,19 @@[:= :notification_recipient.user_id legacy-user-id][:= :notification.creator_id legacy-user-id]])))))-(defn notifications-by-id+(mu/defn notifications-by-id"The Notifications with `notification-ids`."- [notification-ids]+ [notification-ids :- [:sequential ms/PositiveInt]](t2/select :model/Notification :id [:in notification-ids]))-(defn card-notifications+(mu/defn card-notifications"The card Notifications among `notification-ids`."- [notification-ids]+ [notification-ids :- [:sequential ms/PositiveInt]](t2/select :model/Notification :id [:in notification-ids] :payload_type :notification/card))-(defn active-card-notifications-for-card+(mu/defn active-card-notifications-for-card"The active card Notifications attached to the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select :model/Notification:active true:payload_type :notification/card@@ -105,9 +185,9 @@:from [:notification_card]:where [:= :card_id card-id]}]))-(defn active-system-event-notifications+(mu/defn active-system-event-notifications"The active Notifications subscribed to the system event named `event-name`."- [event-name]+ [event-name :- :string](t2/select :model/Notification{:select [:n.*]:from [[:notification :n]]@@ -117,90 +197,91 @@[:= :ns.event_name event-name][:= :ns.type "notification-subscription/system-event"]]}))-(defn insert-notification!+(mu/defn insert-notification!"Insert `notification` and return the new instance."- [notification]+ [notification :- NotificationRow](t2/insert-returning-instance! :model/Notification notification))-(defn update-card-notifications!- "Apply `changes` to the card Notifications among `notification-ids`."- [notification-ids changes]+(mu/defn update-card-notifications!+ "Apply `changes` to the card Notifications among `notification-ids`, returning the number updated."+ [notification-ids :- [:sequential ms/PositiveInt]+ changes :- (mut/select-keys ::notification.schema/notification.update [:active :creator_id])](t2/update! :model/Notification :id [:in notification-ids] :payload_type :notification/card changes))-(defn deactivate-notification!- "Mark the Notification with `notification-id` inactive."- [notification-id]+(mu/defn deactivate-notification!+ "Mark the Notification with `notification-id` inactive, returning the number updated."+ [notification-id :- ms/PositiveInt](t2/update! :model/Notification notification-id {:active false}))-(defn delete-notification!- "Delete the Notification with `notification-id`."- [notification-id]+(mu/defn delete-notification!+ "Delete the Notification with `notification-id`, returning the number deleted."+ [notification-id :- ms/PositiveInt](t2/delete! :model/Notification notification-id))-(defn delete-notifications!- "Delete the Notifications with `notification-ids`."- [notification-ids]+(mu/defn delete-notifications!+ "Delete the Notifications with `notification-ids`, returning the number deleted."+ [notification-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/Notification :id [:in notification-ids]))-(defn delete-notification-by-internal-id!- "Delete the seeded Notification with `internal-id`."- [internal-id]+(mu/defn delete-notification-by-internal-id!+ "Delete the seeded Notification with `internal-id`, returning the number deleted."+ [internal-id :- :string](t2/delete! :model/Notification :internal_id internal-id));;; ------------------------------------------- NotificationCard --------------------------------------------(defn notification-cards+(mu/defn notification-cards"The NotificationCards with `notification-card-ids`."- [notification-card-ids]+ [notification-card-ids :- [:set ::lib.schema.id/card]](t2/select :model/NotificationCard :id [:in notification-card-ids]))-(defn notification-card-exists?+(mu/defn notification-card-exists?"Whether a NotificationCard with `notification-card-id` exists."- [notification-card-id]+ [notification-card-id :- ::lib.schema.id/card](t2/exists? :model/NotificationCard notification-card-id))-(defn notification-card-card-id+(mu/defn notification-card-card-id"The `:card_id` of the NotificationCard with `notification-card-id`."- [notification-card-id]+ [notification-card-id :- ::lib.schema.id/card](t2/select-one-fn :card_id :model/NotificationCard :id notification-card-id))-(defn insert-notification-card!+(mu/defn insert-notification-card!"Insert `notification-card` and return its ID."- [notification-card]+ [notification-card :- NotificationCardRow](t2/insert-returning-pk! :model/NotificationCard notification-card))-(defn delete-notification-card!- "Delete the NotificationCard with `notification-card-id`."- [notification-card-id]+(mu/defn delete-notification-card!+ "Delete the NotificationCard with `notification-card-id`, returning the number deleted."+ [notification-card-id :- ::lib.schema.id/card](t2/delete! :model/NotificationCard notification-card-id));;; --------------------------------------- NotificationSubscription ----------------------------------------(defn subscription+(mu/defn subscription"The NotificationSubscription with `subscription-id`, or nil."- [subscription-id]+ [subscription-id :- ms/PositiveInt](t2/select-one :model/NotificationSubscription subscription-id))-(defn subscriptions-for-notifications- "The NotificationSubscriptions of the Notifications with `notification-ids`."- [notification-ids]+(mu/defn subscriptions-for-notifications+ "The NotificationSubscriptions of the Notifications with `notification-ids` (nil entries are ignored)."+ [notification-ids :- [:sequential [:maybe ms/PositiveInt]]](t2/select :model/NotificationSubscription :notification_id [:in notification-ids]))-(defn cron-subscriptions-for-notification+(mu/defn cron-subscriptions-for-notification"The cron NotificationSubscriptions of the Notification with `notification-id`."- [notification-id]+ [notification-id :- ms/PositiveInt](t2/select :model/NotificationSubscription:notification_id notification-id:type :notification-subscription/cron))-(defn cron-subscription-ids-for-notification+(mu/defn cron-subscription-ids-for-notification"The IDs of the cron NotificationSubscriptions of the Notification with `notification-id`."- [notification-id]+ [notification-id :- ms/PositiveInt](t2/select-pks-set :model/NotificationSubscription:notification_id notification-id:type :notification-subscription/cron))-(defn active-cron-subscriptions-by-id+(mu/defn active-cron-subscriptions-by-id"A map of ID to cron NotificationSubscription for every active Notification."[](t2/select-pk->fn identity :model/NotificationSubscription@@ -212,23 +293,24 @@[:= :ns.type "notification-subscription/cron"][:= :n.active true]]}))-(defn insert-subscriptions!- "Insert the NotificationSubscription `subscriptions`."- [subscriptions]+(mu/defn insert-subscriptions!+ "Insert the NotificationSubscription `subscriptions`, returning the number inserted."+ [subscriptions :- [:sequential NotificationSubscriptionRow]](t2/insert! :model/NotificationSubscription subscriptions));;; ------------------------------------ NotificationHandler / Recipient -------------------------------------(defn handlers-for-notifications+(mu/defn handlers-for-notifications"The NotificationHandlers of the Notifications with `notification-ids`."- [notification-ids]+ [notification-ids :- [:sequential ms/PositiveInt]](t2/select :model/NotificationHandler :notification_id [:in notification-ids]))-(defn handler-notification-ids-for-email+(mu/defn handler-notification-ids-for-email"The set of Notification IDs of the handlers whose recipients (joined to their user) have an exact, lower-cased`email` match, either directly (via the recipient's User) or among `raw-value-handler-ids` (handler IDs alreadyknown to have a matching raw-value recipient)."- [lower-email raw-value-handler-ids]+ [lower-email :- :string+ raw-value-handler-ids :- [:set ms/PositiveInt]](let [user-clause [:and[:= :nr.type "notification-recipient/user"][:= [:lower :cu.email] lower-email]]]@@ -240,42 +322,44 @@[:or user-clause [:in :notification_handler.id raw-value-handler-ids]]user-clause)})))-(defn insert-handler!+(mu/defn insert-handler!"Insert `handler` and return its ID."- [handler]+ [handler :- NotificationHandlerRow](t2/insert-returning-pk! :model/NotificationHandler handler))-(defn recipients-for-handlers- "The NotificationRecipients of the NotificationHandlers with `handler-ids`."- [handler-ids]+(mu/defn recipients-for-handlers+ "The NotificationRecipients of the NotificationHandlers with `handler-ids` (nil entries are ignored)."+ [handler-ids :- [:sequential [:maybe ms/PositiveInt]]](t2/select :model/NotificationRecipient :notification_handler_id [:in handler-ids]))-(defn raw-value-recipients-for-handler+(mu/defn raw-value-recipients-for-handler"The raw-value NotificationRecipients of the NotificationHandler with `handler-id`."- [handler-id]+ [handler-id :- ms/PositiveInt](t2/select :model/NotificationRecipient:notification_handler_id handler-id:type :notification-recipient/raw-value))-(defn raw-value-recipients-reducible+(mu/defn raw-value-recipients-reducible"Reducible handler IDs and details of every raw-value NotificationRecipient."[](t2/reducible-select [:model/NotificationRecipient :notification_handler_id :details]:type :notification-recipient/raw-value))-(defn insert-recipients!- "Insert one NotificationRecipient map or a sequence of them."- [recipients]+(mu/defn insert-recipients!+ "Insert one NotificationRecipient map or a sequence of them, returning the number inserted."+ [recipients :- [:or NotificationRecipientRow [:sequential NotificationRecipientRow]]](t2/insert! :model/NotificationRecipient recipients))-(defn delete-recipient!- "Delete the NotificationRecipient with `recipient-id`."- [recipient-id]+(mu/defn delete-recipient!+ "Delete the NotificationRecipient with `recipient-id`, returning the number deleted."+ [recipient-id :- ms/PositiveInt](t2/delete! :model/NotificationRecipient recipient-id))-(defn delete-user-recipients-for-notification!- "Delete the NotificationRecipients for the User with `user-id` on the Notification with `notification-id`."- [notification-id user-id]+(mu/defn delete-user-recipients-for-notification!+ "Delete the NotificationRecipients for the User with `user-id` on the Notification with `notification-id`,+ returning the number deleted."+ [notification-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/delete! :model/NotificationRecipient:user_id user-id:notification_handler_id [:in ^:allow-subquery {:select [:id]@@ -284,51 +368,66 @@;;; ---------------------------------------------- Channels -----------------------------------------------(defn active-channels-by-id+(mu/defn active-channels-by-id"A map of ID to active Channel for `channel-ids`."- [channel-ids]+ [channel-ids :- [:sequential ms/PositiveInt]](t2/select-fn->fn :id identity :model/Channel :id [:in channel-ids] :active true))-(defn channel-templates-by-id+(mu/defn channel-templates-by-id"A map of ID to ChannelTemplate for `template-ids`."- [template-ids]+ [template-ids :- [:sequential ms/PositiveInt]](t2/select-fn->fn :id identity :model/ChannelTemplate :id [:in template-ids]))-(defn channel-template-channel-type+(mu/defn channel-template-channel-type"The `:channel_type` of the ChannelTemplate with `template-id`."- [template-id]+ [template-id :- ms/PositiveInt](t2/select-one-fn :channel_type [:model/ChannelTemplate :channel_type] template-id))-(defn insert-channel-template!+(mu/defn insert-channel-template!"Insert `template` and return its ID."- [template]+ [template :- ChannelTemplateRow](t2/insert-returning-pk! :model/ChannelTemplate template))-(defn delete-channel-templates!- "Delete the ChannelTemplates with `template-ids`."- [template-ids]+(mu/defn delete-channel-templates!+ "Delete the ChannelTemplates with `template-ids`, returning the number deleted."+ [template-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/ChannelTemplate :id [:in template-ids]));;; ---------------------------------------------- Task runs -----------------------------------------------(defn terminal-alert-runs+(def ^:private admin-run-lookback-days+ "How far back to consider alert-type TaskRuns / TaskHistory rows when computing run summaries."+ 90)++(defn- admin-lookback-cutoff+ "The earliest `started_at` considered for the admin notification list/detail run history:+ [[admin-run-lookback-days]] days before now, as a Honey SQL form."+ []+ (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- admin-run-lookback-days) :day))++(mu/defn terminal-alert-runs"Up to `limit` TaskRuns of `run-type` for the Notification with `notification-id` that reached one of `statuses`- after `cutoff`, newest first."- [run-type notification-id statuses cutoff limit]+ within the admin lookback window, newest first."+ [run-type :- :string+ notification-id :- ms/PositiveInt+ statuses :- [:sequential :string]+ limit :- ms/PositiveInt](t2/select [:model/TaskRun :id :status :started_at]{:where [:and[:= :run_type run-type][:= :notification_id notification-id][:in :status statuses]- [:> :started_at cutoff]]+ [:> :started_at (admin-lookback-cutoff)]]:order-by [[:started_at :desc] [:id :desc]]:limit limit}))-(defn channel-send-history-reducible+(mu/defn channel-send-history-reducible"Reducible TaskHistory rows of `task` for the runs of `run-type` for the Notification with `notification-id`- started after `cutoff`, newest run first with `tr.id` as the tiebreaker so each run's rows stay adjacent, capped- at 500 rows."- [run-type notification-id task cutoff]+ started within the admin lookback window, newest run first with `tr.id` as the tiebreaker so each run's rows stay+ adjacent, capped at 500 rows."+ [run-type :- :string+ notification-id :- ms/PositiveInt+ task :- :string](t2/reducible-select :model/TaskHistory{:select [:th.run_id :th.task_details :th.status[:tr.started_at :run_started_at]]@@ -338,14 +437,16 @@[:= :tr.run_type run-type][:= :tr.notification_id notification-id][:= :th.task task]- [:> :tr.started_at cutoff]]+ [:> :tr.started_at (admin-lookback-cutoff)]]:order-by [[:tr.started_at :desc] [:tr.id :desc]]:limit 500}))-(defn latest-failed-task-history+(mu/defn latest-failed-task-history"The `:run_id` and `:task_details` of the single failed/abandoned TaskHistory row per run among `run-ids` (and, ifgiven, restricted to `task-name`), preferring rows of `preferred-task` and then the latest by `ended_at`."- [preferred-task run-ids task-name]+ [preferred-task :- :string+ run-ids :- [:set ms/PositiveInt]+ task-name :- [:maybe :string]](t2/select :model/TaskHistory{:select [:run_id :task_details]:from [[^:allow-subquery@@ -372,16 +473,6 @@(def ^:private admin-task-channel-send "channel-send")(def ^:private admin-terminal-statuses ["success" "failed" "abandoned"])-(def ^:private admin-run-lookback-days- "How far back to consider alert-type TaskRuns / TaskHistory rows when computing run summaries."- 90)--(defn admin-lookback-cutoff- "The earliest `started_at` considered for the admin notification list/detail run history:- [[admin-run-lookback-days]] days before now."- []- (h2x/add-interval-honeysql-form (mdb/db-type) (mi/now) (- admin-run-lookback-days) :day))-(defn- latest-run-per-notification[lookback]^:allow-subquery@@ -553,86 +644,90 @@(assoc (admin-base-list-query (dissoc filters :sort_column :sort_direction)):order-by (admin-order-by-clauses (or sort_column :last_send) sort_direction)))-(defn admin-notifications-page+(mu/defn admin-notifications-page"A page (`limit`/`offset`) of admin notification-list rows matching `filters` (see[[metabase.notification.api.admin]] for the supported keys), most-relevant first per `:sort_column`/`:sort_direction`."- [filters limit offset]+ [filters :- AdminFilters+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](t2/select :model/Notification (assoc (admin-list-query filters) :limit limit :offset offset)))-(defn admin-notifications-count+(mu/defn admin-notifications-count"The number of admin notification-list rows matching `filters`."- [filters]+ [filters :- AdminFilters](:count (t2/query-one (-> (admin-list-query filters)(assoc :select [[[:count :notification.id] :count]])(dissoc :order-by)))))-(defn admin-notification-detail-row+(mu/defn admin-notification-detail-row"The admin notification-list row (skipping the run-summary joins) for the Notification with `notification-id`, ornil."- [notification-id]+ [notification-id :- ms/PositiveInt](t2/select-one :model/Notification(-> (admin-base-list-query {:skip-run-joins? true})(sql.helpers/where [:= :notification.id notification-id]))));;; ------------------------------------------- Other models --------------------------------------------(defn instance- "The instance of `model` with `id`, or nil."- [model id]+(mu/defn instance+ "The instance of `model` (a model keyword, or a `[model & columns]` vector) with `id`, or nil."+ [model :- [:or :keyword [:sequential :keyword]]+ id :- [:maybe ms/PositiveInt]](t2/select-one model id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card card-id))-(defn unarchived-card+(mu/defn unarchived-card"The Card with `card-id` if it is not archived, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id :archived false))-(defn card-name+(mu/defn card-name"The name of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :name :model/Card card-id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard dashboard-id))-(defn dashboard-name+(mu/defn dashboard-name"The name of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :name :model/Dashboard dashboard-id))-(defn dashboard-tabs+(mu/defn dashboard-tabs"The DashboardTabs of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardTab :dashboard_id dashboard-id))-(defn dashboard-tab-count+(mu/defn dashboard-tab-count"The number of DashboardTabs on the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/count :model/DashboardTab :dashboard_id dashboard-id))-(defn dashcards-for-dashboard+(mu/defn dashcards-for-dashboard"The DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardCard :dashboard_id dashboard-id))-(defn user-email+(mu/defn user-email"The email of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :email [:model/User :email] user-id))-(defn user-summary- "The ID, names, and email of the User with `user-id`."- [user-id]+(mu/defn user-summary+ "The ID, names, email, and derived common name of the User with `user-id`, or nil (also for a nil `user-id`, e.g.+ a system-created notification without a creator)."+ [user-id :- [:maybe ::lib.schema.id/user]](t2/select-one [:model/User :id :first_name :last_name :email] user-id))-(defn active-users-by-id+(mu/defn active-users-by-id"A map of ID to active User for `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select-fn->fn :id identity :model/User :id [:in user-ids] :is_active true))diff --git a/src/metabase/notification/schema.clj b/src/metabase/notification/schema.cljnew file mode 100644index 00000000000..c7f008e765f--- /dev/null+++ b/src/metabase/notification/schema.clj@@ -0,0 +1,121 @@+(ns metabase.notification.schema+ "Malli schemas for the notification module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::notification+ "A Notification as selected from the app DB: every column of `:notification`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:payload_type [:or :keyword :string]]+ [:active :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:internal_id {:optional true} [:maybe :string]]+ [:payload_id [:maybe ms/PositiveInt]]+ [:creator_id [:maybe ::lib.schema.id/user]]])++(mr/def ::notification.update+ "What an update (or insert) of a Notification accepts: every column of `:notification` except `id`, all optional."+ [:map {:closed true}+ [:payload_type {:optional true} [:maybe [:or :keyword :string]]]+ [:active {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:internal_id {:optional true} [:maybe :string]]+ [:payload_id {:optional true} [:maybe ms/PositiveInt]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]])++(mr/def ::notification-card+ "A NotificationCard as selected from the app DB: every column of `:notification_card`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:card_id [:maybe ::lib.schema.id/card]]+ [:send_once :boolean]+ [:send_condition [:or :keyword :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:disable_links [:maybe :boolean]]])++(mr/def ::notification-card.update+ "What an update (or insert) of a NotificationCard accepts: every column of `:notification_card` except `id`, all optional."+ [:map {:closed true}+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:send_once {:optional true} [:maybe :boolean]]+ [:send_condition {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:disable_links {:optional true} [:maybe :boolean]]])++(mr/def ::notification-handler+ "A NotificationHandler as selected from the app DB: every column of `:notification_handler`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:channel_type [:or :keyword :string]]+ [:notification_id ms/PositiveInt]+ [:channel_id [:maybe ms/PositiveInt]]+ [:template_id [:maybe ms/PositiveInt]]+ [:active :boolean]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::notification-handler.update+ "What an update (or insert) of a NotificationHandler accepts: every column of `:notification_handler` except `id`, all optional."+ [:map {:closed true}+ [:channel_type {:optional true} [:maybe [:or :keyword :string]]]+ [:notification_id {:optional true} [:maybe ms/PositiveInt]]+ [:channel_id {:optional true} [:maybe ms/PositiveInt]]+ [:template_id {:optional true} [:maybe ms/PositiveInt]]+ [:active {:optional true} [:maybe :boolean]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::notification-recipient.details+ "The `:details` column of a NotificationRecipient, decoded."+ :map)++(mr/def ::notification-recipient+ "A NotificationRecipient as selected from the app DB: every column of `:notification_recipient`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:notification_handler_id ms/PositiveInt]+ [:type [:or :keyword :string]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:permissions_group_id [:maybe ms/PositiveInt]]+ [:details [:maybe ::notification-recipient.details]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::notification-recipient.update+ "What an update (or insert) of a NotificationRecipient accepts: every column of `:notification_recipient` except `id`, all optional."+ [:map {:closed true}+ [:notification_handler_id {:optional true} [:maybe ms/PositiveInt]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:permissions_group_id {:optional true} [:maybe ms/PositiveInt]]+ [:details {:optional true} [:maybe ::notification-recipient.details]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::notification-subscription+ "A NotificationSubscription as selected from the app DB: every column of `:notification_subscription`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:notification_id ms/PositiveInt]+ [:type [:or :keyword :string]]+ [:event_name [:maybe [:or :keyword :string]]]+ [:created_at ms/TemporalInstant]+ [:cron_schedule [:maybe :string]]+ [:ui_display_type [:maybe [:or :keyword :string]]]])++(mr/def ::notification-subscription.update+ "What an update (or insert) of a NotificationSubscription accepts: every column of `:notification_subscription` except `id`, all optional."+ [:map {:closed true}+ [:notification_id {:optional true} [:maybe ms/PositiveInt]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:event_name {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:cron_schedule {:optional true} [:maybe :string]]+ [:ui_display_type {:optional true} [:maybe [:or :keyword :string]]]])diff --git a/src/metabase/oauth_server/db.clj b/src/metabase/oauth_server/db.cljindex 535165efdd9..29206ab6133 100644--- a/src/metabase/oauth_server/db.clj+++ b/src/metabase/oauth_server/db.clj@@ -2,6 +2,10 @@"Application database queries for the OAuth server module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.oauth-server.schema :as oauth-server.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(defn- client-event-where@@ -14,19 +18,23 @@(when (seq clauses)(into [:and] clauses))))-(defn client-event-count+(mu/defn client-event-count"The number of OAuthClientEvents matching `client-id` and/or `event-type` (all of them when both nil)."- [client-id event-type]+ [client-id :- [:maybe :string]+ event-type :- [:maybe :string]](let [where (client-event-where client-id event-type)](t2/query (cond-> {:select [[[:count :*] :count]]:from [[:oauth_client_event :e]]:left-join [[:oauth_client :c] [:= :e.oauth_client_id :c.id]]}where (assoc :where where)))))-(defn client-events+(mu/defn client-events"Up to `limit` OAuthClientEvents from `offset` matching `client-id` and/or `event-type` (all of them whenboth nil), newest first, with their client and deciding user."- [client-id event-type limit offset]+ [client-id :- [:maybe :string]+ event-type :- [:maybe :string]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](let [where (client-event-where client-id event-type)](t2/query (cond-> {:select [:e.id :e.oauth_client_id :e.user_id :e.event_type :e.created_at[:c.client_id :client_id]@@ -46,127 +54,133 @@:offset offset}where (assoc :where where)))))-(defn active-user-exists?+(mu/defn active-user-exists?"Whether an active User with `user-id` exists."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/exists? :model/User :id user-id :is_active true))-(defn oauth-client-exists?+(mu/defn oauth-client-exists?"Whether an OAuthClient with `client-id` exists."- [client-id]+ [client-id :- :string](t2/exists? :model/OAuthClient :client_id client-id))-(defn revoke-access-tokens-for-user!- "Revoke the unrevoked OAuthAccessTokens of the User with `user-id`."- [user-id]+(mu/defn revoke-access-tokens-for-user!+ "Revoke the unrevoked OAuthAccessTokens of the User with `user-id`, returning the number revoked."+ [user-id :- ::lib.schema.id/user](t2/update! :model/OAuthAccessToken {:user_id user-id, :revoked_at nil} {:revoked_at :%now}))-(defn revoke-refresh-tokens-for-user!- "Revoke the unrevoked OAuthRefreshTokens of the User with `user-id`."- [user-id]+(mu/defn revoke-refresh-tokens-for-user!+ "Revoke the unrevoked OAuthRefreshTokens of the User with `user-id`, returning the number revoked."+ [user-id :- ::lib.schema.id/user](t2/update! :model/OAuthRefreshToken {:user_id user-id, :revoked_at nil} {:revoked_at :%now}))-(defn delete-authorization-codes-for-user!- "Delete the OAuthAuthorizationCodes of the User with `user-id`."- [user-id]+(mu/defn delete-authorization-codes-for-user!+ "Delete the OAuthAuthorizationCodes of the User with `user-id`, returning the number deleted."+ [user-id :- ::lib.schema.id/user](t2/delete! :model/OAuthAuthorizationCode :user_id user-id))-(defn oauth-client-pk+(mu/defn oauth-client-pk"The primary key of the OAuthClient with `client-id`, or nil."- [client-id]+ [client-id :- :string](t2/select-one-pk :model/OAuthClient :client_id client-id))-(defn insert-client-event!- "Insert the OAuthClientEvent `row`."- [row]+(mu/defn insert-client-event!+ "Insert the OAuthClientEvent `row`, returning the number inserted."+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:oauth_client_id {:optional true} [:maybe ms/PositiveInt]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:event_type {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]]](t2/insert! :model/OAuthClientEvent row))-(defn oauth-client+(mu/defn oauth-client"The OAuthClient with `client-id`, or nil."- [client-id]+ [client-id :- :string](t2/select-one :model/OAuthClient :client_id client-id))-(defn insert-oauth-client!- "Insert the OAuthClient `row`."- [row]+(mu/defn insert-oauth-client!+ "Insert the OAuthClient `row`, returning the number inserted."+ [row :- ::oauth-server.schema/oauth-client.update](t2/insert! :model/OAuthClient row))-(defn update-oauth-client!- "Apply `row` to the OAuthClient with primary key `id`."- [id row]+(mu/defn update-oauth-client!+ "Apply `row` to the OAuthClient with primary key `id`, returning the number updated."+ [id :- ms/PositiveInt+ row :- ::oauth-server.schema/oauth-client.update](t2/update! :model/OAuthClient id row))-(defn insert-authorization-code!- "Insert the OAuthAuthorizationCode `row`."- [row]+(mu/defn insert-authorization-code!+ "Insert the OAuthAuthorizationCode `row`, returning the number inserted."+ [row :- ::oauth-server.schema/oauth-authorization-code.update](t2/insert! :model/OAuthAuthorizationCode row))-(defn authorization-code+(mu/defn authorization-code"The OAuthAuthorizationCode `code`, or nil."- [code]+ [code :- :string](t2/select-one :model/OAuthAuthorizationCode :code code))-(defn lock-authorization-code+(mu/defn lock-authorization-code"The OAuthAuthorizationCode `code` locked for update, or nil."- [code]+ [code :- :string](t2/select-one :model/OAuthAuthorizationCode :code code {:for :update}))-(defn delete-authorization-code!- "Delete the OAuthAuthorizationCode `code`."- [code]+(mu/defn delete-authorization-code!+ "Delete the OAuthAuthorizationCode `code`, returning the number deleted."+ [code :- :string](t2/delete! :model/OAuthAuthorizationCode :code code))-(defn insert-access-token!- "Insert the OAuthAccessToken `row`."- [row]+(mu/defn insert-access-token!+ "Insert the OAuthAccessToken `row`, returning the number inserted."+ [row :- ::oauth-server.schema/oauth-access-token.update](t2/insert! :model/OAuthAccessToken row))-(defn unrevoked-access-token+(mu/defn unrevoked-access-token"The unrevoked OAuthAccessToken `token`, or nil."- [token]+ [token :- :string](t2/select-one :model/OAuthAccessToken :token token :revoked_at nil))-(defn insert-refresh-token!- "Insert the OAuthRefreshToken `row`."- [row]+(mu/defn insert-refresh-token!+ "Insert the OAuthRefreshToken `row`, returning the number inserted."+ [row :- ::oauth-server.schema/oauth-refresh-token.update](t2/insert! :model/OAuthRefreshToken row))-(defn unrevoked-refresh-token+(mu/defn unrevoked-refresh-token"The unrevoked OAuthRefreshToken `token`, or nil."- [token]+ [token :- :string](t2/select-one :model/OAuthRefreshToken :token token :revoked_at nil))-(defn revoke-access-token!- "Revoke the OAuthAccessToken `token`."- [token]+(mu/defn revoke-access-token!+ "Revoke the OAuthAccessToken `token`, returning the number revoked."+ [token :- :string](t2/update! :model/OAuthAccessToken {:token token} {:revoked_at :%now}))-(defn revoke-refresh-token!- "Revoke the OAuthRefreshToken `token`."- [token]+(mu/defn revoke-refresh-token!+ "Revoke the OAuthRefreshToken `token`, returning the number revoked."+ [token :- :string](t2/update! :model/OAuthRefreshToken {:token token} {:revoked_at :%now}))-(defn delete-authorization-codes-expired-before!+(mu/defn delete-authorization-codes-expired-before!"Delete the OAuthAuthorizationCodes that expired before `now`, returning the number deleted."- [now]+ [now :- ms/PositiveInt](t2/delete! :model/OAuthAuthorizationCode :expiry [:< now]))-(defn delete-access-tokens-expired-before!+(mu/defn delete-access-tokens-expired-before!"Delete the OAuthAccessTokens that expired before `now`, returning the number deleted."- [now]+ [now :- ms/PositiveInt](t2/delete! :model/OAuthAccessToken :expiry [:< now]))-(defn delete-revoked-access-tokens!+(mu/defn delete-revoked-access-tokens!"Delete the revoked OAuthAccessTokens, returning the number deleted."[](t2/delete! :model/OAuthAccessToken :revoked_at [:not= nil]))-(defn delete-refresh-tokens-expired-before!+(mu/defn delete-refresh-tokens-expired-before!"Delete the OAuthRefreshTokens that expired before `now`, returning the number deleted."- [now]+ [now :- ms/PositiveInt](t2/delete! :model/OAuthRefreshToken :expiry [:< now]))-(defn delete-revoked-refresh-tokens!+(mu/defn delete-revoked-refresh-tokens!"Delete the revoked OAuthRefreshTokens, returning the number deleted."[](t2/delete! :model/OAuthRefreshToken :revoked_at [:not= nil]))diff --git a/src/metabase/oauth_server/schema.clj b/src/metabase/oauth_server/schema.cljnew file mode 100644index 00000000000..95869c72000--- /dev/null+++ b/src/metabase/oauth_server/schema.clj@@ -0,0 +1,147 @@+(ns metabase.oauth-server.schema+ "Malli schemas for the oauth-server module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::oauth-access-token+ "A OAuthAccessToken as selected from the app DB: every column of `:oauth_access_token`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:token :string]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:client_id :string]+ [:scope [:sequential :string]]+ [:expiry :int]+ [:resource [:maybe [:or :string [:sequential :string]]]]+ [:revoked_at [:maybe ms/TemporalInstant]]+ [:created_at ms/TemporalInstant]])++(mr/def ::oauth-access-token.update+ "What an update (or insert) of a OAuthAccessToken accepts: every column of `:oauth_access_token` except `id`, all optional."+ [:map {:closed true}+ [:token {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:client_id {:optional true} [:maybe :string]]+ [:scope {:optional true} [:maybe [:sequential :string]]]+ [:expiry {:optional true} [:maybe :int]]+ [:resource {:optional true} [:maybe [:or :string [:sequential :string]]]]+ [:revoked_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::oauth-authorization-code+ "A OAuthAuthorizationCode as selected from the app DB: every column of `:oauth_authorization_code`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:code :string]+ [:user_id ::lib.schema.id/user]+ [:client_id :string]+ [:redirect_uri :string]+ [:scope [:sequential :string]]+ [:nonce [:maybe :string]]+ [:expiry :int]+ [:code_challenge [:maybe :string]]+ [:code_challenge_method [:maybe [:or :keyword :string]]]+ [:resource [:maybe [:or :string [:sequential :string]]]]+ [:created_at ms/TemporalInstant]])++(mr/def ::oauth-authorization-code.update+ "What an update (or insert) of a OAuthAuthorizationCode accepts: every column of `:oauth_authorization_code` except `id`, all optional."+ [:map {:closed true}+ [:code {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:client_id {:optional true} [:maybe :string]]+ [:redirect_uri {:optional true} [:maybe :string]]+ [:scope {:optional true} [:maybe [:sequential :string]]]+ [:nonce {:optional true} [:maybe :string]]+ [:expiry {:optional true} [:maybe :int]]+ [:code_challenge {:optional true} [:maybe :string]]+ [:code_challenge_method {:optional true} [:maybe [:or :keyword :string]]]+ [:resource {:optional true} [:maybe [:or :string [:sequential :string]]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::oauth-client+ "A OAuthClient as selected from the app DB: every column of `:oauth_client`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:client_id :string]+ [:client_secret_hash [:maybe :string]]+ [:redirect_uris [:sequential :string]]+ [:grant_types [:sequential :string]]+ [:response_types [:sequential :string]]+ [:scopes [:sequential :string]]+ [:token_endpoint_auth_method [:maybe [:or :keyword :string]]]+ [:client_name [:maybe :string]]+ [:client_uri [:maybe :string]]+ [:logo_uri [:maybe :string]]+ [:contacts [:maybe [:sequential :string]]]+ [:registration_type [:or :keyword :string]]+ [:client_type [:or :keyword :string]]+ [:application_type [:maybe [:or :keyword :string]]]+ [:registration_access_token_hash [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::oauth-client.update+ "What an update (or insert) of a OAuthClient accepts: every column of `:oauth_client` except `id`, all optional."+ [:map {:closed true}+ [:client_id {:optional true} [:maybe :string]]+ [:client_secret_hash {:optional true} [:maybe :string]]+ [:redirect_uris {:optional true} [:maybe [:sequential :string]]]+ [:grant_types {:optional true} [:maybe [:sequential :string]]]+ [:response_types {:optional true} [:maybe [:sequential :string]]]+ [:scopes {:optional true} [:maybe [:sequential :string]]]+ [:token_endpoint_auth_method {:optional true} [:maybe [:or :keyword :string]]]+ [:client_name {:optional true} [:maybe :string]]+ [:client_uri {:optional true} [:maybe :string]]+ [:logo_uri {:optional true} [:maybe :string]]+ [:contacts {:optional true} [:maybe [:sequential :string]]]+ [:registration_type {:optional true} [:maybe [:or :keyword :string]]]+ [:client_type {:optional true} [:maybe [:or :keyword :string]]]+ [:application_type {:optional true} [:maybe [:or :keyword :string]]]+ [:registration_access_token_hash {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::oauth-client-event+ "A OAuthClientEvent as selected from the app DB: every column of `:oauth_client_event`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:oauth_client_id [:maybe ms/PositiveInt]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:event_type [:or :keyword :string]]+ [:created_at ms/TemporalInstant]])++(mr/def ::oauth-client-event.update+ "What an update (or insert) of a OAuthClientEvent accepts: every column of `:oauth_client_event` except `id`, all optional."+ [:map {:closed true}+ [:oauth_client_id {:optional true} [:maybe ms/PositiveInt]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:event_type {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::oauth-refresh-token+ "A OAuthRefreshToken as selected from the app DB: every column of `:oauth_refresh_token`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:token :string]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:client_id :string]+ [:scope [:sequential :string]]+ [:resource [:maybe [:or :string [:sequential :string]]]]+ [:expiry [:maybe :int]]+ [:revoked_at [:maybe ms/TemporalInstant]]+ [:created_at ms/TemporalInstant]])++(mr/def ::oauth-refresh-token.update+ "What an update (or insert) of a OAuthRefreshToken accepts: every column of `:oauth_refresh_token` except `id`, all optional."+ [:map {:closed true}+ [:token {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:client_id {:optional true} [:maybe :string]]+ [:scope {:optional true} [:maybe [:sequential :string]]]+ [:resource {:optional true} [:maybe [:or :string [:sequential :string]]]]+ [:expiry {:optional true} [:maybe :int]]+ [:revoked_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/osi/db.clj b/src/metabase/osi/db.cljindex 7671c8ae42a..05de1dccf1e 100644--- a/src/metabase/osi/db.clj+++ b/src/metabase/osi/db.clj@@ -3,40 +3,51 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as app-db]+ [metabase.osi.schema :as osi.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn ai-context+(mu/defn ai-context"The OsiAiContext of the entity with `entity-type` and `entity-local-id`, or nil."- [entity-type entity-local-id]+ [entity-type :- :string+ entity-local-id :- ms/PositiveInt](t2/select-one :model/OsiAiContext :entity_type entity-type :entity_local_id entity-local-id))-(defn ai-contexts-page+(mu/defn ai-contexts-page"Up to `limit` OsiAiContexts from `offset`, ordered by entity type and local id."- [limit offset]+ [limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](t2/select :model/OsiAiContext{:order-by [[:entity_type :asc] [:entity_local_id :asc]]:limit limit:offset offset}))-(defn ai-context-count+(mu/defn ai-context-count"The number of OsiAiContexts."[](t2/count :model/OsiAiContext))-(defn delete-ai-context!- "Delete the OsiAiContext of the entity with `entity-type` and `entity-local-id`."- [entity-type entity-local-id]+(mu/defn delete-ai-context!+ "Delete the OsiAiContext of the entity with `entity-type` and `entity-local-id`, returning the number deleted."+ [entity-type :- :string+ entity-local-id :- ms/PositiveInt](t2/delete! :model/OsiAiContext :entity_type entity-type :entity_local_id entity-local-id))-(defn update-ai-context!- "Apply `changes` to the OsiAiContext of the entity with `entity-type` and `entity-local-id`."- [entity-type entity-local-id changes]+(mu/defn update-ai-context!+ "Apply `changes` to the OsiAiContext of the entity with `entity-type` and `entity-local-id`, returning the+ number updated."+ [entity-type :- :string+ entity-local-id :- ms/PositiveInt+ changes :- ::osi.schema/osi-ai-context.update](t2/update! :model/OsiAiContext :entity_type entity-type :entity_local_id entity-local-id changes))-(defn upsert-ai-context!+(mu/defn upsert-ai-context!"Insert or replace the `:ai_context` of the OsiAiContext of the entity with `entity-type` and `entity-local-id`- with `ai-context`."- [entity-type entity-local-id ai-context]+ with `ai-context`, returning its `[entity_type entity_local_id]` compound key."+ [entity-type :- :string+ entity-local-id :- ms/PositiveInt+ ai-context :- ::osi.schema/osi-ai-context.ai-context](app-db/update-or-insert! :model/OsiAiContext{:entity_type entity-type:entity_local_id entity-local-id}diff --git a/src/metabase/osi/models/osi_ai_context.clj b/src/metabase/osi/models/osi_ai_context.cljindex 354718bf03f..67d1dba62cf 100644--- a/src/metabase/osi/models/osi_ai_context.clj+++ b/src/metabase/osi/models/osi_ai_context.clj@@ -171,5 +171,5 @@[_model-name ingested local];; The default keys updates on (first (primary-keys)), which for this compound key is just :entity_type —;; so it would address the wrong rows. Update by the full (entity_type, entity_local_id) key.- (osi.db/update-ai-context! (:entity_type local) (:entity_local_id local) ingested)+ (osi.db/update-ai-context! (:entity_type local) (:entity_local_id local) (update ingested :ai_context ->ai-context))(osi.db/ai-context (:entity_type local) (:entity_local_id local)))diff --git a/src/metabase/osi/schema.clj b/src/metabase/osi/schema.cljnew file mode 100644index 00000000000..1ee53e5e2ad--- /dev/null+++ b/src/metabase/osi/schema.clj@@ -0,0 +1,27 @@+(ns metabase.osi.schema+ "Malli schemas for the osi module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::osi-ai-context.ai-context+ "The `:ai_context` column of a OsiAiContext, decoded."+ :map)++(mr/def ::osi-ai-context+ "A OsiAiContext as selected from the app DB: every column of `:osi_ai_context`."+ [:map {:closed true}+ [:entity_type [:or :keyword :string]]+ [:entity_local_id ms/PositiveInt]+ [:ai_context ::osi-ai-context.ai-context]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::osi-ai-context.update+ "What an update (or insert) of a OsiAiContext accepts: every column of `:osi_ai_context` except `id`, all optional."+ [:map {:closed true}+ [:entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_local_id {:optional true} [:maybe ms/PositiveInt]]+ [:ai_context {:optional true} [:maybe ::osi-ai-context.ai-context]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/parameters/db.clj b/src/metabase/parameters/db.cljindex ddffb7538f6..7468c6aed60 100644--- a/src/metabase/parameters/db.clj+++ b/src/metabase/parameters/db.clj@@ -5,6 +5,9 @@[clojure.string :as str][honey.sql :as sql][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]))(defn- format-union@@ -30,10 +33,11 @@(mdb/isa :dest.semantic_type :type/Name)]:limit 1})-(defn remapped-field+(mu/defn remapped-field"The id and mapping-type of the Field that `field-id` remaps to via an explicit Field->Field Dimension, or —when `allow-implicit-uuid-remapping?` — an implicit FK->PK->Name or PK->Name mapping, or nil."- [field-id allow-implicit-uuid-remapping?]+ [field-id :- ::lib.schema.id/field+ allow-implicit-uuid-remapping? :- :boolean](t2/query-one{:select [[:mapping.id :id] [:mapping.mapping_type :mapping_type]]:from [[^:allow-subquery@@ -61,88 +65,94 @@:mapping]]:limit 1}))-(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))-(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn fields-fk-info+(mu/defn fields-fk-info"The id, FK target, and semantic type of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :fk_target_field_id :semantic_type] :id [:in field-ids]))-(defn field-fk-target-field-id+(mu/defn field-fk-target-field-id"The FK target Field id of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :fk_target_field_id :model/Field field-id))-(defn field-base-type+(mu/defn field-base-type"The base type of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :base_type :model/Field :id field-id))-(defn field-name+(mu/defn field-name"The name of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :name :model/Field :id field-id))-(defn full-field-values-exist?+(mu/defn full-field-values-exist?"Whether complete, non-remapped FieldValues of type `full` exist for the Field with `field-id`."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/exists? :model/FieldValues:field_id field-id, :values [:not= nil], :human_readable_values nil, :has_more_values false:type "full"))-(defn advanced-field-values-exist?+(mu/defn advanced-field-values-exist?"Whether complete, non-remapped FieldValues of type `advanced` with `hash-key` exist for the Field with `field-id`."- [field-id hash-key]+ [field-id :- ::lib.schema.id/field+ hash-key :- :string](t2/exists? :model/FieldValues:field_id field-id, :values [:not= nil], :human_readable_values nil, :has_more_values false:type "advanced", :hash_key hash-key))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn delete-field-values!- "Delete the FieldValues with `id`."- [id]+(mu/defn delete-field-values!+ "Delete the FieldValues with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/FieldValues :id id))-(defn advanced-field-values+(mu/defn advanced-field-values"The advanced FieldValues of the Field with `field-id` and `hash-key`, or nil."- [field-id hash-key]+ [field-id :- ::lib.schema.id/field+ hash-key :- :string](t2/select-one :model/FieldValues :field_id field-id, :type :advanced, :hash_key hash-key))-(defn find-or-insert-advanced-field-values!+(mu/defn find-or-insert-advanced-field-values!"The advanced FieldValues of the Field with `field-id` and `hash-key`, inserting one built by calling`insert-fn` if none exists yet."- [field-id hash-key insert-fn]+ [field-id :- ::lib.schema.id/field+ hash-key :- :string+ insert-fn :- fn?](mdb/select-or-insert! :model/FieldValues {:field_id field-id, :type :advanced, :hash_key hash-key} insert-fn))-(defn active-name-fields-for-tables+(mu/defn active-name-fields-for-tables"The `columns` of the active `:type/Name` Fields of the Tables with `table-ids`."- [columns table-ids]+ [columns :- [:sequential :keyword]+ table-ids :- [:sequential ::lib.schema.id/table]](t2/select (into [:model/Field] columns):table_id [:in table-ids]:semantic_type (mdb/isa :type/Name):active true))-(defn fields-with-columns+(mu/defn fields-with-columns"The `columns` of the Fields with `field-ids`."- [columns field-ids]+ [columns :- [:sequential :keyword]+ field-ids :- [:set ::lib.schema.id/field]](t2/select (into [:model/Field] columns) :id [:in field-ids]))-(defn fk-relationships-for-database+(mu/defn fk-relationships-for-database"Rows describing FK -> PK Field relationships (`:f1`/`:t1` FK Field/Table ids, `:f2`/`:t2` PK Field/Table ids)for active Fields in the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](mdb/query {:select [[:fk-field.id :f1][:fk-table.id :t1][:pk-field.id :f2]diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.cljindex f91a558ad0c..3c7f1d46af6 100644--- a/src/metabase/permissions/db.clj+++ b/src/metabase/permissions/db.clj@@ -3,12 +3,17 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods,and transactions."(:require+ [malli.util :as mut][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id][metabase.permissions.schema :as permissions.schema][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting]+ [metabase.users.schema :as users.schema][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]));;; --------------------------------------------- DataPermissions ---------------------------------------------@@ -50,11 +55,12 @@"0 for a database-level row, 1 for a table-level row."[:case [:= :p.table_id nil] [:inline 0] :else [:inline 1]])-(defn database-permission-rank-rows+(mu/defn database-permission-rank-rows"For each `(perm-type, db-id)` of the DataPermissions rows of the User with `user-id`'s groups (narrowed to`db-ids`, or every database when nil), the `[min max]` value rank pairs needed to reconstruct the `:database`,`:every-table`, and `:any-table` whole-database permission values."- [user-id db-ids]+ [user-id :- [:maybe ::lib.schema.id/user]+ db-ids :- [:maybe [:or [:set ::lib.schema.id/database] [:sequential ::lib.schema.id/database]]]](let [per-group (-> (perm-rows-query-base user-id db-ids)(assoc :select [:p.perm_type :p.db_id :p.group_id[[:min value-rank-case] :gmin]@@ -69,10 +75,11 @@:from [[per-group :i]]:group-by [:i.perm_type :i.db_id]})))-(defn schema-permission-rank-rows+(mu/defn schema-permission-rank-rows"For each `(perm-type, db-id, schema-name, table-level)` of the DataPermissions rows of the User with `user-id`'sgroups (narrowed to `db-ids`, or every database when nil), the `[min max]` value rank pair."- [user-id db-ids]+ [user-id :- ::lib.schema.id/user+ db-ids :- [:maybe [:sequential ::lib.schema.id/database]]](t2/query (assoc (perm-rows-query-base user-id db-ids):select [:p.perm_type :p.db_id :p.schema_name[table-level-case :table_level]@@ -80,11 +87,13 @@[[:max value-rank-case] :mx]]:group-by [:p.perm_type :p.db_id :p.schema_name table-level-case])))-(defn table-permission-rank-rows+(mu/defn table-permission-rank-rows"For each `(perm-type, db-id, table-id)` of the table-granular DataPermissions rows of the User with `user-id`'sgroups, the `[min max]` value rank pair. Scoped to `table-ids` when given (ignoring `db-ids`), otherwise to`db-ids` (or every database when both are nil)."- [user-id db-ids table-ids]+ [user-id :- [:maybe ::lib.schema.id/user]+ db-ids :- [:maybe [:set ::lib.schema.id/database]]+ table-ids :- [:maybe [:set ms/IntGreaterThanOrEqualToZero]]](t2/query (-> (perm-rows-query-base user-id (when-not (seq table-ids) db-ids))(assoc :select [:p.perm_type :p.db_id :p.table_id[[:min value-rank-case] :mn]@@ -93,11 +102,14 @@(update :where conj [:not= :p.table_id nil])(cond-> (seq table-ids) (update :where conj [:in :p.table_id table-ids])))))-(defn schema-permission-rank-pair+(mu/defn schema-permission-rank-pair"The `[min max]` value rank pair summarizing the DataPermissions rows of the User with `user-id`'s groups for`perm-type` on the Database with `database-id`, restricted to rows naming the schema `schema-name` or no schema atall (database-level rows), or nil when there are no matching rows."- [user-id perm-type database-id schema-name]+ [user-id :- ::lib.schema.id/user+ perm-type :- [:or :keyword :string]+ database-id :- ::lib.schema.id/database+ schema-name :- [:maybe :string]](let [per-group (-> (perm-rows-query-base user-id [database-id])(assoc :select [:p.group_id [[:max value-rank-case] :gmax]]:group-by [:p.group_id])@@ -108,9 +120,12 @@(first (t2/query {:select [[[:min :i.gmax] :mn] [[:max :i.gmax] :mx]]:from [[per-group :i]]}))))-(defn table-permission-values-for-groups+(mu/defn table-permission-values-for-groups"The set of `perm-type` values `group-ids` hold for the Table with `table-id` or for its whole Database."- [group-ids perm-type database-id table-id]+ [group-ids :- [:set ms/PositiveInt]+ perm-type :- [:or :keyword :string]+ database-id :- ::lib.schema.id/database+ table-id :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select-fn-set :value :model/DataPermissions{:select [[:p.perm_value :value]]:from [[:data_permissions :p]]@@ -122,10 +137,12 @@[:= :table_id table-id][:= :table_id nil]]]}))-(defn user-data-permissions+(mu/defn user-data-permissions"The permission type, group, value, database, and table of every DataPermissions row of the groups the User with`user-id` belongs to, optionally narrowed to `database-id` and/or `perm-type`."- [user-id database-id perm-type]+ [user-id :- ::lib.schema.id/user+ database-id :- [:maybe ::lib.schema.id/database]+ perm-type :- [:maybe [:or :keyword :string]]](t2/select :model/DataPermissions{:select [[:p.perm_type :perm-type][:p.group_id :group-id]@@ -140,15 +157,18 @@(when database-id [:= :db_id database-id])(when perm-type [:= :perm_type (u/qualified-name perm-type)])]}))-(defn data-permissions-for-groups-and-databases+(mu/defn data-permissions-for-groups-and-databases"The DataPermissions of `group-ids` on the Databases with `database-ids`."- [group-ids database-ids]+ [group-ids :- [:sequential ms/PositiveInt]+ database-ids :- [:or [:set ::lib.schema.id/database] [:sequential ::lib.schema.id/database]]](t2/select :model/DataPermissions :group_id [:in group-ids] :db_id [:in database-ids]))-(defn database-level-permission+(mu/defn database-level-permission"The database-level `perm-type` DataPermissions row of the group with `group-id` on the Database with`database-id`, or nil."- [perm-type group-id database-id]+ [perm-type :- [:or :keyword :string]+ group-id :- ms/PositiveInt+ database-id :- ::lib.schema.id/database](t2/select-one :model/DataPermissions{:where [:and[:= :perm_type perm-type]@@ -156,34 +176,41 @@[:= :db_id database-id][:= :table_id nil]]}))-(defn database-level-permissions+(mu/defn database-level-permissions"The database-level DataPermissions rows of `perm-types` for `group-ids` on the Database with `database-id`."- [database-id group-ids perm-types]+ [database-id :- ::lib.schema.id/database+ group-ids :- [:sequential ms/PositiveInt]+ perm-types :- [:sequential [:or :keyword :string]]](t2/select :model/DataPermissions{:where [:and [:= :db_id database-id] [:= :table_id nil][:in :group_id group-ids] [:in :perm_type perm-types]]}))-(defn distinct-table-level-permission-values+(mu/defn distinct-table-level-permission-values"The distinct group, permission type, schema, and value combinations of the table-level DataPermissions rows of`perm-types` for `group-ids` on the Database with `database-id`."- [database-id group-ids perm-types]+ [database-id :- ::lib.schema.id/database+ group-ids :- [:sequential ms/PositiveInt]+ perm-types :- [:sequential [:or :keyword :string]]](t2/select :model/DataPermissions{:select-distinct [:group_id :perm_type :schema_name :perm_value]:where [:and [:= :db_id database-id] [:not= :table_id nil][:in :group_id group-ids] [:in :perm_type perm-types]]}))-(defn distinct-database-permission-values-for-group+(mu/defn distinct-database-permission-values-for-group"The distinct database, permission type, and value combinations of the DataPermissions rows of the group with`group-id`."- [group-id]+ [group-id :- ms/PositiveInt](t2/select :model/DataPermissions{:select-distinct [:db_id :perm_type :perm_value]:where [:= :group_id group-id]}))-(defn other-table-permission-values+(mu/defn other-table-permission-values"The distinct `perm-type` values of the group with `group-id` on tables of the Database with `database-id` otherthan `table-ids`."- [group-id database-id perm-type table-ids]+ [group-id :- ms/PositiveInt+ database-id :- ::lib.schema.id/database+ perm-type :- [:or :keyword :string]+ table-ids :- [:set ms/IntGreaterThanOrEqualToZero]](t2/query {:select-distinct [:perm_value]:from [(t2/table-name :model/DataPermissions)]:where [:and@@ -193,19 +220,22 @@[:not= :table_id nil][:not [:in :table_id table-ids]]]}))-(defn table-permission-ids+(mu/defn table-permission-ids"The IDs of the `perm-type` DataPermissions rows of the group with `group-id` on the Tables with `table-ids`."- [perm-type group-id table-ids]+ [perm-type :- [:or :keyword :string]+ group-id :- ms/PositiveInt+ table-ids :- [:set ms/IntGreaterThanOrEqualToZero]](t2/select [:model/DataPermissions :id]{:where [:and[:= :perm_type perm-type][:= :group_id group-id][:in :table_id table-ids]]}))-(defn non-audit-permission-values-for-groups+(mu/defn non-audit-permission-values-for-groups"The distinct group, permission type, and value combinations of the DataPermissions rows of `group-ids` for`perm-types`, excluding rows on the audit Database."- [group-ids perm-types]+ [group-ids :- [:sequential ms/PositiveInt]+ perm-types :- [:sequential [:or :keyword :string]]](t2/query {:select-distinct [:group_id :perm_type :perm_value]:from [[(t2/table-name :model/DataPermissions)]]:where [:and@@ -217,21 +247,21 @@[:= :audit_db.is_audit true][:= :audit_db.id :data_permissions.db_id]]}]]]}))-(defn insert-data-permissions!+(mu/defn insert-data-permissions!"Insert the DataPermissions `rows`."- [rows]+ [rows :- [:sequential (mut/select-keys ::permissions.schema/data-permissions.update [:perm_type :group_id :perm_value :db_id :table_id :schema_name])]](t2/insert! :model/DataPermissions rows))-(defn delete-data-permissions!+(mu/defn delete-data-permissions!"Delete the DataPermissions with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/delete! :model/DataPermissions :id [:in ids]));;; ----------------------------------------------- Permissions ------------------------------------------------(defn group-ids-with-permission-objects+(mu/defn group-ids-with-permission-objects"The set of group IDs holding a Permissions row for one of `objects`."- [objects]+ [objects :- [:sequential :string]](t2/select-fn-set :group_id :model/Permissions {:where [:in :object objects]}))(defn- related-permission-objects-where@@ -242,28 +272,32 @@(map (fn [path-form] [:like :object (str path-form "%")]))also-under-paths)])-(defn related-permission-objects+(mu/defn related-permission-objects"The Permissions objects held by the group with `group-id` that are ancestors or descendants of `path` (alsochecking each of `also-under-paths`, e.g. a v2-equivalent path)."- [group-id path also-under-paths]+ [group-id :- ms/PositiveInt+ path :- :string+ also-under-paths :- [:sequential :string]](t2/select-fn-set :object :model/Permissions{:where (related-permission-objects-where group-id path also-under-paths)}))-(defn delete-related-permissions!+(mu/defn delete-related-permissions!"Delete the Permissions rows held by the group with `group-id` that are ancestors or descendants of `path` (alsochecking each of `also-under-paths`, e.g. a v2-equivalent path)."- [group-id path also-under-paths]+ [group-id :- ms/PositiveInt+ path :- :string+ also-under-paths :- [:sequential :string]](t2/delete! :model/Permissions{:where (related-permission-objects-where group-id path also-under-paths)}))-(defn insert-permissions!+(mu/defn insert-permissions!"Insert the Permissions `rows`."- [rows]+ [rows :- [:sequential (mut/select-keys ::permissions.schema/permissions.update [:group_id :object])]](t2/insert! :model/Permissions rows))-(defn permission-objects-for-user+(mu/defn permission-objects-for-user"The Permissions objects granted, via group membership, to the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](map :object (mdb/query {:select [:p.object]:from [[:permissions_group_membership :pgm]]:join [[:permissions_group :pg] [:= :pgm.group_id :pg.id]@@ -272,69 +306,70 @@;;; --------------------------------------------- PermissionsGroup ----------------------------------------------(defn magic-group+(mu/defn magic-group"The ID, name, and type of the magic PermissionsGroup of `magic-group-type`, or nil."- [magic-group-type]+ [magic-group-type :- :string](t2/select-one [:model/PermissionsGroup :id :name :magic_group_type] :magic_group_type magic-group-type))-(defn group-by-magic-type+(mu/defn group-by-magic-type"The PermissionsGroup of `magic-group-type`, or nil."- [magic-group-type]+ [magic-group-type :- :string](t2/select-one :model/PermissionsGroup :magic_group_type magic-group-type))-(defn group-id-by-magic-type+(mu/defn group-id-by-magic-type"The ID of the PermissionsGroup of `magic-group-type`, or nil."- [magic-group-type]+ [magic-group-type :- :string](t2/select-one-pk :model/PermissionsGroup :magic_group_type magic-group-type))-(defn group-exists-with-lower-name?+(mu/defn group-exists-with-lower-name?"Whether a PermissionsGroup whose lower-cased name is `lower-name` exists."- [lower-name]+ [lower-name :- :string](t2/exists? :model/PermissionsGroup :%lower.name lower-name))-(defn groups-except-magic-type+(mu/defn groups-except-magic-type"The PermissionsGroups other than the magic group of `magic-group-type`."- [magic-group-type]+ [magic-group-type :- :string](t2/select :model/PermissionsGroup :magic_group_type [:not= magic-group-type]))-(defn non-magic-groups+(mu/defn non-magic-groups"The PermissionsGroups that are not magic groups."[](t2/select :model/PermissionsGroup {:where [:= :magic_group_type nil]}))-(defn tenant-group?+(mu/defn tenant-group?"Whether the PermissionsGroup with `group-id` is a tenant group."- [group-id]+ [group-id :- ms/PositiveInt](t2/select-one-fn :is_tenant_group :model/PermissionsGroup :id group-id))-(defn tenant-group-ids+(mu/defn tenant-group-ids"The IDs of every tenant PermissionsGroup."[](t2/select-pks-set :model/PermissionsGroup :is_tenant_group true))-(defn group-tenant-flags+(mu/defn group-tenant-flags"A map of group ID to `:is_tenant_group` for `group-ids`."- [group-ids]+ [group-ids :- [:set ms/PositiveInt]](t2/select-pk->fn :is_tenant_group [:model/PermissionsGroup :id :is_tenant_group] :id [:in group-ids]))-(defn group-names-like+(mu/defn group-names-like"The set of PermissionsGroup names matching the SQL `pattern`."- [pattern]+ [pattern :- :string](t2/select-fn-set :name :model/PermissionsGroup :name [:like pattern]))-(defn group-members+(mu/defn group-members"The active Users in the PermissionsGroups with `group-ids`, with the optional extra `group-manager-column`."- [group-ids group-manager-column]- (t2/select :model/User {:select [:u.id- [:u.id :user_id]- :u.first_name- :u.last_name- :u.email- :u.is_superuser- :u.type- :pgm.group_id- [:pgm.id :membership_id]- group-manager-column]+ [group-ids :- [:sequential ms/PositiveInt]+ group-manager-column :- [:maybe vector?]]+ (t2/select :model/User {:select (cond-> [:u.id+ [:u.id :user_id]+ :u.first_name+ :u.last_name+ :u.email+ :u.is_superuser+ :u.type+ :pgm.group_id+ [:pgm.id :membership_id]]+ group-manager-column (conj group-manager-column)):from [[:core_user :u]]:left-join [[:permissions_group_membership :pgm] [:= :u.id :pgm.user_id]]:where [:and@@ -343,17 +378,18 @@:order-by [[[:lower :u.first_name] :asc][[:lower :u.last_name] :asc]]}))-(defn insert-group!+(mu/defn insert-group!"Insert `group` and return the new instance."- [group]+ [group :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :magic_group_type :is_tenant_group])](t2/insert-returning-instance! :model/PermissionsGroup group))-(defn update-group!+(mu/defn update-group!"Apply `changes` to the PermissionsGroup with `group-id`."- [group-id changes]+ [group-id :- ms/PositiveInt+ changes :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :magic_group_type :is_tenant_group])](t2/update! :model/PermissionsGroup group-id changes))-(defn group-member-counts+(mu/defn group-member-counts"A map of PermissionsGroup ID to number of active members in the group. Groups with no active members have noentry."[]@@ -389,21 +425,22 @@:g.is_tenant_group[:not= :u.tenant_id nil]]]))]}]})-(defn insert-group-memberships-from-mapping!+(mu/defn insert-group-memberships-from-mapping!"Insert a PermissionsGroupMembership (with `is_group_manager`) for each `[user-id group-id]` pair in`user-id-group-id->is-group-manager?`, matching Users to Groups on tenant status; returns the number of rowsinserted."- [user-id-group-id->is-group-manager?]+ [user-id-group-id->is-group-manager? :- [:map-of [:tuple ms/PositiveInt ms/PositiveInt] :boolean]](t2/query-one (insert-group-memberships-from-mapping-query user-id-group-id->is-group-manager?)))-(defn group-membership-count+(mu/defn group-membership-count"The number of memberships of the PermissionsGroup with `group-id`."- [group-id]+ [group-id :- ms/PositiveInt](t2/count :model/PermissionsGroupMembership :group_id group-id))-(defn other-active-member-count+(mu/defn other-active-member-count"The number of active Users other than `user-id` in the PermissionsGroup with `group-id`."- [group-id user-id]+ [group-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/count :model/PermissionsGroupMembership{:join [[:core_user :user] [:= :user.id :user_id]]:where [:and@@ -411,88 +448,111 @@[:= :user.is_active true][:not= :user.id user-id]]}))-(defn memberships-for-user+(mu/defn memberships-for-user"The PermissionsGroupMemberships of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select :model/PermissionsGroupMembership :user_id user-id))-(defn memberships-for-user-in-groups+(mu/defn memberships-for-user-in-groups"The PermissionsGroupMemberships of the User with `user-id` in the groups with `group-ids`."- [user-id group-ids]+ [user-id :- ::lib.schema.id/user+ group-ids :- [:sequential ms/PositiveInt]](t2/select :model/PermissionsGroupMembership :user_id user-id :group_id [:in group-ids]))-(defn memberships-for-group+(mu/defn memberships-for-group"The PermissionsGroupMemberships of the PermissionsGroup with `group-id`."- [group-id]+ [group-id :- ms/PositiveInt](t2/select :model/PermissionsGroupMembership :group_id group-id))-(defn delete-memberships-for-user!+(mu/defn delete-memberships-for-user!"Delete the PermissionsGroupMemberships of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/PermissionsGroupMembership :user_id user-id))-(defn delete-memberships-for-user-in-groups!+(mu/defn delete-memberships-for-user-in-groups!"Delete the PermissionsGroupMemberships of the User with `user-id` in the groups with `group-ids`."- [user-id group-ids]+ [user-id :- ::lib.schema.id/user+ group-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/PermissionsGroupMembership :user_id user-id :group_id [:in group-ids]))-(defn delete-memberships-for-group!+(mu/defn delete-memberships-for-group!"Delete the PermissionsGroupMemberships of the PermissionsGroup with `group-id`."- [group-id]+ [group-id :- ms/PositiveInt](t2/delete! :model/PermissionsGroupMembership :group_id group-id));;; ------------------------------------------------ Revisions -------------------------------------------------(defn latest-permissions-revision-id+(mu/defn latest-permissions-revision-id"The highest ID of any PermissionsRevision, or nil."[](:id (t2/select-one [:model/PermissionsRevision [:%max.id :id]])))-(defn latest-collection-permission-graph-revision-id+(mu/defn latest-collection-permission-graph-revision-id"The highest ID of any CollectionPermissionGraphRevision, or nil."[](:id (t2/select-one [:model/CollectionPermissionGraphRevision [:%max.id :id]])))-(defn latest-application-permissions-revision-id+(mu/defn latest-application-permissions-revision-id"The highest ID of any ApplicationPermissionsRevision, or nil."[](:id (t2/select-one [:model/ApplicationPermissionsRevision [:%max.id :id]])))-(defn insert-collection-permission-graph-revision!+(mu/defn insert-collection-permission-graph-revision!"Insert `revision` into CollectionPermissionGraphRevision."- [revision]+ [revision :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:before {:optional true} [:maybe [:or :map :string]]]+ [:after {:optional true} [:maybe [:or :map :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:remark {:optional true} [:maybe :string]]]](t2/insert! :model/CollectionPermissionGraphRevision revision))-(defn insert-collection-permission-graph-revision-returning-instance!+(mu/defn insert-collection-permission-graph-revision-returning-instance!"Insert `revision` into CollectionPermissionGraphRevision and return the new instance."- [revision]+ [revision :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:before {:optional true} [:maybe [:or :map :string]]]+ [:after {:optional true} [:maybe [:or :map :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:remark {:optional true} [:maybe :string]]]](first (t2/insert-returning-instances! :model/CollectionPermissionGraphRevision revision)))-(defn insert-permissions-revision-returning-instance!+(mu/defn insert-permissions-revision-returning-instance!"Insert `revision` into PermissionsRevision and return the new instance."- [revision]+ [revision :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:before {:optional true} [:maybe [:or :map :string]]]+ [:after {:optional true} [:maybe [:or :map :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:remark {:optional true} [:maybe :string]]]](first (t2/insert-returning-instances! :model/PermissionsRevision revision)))-(defn insert-application-permissions-revision-returning-instance!+(mu/defn insert-application-permissions-revision-returning-instance!"Insert `revision` into ApplicationPermissionsRevision and return the new instance."- [revision]+ [revision :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:before {:optional true} [:maybe [:or :map :string]]]+ [:after {:optional true} [:maybe [:or :map :string]]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:remark {:optional true} [:maybe :string]]]](first (t2/insert-returning-instances! :model/ApplicationPermissionsRevision revision)))-(defn update-collection-graph-revision!+(mu/defn update-collection-graph-revision!"Apply `changes` to the CollectionPermissionGraphRevision with `revision-id`."- [revision-id changes]+ [revision-id :- ms/PositiveInt+ changes :- (mut/select-keys ::permissions.schema/collection-permission-graph-revision.update [:before :after])](t2/update! :model/CollectionPermissionGraphRevision revision-id changes));;; ----------------------------------------------- Collections ------------------------------------------------(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id collection-id))-(defn personal-or-descendant-collection-ids+(mu/defn personal-or-descendant-collection-ids"The IDs among `collection-ids` that are personal Collections, or descendants of one."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select-pks-set :model/Collection{:where [:and[:in :id collection-ids]@@ -505,24 +565,27 @@[:like :collection.location[:concat "/" :pc.id "/%"]]]}]]]}))-(defn collection-ids-in-other-namespace+(mu/defn collection-ids-in-other-namespace"The IDs among `collection-ids` that do not belong to `namespace` (nil meaning the default namespace)."- [collection-ids namespace]+ [collection-ids :- [:set ::lib.schema.id/collection]+ namespace :- [:maybe [:or :keyword :string]]](t2/select-pks-set :model/Collection{:where [:and [:in :id collection-ids](cond->> [[:not= :namespace (some-> namespace name)]](nil? namespace) (into [:and [:not= :namespace "analytics"]])(some? namespace) (into [:or [:= :namespace nil]]))]}))-(defn library-collection-ids+(mu/defn library-collection-ids"The IDs of the library Collections."[](t2/select-pks-set :model/Collection :type [:in ["library" "library-data" "library-metrics"]]))-(defn namespace-clause+(mu/defn namespace-clause"Honey SQL clause to filter `namespace-keyword` by `namespace-val`, also matching the audit-app and tenantnamespaces when applicable."- [namespace-keyword namespace-val & [include-tenant-namespaces?]]+ [namespace-keyword :- :keyword+ namespace-val :- [:maybe [:or :keyword :string]]+ & [include-tenant-namespaces?] :- [:* [:maybe :boolean]]][:or[:= namespace-keyword namespace-val](when (and (nil? namespace-val)@@ -533,12 +596,17 @@(when (and include-tenant-namespaces? (nil? namespace-val) (setting/get :use-tenants))[:= namespace-keyword "tenant-specific"])])-(defn collection-graph-rows+(mu/defn collection-graph-rows"Reducible group/collection/writable/readable rows of the collection permissions graph for `collection-namespace`,restricted to `ids-without-root` (or every Collection when empty) and `group-ids` (or every group when empty).`include-root?` controls whether the root-collection rows are included; `admin-group-id` is the id of theAdministrators group, which implicitly has write access to every Collection."- [collection-namespace include-root? root-object ids-without-root group-ids admin-group-id]+ [collection-namespace :- [:maybe [:or :keyword :string]]+ include-root? :- :boolean+ root-object :- :string+ ids-without-root :- [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]+ group-ids :- [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]+ admin-group-id :- [:maybe ms/PositiveInt]](t2/reducible-query{:with [[:eligible_collections^:allow-subquery@@ -615,47 +683,49 @@;;; ------------------------------------------------- Users --------------------------------------------------(defn user-superuser?+(mu/defn user-superuser?"Whether the User with `user-id` is a superuser."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :is_superuser :model/User :id user-id))-(defn user-data-analyst?+(mu/defn user-data-analyst?"Whether the User with `user-id` is a data analyst."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :is_data_analyst :model/User :id user-id))-(defn user-tenant-ids+(mu/defn user-tenant-ids"A map of User ID to `:tenant_id` for `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn :tenant_id [:model/User :id :tenant_id] :id [:in user-ids]))-(defn earliest-user-join-date+(mu/defn earliest-user-join-date"The earliest `date_joined` of any User, or nil."[](:min (t2/select-one [:model/User [:%min.date_joined :min]])))-(defn update-user!+(mu/defn update-user!"Apply `changes` to the User with `user-id`."- [user-id changes]+ [user-id :- ::lib.schema.id/user+ changes :- (mut/select-keys ::users.schema/user.update [:is_superuser :is_data_analyst])](t2/update! :model/User user-id changes))-(defn update-users!+(mu/defn update-users!"Apply `changes` to the Users with `user-ids`."- [user-ids changes]+ [user-ids :- [:sequential ::lib.schema.id/user]+ changes :- (mut/select-keys ::users.schema/user.update [:is_superuser :is_data_analyst])](t2/update! :model/User :id [:in user-ids] changes))-(defn clear-data-analyst-flags!+(mu/defn clear-data-analyst-flags!"Unset `is_data_analyst` on every User that has it set."[](t2/update! :model/User {:is_data_analyst true} {:is_data_analyst false}))-(defn deactivate-active-tenant-users!+(mu/defn deactivate-active-tenant-users!"Deactivate every active tenant User, marking them as deactivated with their tenant."[](t2/update! :model/User :tenant_id [:not= nil] :is_active true {:is_active false :deactivated_with_tenant true}))-(defn deactivate-all-tenants!+(mu/defn deactivate-all-tenants!"Mark every tenant row inactive."[](t2/query {:update :tenant@@ -663,50 +733,52 @@;;; --------------------------------------------- Databases and Tables ----------------------------------------------(defn non-destination-database-ids+(mu/defn non-destination-database-ids"The IDs of the Databases that are not routing destinations."[](t2/select-pks-vec :model/Database :router_database_id nil))-(defn destination-database?+(mu/defn destination-database?"Whether the Database with `database-id` is a routing destination."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id :router_database_id [:not= nil]))-(defn table-location+(mu/defn table-location"The ID, Database ID, and schema of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/Table :id :db_id :schema] :id table-id))-(defn table-database-id+(mu/defn table-database-id"The Database ID of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table table-id))-(defn table-database-ids+(mu/defn table-database-ids"The ID and Database ID of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Table :id :db_id] :id [:in table-ids]))-(defn active-table-locations-for-database+(mu/defn active-table-locations-for-database"The ID, Database ID, and schema of the active Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select [:model/Table :id :db_id :schema] :db_id database-id :active true))-(defn table-ids-and-schemas-excluding+(mu/defn table-ids-and-schemas-excluding"The ID and schema of the Tables of the Database with `database-id` other than `excluded-table-ids`."- [database-id excluded-table-ids]+ [database-id :- ::lib.schema.id/database+ excluded-table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :schema]{:where [:and[:= :db_id database-id][:not [:in :id excluded-table-ids]]]}))-(defn field-visibility-info+(mu/defn field-visibility-info"The ID, visibility type, and Table ID of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :visibility_type :table_id] :id [:in field-ids]))-(defn instance-by-id+(mu/defn instance-by-id"The instance of `model` with `id`, or nil."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one model :id id))diff --git a/src/metabase/permissions/schema.clj b/src/metabase/permissions/schema.cljindex 4cf7280d201..a70bd78f047 100644--- a/src/metabase/permissions/schema.clj+++ b/src/metabase/permissions/schema.clj@@ -1,6 +1,8 @@(ns metabase.permissions.schema(:require- [metabase.util.malli.registry :as mr]))+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]));;; ---------------------------------------- Permission definitions ---------------------------------------------------@@ -32,3 +34,157 @@"Malli spec for a keyword that matches any value in [[Permissions]]."(into [:enum {:error/message "Invalid permission value"}](distinct (mapcat :values (vals data-permissions)))))++(mr/def ::application-permissions-revision.before+ "The `:before` column of a ApplicationPermissionsRevision, decoded."+ :map)++(mr/def ::application-permissions-revision.after+ "The `:after` column of a ApplicationPermissionsRevision, decoded."+ :map)++(mr/def ::application-permissions-revision+ "A ApplicationPermissionsRevision as selected from the app DB: every column of `:application_permissions_revision`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:before [:maybe ::application-permissions-revision.before]]+ [:after [:maybe ::application-permissions-revision.after]]+ [:user_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:remark [:maybe :string]]])++(mr/def ::application-permissions-revision.update+ "What an update (or insert) of a ApplicationPermissionsRevision accepts: every column of `:application_permissions_revision` except `id`, all optional."+ [:map {:closed true}+ [:before {:optional true} [:maybe ::application-permissions-revision.before]]+ [:after {:optional true} [:maybe ::application-permissions-revision.after]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:remark {:optional true} [:maybe :string]]])++(mr/def ::collection-permission-graph-revision.before+ "The `:before` column of a CollectionPermissionGraphRevision, decoded."+ :map)++(mr/def ::collection-permission-graph-revision.after+ "The `:after` column of a CollectionPermissionGraphRevision, decoded."+ :map)++(mr/def ::collection-permission-graph-revision+ "A CollectionPermissionGraphRevision as selected from the app DB: every column of `:collection_permission_graph_revision`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:before [:maybe ::collection-permission-graph-revision.before]]+ [:after [:maybe ::collection-permission-graph-revision.after]]+ [:user_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:remark [:maybe :string]]])++(mr/def ::collection-permission-graph-revision.update+ "What an update (or insert) of a CollectionPermissionGraphRevision accepts: every column of `:collection_permission_graph_revision` except `id`, all optional."+ [:map {:closed true}+ [:before {:optional true} [:maybe ::collection-permission-graph-revision.before]]+ [:after {:optional true} [:maybe ::collection-permission-graph-revision.after]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:remark {:optional true} [:maybe :string]]])++(mr/def ::data-permissions+ "A DataPermissions as selected from the app DB: every column of `:data_permissions`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:group_id ms/PositiveInt]+ [:perm_type [:or :keyword :string]]+ [:db_id ::lib.schema.id/database]+ [:schema_name [:maybe :string]]+ [:table_id [:maybe ::lib.schema.id/table]]+ [:perm_value [:or :keyword :string]]+ [:unique_perms_helper {:optional true} [:maybe :int]]])++(mr/def ::data-permissions.update+ "What an update (or insert) of a DataPermissions accepts: every column of `:data_permissions` except `id`, all optional."+ [:map {:closed true}+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:perm_type {:optional true} [:maybe [:or :keyword :string]]]+ [:db_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:schema_name {:optional true} [:maybe :string]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:perm_value {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::permissions+ "A Permissions as selected from the app DB: every column of `:permissions`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:object :string]+ [:group_id ms/PositiveInt]+ [:perm_value [:maybe [:or :keyword :string]]]+ [:perm_type [:maybe [:or :keyword :string]]]+ [:collection_id [:maybe ::lib.schema.id/collection]]])++(mr/def ::permissions.update+ "What an update (or insert) of a Permissions accepts: every column of `:permissions` except `id`, all optional."+ [:map {:closed true}+ [:object {:optional true} [:maybe :string]]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:perm_value {:optional true} [:maybe [:or :keyword :string]]]+ [:perm_type {:optional true} [:maybe [:or :keyword :string]]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]])++(mr/def ::permissions-group+ "A PermissionsGroup as selected from the app DB: every column of `:permissions_group`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:entity_id :string]+ [:magic_group_type [:maybe [:or :keyword :string]]]+ [:is_tenant_group :boolean]])++(mr/def ::permissions-group.update+ "What an update (or insert) of a PermissionsGroup accepts: every column of `:permissions_group` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:entity_id {:optional true} [:maybe :string]]+ [:magic_group_type {:optional true} [:maybe [:or :keyword :string]]]+ [:is_tenant_group {:optional true} [:maybe :boolean]]])++(mr/def ::permissions-group-membership+ "A PermissionsGroupMembership as selected from the app DB: every column of `:permissions_group_membership`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:group_id ms/PositiveInt]+ [:is_group_manager :boolean]])++(mr/def ::permissions-group-membership.update+ "What an update (or insert) of a PermissionsGroupMembership accepts: every column of `:permissions_group_membership` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:group_id {:optional true} [:maybe ms/PositiveInt]]+ [:is_group_manager {:optional true} [:maybe :boolean]]])++(mr/def ::permissions-revision.before+ "The `:before` column of a PermissionsRevision, decoded."+ :map)++(mr/def ::permissions-revision.after+ "The `:after` column of a PermissionsRevision, decoded."+ :map)++(mr/def ::permissions-revision+ "A PermissionsRevision as selected from the app DB: every column of `:permissions_revision`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:before [:maybe ::permissions-revision.before]]+ [:after [:maybe ::permissions-revision.after]]+ [:user_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:remark [:maybe :string]]])++(mr/def ::permissions-revision.update+ "What an update (or insert) of a PermissionsRevision accepts: every column of `:permissions_revision` except `id`, all optional."+ [:map {:closed true}+ [:before {:optional true} [:maybe ::permissions-revision.before]]+ [:after {:optional true} [:maybe ::permissions-revision.after]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:remark {:optional true} [:maybe :string]]])diff --git a/src/metabase/permissions_rest/db.clj b/src/metabase/permissions_rest/db.cljindex 7ea1d9e39f1..8d9bf957572 100644--- a/src/metabase/permissions_rest/db.clj+++ b/src/metabase/permissions_rest/db.clj@@ -2,7 +2,10 @@"Application database queries for the permissions REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(defn- managed-groups-clause@@ -14,14 +17,21 @@[:= :user_id manager-user-id][:= :is_group_manager true]]}]))-(defn permissions-groups+(mu/defn permissions-groups"Up to `limit` PermissionsGroups starting at `offset` (both optional), ordered by lower-cased name.`tenancy` (\"external\"/\"internal\"/nil) narrows to tenant/non-tenant groups (nil returns both); when`tenancy` is \"external\" and `tenants-enabled?` is false, no groups are returned. `manager-user-id`, when given,restricts to the groups that User manages. `advanced-permissions-enabled?` false excludes the data-analyst magicgroup. `tenants-enabled?` false also excludes tenant groups outright, independent of `tenancy`."- [limit offset {:keys [tenancy manager-user-id tenants-enabled? advanced-permissions-enabled?]}]+ [limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]+ {:keys [tenancy manager-user-id tenants-enabled? advanced-permissions-enabled?]}+ :- [:map {:closed true}+ [:tenancy {:optional true} [:maybe [:enum "external" "internal"]]]+ [:manager-user-id {:optional true} [:maybe ::lib.schema.id/user]]+ [:tenants-enabled? {:optional true} :boolean]+ [:advanced-permissions-enabled? {:optional true} :boolean]]](let [base-where [:and(managed-groups-clause :id manager-user-id)(when-not tenants-enabled? [:not :is_tenant_group])@@ -38,36 +48,42 @@limit (assoc :limit limit)offset (assoc :offset offset)))))-(defn permissions-group+(mu/defn permissions-group"The PermissionsGroup with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/PermissionsGroup :id id))-(defn permissions-group-exists?+(mu/defn permissions-group-exists?"Whether a PermissionsGroup with `id` exists."- [id]+ [id :- ms/PositiveInt](t2/exists? :model/PermissionsGroup :id id))-(defn insert-permissions-group!+(mu/defn insert-permissions-group!"Insert a PermissionsGroup and return the inserted instance."- [group-name tenant-group?]+ [group-name :- :string+ tenant-group? :- :boolean](t2/insert-returning-instance! :model/PermissionsGroup :name group-name :is_tenant_group tenant-group?))-(defn rename-permissions-group!- "Set the name of the PermissionsGroup with `id`."- [id group-name]+(mu/defn rename-permissions-group!+ "Set the name of the PermissionsGroup with `id`, returning the number updated."+ [id :- ms/PositiveInt+ group-name :- :string](t2/update! :model/PermissionsGroup id {:name group-name}))-(defn delete-permissions-group!- "Delete the PermissionsGroup with `id`."- [id]+(mu/defn delete-permissions-group!+ "Delete the PermissionsGroup with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/PermissionsGroup :id id))-(defn group-memberships+(mu/defn group-memberships"The membership id, group id, user id, and group manager flag of every PermissionsGroupMembership, optionallyrestricted to the groups `manager-user-id` manages, excluding `excluded-group-id`, and excluding tenant groupswhen `exclude-tenant-groups?`."- [{:keys [manager-user-id excluded-group-id exclude-tenant-groups?]}]+ [{:keys [manager-user-id excluded-group-id exclude-tenant-groups?]}+ :- [:map {:closed true}+ [:manager-user-id {:optional true} [:maybe ::lib.schema.id/user]]+ [:excluded-group-id {:optional true} [:maybe ms/PositiveInt]]+ [:exclude-tenant-groups? {:optional true} :boolean]]](t2/select [:model/PermissionsGroupMembership [:id :membership_id] :group_id :user_id :is_group_manager]{:where (into [:and](keep identity)@@ -78,34 +94,41 @@:from [:permissions_group]:where [:= :is_tenant_group true]}])])}))-(defn non-admin-user-exists?+(mu/defn non-admin-user-exists?"Whether the User with `user-id` exists and is not a superuser."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/exists? :model/User :id user-id :is_superuser false))-(defn group-membership+(mu/defn group-membership"The PermissionsGroupMembership with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/PermissionsGroupMembership :id id))-(defn set-group-membership-manager!- "Set the group manager flag of the PermissionsGroupMembership with `id`."- [id group-manager?]+(mu/defn set-group-membership-manager!+ "Set the group manager flag of the PermissionsGroupMembership with `id`, returning the number updated."+ [id :- ms/PositiveInt+ group-manager? :- :boolean](t2/update! :model/PermissionsGroupMembership id {:is_group_manager group-manager?}))-(defn non-destination-database-ids+(mu/defn non-destination-database-ids"The ids of the Databases that are not routing destinations, excluding `excluded-database-id` (nil for noexclusion)."- [excluded-database-id]+ [excluded-database-id :- [:maybe ::lib.schema.id/database]](t2/select-pks-vec :model/Database {:where [:and(when excluded-database-id [:not= :id excluded-database-id])[:= :router_database_id nil]]}))-(defn data-permissions-reducible+(mu/defn data-permissions-reducible"A reducible of the type, group id, value, database id, schema, and table id of the DataPermissions rows ofnon-destination Databases, optionally narrowed to `perm-type`, `db-id`, `group-id`, `group-ids`, and excluding`excluded-database-id` (nil for no restriction), ordered by group and database."- [{:keys [perm-type db-id group-id group-ids excluded-database-id]}]+ [{:keys [perm-type db-id group-id group-ids excluded-database-id]}+ :- [:map {:closed true}+ [:perm-type {:optional true} [:maybe [:or :keyword :string]]]+ [:db-id {:optional true} [:maybe ::lib.schema.id/database]]+ [:group-id {:optional true} [:maybe ms/PositiveInt]]+ [:group-ids {:optional true} [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]]+ [:excluded-database-id {:optional true} [:maybe ::lib.schema.id/database]]]](t2/reducible-query{:select [[:perm_type :type][:group_id :group-id]@@ -128,7 +151,7 @@[:= :router_db.id :db_id]]}]]):order-by [:group_id :db_id]}))-(defn tables-for-databases+(mu/defn tables-for-databases"The id, Database id, and schema of the Tables of the Databases with `database-ids`."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select [:model/Table :id :db_id :schema] :db_id [:in database-ids]))diff --git a/src/metabase/premium_features/db.clj b/src/metabase/premium_features/db.cljindex 9376de29290..a9375e3c0f2 100644--- a/src/metabase/premium_features/db.clj+++ b/src/metabase/premium_features/db.clj@@ -2,34 +2,40 @@"Application database queries for the premium features module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn active-personal-user-count+(mu/defn active-personal-user-count"The number of active personal Users."[];; Because this count is needed *during* token checks, it uses `t2/table-name` to avoid the `after-select` method on;; users, which calls an EE method that needs ... a token check :|(t2/count (t2/table-name :model/User) :is_active true, :type "personal"))-(defn token-status-cache+(mu/defn token-status-cache"The `:token_status_hash` and `:updated_at` cached for `token-hash`, or nil."- [token-hash]+ [token-hash :- :string](t2/select-one [:model/PremiumFeaturesCache :token_status_hash :updated_at] :token_hash token-hash))-(defn update-token-status-cache!+(mu/defn update-token-status-cache!"Set the cached status hash for `token-hash`, returning the number of rows updated."- [token-hash token-status-hash updated-at]+ [token-hash :- :string+ token-status-hash :- :string+ updated-at :- ms/TemporalInstant](t2/update! :model/PremiumFeaturesCache :token_hash token-hash{:token_status_hash token-status-hash, :updated_at updated-at}))-(defn insert-token-status-cache!+(mu/defn insert-token-status-cache!"Insert a cached status hash for `token-hash`."- [token-hash token-status-hash updated-at]+ [token-hash :- :string+ token-status-hash :- :string+ updated-at :- ms/TemporalInstant](t2/insert! :model/PremiumFeaturesCache {:token_hash token-hash:token_status_hash token-status-hash:updated_at updated-at}))-(defn delete-token-status-cache!+(mu/defn delete-token-status-cache!"Delete every cached token status."[](t2/delete! :model/PremiumFeaturesCache))diff --git a/src/metabase/premium_features/schema.clj b/src/metabase/premium_features/schema.cljnew file mode 100644index 00000000000..67191cde34c--- /dev/null+++ b/src/metabase/premium_features/schema.clj@@ -0,0 +1,19 @@+(ns metabase.premium-features.schema+ "Malli schemas for the premium-features module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::premium-features-cache+ "A PremiumFeaturesCache as selected from the app DB: every column of `:premium_features_token_cache`."+ [:map {:closed true}+ [:token_hash :string]+ [:token_status_hash :string]+ [:updated_at ms/TemporalInstant]])++(mr/def ::premium-features-cache.update+ "What an update (or insert) of a PremiumFeaturesCache accepts: every column of `:premium_features_token_cache` except `id`, all optional."+ [:map {:closed true}+ [:token_hash {:optional true} [:maybe :string]]+ [:token_status_hash {:optional true} [:maybe :string]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/product_feedback/db.clj b/src/metabase/product_feedback/db.cljindex 37142444086..590d2314bb9 100644--- a/src/metabase/product_feedback/db.clj+++ b/src/metabase/product_feedback/db.clj@@ -4,13 +4,14 @@(:require[metabase.app-db.core :as mdb][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn creator-sentiment-candidates+(mu/defn creator-sentiment-candidates"The email, join date, first name, and content counts of the active personal Users who created content in the past2 months, with at least 10 Cards (2 native), and at least 1 Dashboard. When `only-superusers?` is true, onlysuperusers are considered."- [only-superusers?]+ [only-superusers? :- :boolean](let [created-after (h2x/add-interval-honeysql-form (mdb/db-type) :%now -2 :month)](t2/query {:select [[:u.email :email][:u.date_joined :created_at]@@ -33,32 +34,32 @@[:>= [:count [:distinct [:case [:= :rc.query_type "native"] :rc.id]]] 2][:>= [:count [:distinct :d.id]] 1]]})))-(defn active-personal-user-count+(mu/defn active-personal-user-count"The number of active personal Users."[](t2/count :model/User :is_active true, :type "personal"))-(defn unarchived-dashboard-count+(mu/defn unarchived-dashboard-count"The number of unarchived Dashboards."[](t2/count :model/Dashboard :archived false))-(defn non-audit-database-count+(mu/defn non-audit-database-count"The number of Databases other than the audit Database."[](t2/count :model/Database :is_audit false))-(defn unarchived-card-count+(mu/defn unarchived-card-count"The number of unarchived Cards of `card-type`."- [card-type]+ [card-type :- [:or :keyword :string]](t2/count :model/Card :archived false :type card-type))-(defn oldest-active-admin+(mu/defn oldest-active-admin"The earliest-joined active superuser, or nil."[](t2/select-one :model/User :is_superuser true, :is_active true, {:order-by [:date_joined]}))-(defn earliest-user-date-joined+(mu/defn earliest-user-date-joined"The join date of the earliest-joined User, or nil."[](t2/select-one-fn :date_joined :model/User, {:order-by [[:date_joined :asc]]}))diff --git a/src/metabase/public_sharing/db.clj b/src/metabase/public_sharing/db.cljindex 0cce80652e2..8c55dbcd892 100644--- a/src/metabase/public_sharing/db.clj+++ b/src/metabase/public_sharing/db.clj@@ -2,44 +2,45 @@"Application database queries for the public sharing module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn unarchived-card-ids-and-public-uuids-by-prefix+(mu/defn unarchived-card-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Cards whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select [:model/Card :id :public_uuid] :public_uuid_prefix prefix :archived false))-(defn unarchived-dashboard-ids-and-public-uuids-by-prefix+(mu/defn unarchived-dashboard-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Dashboards whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select [:model/Dashboard :id :public_uuid] :public_uuid_prefix prefix :archived false))-(defn unarchived-action-ids-and-public-uuids-by-prefix+(mu/defn unarchived-action-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Actions whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select [:model/Action :id :public_uuid] :public_uuid_prefix prefix :archived false))-(defn unarchived-document-ids-and-public-uuids-by-prefix+(mu/defn unarchived-document-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Documents whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select [:model/Document :id :public_uuid] :public_uuid_prefix prefix :archived false))-(defn unarchived-cards-by-public-uuid-prefix+(mu/defn unarchived-cards-by-public-uuid-prefix"The unarchived Cards whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select :model/Card :public_uuid_prefix prefix :archived false))-(defn unarchived-dashboards-by-public-uuid-prefix+(mu/defn unarchived-dashboards-by-public-uuid-prefix"The unarchived Dashboards whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select :model/Dashboard :public_uuid_prefix prefix :archived false))-(defn unarchived-actions-by-public-uuid-prefix+(mu/defn unarchived-actions-by-public-uuid-prefix"The unarchived Actions whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select :model/Action :public_uuid_prefix prefix :archived false))-(defn unarchived-documents-by-public-uuid-prefix+(mu/defn unarchived-documents-by-public-uuid-prefix"The unarchived Documents whose public uuid prefix is `prefix`."- [prefix]+ [prefix :- :string](t2/select :model/Document :public_uuid_prefix prefix :archived false))diff --git a/src/metabase/public_sharing_rest/db.clj b/src/metabase/public_sharing_rest/db.cljindex aaebbc585fc..19630e15fc3 100644--- a/src/metabase/public_sharing_rest/db.clj+++ b/src/metabase/public_sharing_rest/db.clj@@ -2,14 +2,18 @@"Application database queries for the public sharing REST module. Every function here is a direct Toucan 2 call withno additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]));;; ------------------------------------------------------ Cards --------------------------------------------------------(defn public-card+(mu/defn public-card"The non-archived Card with `card-id`, restricted to the columns safe to expose publicly, or nil. With`:enable-embedding? true`, additionally requires embedding to be enabled."- [card-id & {:keys [enable-embedding?]}]+ [card-id :- [:maybe ::lib.schema.id/card]+ & {:keys [enable-embedding?]} :- [:maybe [:map {:closed true} [:enable-embedding? {:optional true} [:maybe :boolean]]]]](t2/select-one [:model/Card :id :dataset_query :description :display :name :parameters :visualization_settings:card_schema]{:where [:and@@ -17,47 +21,50 @@[:= :archived false](when enable-embedding? [:= :enable_embedding true])]}))-(defn active-card+(mu/defn active-card"The non-archived Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id :archived false))-(defn active-card-in-document+(mu/defn active-card-in-document"The non-archived Card with `card-id` that belongs to the Document with `document-id`, or nil."- [card-id document-id]+ [card-id :- ::lib.schema.id/card+ document-id :- ms/PositiveInt](t2/select-one :model/Card :id card-id :document_id document-id :archived false));;; ---------------------------------------------------- Dashboards -----------------------------------------------------(defn public-dashboard+(mu/defn public-dashboard"The non-archived Dashboard with `dashboard-id`, restricted to the columns safe to expose publicly, or nil. With`:enable-embedding? true`, additionally requires embedding to be enabled."- [dashboard-id & {:keys [enable-embedding?]}]+ [dashboard-id :- [:maybe ::lib.schema.id/dashboard]+ & {:keys [enable-embedding?]} :- [:maybe [:map {:closed true} [:enable-embedding? {:optional true} [:maybe :boolean]]]]](t2/select-one [:model/Dashboard :name :description :id :parameters :auto_apply_filters :width]{:where [:and[:= :id dashboard-id][:= :archived false](when enable-embedding? [:= :enable_embedding true])]}))-(defn dashcard+(mu/defn dashcard"The DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one :model/DashboardCard :id dashcard-id))-(defn dashcard-id-in-dashboard+(mu/defn dashcard-id-in-dashboard"`dashcard-id` if that DashboardCard belongs to the Dashboard with `dashboard-id`, otherwise nil."- [dashcard-id dashboard-id]+ [dashcard-id :- ::lib.schema.id/dashcard+ dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-pk :model/DashboardCard :id dashcard-id :dashboard_id dashboard-id));;; ----------------------------------------------------- Documents -----------------------------------------------------(defn public-document+(mu/defn public-document"The non-archived Document with `document-id`, restricted to the columns safe to expose publicly."- [document-id]+ [document-id :- [:maybe ms/PositiveInt]](t2/select-one [:model/Document :id :name :document :content_type :created_at :updated_at]:id document-id, :archived false))-(defn document-content+(mu/defn document-content"The id, content, and content type of the Document with `document-id`, or nil."- [document-id]+ [document-id :- [:maybe ms/PositiveInt]](t2/select-one [:model/Document :id :document :content_type] :id document-id))diff --git a/src/metabase/pulse/api/unsubscribe.clj b/src/metabase/pulse/api/unsubscribe.cljindex 56516a81e7f..656c372221e 100644--- a/src/metabase/pulse/api/unsubscribe.clj+++ b/src/metabase/pulse/api/unsubscribe.clj@@ -45,7 +45,7 @@(api/let-404 [pulse-channel (pulse.db/email-pulse-channel pulse-id)](let [emails (get-in pulse-channel [:details :emails])](if (some #{email} emails)- (pulse.db/update-pulse-channel! (:id pulse-channel) (update-in pulse-channel [:details :emails] #(remove #{email} %)))+ (pulse.db/update-pulse-channel! (:id pulse-channel) (-> pulse-channel (dissoc :id) (update-in [:details :emails] #(remove #{email} %))))(throw (ex-info (tru "Email for pulse-id doesn''t exist."){:type type:status-code 400}))))@@ -73,6 +73,6 @@(throw (ex-info (tru "Email for pulse-id already exists."){:type type:status-code 400}))- (pulse.db/update-pulse-channel! (:id pulse-channel) (update-in pulse-channel [:details :emails] conj email))))+ (pulse.db/update-pulse-channel! (:id pulse-channel) (-> pulse-channel (dissoc :id) (update-in [:details :emails] conj email)))))(events/publish-event! :event/subscription-unsubscribe-undo {:object {:email email}}){:status :success :title (:name (models.pulse/retrieve-notification pulse-id :archived false))})))diff --git a/src/metabase/pulse/db.clj b/src/metabase/pulse/db.cljindex 05ce83cf346..167333d29ed 100644--- a/src/metabase/pulse/db.clj+++ b/src/metabase/pulse/db.clj@@ -3,101 +3,108 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.pulse.schema :as pulse.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card card-id))-(defn card-query+(mu/defn card-query"The query of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :dataset_query [:model/Card :dataset_query] card-id))-(defn dashboard- "The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+(mu/defn dashboard+ "The Dashboard with `dashboard-id`, or nil. `dashboard-id` may be nil (e.g. a legacy Pulse with no Dashboard), in+ which case the result is nil."+ [dashboard-id :- [:maybe ::lib.schema.id/dashboard]](t2/select-one :model/Dashboard :id dashboard-id))-(defn dashboard-collection-id+(mu/defn dashboard-collection-id"The Collection id of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :collection_id :model/Dashboard, :id dashboard-id))-(defn dashcard-ids-by-card+(mu/defn dashcard-ids-by-card"A map of Card id to DashboardCard id for the DashboardCards of the Dashboard with `dashboard-id` showing one of`card-ids`."- [dashboard-id card-ids]+ [dashboard-id :- ::lib.schema.id/dashboard+ card-ids :- [:set ::lib.schema.id/card]](t2/select-fn->pk :card_id :model/DashboardCard :dashboard_id dashboard-id :card_id [:in card-ids]))-(defn pulse-card-pairs-for-dashboard+(mu/defn pulse-card-pairs-for-dashboard"Distinct `:pulse-id`/`:card-id` pairs for the Pulses (subscriptions) attached to the Dashboard with`dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](app-db/query {:select-distinct [[:p.id :pulse-id] [:pc.card_id :card-id]]:from [[:pulse :p]]:left-join [[:pulse_card :pc] [:= :p.id :pc.pulse_id]]:where [:= :p.dashboard_id dashboard-id]}))-(defn dashboard-card-ids-for-dashboard+(mu/defn dashboard-card-ids-for-dashboard"The distinct, non-nil Card ids shown by the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](map :card_id (app-db/query {:select-distinct [:dc.card_id]:from [[:report_dashboardcard :dc]]:where [:and[:= :dc.dashboard_id dashboard-id][:not= :dc.card_id nil]]})))-(defn channel+(mu/defn channel"The Channel with `channel-id`, or nil."- [channel-id]+ [channel-id :- ms/PositiveInt](t2/select-one :model/Channel :id channel-id))-(defn active-http-channel-exists?+(mu/defn active-http-channel-exists?"Whether an active HTTP Channel exists."[](t2/exists? :model/Channel :type :channel/http :active true))-(defn superusers+(mu/defn superusers"The superusers."[](t2/select :model/User :is_superuser true))-(defn user-emails-by-id+(mu/defn user-emails-by-id"A map of User id to email for the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn :email :model/User, :id [:in user-ids]))-(defn pulse+(mu/defn pulse"The Pulse with `pulse-id`, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one :model/Pulse :id pulse-id))-(defn pulse-id+(mu/defn pulse-id"The id of the Pulse with `pulse-id` if it exists, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one-pk :model/Pulse :id pulse-id))-(defn unarchived-pulse+(mu/defn unarchived-pulse"The unarchived Pulse with `pulse-id`, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one :model/Pulse :id pulse-id :archived false))-(defn unarchived-non-alert-pulse+(mu/defn unarchived-non-alert-pulse"The unarchived, non-alert Pulse with `pulse-id`, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one :model/Pulse :id pulse-id :archived false :alert_condition nil))-(defn alert+(mu/defn alert"The Pulse with `pulse-id` if it is an alert, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one :model/Pulse, :id pulse-id, :alert_condition [:not= nil]))-(defn alerts+(mu/defn alerts"The alert-type Pulses (unarchived unless `archived?`), optionally narrowed to those with a recipient or creator`user-id`, ordered by lower-cased name."- [archived? user-id]+ [archived? :- :boolean+ user-id :- [:maybe ::lib.schema.id/user]](t2/select :model/Pulse(merge {:select-distinct [:p.* [[:lower :p.name] :lower-name]]:from [[:pulse :p]]@@ -113,10 +120,12 @@{:left-join [[:pulse_channel :pchan] [:= :p.id :pchan.pulse_id][:pulse_channel_recipient :pcr] [:= :pchan.id :pcr.pulse_channel_id]]}))))-(defn pulses+(mu/defn pulses"The dashboard-subscription Pulses (unarchived unless `archived?`), optionally narrowed to `dashboard-id` and/orthose with a recipient or creator `user-id`, ordered by lower-cased name."- [archived? dashboard-id user-id]+ [archived? :- :boolean+ dashboard-id :- [:maybe ::lib.schema.id/dashboard]+ user-id :- [:maybe ::lib.schema.id/user]](t2/select :model/Pulse{:select-distinct [:p.* [[:lower :p.name] :lower-name]]:from [[:pulse :p]]@@ -141,10 +150,12 @@[:= :pcr.user_id user-id]]])]:order-by [[:lower-name :asc]]}))-(defn alerts-for-card-and-user+(mu/defn alerts-for-card-and-user"The alert-type Pulses (unarchived unless `archived?`) on the Card with `card-id` that the User with `user-id` isset to receive."- [card-id user-id archived?]+ [card-id :- ::lib.schema.id/card+ user-id :- ::lib.schema.id/user+ archived? :- :boolean](t2/select :model/Pulse{:select [:p.*]:from [[:pulse :p]]@@ -157,9 +168,10 @@[:= :pcr.user_id user-id][:= :p.archived archived?]]}))-(defn alerts-for-cards+(mu/defn alerts-for-cards"The alert-type Pulses (unarchived unless `archived?`) on any of the Cards with `card-ids`."- [card-ids archived?]+ [card-ids :- [:sequential ::lib.schema.id/card]+ archived? :- :boolean](t2/select :model/Pulse{:select [:p.*]:from [[:pulse :p]]@@ -169,35 +181,38 @@[:in :pc.card_id card-ids][:= :p.archived archived?]]}))-(defn legacy-pulse-count+(mu/defn legacy-pulse-count"The number of unarchived Pulses that are neither dashboard subscriptions nor alerts."[](t2/count :model/Pulse :dashboard_id nil :alert_condition nil :archived false))-(defn legacy-pulses+(mu/defn legacy-pulses"The unarchived Pulses that are neither dashboard subscriptions nor alerts."[](t2/select :model/Pulse :dashboard_id nil :alert_condition nil :archived false))-(defn insert-pulse!+(mu/defn insert-pulse!"Insert the Pulse `pulse` and return the inserted instance."- [pulse]+ [pulse :- ::pulse.schema/pulse.update](t2/insert-returning-instance! :model/Pulse pulse))-(defn update-pulse!+(mu/defn update-pulse!"Apply `changes` to the Pulse with `pulse-id`."- [pulse-id changes]+ [pulse-id :- ::lib.schema.id/pulse+ changes :- ::pulse.schema/pulse.update](t2/update! :model/Pulse pulse-id changes))-(defn update-pulses-for-dashboard!- "Apply `changes` to the Pulses of the Dashboard with `dashboard-id`."- [dashboard-id changes]- (t2/update! :model/Pulse {:dashboard_id dashboard-id} changes))+(mu/defn update-pulses-for-dashboard!+ "Apply `changes` to the Pulses of the Dashboard with `dashboard-id`, leaving `updated_at` untouched."+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- ::pulse.schema/pulse.update]+ (t2/update! :model/Pulse {:dashboard_id dashboard-id} (assoc changes :updated_at :updated_at)))-(defn pulse-cards-for-pulses+(mu/defn pulse-cards-for-pulses"The Cards of the Pulses with `pulse-ids` together with their PulseCard options, in position order. Excludesarchived Cards unless `include-archived?`."- [pulse-ids include-archived?]+ [pulse-ids :- [:sequential ::lib.schema.id/pulse]+ include-archived? :- :boolean](t2/select:model/Card{:select [:c.id :c.name :c.description :c.collection_id :c.display :pc.include_csv :pc.include_xls :pc.format_rows :pc.pivot_results@@ -211,57 +226,57 @@(when-not include-archived? [:= :c.archived false])]:order-by [[:pc.position :asc]]}))-(defn pulse-card-refs+(mu/defn pulse-card-refs"The Card id (as `:id`), export options, and DashboardCard id of the PulseCards of the Pulse with `pulse-id`, inposition order."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select [:model/PulseCard [:card_id :id] :include_csv :include_xls :dashboard_card_id]:pulse_id pulse-id{:order-by [[:position :asc]]}))-(defn max-pulse-card-position+(mu/defn max-pulse-card-position"The `:max` position of the PulseCards of the Pulse with `pulse-id`."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one [:model/PulseCard [:%max.position :max]] :pulse_id pulse-id))-(defn insert-pulse-cards!+(mu/defn insert-pulse-cards!"Insert the PulseCard `rows`."- [rows]+ [rows :- [:sequential ::pulse.schema/pulse-card.update]](t2/insert! :model/PulseCard rows))-(defn delete-pulse-cards-for-pulse!+(mu/defn delete-pulse-cards-for-pulse!"Delete the PulseCards of the Pulse with `pulse-id`."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/delete! :model/PulseCard :pulse_id pulse-id))-(defn pulse-channels-for-pulse+(mu/defn pulse-channels-for-pulse"The PulseChannels of the Pulse with `pulse-id`."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select :model/PulseChannel :pulse_id pulse-id))-(defn pulse-channels-for-pulses+(mu/defn pulse-channels-for-pulses"The PulseChannels of the Pulses with `pulse-ids`."- [pulse-ids]+ [pulse-ids :- [:sequential ::lib.schema.id/pulse]](t2/select :model/PulseChannel :pulse_id [:in pulse-ids]))-(defn email-pulse-channel+(mu/defn email-pulse-channel"The email PulseChannel of the Pulse with `pulse-id`, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one :model/PulseChannel :pulse_id pulse-id :channel_type "email"))-(defn email-pulse-channel-id+(mu/defn email-pulse-channel-id"The id of the email PulseChannel of the Pulse with `pulse-id`, or nil."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select-one-pk :model/PulseChannel :pulse_id pulse-id :channel_type "email"))-(defn pulse-channel-details+(mu/defn pulse-channel-details"The details of the PulseChannel with `channel-id`, or nil."- [channel-id]+ [channel-id :- ms/PositiveInt](t2/select-one-fn :details :model/PulseChannel :id channel-id))-(defn pulse-channels-without-recipients+(mu/defn pulse-channels-without-recipients"The id, details, Channel, and type of the PulseChannels of the Pulse with `pulse-id` that have no recipients."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/select [:model/PulseChannel :id :details :channel_id :channel_type]{:where [:and[:= :pulse_id pulse-id]@@ -271,12 +286,12 @@:where [:= :pulse_channel_recipient.pulse_channel_id:pulse_channel.id]}]]]}))-(defn enabled-pulse-channel-ids+(mu/defn enabled-pulse-channel-ids"The ids among `channel-ids` of enabled PulseChannels, or nil."- [channel-ids]+ [channel-ids :- [:set ms/PositiveInt]](t2/select-pks-set :model/PulseChannel :id [:in channel-ids] :enabled true))-(defn active-dashboard-subscription-channels+(mu/defn active-dashboard-subscription-channels"The enabled PulseChannels of dashboard subscriptions whose Dashboard is not archived."[](t2/select :model/PulseChannel@@ -291,45 +306,48 @@[:not= :p.dashboard_id nil][:= :d.archived false]]}))-(defn other-pulse-channel-count+(mu/defn other-pulse-channel-count"The number of PulseChannels of the Pulse with `pulse-id` other than `channel-id`."- [pulse-id channel-id]+ [pulse-id :- ::lib.schema.id/pulse+ channel-id :- ms/PositiveInt](t2/count :model/PulseChannel :pulse_id pulse-id, :id [:not= channel-id]))-(defn insert-pulse-channel!+(mu/defn insert-pulse-channel!"Insert the PulseChannel `row` and return its id."- [row]+ [row :- ::pulse.schema/pulse-channel.update](t2/insert-returning-pk! :model/PulseChannel row))-(defn update-pulse-channel!+(mu/defn update-pulse-channel!"Apply `changes` to the PulseChannel with `channel-id`."- [channel-id changes]+ [channel-id :- ms/PositiveInt+ changes :- ::pulse.schema/pulse-channel.update](t2/update! :model/PulseChannel channel-id changes))-(defn set-pulse-channels-enabled!+(mu/defn set-pulse-channels-enabled!"Set the enabled flag of the PulseChannels of the Pulse with `pulse-id`."- [pulse-id enabled?]+ [pulse-id :- ::lib.schema.id/pulse+ enabled? :- :boolean](t2/update! :model/PulseChannel :pulse_id pulse-id {:enabled enabled?}))-(defn delete-pulse-channel!+(mu/defn delete-pulse-channel!"Delete the PulseChannel with `channel-id`."- [channel-id]+ [channel-id :- ms/PositiveInt](t2/delete! :model/PulseChannel :id channel-id))-(defn delete-pulse-channels!+(mu/defn delete-pulse-channels!"Delete the PulseChannels with `channel-ids`."- [channel-ids]+ [channel-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/PulseChannel :id [:in channel-ids]))-(defn delete-pulse-channels-for-pulse!+(mu/defn delete-pulse-channels-for-pulse!"Delete the PulseChannels of the Pulse with `pulse-id`."- [pulse-id]+ [pulse-id :- ::lib.schema.id/pulse](t2/delete! :model/PulseChannel :pulse_id pulse-id))-(defn active-recipients-for-channels+(mu/defn active-recipients-for-channels"The id, email, name, and PulseChannel id of the active User recipients of the PulseChannels with `channel-ids`, inUser id order."- [channel-ids]+ [channel-ids :- [:sequential ms/PositiveInt]](t2/select [:model/User :id :email :first_name :last_name :pcr.pulse_channel_id]{:left-join [[:pulse_channel_recipient :pcr] [:= :core_user.id :pcr.user_id]]:where [:and@@ -337,43 +355,46 @@[:= :core_user.is_active true]]:order-by [[:core_user.id :asc]]}))-(defn pulse-channel-recipient-id+(mu/defn pulse-channel-recipient-id"The id of the PulseChannelRecipient of the User with `user-id` on the PulseChannel with `channel-id`, or nil."- [channel-id user-id]+ [channel-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/select-one-pk :model/PulseChannelRecipient :pulse_channel_id channel-id :user_id user-id))-(defn pulse-channel-recipient-user-ids+(mu/defn pulse-channel-recipient-user-ids"The User ids of the PulseChannelRecipients of the PulseChannel with `channel-id`."- [channel-id]+ [channel-id :- ms/PositiveInt](t2/select-fn-set :user_id :model/PulseChannelRecipient, :pulse_channel_id channel-id))-(defn other-pulse-channel-recipient-count+(mu/defn other-pulse-channel-recipient-count"The number of PulseChannelRecipients of the PulseChannel with `channel-id` other than `recipient-id`."- [channel-id recipient-id]+ [channel-id :- ms/PositiveInt+ recipient-id :- ms/PositiveInt](t2/count :model/PulseChannelRecipient :pulse_channel_id channel-id :id [:not= recipient-id]))-(defn insert-pulse-channel-recipients!+(mu/defn insert-pulse-channel-recipients!"Insert the PulseChannelRecipient `rows`."- [rows]+ [rows :- [:sequential ::pulse.schema/pulse-channel-recipient.update]](t2/insert! :model/PulseChannelRecipient rows))-(defn delete-pulse-channel-recipient!+(mu/defn delete-pulse-channel-recipient!"Delete the PulseChannelRecipient with `recipient-id`."- [recipient-id]+ [recipient-id :- ms/PositiveInt](t2/delete! :model/PulseChannelRecipient :id recipient-id))-(defn delete-pulse-channel-recipients-raw!+(mu/defn delete-pulse-channel-recipients-raw!"Delete the PulseChannelRecipients of the Users with `user-ids` on the PulseChannel with `channel-id`, withoutrunning model hooks."- [channel-id user-ids]+ [channel-id :- ms/PositiveInt+ user-ids :- [:set ::lib.schema.id/user]](t2/delete! (t2/table-name :model/PulseChannelRecipient) :pulse_channel_id channel-id :user_id [:in user-ids]))-(defn delete-notifications!+(mu/defn delete-notifications!"Delete the Notifications with `notification-ids`."- [notification-ids]+ [notification-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/Notification :id [:in notification-ids]))-(defn user-tenant-ids+(mu/defn user-tenant-ids"A map of User ID to `:tenant_id` for `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn :tenant_id :model/User :id [:in user-ids]))diff --git a/src/metabase/pulse/events/dashboard_subscription.clj b/src/metabase/pulse/events/dashboard_subscription.cljindex 421afde59e9..0c34e8d4d88 100644--- a/src/metabase/pulse/events/dashboard_subscription.clj+++ b/src/metabase/pulse/events/dashboard_subscription.clj@@ -42,8 +42,5 @@;; pulse.name is no longer used for generating title.;; pulse.collection_id is a thing for the old "Pulse" feature, but it was removed{:name (:name dashboard)- :collection_id (:collection_id dashboard)- ;; not allowing updated_at to change because this is usually triggered after a "last_viewed_at" update on dashboard which doesn't warrant a pulse update.- ;; plus, the name and collection_id are not really used for anything so it's fine they don't trigger a pulse update_at change even if they do change.- :updated_at :updated_at})+ :collection_id (:collection_id dashboard)})(pulse-card/bulk-create! new-pulse-cards)))))))diff --git a/src/metabase/pulse/schema.clj b/src/metabase/pulse/schema.cljnew file mode 100644index 00000000000..dbb61e7e0ca--- /dev/null+++ b/src/metabase/pulse/schema.clj@@ -0,0 +1,126 @@+(ns metabase.pulse.schema+ "Malli schemas for the pulse module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::pulse.parameter+ "One entry of the `:parameters` column of a Pulse, decoded."+ :map)++(mr/def ::pulse+ "A Pulse as selected from the app DB: every column of `:pulse`."+ [:map {:closed true}+ [:id ::lib.schema.id/pulse]+ [:creator_id ::lib.schema.id/user]+ [:name [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:skip_if_empty :boolean]+ [:alert_condition [:maybe :string]]+ [:alert_first_only [:maybe :boolean]]+ [:alert_above_goal [:maybe :boolean]]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:collection_position [:maybe :int]]+ [:archived [:maybe :boolean]]+ [:dashboard_id [:maybe ::lib.schema.id/dashboard]]+ [:parameters [:sequential ::pulse.parameter]]+ [:entity_id :string]+ [:disable_links [:maybe :boolean]]])++(mr/def ::pulse.update+ "What an update (or insert) of a Pulse accepts: every column of `:pulse` except `id`, all optional."+ [:map {:closed true}+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:name {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:skip_if_empty {:optional true} [:maybe :boolean]]+ [:alert_condition {:optional true} [:maybe :string]]+ [:alert_first_only {:optional true} [:maybe :boolean]]+ [:alert_above_goal {:optional true} [:maybe :boolean]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:collection_position {:optional true} [:maybe :int]]+ [:archived {:optional true} [:maybe :boolean]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:parameters {:optional true} [:maybe [:sequential ::pulse.parameter]]]+ [:entity_id {:optional true} [:maybe :string]]+ [:disable_links {:optional true} [:maybe :boolean]]])++(mr/def ::pulse-card+ "A PulseCard as selected from the app DB: every column of `:pulse_card`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:pulse_id ::lib.schema.id/pulse]+ [:card_id ::lib.schema.id/card]+ [:position :int]+ [:include_csv :boolean]+ [:include_xls :boolean]+ [:dashboard_card_id [:maybe ::lib.schema.id/card]]+ [:entity_id :string]+ [:format_rows [:maybe :boolean]]+ [:pivot_results [:maybe :boolean]]])++(mr/def ::pulse-card.update+ "What an update (or insert) of a PulseCard accepts: every column of `:pulse_card` except `id`, all optional."+ [:map {:closed true}+ [:pulse_id {:optional true} [:maybe ::lib.schema.id/pulse]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:position {:optional true} [:maybe :int]]+ [:include_csv {:optional true} [:maybe :boolean]]+ [:include_xls {:optional true} [:maybe :boolean]]+ [:dashboard_card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:entity_id {:optional true} [:maybe :string]]+ [:format_rows {:optional true} [:maybe :boolean]]+ [:pivot_results {:optional true} [:maybe :boolean]]])++(mr/def ::pulse-channel.details+ "The `:details` column of a PulseChannel, decoded."+ :map)++(mr/def ::pulse-channel+ "A PulseChannel as selected from the app DB: every column of `:pulse_channel`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:pulse_id ::lib.schema.id/pulse]+ [:channel_type [:or :keyword :string]]+ [:details ::pulse-channel.details]+ [:schedule_type [:or :keyword :string]]+ [:schedule_hour [:maybe :int]]+ [:schedule_day [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:schedule_frame [:maybe [:or :keyword :string]]]+ [:enabled :boolean]+ [:entity_id :string]+ [:channel_id [:maybe ms/PositiveInt]]])++(mr/def ::pulse-channel.update+ "What an update (or insert) of a PulseChannel accepts: every column of `:pulse_channel` except `id`, all optional."+ [:map {:closed true}+ [:pulse_id {:optional true} [:maybe ::lib.schema.id/pulse]]+ [:channel_type {:optional true} [:maybe [:or :keyword :string]]]+ [:details {:optional true} [:maybe ::pulse-channel.details]]+ [:schedule_type {:optional true} [:maybe [:or :keyword :string]]]+ [:schedule_hour {:optional true} [:maybe :int]]+ [:schedule_day {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:schedule_frame {:optional true} [:maybe [:or :keyword :string]]]+ [:enabled {:optional true} [:maybe :boolean]]+ [:entity_id {:optional true} [:maybe :string]]+ [:channel_id {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::pulse-channel-recipient+ "A PulseChannelRecipient as selected from the app DB: every column of `:pulse_channel_recipient`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:pulse_channel_id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]])++(mr/def ::pulse-channel-recipient.update+ "What an update (or insert) of a PulseChannelRecipient accepts: every column of `:pulse_channel_recipient` except `id`, all optional."+ [:map {:closed true}+ [:pulse_channel_id {:optional true} [:maybe ms/PositiveInt]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]])diff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.cljindex 793104f3a0c..ef4af5ec333 100644--- a/src/metabase/queries/db.clj+++ b/src/metabase/queries/db.clj@@ -4,108 +4,114 @@methods."(:require[metabase.app-db.core :as mdb]+ [metabase.dashboards.schema :as dashboards.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]));;; ------------------------------------------------ Cards -------------------------------------------------(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn cards- "The Cards with `card-ids`."- [card-ids]+(mu/defn cards+ "The Cards with `card-ids` (nil entries, e.g. from virtual dashcards, are ignored)."+ [card-ids :- [:maybe [:or [:set [:maybe ::lib.schema.id/card]] [:sequential [:maybe ::lib.schema.id/card]]]]](t2/select :model/Card :id [:in card-ids]))-(defn card-query-info+(mu/defn card-query-info"The query, type, result metadata, and schema of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :dataset_query :type :result_metadata :card_schema] :id card-id))-(defn card-dataset-query+(mu/defn card-dataset-query"The `:dataset_query` of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :dataset_query [:model/Card :dataset_query :card_schema] :id card-id))-(defn card-document-id+(mu/defn card-document-id"The `:document_id` of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :document_id :model/Card :id card-id))-(defn card-parameters+(mu/defn card-parameters"The `:parameters` of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :parameters [:model/Card :parameters] :id card-id))-(defn card-dimensions+(mu/defn card-dimensions"The raw `:dimensions` row of the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/query-one {:select [:dimensions]:from [:report_card]:where [:= :id card-id]}))-(defn card-database-and-table-ids+(mu/defn card-database-and-table-ids"The database and primary table IDs of the Card with `card-id`, as `:database-id` and `:table-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card [:database_id :database-id] [:table_id :table-id]] :id card-id))-(defn card-queries+(mu/defn card-queries"The IDs and queries of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select [:model/Card :id :dataset_query :card_schema] :id [:in card-ids]))-(defn source-card-dependents+(mu/defn source-card-dependents"The IDs and source Card IDs of the Cards whose source Card is one of `source-card-ids`."- [source-card-ids]+ [source-card-ids :- [:or [:set ::lib.schema.id/card] [:sequential ::lib.schema.id/card]]](t2/select [:model/Card :id :source_card_id :card_schema] :source_card_id [:in source-card-ids]))-(defn metric-cards-for-source-cards+(mu/defn metric-cards-for-source-cards"The unarchived metric Cards built on one of `source-card-ids`, ordered by name."- [source-card-ids]+ [source-card-ids :- [:sequential ::lib.schema.id/card]](t2/select :model/Card:source_card_id [:in source-card-ids]:archived false:type :metric{:order-by [[:name :asc]]}))-(defn document-card-ids+(mu/defn document-card-ids"The IDs of the Cards that belong to the Document with `document-id`."- [document-id]+ [document-id :- ms/PositiveInt](t2/select-pks-set :model/Card :document_id document-id))-(defn insert-card!+(mu/defn insert-card!"Insert `card` and return the new instance."- [card]+ [card :- ::queries.schema/card.update](t2/insert-returning-instance! :model/Card card))-(defn update-card!- "Apply `changes` to the Card with `card-id`."- [card-id changes]+(mu/defn update-card!+ "Apply `changes` to the Card with `card-id`, returning the number updated."+ [card-id :- ::lib.schema.id/card+ changes :- ::queries.schema/card.update](t2/update! :model/Card card-id changes));;; ------------------------------------------- Card statistics --------------------------------------------(defn dashcard-counts-by-card+(mu/defn dashcard-counts-by-card"Rows of `:card_id` and `:count` of DashboardCards for each of `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/query {:select [[:%count.* :count] :card_id]:from [:report_dashboardcard]:where [:in :card_id card-ids]:group-by [:card_id]}))-(defn parameter-card-counts-by-card+(mu/defn parameter-card-counts-by-card"Rows of `:card_id` and `:count` of ParameterCards for each of `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/query {:select [[:%count.* :count] :card_id]:from [:parameter_card]:where [:in :card_id card-ids]:group-by [:card_id]}))-(defn average-running-times-by-card+(mu/defn average-running-times-by-card"Rows of `:card_id` and average `:running_time` of uncached executions for each of `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/query {:select [[:%avg.running_time :running_time] :card_id]:from [:query_execution]:where [:and@@ -114,9 +120,9 @@[:in :card_id card-ids]]:group-by [:card_id]}))-(defn last-query-starts-by-card+(mu/defn last-query-starts-by-card"Rows of `:card_id` and latest `:started_at` of uncached executions for each of `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/query {:select [[:%max.started_at :started_at] :card_id]:from [:query_execution]:where [:and@@ -125,9 +131,9 @@[:in :card_id card-ids]]:group-by [:card_id]}))-(defn dashboards-for-cards+(mu/defn dashboards-for-cards"Rows of `:card_id` plus Dashboard columns for every Dashboard each of `card-ids` appears on, directly or as a series."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/query {:union-all [^:allow-subquery {:nest^:allow-subquery {:select [[:dc.card_id :card_id]:d.name@@ -156,19 +162,19 @@;;; ------------------------------------------- Related models ---------------------------------------------(defn databases+(mu/defn databases"The Databases with `database-ids`."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select :model/Database :id [:in database-ids]))-(defn database-name+(mu/defn database-name"The name of the Database with `database-id`."- [database-id]+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one-fn :name :model/Database :id database-id))-(defn field-database-info-for-ids+(mu/defn field-database-info-for-ids"The id, name, table name, and Database id of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/query {:select [[:field.id :field-id][:field.name :field-name][:table.name :table-name]@@ -178,69 +184,76 @@[:= :field.table_id :table.id]]:where [:in :field.id field-ids]}))-(defn field-table-ids+(mu/defn field-table-ids"The set of Table IDs of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select-fn-set :table_id :model/Field :id [:in field-ids]))-(defn snippets+(mu/defn snippets"The NativeQuerySnippets with `snippet-ids`."- [snippet-ids]+ [snippet-ids :- [:set ::lib.schema.id/native-query-snippet]](t2/select :model/NativeQuerySnippet :id [:in snippet-ids]))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard :id dashboard-id))-(defn dashboards+(mu/defn dashboards"The Dashboards with `dashboard-ids`."- [dashboard-ids]+ [dashboard-ids :- [:set ::lib.schema.id/dashboard]](t2/select :model/Dashboard :id [:in dashboard-ids]))-(defn dashboard-collection-id+(mu/defn dashboard-collection-id"The `:collection_id` of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :collection_id [:model/Dashboard :collection_id] :id dashboard-id))-(defn dashboard-parameters+(mu/defn dashboard-parameters"The `:parameters` of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :parameters [:model/Dashboard :parameters] :id dashboard-id))-(defn update-dashboard!- "Apply `changes` to the Dashboard with `dashboard-id`."- [dashboard-id changes]+(mu/defn update-dashboard!+ "Apply `changes` to the Dashboard with `dashboard-id`, returning the number updated."+ [dashboard-id :- ::lib.schema.id/dashboard+ changes :- ::dashboards.schema/dashboard.update](t2/update! :model/Dashboard dashboard-id changes))-(defn dashcards-for-card+(mu/defn dashcards-for-card"The DashboardCards showing the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select :model/DashboardCard :card_id card-id))-(defn insert-dashcard!- "Insert `dashcard`."- [dashcard]+(mu/defn insert-dashcard!+ "Insert `dashcard`, returning the number inserted."+ [dashcard :- ::dashboards.schema/dashboard-card.update](t2/insert! :model/DashboardCard dashcard))-(defn update-dashcard!- "Apply `changes` to the DashboardCard with `dashcard-id`."- [dashcard-id changes]+(mu/defn update-dashcard!+ "Apply `changes` to the DashboardCard with `dashcard-id`, returning the number updated."+ [dashcard-id :- ::lib.schema.id/dashcard+ changes :- ::dashboards.schema/dashboard-card.update](t2/update! :model/DashboardCard :id dashcard-id changes))-(defn delete-dashcards-for-card-on-dashboard!- "Delete the DashboardCards showing the Card with `card-id` on the Dashboard with `dashboard-id`."- [card-id dashboard-id]+(mu/defn delete-dashcards-for-card-on-dashboard!+ "Delete the DashboardCards showing the Card with `card-id` on the Dashboard with `dashboard-id`, returning the+ number deleted."+ [card-id :- ::lib.schema.id/card+ dashboard-id :- ::lib.schema.id/dashboard](t2/delete! :model/DashboardCard :card_id card-id :dashboard_id dashboard-id))-(defn delete-dashcards-for-card-off-dashboard!- "Delete the DashboardCards showing the Card with `card-id` on any Dashboard other than `dashboard-id`."- [card-id dashboard-id]+(mu/defn delete-dashcards-for-card-off-dashboard!+ "Delete the DashboardCards showing the Card with `card-id` on any Dashboard other than `dashboard-id`, returning+ the number deleted."+ [card-id :- ::lib.schema.id/card+ dashboard-id :- ::lib.schema.id/dashboard](t2/delete! :model/DashboardCard :card_id card-id :dashboard_id [:not= dashboard-id]))-(defn dashcard-series-for-card-on-dashboard+(mu/defn dashcard-series-for-card-on-dashboard"The IDs of the DashboardCardSeries showing the Card with `card-id` on the Dashboard with `dashboard-id`."- [card-id dashboard-id]+ [card-id :- ::lib.schema.id/card+ dashboard-id :- ::lib.schema.id/dashboard](t2/query {:select [[:dcs.id]]:from [[:dashboardcard_series :dcs]]:join [[:report_dashboardcard :dc] [:= :dc.id :dcs.dashboardcard_id]]@@ -248,9 +261,10 @@[:= :dc.dashboard_id dashboard-id][:= :dcs.card_id card-id]]}))-(defn dashcard-series-for-card-off-dashboard+(mu/defn dashcard-series-for-card-off-dashboard"The IDs of the DashboardCardSeries showing the Card with `card-id` on any Dashboard other than `dashboard-id`."- [card-id dashboard-id]+ [card-id :- ::lib.schema.id/card+ dashboard-id :- ::lib.schema.id/dashboard](t2/query {:select [[:dcs.id]]:from [[:dashboardcard_series :dcs]]:join [[:report_dashboardcard :dc] [:= :dc.id :dcs.dashboardcard_id]]@@ -258,109 +272,120 @@[:= :dcs.card_id card-id][:not= :dc.dashboard_id dashboard-id]]}))-(defn delete-dashcard-series!- "Delete the DashboardCardSeries with `series-ids`."- [series-ids]+(mu/defn delete-dashcard-series!+ "Delete the DashboardCardSeries with `series-ids`, returning the number deleted."+ [series-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/DashboardCardSeries :id [:in series-ids]))-(defn implicit-action-ids-for-model+(mu/defn implicit-action-ids-for-model"The IDs of the implicit Actions of the model Card with `model-id`."- [model-id]+ [model-id :- ms/PositiveInt](t2/select-pks-set :model/Action {:select [:action.id]:from [:action]:join [:implicit_action [:= :action.id :implicit_action.action_id]]:where [:= :action.model_id model-id]}))-(defn delete-actions!- "Delete the Actions with `action-ids`."- [action-ids]+(mu/defn delete-actions!+ "Delete the Actions with `action-ids`, returning the number deleted."+ [action-ids :- [:set ::lib.schema.id/action]](t2/delete! :model/Action :id [:in action-ids]))-(defn archive-explicit-actions-for-model!- "Archive the non-implicit Actions of the model Card with `model-id`."- [model-id]+(mu/defn archive-explicit-actions-for-model!+ "Archive the non-implicit Actions of the model Card with `model-id`, returning the number updated."+ [model-id :- ms/PositiveInt](t2/update! :model/Action {:model_id model-id :type [:not= :implicit]} {:archived true}))-(defn delete-implicit-actions-for-model!- "Delete the implicit Actions of the model Card with `model-id`."- [model-id]+(mu/defn delete-implicit-actions-for-model!+ "Delete the implicit Actions of the model Card with `model-id`, returning the number deleted."+ [model-id :- ms/PositiveInt](t2/delete! :model/Action :model_id model-id :type :implicit))-(defn delete-card-moderation-reviews!- "Delete the ModerationReviews of the Card with `card-id`."- [card-id]+(mu/defn delete-card-moderation-reviews!+ "Delete the ModerationReviews of the Card with `card-id`, returning the number deleted."+ [card-id :- ::lib.schema.id/card](t2/delete! :model/ModerationReview :moderated_item_type "card" :moderated_item_id card-id))-(defn delete-card-revisions!- "Delete the Revisions of the Card with `card-id`."- [card-id]+(mu/defn delete-card-revisions!+ "Delete the Revisions of the Card with `card-id`, returning the number deleted."+ [card-id :- ::lib.schema.id/card](t2/delete! :model/Revision :model "Card" :model_id card-id))-(defn card-notification-ids+(mu/defn card-notification-ids"The IDs of the card Notifications attached to the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-pks-set :model/Notification:payload_type :notification/card:payload_id [:in ^:allow-subquery {:select [:id]:from [:notification_card]:where [:= :card_id card-id]}]))-(defn delete-notifications!- "Delete the Notifications with `notification-ids`."- [notification-ids]+(mu/defn delete-notifications!+ "Delete the Notifications with `notification-ids`, returning the number deleted."+ [notification-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/Notification :id [:in notification-ids]));;; --------------------------------------------- ParameterCard ----------------------------------------------(defn parameter-cards-for-card+(mu/defn parameter-cards-for-card"The ParameterCards drawing values from the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select :model/ParameterCard :card_id card-id))-(defn parameter-card-card-ids+(mu/defn parameter-card-card-ids"The set of Card IDs the parameters of the given parameterized object draw values from."- [object-type object-id]+ [object-type :- [:or :keyword :string]+ object-id :- ms/PositiveInt](t2/select-fn-set :card_id :model/ParameterCard:parameterized_object_type object-type:parameterized_object_id object-id))-(defn parameter-card-exists?+(mu/defn parameter-card-exists?"Whether the given parameter of the given parameterized object has a ParameterCard."- [object-type object-id parameter-id]+ [object-type :- [:or :keyword :string]+ object-id :- ms/PositiveInt+ parameter-id :- :string](t2/exists? :model/ParameterCard:parameterized_object_type object-type:parameterized_object_id object-id:parameter_id parameter-id))-(defn set-parameter-card-card-id!- "Point the ParameterCard of the given parameter of the given parameterized object at `card-id`."- [object-type object-id parameter-id card-id]+(mu/defn set-parameter-card-card-id!+ "Point the ParameterCard of the given parameter of the given parameterized object at `card-id`, returning the+ number updated."+ [object-type :- [:or :keyword :string]+ object-id :- ms/PositiveInt+ parameter-id :- :string+ card-id :- ::lib.schema.id/card](t2/update! :model/ParameterCard{:parameterized_object_type object-type:parameterized_object_id object-id:parameter_id parameter-id}{:card_id card-id}))-(defn insert-parameter-card!- "Insert `parameter-card`."- [parameter-card]+(mu/defn insert-parameter-card!+ "Insert `parameter-card`, returning the number inserted."+ [parameter-card :- ::queries.schema/parameter-card.update](t2/insert! :model/ParameterCard parameter-card))-(defn delete-parameter-cards-for-card!- "Delete the ParameterCards drawing values from the Card with `card-id`."- [card-id]+(mu/defn delete-parameter-cards-for-card!+ "Delete the ParameterCards drawing values from the Card with `card-id`, returning the number deleted."+ [card-id :- ::lib.schema.id/card](t2/delete! :model/ParameterCard :card_id card-id))-(defn delete-parameter-cards-for-object!- "Delete every ParameterCard of the given parameterized object."- [object-type object-id]+(mu/defn delete-parameter-cards-for-object!+ "Delete every ParameterCard of the given parameterized object, returning the number deleted."+ [object-type :- [:or :keyword :string]+ object-id :- ms/PositiveInt](t2/delete! :model/ParameterCard:parameterized_object_type object-type:parameterized_object_id object-id))-(defn delete-parameter-cards-for-object-except!- "Delete the ParameterCards of the given parameterized object whose parameter is not one of `parameter-ids`."- [object-type object-id parameter-ids]+(mu/defn delete-parameter-cards-for-object-except!+ "Delete the ParameterCards of the given parameterized object whose parameter is not one of `parameter-ids`,+ returning the number deleted."+ [object-type :- [:or :keyword :string]+ object-id :- ms/PositiveInt+ parameter-ids :- [:sequential :string]](t2/delete! :model/ParameterCard:parameterized_object_type object-type:parameterized_object_id object-id@@ -368,9 +393,9 @@;;; -------------------------------------------------- Query ---------------------------------------------------(defn average-execution-time+(mu/defn average-execution-time"The recorded average execution time of the query with `query-hash`."- [query-hash]+ [query-hash :- bytes?](t2/select-one-fn :average_execution_time :model/Query :query_hash query-hash))(defn- int-casting-type@@ -388,25 +413,30 @@c1)[:inline 0])))-(defn backfill-query-and-average-execution-time!+(mu/defn backfill-query-and-average-execution-time!"Set the query text and average execution time (`c0 * average_execution_time + c1`, rounded and cast to an integer)- of the query with `query-hash` if its text was never stored."- [query-hash query-json c0 c1]+ of the query with `query-hash` if its text was never stored, returning the number updated."+ [query-hash :- bytes?+ query-json :- :string+ c0 :- number?+ c1 :- number?](t2/update! :model/Query{:query_hash query-hash, :query nil}{:query query-json:average_execution_time (rolling-average-expr c0 c1)}))-(defn update-average-execution-time!+(mu/defn update-average-execution-time!"Set the average execution time of the query with `query-hash` to `c0 * average_execution_time + c1`, rounded and- cast to an integer."- [query-hash c0 c1]+ cast to an integer, returning the number updated."+ [query-hash :- bytes?+ c0 :- number?+ c1 :- number?](t2/update! :model/Query {:query_hash query-hash} {:average_execution_time (rolling-average-expr c0 c1)}))-(defn update-average-execution-times!+(mu/defn update-average-execution-times!"Set the average execution time of each Query identified by the `[query-hash c0 c1]` triples in `hash+coefficients`to `c0 * average_execution_time + c1`, rounded and cast to an integer."- [hash+coefficients]+ [hash+coefficients :- [:sequential [:tuple bytes? number? number?]]](t2/query {:update (t2/table-name :model/Query):set {:average_execution_time (into [:case](mapcat (fn [[query-hash c0 c1]]@@ -414,36 +444,38 @@hash+coefficients)}:where [:in :query_hash (map first hash+coefficients)]}))-(defn insert-queries!- "Insert the Query `rows`."- [rows]+(mu/defn insert-queries!+ "Insert the Query `rows`, returning the number inserted."+ [rows :- [:sequential :map]](t2/insert! :model/Query rows))-(defn query-hash-statuses-reducible+(mu/defn query-hash-statuses-reducible"Reducible rows of `:query_hash` and `:missing_query` for the Query rows with `query-hashes`."- [query-hashes]+ [query-hashes :- [:sequential bytes?]](t2/reducible-query {:select [:query_hash [[:= :query nil] :missing_query]]:from [(t2/table-name :model/Query)]:where [:in :query_hash query-hashes]}));;; ---------------------------------------------- StoredResult -----------------------------------------------(defn stored-result-ids-used-by-cards+(mu/defn stored-result-ids-used-by-cards"The subset of `stored-result-ids` used by one of the Cards with `card-ids`."- [card-ids stored-result-ids]+ [card-ids :- [:set ::lib.schema.id/card]+ stored-result-ids :- [:set ms/PositiveInt]](t2/select-fn-set :stored_result_id :model/StoredResultUse:card_id [:in card-ids]:stored_result_id [:in stored-result-ids]))-(defn insert-stored-result-use!- "Record that the Card with `card-id` uses the StoredResult with `stored-result-id`."- [stored-result-id card-id]+(mu/defn insert-stored-result-use!+ "Record that the Card with `card-id` uses the StoredResult with `stored-result-id`, returning the number inserted."+ [stored-result-id :- ms/PositiveInt+ card-id :- ::lib.schema.id/card](t2/insert! :model/StoredResultUse {:stored_result_id stored-result-id:card_id card-id}))-(defn stored-results-for-card+(mu/defn stored-results-for-card"The StoredResults used by the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select :model/StoredResult:id [:in ^:allow-subquery {:select [:stored_result_id]:from [:stored_result_use]diff --git a/src/metabase/queries/schema.clj b/src/metabase/queries/schema.cljindex f2e5fbdf655..a7b7ed2c2e8 100644--- a/src/metabase/queries/schema.clj+++ b/src/metabase/queries/schema.clj@@ -9,13 +9,14 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms][potemkin :as p]))(p/import-vars[lib.schema.metadatacard-types])-(mr/def ::card-type+(mr/def ::card.type::lib.schema.metadata/card.type)(mr/def ::card.result-metadata@@ -34,12 +35,6 @@nil)))}:any]])-;;; TODO (Cam 9/29/25) -- fill this out more, `:metabase.lib.schema.metadata/card` has a lot of stuff and there's also-;;; stuff sprinkled throughout this module. For example [[metabase.queries-rest.api.card/CardUpdateSchema]] should get merged-;;; into this-;;;-;;; TODO (Cam 9/30/25) -- consider renaming this to `:model/Card` so it can serve as the "official" schema of a Card-;;; instance(mr/def ::card"Schema for an instance of a `:model/Card` (everything is optional to support updates)."[:map@@ -53,10 +48,274 @@[:name {:optional true} [:maybe :string]][:parameters {:optional true} [:maybe [:ref ::parameters.schema/parameters]]][:parameter_mappings {:optional true} [:maybe [:ref ::parameters.schema/parameter-mappings]]]- [:type {:optional true} [:maybe ::lib.schema.metadata/card.type]]+ [:type {:optional true} [:maybe ::card.type]][:result_metadata {:optional true} [:maybe [:ref ::card.result-metadata]]]])(mu/defn normalize-card :- [:maybe ::card]"Normalize a `card` so it satisfies the `::card` schema."[card :- [:maybe :map]](lib/normalize ::card card))++(mr/def ::card.dataset-query+ "The `:dataset_query` column of a Card, decoded."+ :map)++(mr/def ::card.visualization-settings+ "The `:visualization_settings` column of a Card, decoded."+ :map)++(mr/def ::card.parameter+ "One entry of the `:parameters` column of a Card, decoded."+ :map)++(mr/def ::card.parameter-mapping+ "One entry of the `:parameter_mappings` column of a Card, decoded."+ :map)++(mr/def ::card.dimension+ "One entry of the `:dimensions` column of a Card, decoded."+ :map)++(mr/def ::card.dimension-mapping+ "One entry of the `:dimension_mappings` column of a Card, decoded."+ :map)++(mr/def ::card.update+ "What an update (or insert) of a Card accepts: every column of `:report_card` except `id`, all optional, plus `:verified-result-metadata?` consumed by the model's hooks."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:display {:optional true} [:maybe [:or :keyword :string]]]+ [:dataset_query {:optional true} [:maybe ::card.dataset-query]]+ [:visualization_settings {:optional true} [:maybe ::card.visualization-settings]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:query_type {:optional true} [:maybe [:or :keyword :string]]]+ [:archived {:optional true} [:maybe :boolean]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:public_uuid {:optional true} [:maybe :string]]+ [:made_public_by_id {:optional true} [:maybe ms/PositiveInt]]+ [:enable_embedding {:optional true} [:maybe :boolean]]+ [:embedding_params {:optional true} [:maybe ms/EmbeddingParams]]+ [:cache_ttl {:optional true} [:maybe :int]]+ [:result_metadata {:optional true} [:maybe ::card.result-metadata]]+ [:collection_position {:optional true} [:maybe :int]]+ [:entity_id {:optional true} [:maybe :string]]+ [:parameters {:optional true} [:maybe [:sequential ::card.parameter]]]+ [:parameter_mappings {:optional true} [:maybe [:sequential ::card.parameter-mapping]]]+ [:collection_preview {:optional true} [:maybe :boolean]]+ [:metabase_version {:optional true} [:maybe :string]]+ [:type {:optional true} [:maybe ::lib.schema.metadata/card.type]]+ [:initially_published_at {:optional true} [:maybe ms/TemporalInstant]]+ [:cache_invalidated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]+ [:view_count {:optional true} [:maybe :int]]+ [:archived_directly {:optional true} [:maybe :boolean]]+ [:dataset_query_metrics_v2_migration_backup {:optional true} [:maybe :string]]+ [:source_card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:card_schema {:optional true} [:maybe :int]]+ [:document_id {:optional true} [:maybe ::documents.schema/document.id]]+ [:legacy_query {:optional true} [:maybe :string]]+ [:embedding_type {:optional true} [:maybe [:or :keyword :string]]]+ [:public_uuid_prefix {:optional true} [:maybe :string]]+ [:dimensions {:optional true} [:maybe [:sequential ::card.dimension]]]+ [:dimension_mappings {:optional true} [:maybe [:sequential ::card.dimension-mapping]]]+ [:metabot_conversation_id {:optional true} [:maybe :string]]+ [:metabot_chart_id {:optional true} [:maybe :string]]+ [:verified-result-metadata? {:optional true} :boolean]])++(mr/def ::parameter-card+ "A ParameterCard as selected from the app DB: every column of `:parameter_card`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:updated_at ms/TemporalInstant]+ [:created_at ms/TemporalInstant]+ [:card_id ::lib.schema.id/card]+ [:parameterized_object_type [:or :keyword :string]]+ [:parameterized_object_id ms/PositiveInt]+ [:parameter_id :string]])++(mr/def ::parameter-card.update+ "What an update (or insert) of a ParameterCard accepts: every column of `:parameter_card` except `id`, all optional."+ [:map {:closed true}+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:parameterized_object_type {:optional true} [:maybe [:or :keyword :string]]]+ [:parameterized_object_id {:optional true} [:maybe ms/PositiveInt]]+ [:parameter_id {:optional true} [:maybe :string]]])++(mr/def ::query.query+ "The `:query` column of a Query, decoded."+ :map)++(mr/def ::query+ "A Query as selected from the app DB: every column of `:query`."+ [:map {:closed true}+ [:query_hash [:or bytes? :string]]+ [:average_execution_time :int]+ [:query [:maybe ::query.query]]])++(mr/def ::query.update+ "What an update (or insert) of a Query accepts: every column of `:query` except `id`, all optional."+ [:map {:closed true}+ [:query_hash {:optional true} [:maybe [:or bytes? :string]]]+ [:average_execution_time {:optional true} [:maybe :int]]+ [:query {:optional true} [:maybe ::query.query]]])++(mr/def ::query-execution.lens-params+ "The `:lens_params` column of a QueryExecution, decoded."+ :map)++(mr/def ::query-execution+ "A QueryExecution as selected from the app DB: every column of `:query_execution`, plus `:row_count` added by the model's after-select hook."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:hash [:or bytes? :string]]+ [:started_at ms/TemporalInstant]+ [:running_time :int]+ [:result_rows :int]+ [:native :boolean]+ [:context [:maybe [:or :keyword :string]]]+ [:error [:maybe :string]]+ [:executor_id [:maybe ::lib.schema.id/user]]+ [:card_id [:maybe ::lib.schema.id/card]]+ [:dashboard_id [:maybe ::lib.schema.id/dashboard]]+ [:pulse_id [:maybe ::lib.schema.id/pulse]]+ [:database_id [:maybe ::lib.schema.id/database]]+ [:cache_hit [:maybe :boolean]]+ [:action_id [:maybe ::lib.schema.id/action]]+ [:is_sandboxed [:maybe :boolean]]+ [:cache_hash [:maybe [:or bytes? :string]]]+ [:embedding_client [:maybe :string]]+ [:embedding_sdk_version [:maybe :string]]+ [:parameterized [:maybe :boolean]]+ [:transform_id [:maybe ::lib.schema.id/transform]]+ [:lens_id [:maybe :string]]+ [:lens_params [:maybe ::query-execution.lens-params]]+ [:auth_method [:maybe [:or :keyword :string]]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:is_impersonated [:maybe :boolean]]+ [:is_db_routed [:maybe :boolean]]+ [:parameters [:maybe :string]]+ [:embedding_hostname [:maybe :string]]+ [:embedding_path [:maybe :string]]+ [:user_agent [:maybe :string]]+ [:ip_address [:maybe :string]]+ [:sanitized_user_agent [:maybe :string]]+ [:embedding_route [:maybe :string]]+ [:metabase_version [:maybe :string]]+ [:embedding_client_identifier [:maybe :string]]+ [:row_count {:optional true} :int]])++(mr/def ::query-execution.update+ "What an update (or insert) of a QueryExecution accepts: every column of `:query_execution` except `id`, all optional."+ [:map {:closed true}+ [:hash {:optional true} [:maybe [:or bytes? :string]]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:running_time {:optional true} [:maybe :int]]+ [:result_rows {:optional true} [:maybe :int]]+ [:native {:optional true} [:maybe :boolean]]+ [:context {:optional true} [:maybe [:or :keyword :string]]]+ [:error {:optional true} [:maybe :string]]+ [:executor_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]+ [:pulse_id {:optional true} [:maybe ::lib.schema.id/pulse]]+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:cache_hit {:optional true} [:maybe :boolean]]+ [:action_id {:optional true} [:maybe ::lib.schema.id/action]]+ [:is_sandboxed {:optional true} [:maybe :boolean]]+ [:cache_hash {:optional true} [:maybe [:or bytes? :string]]]+ [:embedding_client {:optional true} [:maybe :string]]+ [:embedding_sdk_version {:optional true} [:maybe :string]]+ [:parameterized {:optional true} [:maybe :boolean]]+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:lens_id {:optional true} [:maybe :string]]+ [:lens_params {:optional true} [:maybe ::query-execution.lens-params]]+ [:auth_method {:optional true} [:maybe [:or :keyword :string]]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:is_impersonated {:optional true} [:maybe :boolean]]+ [:is_db_routed {:optional true} [:maybe :boolean]]+ [:parameters {:optional true} [:maybe :string]]+ [:embedding_hostname {:optional true} [:maybe :string]]+ [:embedding_path {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]+ [:sanitized_user_agent {:optional true} [:maybe :string]]+ [:embedding_route {:optional true} [:maybe :string]]+ [:metabase_version {:optional true} [:maybe :string]]+ [:embedding_client_identifier {:optional true} [:maybe :string]]])++(mr/def ::query-table+ "A QueryTable as selected from the app DB: every column of `:query_table`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:card_id ::lib.schema.id/card]+ [:table_id [:maybe ::lib.schema.id/table]]+ [:schema [:maybe :string]]+ [:table :string]])++(mr/def ::query-table.update+ "What an update (or insert) of a QueryTable accepts: every column of `:query_table` except `id`, all optional."+ [:map {:closed true}+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:schema {:optional true} [:maybe :string]]+ [:table {:optional true} [:maybe :string]]])++(mr/def ::stored-result.dataset-query+ "The `:dataset_query` column of a StoredResult, decoded."+ :map)++(mr/def ::stored-result.data-access-token+ "The `:data_access_token` column of a StoredResult, decoded."+ :map)++(mr/def ::stored-result+ "A StoredResult as selected from the app DB: every column of `:stored_result`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:result_data [:or bytes? :string]]+ [:creator_id [:maybe ::lib.schema.id/user]]+ [:database_id [:maybe ::lib.schema.id/database]]+ [:dataset_query ::stored-result.dataset-query]+ [:data_access_token [:maybe ::stored-result.data-access-token]]+ [:row_count [:maybe :int]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::stored-result.update+ "What an update (or insert) of a StoredResult accepts: every column of `:stored_result` except `id`, all optional."+ [:map {:closed true}+ [:result_data {:optional true} [:maybe [:or bytes? :string]]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:dataset_query {:optional true} [:maybe ::stored-result.dataset-query]]+ [:data_access_token {:optional true} [:maybe ::stored-result.data-access-token]]+ [:row_count {:optional true} [:maybe :int]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::stored-result-use+ "A StoredResultUse as selected from the app DB: every column of `:stored_result_use`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:stored_result_id ms/PositiveInt]+ [:exploration_id [:maybe ms/PositiveInt]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:card_id [:maybe ::lib.schema.id/card]]])++(mr/def ::stored-result-use.update+ "What an update (or insert) of a StoredResultUse accepts: every column of `:stored_result_use` except `id`, all optional."+ [:map {:closed true}+ [:stored_result_id {:optional true} [:maybe ms/PositiveInt]]+ [:exploration_id {:optional true} [:maybe ms/PositiveInt]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:card_id {:optional true} [:maybe ::lib.schema.id/card]]])diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.cljindex 95c7a5a119f..de5b6a0f431 100644--- a/src/metabase/queries_rest/api/card.clj+++ b/src/metabase/queries_rest/api/card.clj@@ -445,7 +445,7 @@(mu/defn- check-if-card-can-be-saved[dataset-query :- [:maybe ::lib-be.schema/maybe-legacy-query]- card-type :- [:maybe ::queries.schema/card-type]]+ card-type :- [:maybe ::queries.schema/card.type]](when (and (seq dataset-query) (= card-type :metric))(when-not (lib/can-save? dataset-query card-type)(throw (ex-info (tru "Card of type {0} is invalid, cannot be saved." (name card-type))@@ -483,7 +483,7 @@"Schema for creating a new card"[:map[:name ms/NonBlankString]- [:type {:optional true} [:maybe ::queries.schema/card-type]]+ [:type {:optional true} [:maybe ::queries.schema/card.type]][:dataset_query ::lib-be.schema/maybe-legacy-query];; TODO: Make entity_id a NanoID regex schema?[:entity_id {:optional true} [:maybe ms/NonBlankString]]@@ -605,7 +605,7 @@[:parameters {:optional true} [:maybe ::parameters.schema/parameters]][:parameter_mappings {:optional true} [:maybe ::parameters.schema/parameter-mappings]][:dataset_query {:optional true} [:maybe ::lib-be.schema/maybe-legacy-query]]- [:type {:optional true} [:maybe ::queries.schema/card-type]]+ [:type {:optional true} [:maybe ::queries.schema/card.type]][:display {:optional true} [:maybe ms/NonBlankString]][:description {:optional true} [:maybe :string]][:visualization_settings {:optional true} [:maybe ms/Map]]diff --git a/src/metabase/queries_rest/db.clj b/src/metabase/queries_rest/db.cljindex d7ccfc1ae1b..c9923f480c4 100644--- a/src/metabase/queries_rest/db.clj+++ b/src/metabase/queries_rest/db.clj@@ -3,49 +3,54 @@additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require[honey.sql.helpers :as sql.helpers]+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private order-by-name {:order-by [[:%lower.name :asc]]})-(defn unarchived-cards+(mu/defn unarchived-cards"The unarchived Cards, in case-insensitive name order."[](t2/select :model/Card, :archived false, order-by-name))-(defn unarchived-cards-by-creator+(mu/defn unarchived-cards-by-creator"The unarchived Cards created by the User with `creator-id`, in case-insensitive name order."- [creator-id]+ [creator-id :- ::lib.schema.id/user](t2/select :model/Card, :creator_id creator-id, :archived false, order-by-name))-(defn unarchived-cards-for-database+(mu/defn unarchived-cards-for-database"The unarchived Cards of the Database with `database-id`, in case-insensitive name order."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select :model/Card, :database_id database-id, :archived false, order-by-name))-(defn unarchived-cards-for-table+(mu/defn unarchived-cards-for-table"The unarchived Cards of the Table with `table-id`, in case-insensitive name order."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Card, :table_id table-id, :archived false, order-by-name))-(defn archived-cards+(mu/defn archived-cards"The archived Cards, in case-insensitive name order."[](t2/select :model/Card, :archived true, order-by-name))-(defn cards-with-query-like+(mu/defn cards-with-query-like"The Cards whose query matches the SQL LIKE `pattern`, in case-insensitive name order."- [pattern]+ [pattern :- :string](t2/select :model/Card (merge order-by-name {:where [:like :dataset_query pattern]})))-(defn card-bookmarks-for-user+(mu/defn card-bookmarks-for-user"The Card ids bookmarked by the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select [:model/CardBookmark :card_id] :user_id user-id))-(defn cards-using-model+(mu/defn cards-using-model"The unarchived Cards of the same Database as the model Card with `model-id` whose query mentions it, incase-insensitive name order."- [model-id]+ [model-id :- ms/PositiveInt](t2/select :model/Card {:select [:c.*]:from [[:report_card :m]]:join [[:report_card :c] [:and@@ -56,54 +61,59 @@:where [:and [:= :m.id model-id] [:not :c.archived]]:order-by [[[:lower :c.name] :asc]]}))-(defn public-cards+(mu/defn public-cards"The name, id, public uuid, and schema of the unarchived Cards that are publicly shared."[](t2/select [:model/Card :name :id :public_uuid :card_schema], :public_uuid [:not= nil], :archived false))-(defn embeddable-cards+(mu/defn embeddable-cards"The name, id, and schema of the unarchived Cards with embedding enabled."[](t2/select [:model/Card :name :id :card_schema], :enable_embedding true, :archived false))-(defn segment-database-id+(mu/defn segment-database-id"The Database id of the Table of the Segment with `segment-id`, or nil."- [segment-id]+ [segment-id :- ::lib.schema.id/segment](t2/select-one-fn :db_id :model/Table {:select [:t.db_id]:from [[:metabase_table :t]]:join [[:segment :m] [:= :t.id :m.table_id]]:where [:= :m.id segment-id]}))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table, :id table-id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn cards-by-id+(mu/defn cards-by-id"A map of Card id to Card for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:sequential ::lib.schema.id/card]](t2/select-fn->fn :id identity :model/Card :id [:in card-ids]))-(defn card-query+(mu/defn card-query"The query of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :dataset_query :model/Card :id card-id))-(defn card-public-uuid-columns+(mu/defn card-public-uuid-columns"The public uuid and schema of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :public_uuid :card_schema] :id card-id))-(defn compatible-series-cards+(mu/defn compatible-series-cards"The unarchived Cards other than `excluded-card-id` displayed as one of `display-types`, newest first, whose id isless than `last-cursor` (when given), excluding `exclude-ids`, and whose lower-cased name matches the SQL LIKE`name-pattern` (when given). Up to `limit` results (when given)."- [excluded-card-id display-types last-cursor exclude-ids name-pattern limit]+ [excluded-card-id :- ::lib.schema.id/card+ display-types :- [:set :keyword]+ last-cursor :- [:maybe ms/PositiveInt]+ exclude-ids :- [:maybe [:sequential ms/PositiveInt]]+ name-pattern :- [:maybe [:or :string vector?]]+ limit :- [:maybe ms/PositiveInt]](t2/select :model/Card:archived false:display [:in display-types]@@ -121,42 +131,45 @@limit(assoc :limit limit))))-(defn dashboard-collection-id+(mu/defn dashboard-collection-id"The Collection id of the Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one-fn :collection_id [:model/Dashboard :collection_id] dashboard-id))-(defn update-card!+(mu/defn update-card!"Apply `changes` to the Card with `card-id`."- [card-id changes]+ [card-id :- ::lib.schema.id/card+ changes :- (mut/select-keys ::queries.schema/card.update [:collection_position :collection_id :public_uuid :made_public_by_id])](t2/update! :model/Card card-id changes))-(defn delete-card!+(mu/defn delete-card!"Delete the Card with `card-id`."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/delete! :model/Card :id card-id))-(defn max-collection-position+(mu/defn max-collection-position"The `:max_position` of the Cards in the Collection with `collection-id` (nil for the root)."- [collection-id]+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-one [:model/Card [:%max.collection_position :max_position]] :collection_id collection-id))-(defn cards-to-move-to-collection+(mu/defn cards-to-move-to-collection"The id, Collection, position, query, and schema of the Cards among `card-ids` not already in`new-collection-id-or-nil`."- [card-ids new-collection-id-or-nil]+ [card-ids :- [:set ::lib.schema.id/card]+ new-collection-id-or-nil :- [:maybe ms/PositiveInt]](t2/select [:model/Card :id :collection_id :collection_position :dataset_query :card_schema]{:where [:and [:in :id card-ids][:or [:not= :collection_id new-collection-id-or-nil](when new-collection-id-or-nil[:= :collection_id nil])]]}))-(defn set-cards-collection-raw!+(mu/defn set-cards-collection-raw!"Move the Cards with `card-ids` to the Collection with `collection-id` without running model hooks."- [card-ids collection-id]+ [card-ids :- [:set ::lib.schema.id/card]+ collection-id :- [:maybe ::lib.schema.id/collection]](t2/update! (t2/table-name :model/Card) {:id [:in card-ids]} {:collection_id collection-id}))-(defn stored-result+(mu/defn stored-result"The StoredResult with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/StoredResult :id id))diff --git a/src/metabase/query_permissions/db.clj b/src/metabase/query_permissions/db.cljindex 775b8031f3a..68da545024e 100644--- a/src/metabase/query_permissions/db.clj+++ b/src/metabase/query_permissions/db.clj@@ -2,24 +2,27 @@"Application database queries for the query permissions module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn card-collection-id+(mu/defn card-collection-id"The `:collection_id` and `:card_schema` of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :collection_id :card_schema] :id card-id))-(defn card-not-in-database+(mu/defn card-not-in-database"The Card with `card-id` if it does not belong to the Database with `database-id`, or nil."- [card-id database-id]+ [card-id :- ::lib.schema.id/card+ database-id :- ::lib.schema.id/database](t2/select-one :model/Card :id card-id :database_id [:!= database-id]))-(defn field-table-ids+(mu/defn field-table-ids"The set of Table ids of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:or [:set ::lib.schema.id/field] [:sequential ::lib.schema.id/field]]](t2/select-fn-set :table_id :model/Field :id [:in field-ids]))-(defn table-id->database-id+(mu/defn table-id->database-id"A map of Table id to Database id for the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-pk->fn :db_id :model/Table :id [:in table-ids]))diff --git a/src/metabase/query_processor/db.clj b/src/metabase/query_processor/db.cljindex d3d83f47ee6..f68fdabbc1b 100644--- a/src/metabase/query_processor/db.clj+++ b/src/metabase/query_processor/db.clj@@ -4,62 +4,76 @@(:require[java-time.api :as t][metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2]))-(defn table+(def ^:private Temporal+ "A `java.time` instant, date, or date-time."+ [:fn #(instance? java.time.temporal.Temporal %)])++(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn source-card-metadata+(mu/defn source-card-metadata"The entity id, result metadata, and type of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :entity_id :result_metadata :type :card_schema] :id card-id))-(defn dashcard-series-exists?+(mu/defn dashcard-series-exists?"Whether the Card with `card-id` is a series of the DashboardCard with `dashcard-id`."- [card-id dashcard-id]+ [card-id :- [:maybe ::lib.schema.id/card]+ dashcard-id :- [:maybe ::lib.schema.id/dashcard]](t2/exists? :model/DashboardCardSeries :card_id card-id :dashboardcard_id dashcard-id))-(defn cache-entry+(mu/defn cache-entry"`row-fn` applied to the results and updated-at of the QueryCache entry for `query-hash`, or nil."- [row-fn query-hash]+ [row-fn :- fn?+ query-hash :- bytes?](t2/select-one-fn row-fn [:model/QueryCache :results :updated_at] :query_hash query-hash))-(defn claim-cache-refresh-lease!+(mu/defn claim-cache-refresh-lease!"Set `refresh_started_at` of the QueryCache entry for `query-hash` to `started-at` if its lease (defaulting to`lease-free-sentinel`) is older than `lease-cutoff`, returning the number of rows updated."- [query-hash lease-free-sentinel lease-cutoff started-at]+ [query-hash :- bytes?+ lease-free-sentinel :- Temporal+ lease-cutoff :- Temporal+ started-at :- Temporal](t2/update! (t2/table-name :model/QueryCache){:query_hash query-hash[:coalesce :refresh_started_at lease-free-sentinel] [:< lease-cutoff]}{:refresh_started_at started-at}))-(defn delete-cache-entry!+(mu/defn delete-cache-entry!"Delete the QueryCache entry for `query-hash`."- [query-hash]+ [query-hash :- bytes?](t2/delete! (t2/table-name :model/QueryCache) :query_hash query-hash))-(defn delete-cache-entries-updated-before!+(mu/defn delete-cache-entries-updated-before!"Delete the QueryCache entries last updated at or before `updated-before`."- [updated-before]+ [updated-before :- Temporal](t2/delete! (t2/table-name :model/QueryCache) :updated_at [:<= updated-before]))-(defn insert-query-executions!+(mu/defn insert-query-executions!"Insert the QueryExecution `rows`."- [rows]+ [rows :- [:sequential+ ::queries.schema/query-execution.update]](t2/insert! :model/QueryExecution rows))-(defn set-card-result-metadata!+(mu/defn set-card-result-metadata!"Set the result metadata of the Card with `card-id` without touching `updated_at`."- [card-id result-metadata]+ [card-id :- ::lib.schema.id/card+ result-metadata :- [:maybe [:sequential :map]]](t2/update! :model/Card card-id {:result_metadata result-metadata:updated_at :updated_at}))-(defn update-cards-last-used-at!+(mu/defn update-cards-last-used-at!"Move `last_used_at` of each Card in `card-id->timestamp` forward to its timestamp, without touching `updated_at`."- [card-id->timestamp]+ [card-id->timestamp :- [:map-of ::lib.schema.id/card Temporal]](t2/query {:update [(t2/table-name :model/Card)]:where [:in :id (keys card-id->timestamp)]:set {:last_used_at (into [:case]@@ -68,15 +82,17 @@card-id->timestamp)):updated_at :updated_at}}))-(defn card-database-ids+(mu/defn card-database-ids"The `:id`, `:database_id`, and `:card_schema` of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card :id :database_id :card_schema] :id [:in card-ids]))-(defn upsert-cache-entry!+(mu/defn upsert-cache-entry!"Insert or update the QueryCache entry for `query-hash`, setting `:results` to `results` and `:updated_at` to- `timestamp`, and clearing `:refresh_started_at`."- [query-hash timestamp results]+ `timestamp`, and clearing `:refresh_started_at`. Returns the entry's primary key, which is the hash bytes."+ [query-hash :- bytes?+ timestamp :- Temporal+ results :- bytes?](app-db/update-or-insert! :model/QueryCache {:query_hash query-hash}(constantly {:updated_at timestamp:results resultsdiff --git a/src/metabase/remote_sync/db.clj b/src/metabase/remote_sync/db.cljindex 3fadd19c036..9d0f3a840bd 100644--- a/src/metabase/remote_sync/db.clj+++ b/src/metabase/remote_sync/db.clj@@ -2,16 +2,20 @@"Application database queries for the remote sync module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (hydration definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn remote-synced-collection-ids+(mu/defn remote-synced-collection-ids"The ids among `collection-ids` of Collections that are remote synced, or nil."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select-pks-set :model/Collection :id [:in collection-ids] :is_remote_synced true))-(defn is-remote-synced-by-id+(mu/defn is-remote-synced-by-id"The `:id` and the containing Collection's `:is_remote_synced` of the `model` rows with `ids`."- [model ids]+ [model :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/select [model :id [:c.is_remote_synced :is_remote_synced]]{:where [:in (keyword (str (name (t2/table-name model)) ".id")) ids]:join [[:collection :c] [:= :collection_id :c.id]]}))diff --git a/src/metabase/request/db.clj b/src/metabase/request/db.cljindex fe279da38fa..87777a4a25f 100644--- a/src/metabase/request/db.clj+++ b/src/metabase/request/db.clj@@ -2,21 +2,23 @@"Application database queries for the request module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.users.models.user :as user]+ [metabase.util.malli :as mu][toucan2.core :as t2]))(def ^:private current-user-columns;; `:type` is needed so [[user/add-attributes]] can refuse to add attributes to non-personal (API-key/internal) users(into [:model/User :type] user/admin-or-self-visible-columns))-(defn current-user+(mu/defn current-user"The User with `user-id` with the columns the current user may see of themselves, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one current-user-columns :id user-id))-(defn current-user-for-id+(mu/defn current-user-for-id"The User with `user-id` as `{:metabase-user-id :is-superuser? :is-data-analyst? :user-locale :settings}`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User[:id :metabase-user-id][:is_superuser :is-superuser?]diff --git a/src/metabase/revisions/db.clj b/src/metabase/revisions/db.cljindex 3d2f7facbf9..6ae593fa8d7 100644--- a/src/metabase/revisions/db.clj+++ b/src/metabase/revisions/db.clj@@ -2,72 +2,85 @@"Application database queries for the revisions module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn entity- "The `model` row with `id`, or nil."- [model id]+(mu/defn entity+ "The `model` row with `id`, or nil. `model` is a generic `:model/X` keyword — revisions track many kinds of+ entities (Card, Dashboard, Segment, ...) — so the returned row can't be tied to a single Instance schema."+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one model :id id))-(defn raw-row- "The `model` row with `id` read straight from its table, or nil."- [model id]+(mu/defn raw-row+ "The `model` row with `id` read straight from its table, or nil. See [[entity]] for why `model` is generic."+ [model :- :keyword+ id :- ms/PositiveInt](t2/select-one (t2/table-name model) :id id))-(defn update-entity!- "Apply `changes` to the `model` row with `id`."- [model id changes]+(mu/defn update-entity!+ "Apply `changes` to the `model` row with `id`, returning the number updated. `changes` is a column map whose shape+ can't be pinned to one model because `model` varies (see [[entity]])."+ [model :- :keyword+ id :- ms/PositiveInt+ changes :- [:map-of :keyword [:maybe :some]]](t2/update! model id changes))-(defn parameter-card-ids+(mu/defn parameter-card-ids"The Card ids of the ParameterCards of the `parameterized-object-type` with `parameterized-object-id`."- [parameterized-object-type parameterized-object-id]+ [parameterized-object-type :- :string+ parameterized-object-id :- ms/PositiveInt](t2/select-fn-vec :card_id :model/ParameterCard:parameterized_object_type parameterized-object-type:parameterized_object_id parameterized-object-id))-(defn dashboard-card-ids+(mu/defn dashboard-card-ids"The Card ids of the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :card_id :model/DashboardCard :dashboard_id dashboard-id))-(defn dashboard-series-card-ids+(mu/defn dashboard-series-card-ids"The Card ids of the DashboardCardSeries of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-vec :card_id :model/DashboardCardSeries{:where [:in :dashboardcard_id^:allow-subquery {:select [:id]:from [(t2/table-name :model/DashboardCard)]:where [:= :dashboard_id dashboard-id]}]}))-(defn card-queries+(mu/defn card-queries"A map of Card id to query for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn :dataset_query :model/Card :id [:in card-ids]))-(defn revision+(mu/defn revision"The Revision with `revision-id` of the `model-name` row with `model-id`, or nil."- [model-name model-id revision-id]+ [model-name :- :string+ model-id :- ms/PositiveInt+ revision-id :- ms/PositiveInt](t2/select-one :model/Revision :model model-name, :model_id model-id, :id revision-id))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn dashcards+(mu/defn dashcards"The DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardCard :dashboard_id dashboard-id))-(defn dashboard-tabs+(mu/defn dashboard-tabs"The DashboardTabs of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select :model/DashboardTab :dashboard_id dashboard-id))-(defn active-card-ids-for-dashboard+(mu/defn active-card-ids-for-dashboard"The ids among `card-ids` of unarchived Cards that belong to the Dashboard with `dashboard-id` or to no Dashboard."- [card-ids dashboard-id]+ [card-ids :- [:sequential ::lib.schema.id/card]+ dashboard-id :- ::lib.schema.id/dashboard](t2/select-pks-set :model/Card{:where [:and[:in :id card-ids]@@ -76,14 +89,15 @@[:= :dashboard_id dashboard-id][:= :dashboard_id nil]]]}))-(defn active-card-ids+(mu/defn active-card-ids"The ids among `card-ids` of unarchived Cards."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pks-set :model/Card {:where [:and [:in :id card-ids] [:= :archived false]]}))-(defn lock-revisions!+(mu/defn lock-revisions!"Lock the Revisions of the `model-name` row with `model-id` for update."- [model-name model-id]+ [model-name :- :string+ model-id :- ms/PositiveInt](t2/query {:select [:id]:from [:revision]:where [:and@@ -91,62 +105,82 @@[:= :model_id model-id]]:for :update}))-(defn revision-ids-newest-first+(mu/defn revision-ids-newest-first"The ids of the Revisions of the `model-name` row with `model-id`, newest first."- [model-name model-id]+ [model-name :- :string+ model-id :- ms/PositiveInt](t2/select-fn-vec :id :model/Revision:model model-name:model_id model-id{:order-by [[:timestamp :desc][:id :desc]]}))-(defn delete-revisions!- "Delete the Revisions with `ids`."- [ids]+(mu/defn delete-revisions!+ "Delete the Revisions with `ids`, returning the number deleted."+ [ids :- [:sequential ms/PositiveInt]](t2/delete! :model/Revision :id [:in ids]))-(defn unmark-most-recent-revisions!- "Clear `most_recent` on the Revisions of the `model-name` row with `model-id` other than `revision-id`."- [model-name model-id revision-id]+(mu/defn unmark-most-recent-revisions!+ "Clear `most_recent` on the Revisions of the `model-name` row with `model-id` other than `revision-id`, returning+ the number updated."+ [model-name :- :string+ model-id :- ms/PositiveInt+ revision-id :- ms/PositiveInt];; updates the table directly: revisions are otherwise immutable(t2/update! (t2/table-name :model/Revision){:model model-name :model_id model-id :most_recent true :id [:not= revision-id]}{:most_recent false}))-(defn revisions+(mu/defn revisions"The Revisions of the `model-name` row with `model-id`, newest first."- [model-name model-id]+ [model-name :- :string+ model-id :- ms/PositiveInt](t2/select :model/Revision :model model-name :model_id model-id {:order-by [[:id :desc]]}))-(defn latest-revision+(mu/defn latest-revision"The newest Revision of the `model-name` row with `model-id`, or nil."- [model-name model-id]+ [model-name :- :string+ model-id :- ms/PositiveInt](t2/select-one :model/Revision :model model-name, :model_id model-id, {:order-by [[:id :desc]]}))-(defn latest-revision-object+(mu/defn latest-revision-object"The serialized object of the newest Revision of the `model-name` row with `model-id`, or nil."- [model-name model-id]+ [model-name :- :string+ model-id :- ms/PositiveInt](t2/select-one-fn :object :model/Revision :model model-name :model_id model-id {:order-by [[:id :desc]]}))-(defn revision-object+(mu/defn revision-object"The serialized object of the Revision with `revision-id` of the `model-name` row with `model-id`, or nil."- [model-name model-id revision-id]+ [model-name :- :string+ model-id :- ms/PositiveInt+ revision-id :- ms/PositiveInt](t2/select-one-fn :object :model/Revision :model model-name :model_id model-id :id revision-id))-(defn insert-revision!- "Insert the Revision `row`."- [row]+(def ^:private RevisionRow+ [:map {:closed true}+ [:model [:or :keyword :string]]+ [:model_id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:object :map]+ [:is_creation :boolean]+ [:is_reversion :boolean]+ [:message {:optional true} [:maybe :string]]])++(mu/defn insert-revision!+ "Insert the Revision `row`, returning the number inserted."+ [row :- RevisionRow](t2/insert! :model/Revision row))-(defn insert-revision-returning!+(mu/defn insert-revision-returning!"Insert the Revision `row` and return the inserted instance."- [row]+ [row :- RevisionRow](t2/insert-returning-instance! :model/Revision row))-(defn latest-editors-reducible+(mu/defn latest-editors-reducible"A reducible of the model id, editing User, and timestamp of the most recent Revisions of the `db-model` rows with`ids`."- [db-model ids]+ [db-model :- :string+ ids :- [:set ms/PositiveInt]](t2/reducible-query{:select [:r.model_id :u.id :u.email :u.first_name :u.last_name :r.timestamp]:from [[:revision :r]]@@ -156,10 +190,11 @@[:= :r.model db-model][:in :r.model_id ids]]}))-(defn latest-changes+(mu/defn latest-changes"The editing User, model, model id, and timestamp of the most recent Revisions of the Cards with `card-ids`and/or the Dashboards with `dashboard-ids`."- [card-ids dashboard-ids]+ [card-ids :- [:maybe [:sequential ::lib.schema.id/card]]+ dashboard-ids :- [:maybe [:sequential ::lib.schema.id/dashboard]]](t2/query {:select [:u.id :u.email :u.first_name :u.last_name:r.model :r.model_id :r.timestamp]:from [[:revision :r]]diff --git a/src/metabase/revisions/schema.clj b/src/metabase/revisions/schema.cljnew file mode 100644index 00000000000..cc1fd8850cf--- /dev/null+++ b/src/metabase/revisions/schema.clj@@ -0,0 +1,39 @@+(ns metabase.revisions.schema+ "Malli schemas for the revisions module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::revision.object+ "The `:object` column of a Revision, decoded."+ :map)++(mr/def ::revision+ "A Revision as selected from the app DB: every column of `:revision`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:model [:or :keyword :string]]+ [:model_id [:maybe :int]]+ [:user_id ::lib.schema.id/user]+ [:timestamp ms/TemporalInstant]+ [:object ::revision.object]+ [:is_reversion :boolean]+ [:is_creation :boolean]+ [:message [:maybe :string]]+ [:most_recent :boolean]+ [:metabase_version [:maybe :string]]])++(mr/def ::revision.update+ "What an update (or insert) of a Revision accepts: every column of `:revision` except `id`, all optional."+ [:map {:closed true}+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:object {:optional true} [:maybe ::revision.object]]+ [:is_reversion {:optional true} [:maybe :boolean]]+ [:is_creation {:optional true} [:maybe :boolean]]+ [:message {:optional true} [:maybe :string]]+ [:most_recent {:optional true} [:maybe :boolean]]+ [:metabase_version {:optional true} [:maybe :string]]])diff --git a/src/metabase/run_tracking/core.clj b/src/metabase/run_tracking/core.cljindex 6c46c5650fc..e7f5bfe163d 100644--- a/src/metabase/run_tracking/core.clj+++ b/src/metabase/run_tracking/core.clj@@ -10,7 +10,6 @@(p/import-vars[metabase.run-tracking.ops- cutoffdetection-latency-msheartbeat-and-reconcile!heartbeat-ids!diff --git a/src/metabase/run_tracking/db.clj b/src/metabase/run_tracking/db.cljindex 9a9d8105a1d..f8394b89aad 100644--- a/src/metabase/run_tracking/db.clj+++ b/src/metabase/run_tracking/db.clj@@ -2,28 +2,83 @@"Application database queries for the run tracking module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.app-db.core :as mdb]+ [metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn heartbeat!- "Set `heartbeat-column` to now on the `model` rows in `ids` matching the Honey SQL predicate `active`."- [model active heartbeat-column ids]+(def ^:private ActivePredicate+ "An `[column value]` pair identifying the 'still active/open' state for a run-tracked model, e.g.+ `[:is_active true]` or `[:status \"started\"]`. Plain data; db.clj turns it into the `[:= column value]`+ Honey SQL clause."+ [:tuple :keyword [:or :string :keyword :boolean :int]])++(def ^:private StaleUnit+ [:enum :second :minute :hour])++(def ^:private StaleSpec+ "A single staleness cutoff: a `model` row is stale under this spec when `column` is older than `age`+ `unit`s ago."+ [:map+ [:column :keyword]+ [:age pos-int?]+ [:unit StaleUnit]])++(defn- active-clause+ [[column value]]+ [:= column value])++(defn- cutoff+ "Honey SQL form for `(now - age unit)` in the app-db dialect."+ [age unit]+ (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit))++(defn- stale-clause+ "ORs together the `[:< column (cutoff age unit)]` predicate for each spec in `stale-specs`."+ [stale-specs]+ (let [clauses (mapv (fn [{:keys [column age unit]}]+ [:< column (cutoff age unit)])+ stale-specs)]+ (if (= 1 (count clauses))+ (first clauses)+ (into [:or] clauses))))++(mu/defn heartbeat!+ "Set `heartbeat-column` to now on the `model` rows in `ids` matching `active`, returning the number updated."+ [model :- :keyword+ active :- ActivePredicate+ heartbeat-column :- :keyword+ ids :- [:sequential ms/PositiveInt]](t2/query {:update (t2/table-name model):set {heartbeat-column :%now}- :where [:and active [:in :id ids]]}))+ :where [:and (active-clause active) [:in :id ids]]}))++(mu/defn active-ids+ "The ids among `ids` of the `model` rows matching `active`."+ [model :- :keyword+ active :- ActivePredicate+ ids :- [:sequential ms/PositiveInt]]+ (t2/select-fn-set :id model {:where [:and [:in :id ids] (active-clause active)]}))-(defn active-ids- "The ids among `ids` of the `model` rows matching the Honey SQL predicate `active`."- [model active ids]- (t2/select-fn-set :id model {:where [:and [:in :id ids] active]}))+(mu/defn lock-active-stale-rows+ "The `model` rows matching `active` and any one of `stale`, locked for update."+ [model :- :keyword+ active :- ActivePredicate+ stale :- [:sequential {:min 1} StaleSpec]]+ (t2/select model {:where [:and (active-clause active) (stale-clause stale)] :for :update}))-(defn lock-active-stale-rows- "The `model` rows matching the Honey SQL predicates `active` and `stale`, locked for update."- [model active stale]- (t2/select model {:where [:and active stale] :for :update}))+(mu/defn set-terminal!+ "Apply the `terminal` column values to the `model` rows in `ids` matching `active`, returning the number updated.-(defn set-terminal!- "Apply the `terminal` column values to the `model` rows in `ids` matching the Honey SQL predicate `active`."- [model active ids terminal]+ `terminal` is typed as a generic column -> value map rather than a closed schema because the columns are+ model-specific (TaskRun, TransformRun, TransformJobRun, TransformDagRun each have their own terminal-state+ columns, e.g. `{:status \"timeout\" :end_time :%now :is_active nil :message m}`); callers are trusted to pass+ real column names for `model`."+ [model :- :keyword+ active :- ActivePredicate+ ids :- [:sequential ms/PositiveInt]+ terminal :- [:map-of :keyword [:maybe [:or :string :keyword :boolean :int]]]](t2/query {:update (t2/table-name model):set terminal- :where [:and active [:in :id ids]]}))+ :where [:and (active-clause active) [:in :id ids]]}))diff --git a/src/metabase/run_tracking/ops.clj b/src/metabase/run_tracking/ops.cljindex 012a8866ba6..6f7ec71150c 100644--- a/src/metabase/run_tracking/ops.clj+++ b/src/metabase/run_tracking/ops.clj@@ -2,20 +2,13 @@"Data operations for run-tracking heartbeats and orphan reaping."(:require[metabase.analytics-interface.core :as analytics]- [metabase.app-db.core :as mdb][metabase.run-tracking.db :as run-tracking.db]- [metabase.util.honey-sql-2 :as h2x][toucan2.core :as t2])(:import(java.time OffsetDateTime)))(set! *warn-on-reflection* true)-(defn cutoff- "Honeysql form for `(now - age unit)` in the app-db dialect."- [age unit]- (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit))-(defn unit->ms"Convert `age` of `unit` (`:second`, `:minute`, or `:hour`) to milliseconds."[age unit]@@ -32,14 +25,14 @@(defn heartbeat-ids!"Stamp `heartbeat-column = now` on the `active` rows of `model` in `ids`; no-op on empty `ids`.- `active` is a HoneySQL predicate, e.g. `[:= :is_active true]`."+ `active` is an `[column value]` pair, e.g. `[:is_active true]`."[model active heartbeat-column ids](when (seq ids)(run-tracking.db/heartbeat! model active heartbeat-column ids)))(defn heartbeat-and-reconcile!"Per-node tick for the runs this process owns: call `(heartbeat! ids)`, then `(on-gone id)` for- each id whose row no longer matches `active` (a HoneySQL predicate, e.g. `[:= :is_active true]`).+ each id whose row no longer matches `active` (an `[column value]` pair, e.g. `[:is_active true]`).`on-gone` runs on the shared heartbeat thread and must not block: a slow callback delaysheartbeats for every run on this node, leaving them to be reaped as stale."@@ -52,10 +45,12 @@(on-gone id)))))(defn reap-rows!- "Atomically move the `active` rows of `model` matching the `stale` honeysql predicate into- `terminal`, returning the pre-update rows. `SELECT … FOR UPDATE` + `UPDATE` in one transaction,- so the returned rows are exactly those transitioned.- `active` is a HoneySQL predicate, e.g. `[:= :is_active true]`."+ "Atomically move the `active` rows of `model` matching any of the `stale` cutoffs into `terminal`,+ returning the pre-update rows. `SELECT … FOR UPDATE` + `UPDATE` in one transaction, so the returned rows+ are exactly those transitioned.+ `active` is an `[column value]` pair, e.g. `[:is_active true]`. `stale` is a (non-empty) sequence of+ `{:column :age :unit}` staleness cutoffs, ORed together, e.g. `[{:column :last_heartbeat :age 5 :unit+ :minute}]`."[{:keys [model active stale terminal]}](t2/with-transaction [_conn](when-let [rows (not-empty (run-tracking.db/lock-active-stale-rows model active stale))]diff --git a/src/metabase/sample_data/db.clj b/src/metabase/sample_data/db.cljindex db039163d91..38e0bf5181d 100644--- a/src/metabase/sample_data/db.clj+++ b/src/metabase/sample_data/db.clj@@ -2,56 +2,65 @@"Application database queries for the sample data module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn sample-database-exists?+(mu/defn sample-database-exists?"Whether a sample Database exists."[](t2/exists? :model/Database :is_sample true))-(defn sample-database+(mu/defn sample-database"The sample Database, or nil."[](t2/select-one :model/Database :is_sample true))-(defn sample-database-id+(mu/defn sample-database-id"The id of the sample Database, or nil."[](t2/select-one-pk :model/Database :is_sample true))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn set-sample-database-details!+(mu/defn set-sample-database-details!"Set the `details` of the sample Database, returning the ids of the updated rows."- [details]+ [details :- ::warehouses.schema/database.details](t2/update-returning-pks! :model/Database :is_sample true {:details details}))-(defn insert-sample-database!+(mu/defn insert-sample-database!"Insert the sample Database and return the inserted instance."- [database-name details engine]+ [database-name :- :string+ details :- ::warehouses.schema/database.details+ engine :- :keyword](t2/insert-returning-instance! :model/Database:name database-name:details details:engine engine:is_sample true))-(defn update-database!- "Apply `changes` to the Database with `database-id`."- [database-id changes]+(mu/defn update-database!+ "Apply `changes` to the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ changes :- (mut/select-keys ::warehouses.schema/database.update [:engine :details :settings])](t2/update! :model/Database database-id changes))-(defn set-database-tables-schema!- "Set the `schema` of every Table of the Database with `database-id`."- [database-id schema]+(mu/defn set-database-tables-schema!+ "Set the `schema` of every Table of the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/update! :model/Table :db_id database-id {:schema schema}))-(defn set-database-table-permissions-schema-name!+(mu/defn set-database-table-permissions-schema-name!"Set the `schema_name` of the table-level DataPermissions rows of the Database with `database-id`, via a raw tableupdate because the model's before-update rejects all updates."- [database-id schema-name]+ [database-id :- ::lib.schema.id/database+ schema-name :- [:maybe :string]](t2/query {:update (t2/table-name :model/DataPermissions):set {:schema_name schema-name}:where [:anddiff --git a/src/metabase/search/appdb/core.clj b/src/metabase/search/appdb/core.cljindex 63588f4f99f..0d7f9988cf7 100644--- a/src/metabase/search/appdb/core.clj+++ b/src/metabase/search/appdb/core.clj@@ -1,13 +1,14 @@(ns metabase.search.appdb.core(:require+ [clojure.core.memoize :as memoize][clojure.string :as str][environ.core :as env]- [honey.sql.helpers :as sql.helpers][java-time.api :as t][metabase.app-db.core :as mdb][metabase.config.core :as config][metabase.events.core :as events][metabase.search.appdb.index :as search.index]+ [metabase.search.appdb.query :as appdb.query][metabase.search.appdb.scoring :as search.scoring][metabase.search.appdb.specialization.postgres :as specialization.postgres][metabase.search.config :as search.config]@@ -17,7 +18,6 @@[metabase.search.hierarchy :as search.hierarchy][metabase.search.impl :as search.impl][metabase.search.ingestion :as search.ingestion]- [metabase.search.permissions :as search.permissions][metabase.search.spec :as search.spec][metabase.search.util :as search.util][metabase.settings.core :as setting]@@ -72,65 +72,18 @@(update :updated_at parse-datetime)(update :last_edited_at parse-datetime)))-(defn add-table-where-clauses- "Add a `WHERE` clause to the query to only return tables the current user has access to.- Also adds any CTEs required for permission filtering."- [search-ctx qry]- (let [model-id-col [:cast :search_index.model_id (case (mdb/db-type)- :mysql :signed- :integer)]- {:keys [with clause]} (search.permissions/permitted-tables-clause search-ctx model-id-col)]- (cond-> qry- (seq with) (update :with (fnil into []) with)- true (sql.helpers/where- [:or- [:= :search_index.model nil]- [:!= :search_index.model "table"]- [:and- [:= :search_index.model "table"]- clause]]))))+(defn- view-count-percentiles*+ [p-value]+ (into {} (for [{:keys [model vcp]} (search.db/view-count-percentile-rows (search.index/active-table) p-value)]+ [(keyword model) vcp])))-(defn add-collection-join-and-where-clauses- "Add a `WHERE` clause to the query to only return Collections the Current User has access to; join against Collection,- so we can return its `:name`."- [search-ctx qry]- (let [collection-id-col :search_index.collection_id- permitted-clause (search.permissions/permitted-collections-clause search-ctx collection-id-col)- personal-clause (search.filter/personal-collections-where-clause search-ctx collection-id-col)- ;; Tables have their own dedicated permission filter (add-table-where-clauses) that checks both data- ;; permissions and published-via-collection access, so we exclude them from collection filtering here.- excluded-models (conj (vec (search.filter/models-without-collection)) "table")- or-null #(vector :or- [:in :search_index.model excluded-models]- %)]- (cond-> qry- true (sql.helpers/left-join [:collection :collection] [:= collection-id-col :collection.id])- true (sql.helpers/where (or-null permitted-clause))- personal-clause (sql.helpers/where (or-null personal-clause)))))--(defn- filter-layers- "Ordered `[label add-clauses-fn]` pairs that layer the structural + permission `WHERE` clauses onto an index- query. Defined once so [[results]] and the debug [[diagnose]] probe share the exact same chain, and so the- diagnostic can attribute exclusion to the first layer (per-permission, then per-filter) that drops a row."- [search-ctx]- (concat- [[:collection-permissions (partial add-collection-join-and-where-clauses search-ctx)]- [:table-permissions (partial add-table-where-clauses search-ctx)]- [:transform-source-type #(sql.helpers/where % (search.filter/transform-source-type-where-clause- search-ctx- :search_index.model- :search_index.source_type))]]- (for [[filter-key clause] (search.filter/filter-clauses search-ctx)]- [filter-key #(sql.helpers/where % clause)])))--(defn- base-filtered-query- "The structural + permission filtered index query (no scoring), parameterized by `search-string`. Passing a- blank/nil `search-string` drops the fulltext predicate while keeping every other filter (see- `specialization/base-query`)."- [search-ctx search-string select-items]- (reduce (fn [qry [_ f]] (f qry))- (search.index/search-query search-string search-ctx select-items)- (filter-layers search-ctx)))+(def ^{:private true+ :arglists '([p-value])}+ view-count-percentiles+ (if config/is-prod?+ (memoize/ttl view-count-percentiles*+ :ttl/threshold (u/hours->ms 1))+ view-count-percentiles*))(defn- results[{:keys [search-engine search-string] :as search-ctx}]@@ -169,12 +122,11 @@:timeout-ms 2000:interval-ms 100})(log/warn "Returning search results even though they may be stale. Queue size:" (pending-updates)))))- (let [weights (search.config/weights search-ctx)- scorers (search.scoring/scorers search-ctx)- query (->> (base-filtered-query search-ctx search-string [:legacy_input])- (search.scoring/with-scores search-ctx scorers))]- (->> (search.db/scored-search-rows query)- (map (partial rehydrate weights (keys scorers)))))+ (let [weights (search.config/weights search-ctx)+ percentiles (view-count-percentiles search.config/view-count-scaling-percentile)+ scorer-keys (keys (search.scoring/scorers search-ctx percentiles))]+ (->> (search.db/scored-search-rows (search.index/active-table) search-ctx search-string percentiles)+ (map (partial rehydrate weights scorer-keys))))(catch Exception e;; Rule out the error coming from stale index metadata.(#'search.index/sync-tracking-atoms!)@@ -189,42 +141,25 @@;; We ignore any current models filter(let [unfiltered-context (assoc search-ctx :models search.config/all-models)applicable-models (search.filter/search-context->applicable-models unfiltered-context)- search-ctx (assoc search-ctx :models applicable-models)]- (->> (search.index/search-query (:search-string search-ctx) search-ctx [[[:distinct :model] :model]])- (add-collection-join-and-where-clauses search-ctx)- (#(sql.helpers/where % (search.filter/transform-source-type-where-clause- search-ctx- :search_index.model- :search_index.source_type)))- (search.filter/with-filters search-ctx)- search.db/distinct-model-rows- (into #{} (map :model)))))+ search-ctx (assoc search-ctx :models (set applicable-models))]+ (if-let [index-table (search.index/active-table)]+ (into #{} (map :model) (search.db/distinct-model-rows index-table search-ctx))+ #{})))-(defn- restrict-to-row [model id qry]- (sql.helpers/where qry [:and- [:= :search_index.model model]- [:= :search_index.model_id (str id)]]))--(defn- row-present? [qry]- (-> qry- (assoc :select [[[:inline 1] :one]] :limit 1)- (dissoc :order-by)- search.db/search-index-probe-rows- seq- boolean))+(defn- row-present?+ [index-table search-ctx search-string model id layer-count]+ (some? (search.db/search-index-probe-row index-table search-ctx search-string model id layer-count)))(defn- first-excluding-layer- "Apply the structural + permission [[filter-layers]] cumulatively to the row-restricted, text-free query.- Returns the label of the first layer after which the row disappears, or nil if it survives all layers."- [search-ctx model id]- (loop [qry (->> (search.index/search-query nil search-ctx [:model_id])- (restrict-to-row model id))- layers (filter-layers search-ctx)]- (when-let [[[label f] & more] (seq layers)]- (let [qry' (f qry)]- (if-not (row-present? qry')- label- (recur qry' more))))))+ "Apply the structural + permission `metabase.search.appdb.query/filter-layers` cumulatively to the row-restricted,+ text-free query. Returns the label of the first layer after which the row disappears, or nil if it survives all+ layers."+ [index-table search-ctx model id]+ (->> (appdb.query/filter-layer-labels search-ctx)+ (map-indexed (fn [i label] [(inc i) label]))+ (some (fn [[layer-count label]]+ (when-not (row-present? index-table search-ctx nil model id layer-count)+ label)))))(defn- appdb-diagnose[search-ctx model id]@@ -245,12 +180,11 @@:else(if-not (search.impl/check-result-permissions search-ctx (rehydrate {} [] index-row)){:type :filtered :details {:excluded-by :permissions}}- (if-let [layer (first-excluding-layer search-ctx model id)]+ (if-let [layer (first-excluding-layer active search-ctx model id)]{:type :filtered :details {:excluded-by layer}}(let [search-string (:search-string search-ctx)](if (and (not (str/blank? search-string))- (not (row-present? (->> (base-filtered-query search-ctx search-string [:model_id])- (restrict-to-row model id)))))+ (not (row-present? active search-ctx search-string model id nil))){:type :not-matching:details {:search-string search-string :search-native-query (boolean (:search-native-query search-ctx))}}{:type :candidate :details {:search-string search-string}})))))))))diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.cljindex 41c69d42f80..933907c263a 100644--- a/src/metabase/search/appdb/index.clj+++ b/src/metabase/search/appdb/index.clj@@ -424,19 +424,12 @@[](search.db/active-index-created-at (search.spec/index-version-hash) (i18n/site-locale-string)))-(defn search-query- "Query fragment for all models corresponding to a query parameter `:search-term`."- ([search-term search-ctx]- (search-query search-term search-ctx [:model_id :model]))- ([search-term search-ctx select-items]- (when-let [index-table (active-table)]- (specialization/base-query index-table search-term search-ctx select-items))))-(defn search"Use the index table to search for records."[search-term & [search-ctx]]- (map (juxt :model :name)- (search.db/search-index-rows (search-query search-term search-ctx [:model :name]))))+ (when-let [index-table (active-table)]+ (map (juxt :model :name)+ (search.db/search-index-rows index-table search-term (:search-native-query search-ctx) [:model :name]))))(defn reset-index!"Ensure we have a blank slate; in case the table schema or stored data format has changed."diff --git a/src/metabase/search/appdb/query.clj b/src/metabase/search/appdb/query.cljnew file mode 100644index 00000000000..02b94a887f9--- /dev/null+++ b/src/metabase/search/appdb/query.clj@@ -0,0 +1,107 @@+(ns metabase.search.appdb.query+ "Builds the app-DB search index queries of the appdb engine from a search context. Query building only:+ `metabase.search.db` runs the queries, so this namespace must not require it (nor `metabase.search.appdb.index`)."+ (:require+ [honey.sql.helpers :as sql.helpers]+ [metabase.app-db.core :as mdb]+ [metabase.search.appdb.specialization.api :as specialization]+ [metabase.search.filter :as search.filter]+ [metabase.search.permissions :as search.permissions]))++(defn add-table-where-clauses+ "Add a `WHERE` clause to the query to only return tables the current user has access to.+ Also adds any CTEs required for permission filtering."+ [search-ctx qry]+ (let [model-id-col [:cast :search_index.model_id (case (mdb/db-type)+ :mysql :signed+ :integer)]+ {:keys [with clause]} (search.permissions/permitted-tables-clause search-ctx model-id-col)]+ (cond-> qry+ (seq with) (update :with (fnil into []) with)+ true (sql.helpers/where+ [:or+ [:= :search_index.model nil]+ [:!= :search_index.model "table"]+ [:and+ [:= :search_index.model "table"]+ clause]]))))++(defn add-collection-join-and-where-clauses+ "Add a `WHERE` clause to the query to only return Collections the Current User has access to; join against Collection,+ so we can return its `:name`."+ [search-ctx qry]+ (let [collection-id-col :search_index.collection_id+ permitted-clause (search.permissions/permitted-collections-clause search-ctx collection-id-col)+ personal-clause (search.filter/personal-collections-where-clause search-ctx collection-id-col)+ ;; Tables have their own dedicated permission filter (add-table-where-clauses) that checks both data+ ;; permissions and published-via-collection access, so we exclude them from collection filtering here.+ excluded-models (conj (vec (search.filter/models-without-collection)) "table")+ or-null #(vector :or+ [:in :search_index.model excluded-models]+ %)]+ (cond-> qry+ true (sql.helpers/left-join [:collection :collection] [:= collection-id-col :collection.id])+ true (sql.helpers/where (or-null permitted-clause))+ personal-clause (sql.helpers/where (or-null personal-clause)))))++(defn filter-layers+ "Ordered `[label add-clauses-fn]` pairs that layer the structural + permission `WHERE` clauses onto an index+ query. Defined once so `metabase.search.appdb.core` results and its debug diagnose probe share the exact same chain, and so the+ diagnostic can attribute exclusion to the first layer (per-permission, then per-filter) that drops a row."+ [search-ctx]+ (concat+ [[:collection-permissions (partial add-collection-join-and-where-clauses search-ctx)]+ [:table-permissions (partial add-table-where-clauses search-ctx)]+ [:transform-source-type #(sql.helpers/where % (search.filter/transform-source-type-where-clause+ search-ctx+ :search_index.model+ :search_index.source_type))]]+ (for [[filter-key clause] (search.filter/filter-clauses search-ctx)]+ [filter-key #(sql.helpers/where % clause)])))++(defn filter-layer-labels+ "The labels of [[filter-layers]], in order."+ [search-ctx]+ (mapv first (filter-layers search-ctx)))++(defn- apply-layers+ [qry layers]+ (reduce (fn [qry [_ f]] (f qry)) qry layers))++(defn base-filtered-query+ "The structural + permission filtered query of the search index `index-table` (no scoring), parameterized by+ `search-string`. Passing a blank/nil `search-string` drops the fulltext predicate while keeping every other filter+ (see `specialization/base-query`)."+ [index-table search-ctx search-string select-items]+ (apply-layers (specialization/base-query index-table search-string search-ctx select-items)+ (filter-layers search-ctx)))++(defn model-set-query+ "The query returning the distinct `:model`s of the search index `index-table` with at least one visible result for+ `search-ctx`."+ [index-table search-ctx]+ (->> (specialization/base-query index-table (:search-string search-ctx) search-ctx [[[:distinct :model] :model]])+ (add-collection-join-and-where-clauses search-ctx)+ (#(sql.helpers/where % (search.filter/transform-source-type-where-clause+ search-ctx+ :search_index.model+ :search_index.source_type)))+ (search.filter/with-filters search-ctx)))++(defn- restrict-to-row+ [model id qry]+ (sql.helpers/where qry [:and+ [:= :search_index.model model]+ [:= :search_index.model_id (str id)]]))++(defn probe-query+ "A single-row probe: does the `model`/`id` row of the search index `index-table` survive the first `layer-count`+ [[filter-layers]] (nil = every layer) for `search-ctx` and `search-string`?"+ [index-table search-ctx search-string model id layer-count]+ (let [layers (cond->> (filter-layers search-ctx)+ layer-count (take layer-count))]+ (-> (specialization/base-query index-table search-string search-ctx [:model_id])+ (->> (restrict-to-row model id))+ (apply-layers layers)+ (assoc :select [[[:inline 1] :one]] :limit 1)+ (dissoc :order-by))))diff --git a/src/metabase/search/appdb/scoring.clj b/src/metabase/search/appdb/scoring.cljindex d648bd99fad..0e62cfe2f6e 100644--- a/src/metabase/search/appdb/scoring.clj+++ b/src/metabase/search/appdb/scoring.clj@@ -1,51 +1,33 @@(ns metabase.search.appdb.scoring(:require- [clojure.core.memoize :as memoize][honey.sql.helpers :as sql.helpers]- [metabase.config.core :as config][metabase.premium-features.core :refer [defenterprise]]- [metabase.search.appdb.index :as search.index][metabase.search.appdb.specialization.api :as specialization][metabase.search.config :as search.config]- [metabase.search.db :as search.db]- [metabase.search.scoring :as search.scoring]- [metabase.util :as u]))+ [metabase.search.scoring :as search.scoring]))(defn all-scores"Score stats for each scorer"[weights scorers index-row](search.scoring/all-scores weights scorers index-row))-(defn- view-count-percentiles*- [p-value]- (into {} (for [{:keys [model vcp]} (search.db/view-count-percentile-rows (search.index/active-table) p-value)]- [(keyword model) vcp])))--(def ^{:private true- :arglists '([p-value])}- view-count-percentiles- (if config/is-prod?- (memoize/ttl view-count-percentiles*- :ttl/threshold (u/hours->ms 1))- view-count-percentiles*))--(defn- view-count-expr [percentile]- (let [views (view-count-percentiles percentile)- cases (for [[sm v] views]+(defn- view-count-expr [view-count-percentiles]+ (let [cases (for [[sm v] view-count-percentiles][[:= :search_index.model (name sm)] (max (or v 0) 1)])](search.scoring/size :view_count (if (seq cases)(into [:case] cat cases)1))))(defn base-scorers- "The default constituents of the search ranking scores."- [{:keys [search-string] :as search-ctx}]+ "The default constituents of the search ranking scores. `view-count-percentiles` maps each model to its view-count+ percentile (see `metabase.search.db/view-count-percentile-rows`)."+ [{:keys [search-string] :as search-ctx} view-count-percentiles](if (search.scoring/no-scoring-required? search-ctx){:model [:inline 1]};; NOTE: we calculate scores even if the weight is zero, so that it's easy to consider how we could affect any;; given set of results. At some point, we should optimize away the irrelevant scores for any given context.{:text (specialization/text-score)- :view-count (view-count-expr search.config/view-count-scaling-percentile)+ :view-count (view-count-expr view-count-percentiles):pinned (search.scoring/truthy :pinned):bookmarked search.scoring/bookmark-score-expr:recency (search.scoring/inverse-duration [:coalesce :last_viewed_at :model_updated_at] [:now] search.config/stale-time-in-days)@@ -69,8 +51,8 @@(defenterprise scorers"Return the select-item expressions used to calculate the score for each search result."metabase-enterprise.search.scoring- [search-ctx]- (base-scorers search-ctx))+ [search-ctx view-count-percentiles]+ (base-scorers search-ctx view-count-percentiles))(defn with-scores"Add a bunch of SELECT columns for the individual and total scores, and a corresponding ORDER BY."diff --git a/src/metabase/search/core.clj b/src/metabase/search/core.cljindex 01cf29eada2..5ed0db4be15 100644--- a/src/metabase/search/core.clj+++ b/src/metabase/search/core.clj@@ -10,6 +10,7 @@[metabase.search.engine :as search.engine][metabase.search.impl :as search.impl][metabase.search.ingestion :as search.ingestion]+ [metabase.search.ingestion.query :as search.ingestion.query][metabase.search.spec :as search.spec][metabase.search.util :as search.util][metabase.settings.core :as setting]@@ -40,7 +41,8 @@;; We could avoid exposing this by wrapping `query-model-set` and `search` with it.search-context][search.ingestion- bulk-ingest!+ bulk-ingest!]+ [search.ingestion.querymax-searchable-value-lengthsearchable-value-trim-sql][search.specdiff --git a/src/metabase/search/db.clj b/src/metabase/search/db.cljindex baeec9605c4..3d486009005 100644--- a/src/metabase/search/db.clj+++ b/src/metabase/search/db.clj@@ -4,90 +4,131 @@(:require[honey.sql.helpers :as sql.helpers][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.search.appdb.query :as appdb.query]+ [metabase.search.appdb.scoring :as search.scoring][metabase.search.appdb.specialization.api :as specialization]+ [metabase.search.config :as search.config]+ [metabase.search.in-place.legacy :as legacy]+ [metabase.search.ingestion.query :as ingestion.query]+ [metabase.search.schema :as search.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn spec-index-rows- "The rows matching the Honey SQL `query` built from a search model's spec by `metabase.search.ingestion`."- [query]- (t2/query query))--(defn spec-index-reducible-rows- "A reducible of the rows matching the Honey SQL `query` built from a search model's spec by- `metabase.search.ingestion`."- [query]- (mdb/streaming-reducible-query query))--(defn in-place-model-set-rows- "The rows matching the Honey SQL `query` built by `metabase.search.in-place.legacy` to find the distinct set of- models with results."- [query]- (mdb/query query))--(defn in-place-search-reducible- "A reducible of the rows matching the full in-place search Honey SQL `query` built by- `metabase.search.in-place.legacy`."- [query]- (mdb/streaming-reducible-query query))--(defn scored-search-rows- "The rows matching the scored, filtered search Honey SQL `query` built by `metabase.search.appdb.core`."- [query]- (t2/query query))--(defn distinct-model-rows- "The rows matching the Honey SQL `query` built by `metabase.search.appdb.core` to find the distinct search models- present in the results."- [query]- (t2/query query))--(defn search-index-probe-rows- "The rows matching the Honey SQL `query` built by `metabase.search.appdb.core` to check whether a single row- survives a filter."- [query]- (t2/query query))--(defn search-index-rows- "The rows matching the Honey SQL `query` built by `metabase.search.appdb.index/search-query`."- [query]- (t2/query query))--(defn view-count-percentile-rows+(mu/defn spec-index-reducible-rows+ "A reducible of the indexable rows of `search-model` (see `metabase.search.ingestion.query/spec-index-query`)+ matching `where-clause`: the search-spec generated `:where` fragment of `metabase.search.spec/search-models-to-update`,+ or nil for every row."+ [search-model :- :string+ where-clause :- [:maybe vector?]]+ (mdb/streaming-reducible-query (ingestion.query/spec-index-query-where search-model where-clause)))++(mu/defn spec-index-row+ "A probe row when the `search-model` row with the underlying model PK `id` is indexable, or nil."+ [search-model :- :string+ id :- ms/PositiveInt]+ (t2/query-one (-> (ingestion.query/spec-index-query-where search-model [:= :this.id id])+ (assoc :select [[[:inline 1] :one]] :limit 1))))++(mu/defn spec-index-count+ "The number of indexable rows of `search-model`."+ [search-model :- :string]+ (:count (t2/query-one (assoc (ingestion.query/spec-index-query search-model) :select [[:%count.* :count]]))))++(mu/defn in-place-model-set-rows+ "The `:model` rows of the in-place search engine's model-set query for `search-ctx`, or nil when no model applies."+ [search-ctx :- search.config/SearchContext]+ (some-> (legacy/model-set-query search-ctx) mdb/query))++(mu/defn in-place-search-reducible+ "A reducible of the in-place (index-free) search results for `search-ctx`."+ [search-ctx :- search.config/SearchContext]+ (mdb/streaming-reducible-query (legacy/full-search-query search-ctx)))++(mu/defn scored-search-rows+ "The scored, filtered rows of the search index `index-table` for `search-ctx` and `search-string`, best first.+ `view-count-percentiles` maps each model to its view-count percentile (see [[view-count-percentile-rows]])."+ [index-table :- [:or :keyword :string]+ search-ctx :- search.config/SearchContext+ search-string :- [:maybe :string]+ view-count-percentiles :- [:map-of :keyword [:maybe number?]]]+ (t2/query (search.scoring/with-scores search-ctx+ (search.scoring/scorers search-ctx view-count-percentiles)+ (appdb.query/base-filtered-query index-table search-ctx search-string [:legacy_input]))))++(mu/defn distinct-model-rows+ "The distinct `:model` rows of the search index `index-table` with at least one visible result for `search-ctx`."+ [index-table :- [:or :keyword :string]+ search-ctx :- search.config/SearchContext]+ (t2/query (appdb.query/model-set-query index-table search-ctx)))++(mu/defn search-index-probe-row+ "A probe row when the `model`/`id` row of the search index `index-table` survives the first `layer-count`+ `metabase.search.appdb.query/filter-layers` (nil = every layer) for `search-ctx` and `search-string`, or nil."+ [index-table :- [:or :keyword :string]+ search-ctx :- search.config/SearchContext+ search-string :- [:maybe :string]+ model :- :string+ id :- [:or :string ms/PositiveInt]+ layer-count :- [:maybe nat-int?]]+ (t2/query-one (appdb.query/probe-query index-table search-ctx search-string model id layer-count)))++(defn- index-search-query+ "The Honey SQL query selecting `select-items` from the search index `table-name`, matching `search-term` (or every+ row, when blank/nil), honoring `search-native-query?` (see `metabase.search.appdb.specialization.postgres/base-query`+ and `.h2/base-query`)."+ [table-name search-term search-native-query? select-items]+ (specialization/base-query table-name search-term {:search-native-query search-native-query?} select-items))++(mu/defn search-index-rows+ "The `select-items` rows of the search index `table-name` matching `search-term`, honoring+ `search-native-query?`."+ [table-name :- [:or :keyword :string]+ search-term :- [:maybe :string]+ search-native-query? :- [:maybe :boolean]+ select-items :- [:sequential :keyword]]+ (t2/query (index-search-query table-name search-term search-native-query? select-items)))++(mu/defn view-count-percentile-rows"The Model to view-count-percentile rows for the search index table `index-table` at percentile `p-value`."- [index-table p-value]+ [index-table :- [:or :keyword :string]+ p-value :- number?](t2/query (specialization/view-count-percentile-query index-table p-value)))-(defn drop-search-index-table-if-exists!+(mu/defn drop-search-index-table-if-exists!"Drop the search index table named `table-name`, if it exists."- [table-name]+ [table-name :- [:or :keyword :string]](t2/query (sql.helpers/drop-table :if-exists table-name)))-(defn drop-search-index-table!+(mu/defn drop-search-index-table!"Drop the search index table named `table-name`."- [table-name]+ [table-name :- [:or :keyword :string]](t2/query (sql.helpers/drop-table table-name)))-(defn create-search-index-table!+(mu/defn create-search-index-table!"Create the search index table named `table-name` with `columns` (the Honey SQL column definitions built by theactive search engine specialization)."- [table-name columns]+ [table-name :- [:or :keyword :string]+ columns :- [:sequential vector?]](t2/query (-> (sql.helpers/create-table table-name)(sql.helpers/with-columns columns))))-(defn run-search-index-statement!+(mu/defn run-search-index-statement!"Run a single post-creation SQL statement (e.g. an index creation) for a search index table."- [statement]+ [statement :- [:or :string vector? :map]](t2/query statement))-(defn analyze-search-index-table!+(mu/defn analyze-search-index-table!"Run `ANALYZE` on the search index table `table-name` (Postgres only)."- [table-name]+ [table-name :- [:or :keyword :string]](t2/query (str "ANALYZE " (name table-name))))-(defn postgres-batch-upsert!+(mu/defn postgres-batch-upsert!"Upsert `entries` into the search index `table`, on conflict of `(model, model_id)` overwriting every other columnwith the new value."- [table entries]+ [table :- [:or :keyword :string]+ entries :- [:sequential :map]](when (seq entries)(let [update-keys (vec (disj (set (mapcat keys entries)) :id :model :model_id))excluded-kw (fn [column] (keyword (str "excluded." (name column))))]@@ -97,62 +138,63 @@:do-update-set (with-meta (zipmap update-keys (map excluded-kw update-keys)){:allow-subquery true})}))))-(defn commit!+(mu/defn commit!"Commit the current transaction."[](t2/query ["commit"]))-(defn user-exists?+(mu/defn user-exists?"Whether a User with `user-id` exists."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/exists? :model/User :id user-id))-(defn entity-exists?+(mu/defn entity-exists?"Whether a `model` row with `id` exists."- [model id]+ [model :- :keyword+ id :- ms/PositiveInt](t2/exists? model :id id))-(defn any-card- "Some Card, or nil."- []- (t2/select-one :model/Card))--(defn index-metadata-for-engine+(mu/defn index-metadata-for-engine"The SearchIndexMetadata rows of `engine`."- [engine]+ [engine :- :keyword](t2/select :model/SearchIndexMetadata :engine engine))-(defn index-row+(mu/defn index-row"The row of the search index `table` for `model` and `model-id`, or nil."- [table model model-id]+ [table :- [:or :keyword :string]+ model :- [:or :keyword :string]+ model-id :- [:or :string ms/PositiveInt]](t2/select-one table :model model :model_id model-id))-(defn delete-all-rows!+(mu/defn delete-all-rows!"Delete every row of the search index `table`."- [table]+ [table :- [:or :keyword :string]](t2/delete! table))-(defn delete-index-rows!+(mu/defn delete-index-rows!"Delete the rows of the search index `table` for `model` and `model-ids`."- [table model model-ids]+ [table :- [:or :keyword :string]+ model :- [:or :keyword :string]+ model-ids :- [:or [:set [:or :string ms/PositiveInt]] [:sequential [:or :string ms/PositiveInt]]]](t2/delete! table :model model :model_id [:in model-ids]))-(defn insert-rows!+(mu/defn insert-rows!"Insert `entries` into the search index `table`."- [table entries]+ [table :- [:or :keyword :string]+ entries :- [:sequential :map]](t2/insert! table entries))-(defn index-entry-count+(mu/defn index-entry-count"The number of entries in the search index table `index-table`."- [index-table]+ [index-table :- [:or :keyword :string]](t2/count index-table))-(defn table-exists?+(mu/defn table-exists?"Whether a table named `table-name` exists in the app DB."- [table-name]+ [table-name :- :string](t2/exists? :information_schema.tables :table_name table-name))-(defn orphan-index-table-names+(mu/defn orphan-index-table-names"The `:table_name`s of search index tables in the current schema with no SearchIndexMetadata."[](t2/query {:select [:ist.table_name]@@ -172,22 +214,23 @@[:= :sim.engine "appdb"][:= [:lower :sim.index_name] [:lower :ist.table_name]]]}]]]}))-(defn pg-class-estimate+(mu/defn pg-class-estimate"The planner's `:reltuples` and `:relpages` estimate for `table-name` (Postgres only), or nil."- [table-name]+ [table-name :- :string](t2/query-one {:select [:reltuples :relpages]:from [:pg_class]:where [:= :oid [:to_regclass table-name]]}))-(defn pg-text-search-configs+(mu/defn pg-text-search-configs"The `:cfgname`s of the Postgres text search configurations."[](t2/query {:select [:cfgname]:from [:pg_ts_config]}))-(defn active-index-created-at+(mu/defn active-index-created-at"When the active `appdb` search index for `version` and `lang-code` was created, or nil."- [version lang-code]+ [version :- :string+ lang-code :- :string](t2/select-one-fn :created_at:model/SearchIndexMetadata:engine :appdb@@ -196,73 +239,90 @@:status :active{:order-by [[:created_at :desc]]}))-(defn insert-index-metadata!+(mu/defn insert-index-metadata!"Insert the SearchIndexMetadata `row`."- [row]+ [row :- ::search.schema/search-index-metadata.update](t2/insert! :model/SearchIndexMetadata row))-(defn delete-index-metadata-by-version!+(mu/defn delete-index-metadata-by-version!"Delete the SearchIndexMetadata rows of `version`."- [version]+ [version :- :string](t2/delete! :model/SearchIndexMetadata :version version))-(defn delete-index-metadata-by-name-on-conn!+(mu/defn delete-index-metadata-by-name-on-conn!"Delete the SearchIndexMetadata rows named `index-name`, on `conn`."- [conn index-name]+ [conn :- (ms/InstanceOfClass java.sql.Connection)+ index-name :- :string](t2/delete! :conn conn :model/SearchIndexMetadata :index_name index-name))-(defn delete-index-metadata!+(mu/defn delete-index-metadata!"Delete the SearchIndexMetadata row of `engine`, `version`, `lang-code`, and `index-name`."- [engine version lang-code index-name]+ [engine :- :keyword+ version :- :string+ lang-code :- :string+ index-name :- :string](t2/delete! :model/SearchIndexMetadata :engine engine :version version :lang_code lang-code :index_name index-name))-(defn index-metadata+(mu/defn index-metadata"The name, status, and creation time of the active and pending SearchIndexMetadata rows of `engine`, `version`, and`lang-code`."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/select [:model/SearchIndexMetadata :index_name :status :created_at]:engine engine:version version:lang_code lang-code:status [:in [:active :pending]]))-(defn delete-expired-pending-index-metadata!+(mu/defn delete-expired-pending-index-metadata!"Delete the pending SearchIndexMetadata rows of `lang-code` created before `created-before`."- [lang-code created-before]+ [lang-code :- :string+ created-before :- ms/TemporalInstant](t2/delete! :model/SearchIndexMetadata{:where [:and[:= :lang_code lang-code][:= :status "pending"][:< :created_at created-before]]}))-(defn pending-index-metadata-exists?+(mu/defn pending-index-metadata-exists?"Whether a pending SearchIndexMetadata row of `engine`, `version`, and `lang-code` exists."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/exists? :model/SearchIndexMetadata :engine engine :version version :lang_code lang-code :status :pending))-(defn delete-retired-index-metadata!+(mu/defn delete-retired-index-metadata!"Delete the retired SearchIndexMetadata rows of `engine`, `version`, and `lang-code`."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/delete! :model/SearchIndexMetadata :engine engine :version version :lang_code lang-code :status :retired))-(defn retire-active-index-metadata!+(mu/defn retire-active-index-metadata!"Retire the active SearchIndexMetadata rows of `engine`, `version`, and `lang-code`."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/update! :model/SearchIndexMetadata {:engine engine :version version :lang_code lang-code :status :active} {:status :retired}))-(defn activate-pending-index-metadata!+(mu/defn activate-pending-index-metadata!"Activate the pending SearchIndexMetadata rows of `engine`, `version`, and `lang-code`."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/update! :model/SearchIndexMetadata {:engine engine :version version :lang_code lang-code :status :pending} {:status :active}))-(defn active-index-name+(mu/defn active-index-name"The name of the active SearchIndexMetadata row of `engine`, `version`, and `lang-code`, or nil."- [engine version lang-code]+ [engine :- :keyword+ version :- :string+ lang-code :- :string](t2/select-one-fn :index_name :model/SearchIndexMetadata :engine engine :version version :lang_code lang-code :status :active))-(defn recent-index-versions+(mu/defn recent-index-versions"The `:version`s of the `limit` most recently updated SearchIndexMetadata versions."- [limit]+ [limit :- ms/PositiveInt](t2/query {:select [:version]:from [(t2/table-name :model/SearchIndexMetadata)]:group-by [:version]@@ -271,10 +331,12 @@[[:max :id] :desc]]:limit limit}))-(defn delete-obsolete-index-metadata!+(mu/defn delete-obsolete-index-metadata!"Delete the SearchIndexMetadata rows whose version is not in `recent-versions`, or not in `keep-versions` and lastupdated before `updated-before`."- [recent-versions keep-versions updated-before]+ [recent-versions :- [:sequential :string]+ keep-versions :- [:sequential :string]+ updated-before :- ms/TemporalInstant](t2/query-one {:delete-from [(t2/table-name :model/SearchIndexMetadata)]:where [:or[:not-in :version recent-versions]@@ -282,22 +344,17 @@[:not-in :version keep-versions][:< :updated_at updated-before]]]}))-(defn personal-collection-root-id- "The id of the root personal Collection of the User with `user-id`, or nil."- [user-id]- (t2/select-one-pk :model/Collection :personal_owner_id [:= user-id] :location "/"))--(defn non-destination-database-ids+(mu/defn non-destination-database-ids"The ids of the Databases that are not routing destinations, or nil."[](t2/select-pks-set :model/Database :router_database_id nil))-(defn user-common-names+(mu/defn user-common-names"A map of User id to common name for the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn :common_name [:model/User :id :first_name :last_name :email] :id [:in user-ids]))-(defn card-result-metadata+(mu/defn card-result-metadata"A map of Card id to result metadata for the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select-pk->fn :result_metadata [:model/Card :id :card_schema :result_metadata] :id [:in card-ids]))diff --git a/src/metabase/search/filter.clj b/src/metabase/search/filter.cljindex 9c3a7eb66cb..51f7d5dcc17 100644--- a/src/metabase/search/filter.clj+++ b/src/metabase/search/filter.clj@@ -6,7 +6,6 @@[metabase.premium-features.core :as premium-features][metabase.query-processor.parameters.dates :as qp.parameters.dates][metabase.search.config :as search.config]- [metabase.search.db :as search.db][metabase.search.permissions :as search.permissions][metabase.search.spec :as search.spec][metabase.util.date-2 :as u.date]@@ -132,7 +131,7 @@"only-mine"[:or[:= :collection.personal_owner_id current-user-id]- [:like :collection.location (format "/%d/%%" (search.db/personal-collection-root-id current-user-id))]]+ [:like :collection.location (format "/%d/%%" (:id (collection/user->existing-personal-collection current-user-id)))]]"exclude-others"(let [with-filter #(personal-collections-where-clausediff --git a/src/metabase/search/in_place/engine.clj b/src/metabase/search/in_place/engine.cljnew file mode 100644index 00000000000..7c5c8fa9da6--- /dev/null+++ b/src/metabase/search/in_place/engine.clj@@ -0,0 +1,16 @@+(ns metabase.search.in-place.engine+ "The in-place (index-free) search engine: runs the queries built by `metabase.search.in-place.legacy`."+ (:require+ [metabase.search.db :as search.db]+ [metabase.search.engine :as search.engine]+ [metabase.search.in-place.legacy]))++(comment metabase.search.in-place.legacy/keep-me)++(defmethod search.engine/model-set :search.engine/in-place+ [search-ctx]+ (into #{} (map :model) (search.db/in-place-model-set-rows search-ctx)))++(defmethod search.engine/results :search.engine/in-place+ [search-ctx]+ (search.db/in-place-search-reducible search-ctx))diff --git a/src/metabase/search/in_place/filter.clj b/src/metabase/search/in_place/filter.cljindex 27b5c35badb..0365e68946a 100644--- a/src/metabase/search/in_place/filter.clj+++ b/src/metabase/search/in_place/filter.clj@@ -34,7 +34,7 @@(def ^:private max-document-search-length"Cap the number of characters of a document's prose-mirror body that the legacy engine LIKE-scans.A leading-wildcard LIKE can't use an index, so this bounds the worst case for pathologically large- documents. Mirrors the appdb engine's `metabase.search.ingestion/max-searchable-value-length`."+ documents. Mirrors the appdb engine's `metabase.search.ingestion.query/max-searchable-value-length`."500000);; ------------------------------------------------------------------------------------------------;;diff --git a/src/metabase/search/in_place/legacy.clj b/src/metabase/search/in_place/legacy.cljindex 80b3124d7a1..448d1043a57 100644--- a/src/metabase/search/in_place/legacy.clj+++ b/src/metabase/search/in_place/legacy.clj@@ -10,7 +10,6 @@[metabase.search.config:as search.config:refer [SearchContext SearchableModel]]- [metabase.search.db :as search.db][metabase.search.engine :as search.engine][metabase.search.filter :as search.filter][metabase.search.in-place.filter :as search.in-place.filter]@@ -540,7 +539,7 @@(search.in-place.filter/build-filters model context)))(mu/defn- shared-card-impl- [model :- ::queries.schema/card-type+ [model :- ::queries.schema/card.typesearch-ctx :- SearchContext](-> (base-query-for-model "card" search-ctx)(sql.helpers/where [:= :card.type (name model)])@@ -689,7 +688,9 @@{:ctes all-ctes:queries queries-without-ctes}))-(defmethod search.engine/model-set :search.engine/in-place+(defn model-set-query+ "The Honey SQL query returning one row per search model with at least one result for `search-ctx` (every model is+ considered, regardless of the context's `:models`), or nil when no model applies."[search-ctx](let [raw-queries (vec (for [model (search.in-place.filter/search-context->applicable-models;; It's unclear why we don't use the existing :models@@ -698,12 +699,11 @@{:keys [ctes queries]} (extract-and-hoist-ctes raw-queries)nested-queries (mapv #(vary-meta (hash-map :nest (vary-meta (sql.helpers/limit % 1) assoc :allow-subquery true))assoc :allow-subquery true)- queries)- query (when (pos-int? (count nested-queries))- (cond-> {:select [:*]- :from [[^:allow-subquery {:union-all nested-queries} :dummy_alias]]}- (seq ctes) (assoc :with ctes)))]- (into #{} (map :model) (some-> query search.db/in-place-model-set-rows))))+ queries)]+ (when (pos-int? (count nested-queries))+ (cond-> {:select [:*]+ :from [[^:allow-subquery {:union-all nested-queries} :dummy_alias]]}+ (seq ctes) (assoc :with ctes)))))(mu/defn full-search-query"Postgres 9 is not happy with the type munging it needs to do to make the union-all degenerate down to a trivial case@@ -732,16 +732,5 @@:limit search.config/*db-max-results*}(seq ctes) (assoc :with ctes))))))-;; Return a reducible-query corresponding to searching the entities without an index.-(defn- results- [search-ctx]- (let [search-query (full-search-query search-ctx)]- (search.db/in-place-search-reducible search-query)))--(defmethod search.engine/results- :search.engine/in-place- [search-ctx]- (results search-ctx))-(defmethod search.engine/score :search.engine/in-place [search-ctx result](scoring/score-and-result result search-ctx))diff --git a/src/metabase/search/ingestion.clj b/src/metabase/search/ingestion.cljindex fc47d77df90..37046facc59 100644--- a/src/metabase/search/ingestion.clj+++ b/src/metabase/search/ingestion.clj@@ -1,10 +1,8 @@(ns metabase.search.ingestion(:require[clojure.string :as str]- [honey.sql.helpers :as sql.helpers][medley.core :as m][metabase.analytics-interface.core :as analytics]- [metabase.app-db.core :as mdb][metabase.collections.curation :as collections.curation][metabase.lib-be.core :as lib-be][metabase.search.db :as search.db]@@ -16,7 +14,6 @@[metabase.util.log :as log][metabase.util.performance :as perf][metabase.util.queue :as queue]- [toucan2.core :as t2][toucan2.realize :as t2.realize])(:import(java.util.concurrent DelayQueue)))@@ -37,22 +34,6 @@"The name of the listener that consumes the queue""search-index-update")-(def max-searchable-value-length- "The maximum length of a searchable value. This is mostly driven by postgresql max-lengths on tsvector columns.- And is about half of postgresql's max, since we concat two values together often. That is likely aggressive, but being safe until we can better understand normal data shapes"- 500000)--(defn searchable-value-trim-sql- "Returns the honeysql expression to trim a searchable value to the max length.- The passed column should be a keyword that is qualified as needed.- Uses a slightly larger value that what will be stored in the db so we can better use word boundaries on the actual end"- [column]- (if (#{:postgres :h2} (mdb/db-type))- [:left- column- [:cast (+ max-searchable-value-length 100) :integer]]- column))-(defn- searchable-text [m];; For now, we never index the native query content(let [search-terms (:search-terms (search.spec/spec (:model m)))@@ -94,11 +75,6 @@field-keys)](str header "\n" (str/join "\n" fields))))-(defn- search-term-columns- "Extract column names from search-terms spec for SQL query generation"- [search-terms]- (if (map? search-terms) (keys search-terms) search-terms))-(defn- display-data [m](perf/select-keys m [:name :display_name :description :collection_name]))@@ -152,58 +128,11 @@:legacy_input (json/encode (assoc (apply dissoc m search.spec/legacy-input-excluded-keys):curated curated)))))-(defn- attrs->select-items [attrs]- (for [[k v] attrs- :when (and v (not (search.spec/function-attr? v)))]- (let [as (keyword (u/->snake_case_en (name k)))]- (if (true? v) as [v as]))))--(defn- spec-index-query*- [_db-type search-model]- (let [spec (search.spec/spec search-model)- fn-deps (search.spec/collect-fn-attr-req-fields spec)- fn-selects (map (fn [field]- [(keyword (str "this." (name field))) field])- fn-deps)- search-terms (set (search-term-columns (:search-terms spec)))]- (u/remove-nils- {:select (search.spec/qualify-columns :this- (concat- (map (fn [term] [(searchable-value-trim-sql (keyword (str "this." (name term))))- term])- search-terms)- (mapcat (fn [k] (attrs->select-items- (->> (get spec k)- (remove (comp search-terms key)))))- [:attrs :render-terms])- fn-selects))- :from [[(t2/table-name (:model spec)) :this]]- :where (:where spec [:= [:inline 1] [:inline 1]])- :left-join (when (:joins spec)- (into []- cat- (for [[join-alias [join-model join-condition]] (:joins spec)]- [[(t2/table-name join-model) join-alias]- join-condition])))})))--(def ^:private spec-index-query-memo (memoize spec-index-query*))--(defn- spec-index-query- ;; Memoized per db-type since the generated HoneySQL varies by database engine- ;; (e.g. searchable-value-trim-sql emits LEFT/CAST only for postgres and h2).- [search-model]- (spec-index-query-memo (mdb/db-type) search-model))--(defn- spec-index-query-where [search-model where-clause]- (-> (spec-index-query search-model)- (sql.helpers/where where-clause)))-(defn- spec-index-reducible [search-model & [where-clause]];; Joins in the spec (e.g. card → revision on most_recent = true) can produce duplicate rows when the;; joined side has its own integrity violations. Downstream upserts hit a unique constraint on;; (model, model_id), so dedup here at the streaming boundary — bounded by per-model row count.- (->> (spec-index-query-where search-model where-clause)- search.db/spec-index-reducible-rows+ (->> (search.db/spec-index-reducible-rows search-model where-clause)(eduction (comp (map #(assoc % :model search-model))(m/distinct-by (juxt :id :model))))))@@ -222,11 +151,7 @@(if-not (contains? (set search.spec/search-models) search-model):no-spec(let [spec (search.spec/spec search-model)- indexed? (-> (spec-index-query-where search-model [:= :this.id id])- (assoc :select [[[:inline 1] :one]] :limit 1)- search.db/spec-index-rows- seq- boolean)]+ indexed? (some? (search.db/spec-index-row search-model id))](condindexed? :indexable(search.db/entity-exists? (:model spec) id) :excluded@@ -274,11 +199,7 @@"Returns a count of all searchable items in the database."[](->> (for [model search.spec/search-models]- (-> (spec-index-query-where model nil)- (assoc :select [[:%count.* :count]])- search.db/spec-index-rows- first- :count))+ (search.db/spec-index-count model))(filter some?)(reduce + 0)))diff --git a/src/metabase/search/ingestion/query.clj b/src/metabase/search/ingestion/query.cljnew file mode 100644index 00000000000..0cecddf214a--- /dev/null+++ b/src/metabase/search/ingestion/query.clj@@ -0,0 +1,78 @@+(ns metabase.search.ingestion.query+ "Builds the app-DB queries that read the indexable rows of a search model from its `metabase.search.spec` spec.+ Query building only: `metabase.search.db` runs the queries, so this namespace must not require it."+ (:require+ [honey.sql.helpers :as sql.helpers]+ [metabase.app-db.core :as mdb]+ [metabase.search.spec :as search.spec]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(def max-searchable-value-length+ "The maximum length of a searchable value. This is mostly driven by postgresql max-lengths on tsvector columns.+ And is about half of postgresql's max, since we concat two values together often. That is likely aggressive, but being safe until we can better understand normal data shapes"+ 500000)++(defn searchable-value-trim-sql+ "Returns the honeysql expression to trim a searchable value to the max length.+ The passed column should be a keyword that is qualified as needed.+ Uses a slightly larger value that what will be stored in the db so we can better use word boundaries on the actual end"+ [column]+ (if (#{:postgres :h2} (mdb/db-type))+ [:left+ column+ [:cast (+ max-searchable-value-length 100) :integer]]+ column))++(defn- search-term-columns+ "Extract column names from search-terms spec for SQL query generation"+ [search-terms]+ (if (map? search-terms) (keys search-terms) search-terms))++(defn- attrs->select-items [attrs]+ (for [[k v] attrs+ :when (and v (not (search.spec/function-attr? v)))]+ (let [as (keyword (u/->snake_case_en (name k)))]+ (if (true? v) as [v as]))))++(defn- spec-index-query*+ [_db-type search-model]+ (let [spec (search.spec/spec search-model)+ fn-deps (search.spec/collect-fn-attr-req-fields spec)+ fn-selects (map (fn [field]+ [(keyword (str "this." (name field))) field])+ fn-deps)+ search-terms (set (search-term-columns (:search-terms spec)))]+ (u/remove-nils+ {:select (search.spec/qualify-columns :this+ (concat+ (map (fn [term] [(searchable-value-trim-sql (keyword (str "this." (name term))))+ term])+ search-terms)+ (mapcat (fn [k] (attrs->select-items+ (->> (get spec k)+ (remove (comp search-terms key)))))+ [:attrs :render-terms])+ fn-selects))+ :from [[(t2/table-name (:model spec)) :this]]+ :where (:where spec [:= [:inline 1] [:inline 1]])+ :left-join (when (:joins spec)+ (into []+ cat+ (for [[join-alias [join-model join-condition]] (:joins spec)]+ [[(t2/table-name join-model) join-alias]+ join-condition])))})))++(def ^:private spec-index-query-memo (memoize spec-index-query*))++(defn spec-index-query+ "The Honey SQL query selecting the indexable rows of `search-model` from its spec. Memoized per app-DB type since+ the generated Honey SQL varies by engine (e.g. `searchable-value-trim-sql` emits LEFT/CAST only for Postgres and H2)."+ [search-model]+ (spec-index-query-memo (mdb/db-type) search-model))++(defn spec-index-query-where+ "[[spec-index-query]] restricted to the rows matching `where-clause` (nil = every row)."+ [search-model where-clause]+ (-> (spec-index-query search-model)+ (sql.helpers/where where-clause)))diff --git a/src/metabase/search/init.clj b/src/metabase/search/init.cljindex 0979fcc541f..123f03ba911 100644--- a/src/metabase/search/init.clj+++ b/src/metabase/search/init.clj@@ -3,6 +3,7 @@(:require[metabase.search.appdb.core][metabase.search.appdb.metrics]+ [metabase.search.in-place.engine][metabase.search.in-place.legacy][metabase.search.models][metabase.search.semantic.core]diff --git a/src/metabase/search/schema.clj b/src/metabase/search/schema.cljnew file mode 100644index 00000000000..7c1b3f27201--- /dev/null+++ b/src/metabase/search/schema.clj@@ -0,0 +1,28 @@+(ns metabase.search.schema+ "Malli schemas for the search module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::search-index-metadata+ "A SearchIndexMetadata as selected from the app DB: every column of `:search_index_metadata`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:engine [:or :keyword :string]]+ [:version :string]+ [:index_name :string]+ [:status [:maybe [:or :keyword :string]]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:lang_code :string]])++(mr/def ::search-index-metadata.update+ "What an update (or insert) of a SearchIndexMetadata accepts: every column of `:search_index_metadata` except `id`, all optional."+ [:map {:closed true}+ [:engine {:optional true} [:maybe [:or :keyword :string]]]+ [:version {:optional true} [:maybe :string]]+ [:index_name {:optional true} [:maybe :string]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:lang_code {:optional true} [:maybe :string]]])diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.cljindex 351decf3fef..173b1cf0fb6 100644--- a/src/metabase/search/spec.clj+++ b/src/metabase/search/spec.clj@@ -9,7 +9,6 @@[malli.error :as me][metabase.config.core :as config][metabase.search.config :as search.config]- [metabase.search.db :as search.db][metabase.util :as u][metabase.util.json :as json][metabase.util.malli.registry :as mr]@@ -478,7 +477,6 @@(doseq [d (keys (model-hooks))](derive d :hook/search-index))- (search-models-to-update (search.db/any-card))(methods spec)(model-hooks)diff --git a/src/metabase/search/util.clj b/src/metabase/search/util.cljindex baf64cf1bf2..8138ea6393f 100644--- a/src/metabase/search/util.clj+++ b/src/metabase/search/util.clj@@ -3,7 +3,7 @@[clojure.string :as str][metabase.app-db.core :as mdb][metabase.search.db :as search.db]- [metabase.search.ingestion :as search.ingestion]+ [metabase.search.ingestion.query :as search.ingestion.query][metabase.search.settings :as search.settings][metabase.util :as u][metabase.util.i18n :as i18n]@@ -162,4 +162,4 @@(weighted-tsvector weight text (tsv-language)))([weight text lang];; tsvector has a max value size of 1048575 bytes, limit to less than that because the multiple values get concatenated together- [:setweight [:to_tsvector ^:allow-raw-sql [:inline lang] [:cast (u.str/limit-bytes text search.ingestion/max-searchable-value-length) :text]] ^:allow-raw-sql [:inline weight]]))+ [:setweight [:to_tsvector ^:allow-raw-sql [:inline lang] [:cast (u.str/limit-bytes text search.ingestion.query/max-searchable-value-length) :text]] ^:allow-raw-sql [:inline weight]]))diff --git a/src/metabase/secrets/db.clj b/src/metabase/secrets/db.cljindex 422c85bdd0b..cf23cf83127 100644--- a/src/metabase/secrets/db.clj+++ b/src/metabase/secrets/db.clj@@ -2,24 +2,35 @@"Application database queries for the secrets module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn latest-secret+(mu/defn latest-secret"The highest-version Secret with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Secret :id id {:order-by [[:version :desc]]}))-(defn insert-secret!+(mu/defn insert-secret!"Insert the Secret `row` and return the inserted instance."- [row]+ [row :- [:map {:closed true}+ [:id {:optional true} ms/PositiveInt]+ [:version {:optional true} [:maybe :int]]+ [:name {:optional true} [:maybe :string]]+ [:kind {:optional true} [:maybe [:or :keyword :string]]]+ [:source {:optional true} [:maybe [:or :keyword :string]]]+ [:value {:optional true} [:maybe [:or bytes? :string]]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]]](t2/insert-returning-instance! :model/Secret row))-(defn secret-version+(mu/defn secret-version"The Secret with `id` and `version`, or nil."- [id version]+ [id :- ms/PositiveInt+ version :- ms/PositiveInt](t2/select-one :model/Secret :id id :version version))-(defn delete-secret!- "Delete every version of the Secret with `id`."- [id]+(mu/defn delete-secret!+ "Delete every version of the Secret with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/Secret :id id))diff --git a/src/metabase/secrets/models/secret.clj b/src/metabase/secrets/models/secret.cljindex 480256eee97..2fcd3308812 100644--- a/src/metabase/secrets/models/secret.clj+++ b/src/metabase/secrets/models/secret.clj@@ -387,7 +387,8 @@(:value secret))](assoc cleared-details id-kw id))(do- (secrets.db/delete-secret! secret-id)+ (when secret-id+ (secrets.db/delete-secret! secret-id))(dissoc cleared-details id-kw)));; Don't throw out a secret even if the client didn't send it back(m/assoc-some cleared-details id-kw secret-id)))))]diff --git a/src/metabase/secrets/schema.clj b/src/metabase/secrets/schema.cljnew file mode 100644index 00000000000..d09a7812788--- /dev/null+++ b/src/metabase/secrets/schema.clj@@ -0,0 +1,31 @@+(ns metabase.secrets.schema+ "Malli schemas for the secrets module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::secret+ "A Secret as selected from the app DB: every column of `:secret`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:version :int]+ [:creator_id [:maybe ::lib.schema.id/user]]+ [:created_at ms/TemporalInstant]+ [:updated_at [:maybe ms/TemporalInstant]]+ [:name :string]+ [:kind [:or :keyword :string]]+ [:source [:maybe [:or :keyword :string]]]+ [:value [:or bytes? :string]]])++(mr/def ::secret.update+ "What an update (or insert) of a Secret accepts: every column of `:secret` except `id`, all optional."+ [:map {:closed true}+ [:version {:optional true} [:maybe :int]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:kind {:optional true} [:maybe [:or :keyword :string]]]+ [:source {:optional true} [:maybe [:or :keyword :string]]]+ [:value {:optional true} [:maybe [:or bytes? :string]]]])diff --git a/src/metabase/security_center/schema.clj b/src/metabase/security_center/schema.cljnew file mode 100644index 00000000000..3fd4fe4a67e--- /dev/null+++ b/src/metabase/security_center/schema.clj@@ -0,0 +1,87 @@+(ns metabase.security-center.schema+ "Malli schemas for the Security Center module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::advisory-id+ [:string {:min 1}])++(mr/def ::semver+ [:re #"^\d+(?:\.\d+)*$"])++(mr/def ::severity+ [:enum :critical :high :medium :low])++(mr/def ::match-status+ [:enum :unknown :active :resolved :not_affected :error])++(mr/def ::version-range+ "A single affected version range with inclusive min and exclusive fixed."+ [:map+ [:min ::semver]+ [:fixed ::semver]])++(mr/def ::affected-versions+ [:sequential ::version-range])++(mr/def ::download-jar-url+ "A downloadable JAR for a given fixed version."+ [:map+ [:version ::semver]+ [:url :string]])++(mr/def ::download-jar-urls+ [:sequential ::download-jar-url])++(mr/def ::matching-query+ "HoneySQL query keyed by dialect. nil means affects all instances.+ Stored as EDN to preserve keywords that HoneySQL requires for identifiers/operators."+ [:maybe [:map-of :keyword :map]])++(mr/def ::security-advisory.matching-query+ "The `:matching_query` column of a SecurityAdvisory, decoded."+ :map)++(mr/def ::security-advisory+ "A SecurityAdvisory as selected from the app DB: every column of `:security_advisory`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:advisory_id :string]+ [:severity [:or :keyword :string]]+ [:title :string]+ [:description :string]+ [:advisory_url [:maybe :string]]+ [:remediation :string]+ [:affected_versions ::affected-versions]+ [:matching_query [:maybe ::security-advisory.matching-query]]+ [:published_at ms/TemporalInstant]+ [:fetched_at ms/TemporalInstant]+ [:match_status [:or :keyword :string]]+ [:last_evaluated_at [:maybe ms/TemporalInstant]]+ [:acknowledged_by [:maybe :int]]+ [:acknowledged_at [:maybe ms/TemporalInstant]]+ [:last_notified_at [:maybe ms/TemporalInstant]]+ [:updated_at ms/TemporalInstant]+ [:download_jar_urls [:maybe ::download-jar-urls]]])++(mr/def ::security-advisory.update+ "What an update (or insert) of a SecurityAdvisory accepts: every column of `:security_advisory` except `id`, all optional."+ [:map {:closed true}+ [:advisory_id {:optional true} [:maybe :string]]+ [:severity {:optional true} [:maybe [:or :keyword :string]]]+ [:title {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:advisory_url {:optional true} [:maybe :string]]+ [:remediation {:optional true} [:maybe :string]]+ [:affected_versions {:optional true} [:maybe ::affected-versions]]+ [:matching_query {:optional true} [:maybe ::security-advisory.matching-query]]+ [:published_at {:optional true} [:maybe ms/TemporalInstant]]+ [:fetched_at {:optional true} [:maybe ms/TemporalInstant]]+ [:match_status {:optional true} [:maybe [:or :keyword :string]]]+ [:last_evaluated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:acknowledged_by {:optional true} [:maybe :int]]+ [:acknowledged_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_notified_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:download_jar_urls {:optional true} [:maybe ::download-jar-urls]]])diff --git a/src/metabase/segments/db.clj b/src/metabase/segments/db.cljindex 86bc2f07d31..2c67d0d10ee 100644--- a/src/metabase/segments/db.clj+++ b/src/metabase/segments/db.clj@@ -2,11 +2,19 @@"Application database queries for the segments module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.segments.schema :as segments.schema]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn insert-segment!+(mu/defn insert-segment!"Insert a Segment and return the inserted instance."- [table-id creator-id segment-name description definition]+ [table-id :- ::lib.schema.id/table+ creator-id :- ::lib.schema.id/user+ segment-name :- :string+ description :- [:maybe :string]+ definition :- ::segments.schema/segment.definition](t2/insert-returning-instance! :model/Segment:table_id table-id:creator_id creator-id@@ -14,42 +22,43 @@:description description:definition definition))-(defn segment+(mu/defn segment"The Segment with `id`, or nil."- [id]+ [id :- ::lib.schema.id/segment](t2/select-one :model/Segment :id id))-(defn unarchived-segments+(mu/defn unarchived-segments"The unarchived Segments, in case-insensitive name order."[](t2/select :model/Segment :archived false {:order-by [[:%lower.name :asc]]}))-(defn table-database-ids+(mu/defn table-database-ids"The set of Database ids of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-fn-set :db_id :model/Table :id [:in table-ids]))-(defn update-segment!+(mu/defn update-segment!"Apply `changes` to the Segment with `id`."- [id changes]+ [id :- ::lib.schema.id/segment+ changes :- (mut/select-keys ::segments.schema/segment.update [:description :caveats :points_of_interest :archived :definition :name :show_in_getting_started])](t2/update! :model/Segment id changes))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table :id table-id))-(defn table-perms-columns+(mu/defn table-perms-columns"The Database id, schema, and id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/Table :db_id :schema :id] :id table-id))-(defn collections+(mu/defn collections"The Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select :model/Collection :id [:in collection-ids]))diff --git a/src/metabase/segments/schema.clj b/src/metabase/segments/schema.cljindex 81dfbd5144d..bcb3bd140cb 100644--- a/src/metabase/segments/schema.clj+++ b/src/metabase/segments/schema.clj@@ -2,8 +2,10 @@(:require[metabase.lib-be.schema :as lib-be.schema][metabase.lib.core :as lib]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.i18n :refer [deferred-tru]]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::definition"Schema for a segment's `:definition`; accepts a full MBQL query, converting legacy MBQL on decode."@@ -23,3 +25,40 @@(empty? (lib/aggregations query))(empty? (lib/order-bys query))(nil? (lib/current-limit query))))]]])++(mr/def ::segment.definition+ "The `:definition` column of a Segment, decoded."+ :map)++(mr/def ::segment+ "A Segment as selected from the app DB: every column of `:segment`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:table_id ::lib.schema.id/table]+ [:creator_id ::lib.schema.id/user]+ [:name :string]+ [:description [:maybe :string]]+ [:archived :boolean]+ [:definition [:maybe ::segment.definition]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:points_of_interest [:maybe :string]]+ [:caveats [:maybe :string]]+ [:show_in_getting_started :boolean]+ [:entity_id :string]])++(mr/def ::segment.update+ "What an update (or insert) of a Segment accepts: every column of `:segment` except `id`, all optional."+ [:map {:closed true}+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:archived {:optional true} [:maybe :boolean]]+ [:definition {:optional true} [:maybe ::segment.definition]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:caveats {:optional true} [:maybe :string]]+ [:show_in_getting_started {:optional true} [:maybe :boolean]]+ [:entity_id {:optional true} [:maybe :string]]])diff --git a/src/metabase/server/db.clj b/src/metabase/server/db.cljindex 0011f9155e7..f1fa7932ed4 100644--- a/src/metabase/server/db.clj+++ b/src/metabase/server/db.clj@@ -4,7 +4,9 @@(:require[honey.sql.helpers :as sql.helpers][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu][toucan2.core :as t2][toucan2.pipeline :as t2.pipeline]))@@ -110,12 +112,17 @@[:= :pgm.user_id :user.id][:is :pgm.is_group_manager true]]))))))))-(defn session-user-info+(mu/defn session-user-info"The user id, superuser/data-analyst/group-manager flags, locale, and auth provider for the active, unexpiredSession whose `key_hashed` is `session-key-hash`, or nil if there is none. `anti-csrf-token`, when present,additionally requires the Session's `anti_csrf_token` to match it (a full-app-embed session); `max-age-minutes`and `session-timeout-seconds` (which may be nil) bound how old or idle the Session may be."- [session-key-hash anti-csrf-token max-age-minutes enable-advanced-permissions? enable-tenants? session-timeout-seconds]+ [session-key-hash :- :string+ anti-csrf-token :- [:maybe :string]+ max-age-minutes :- [:maybe :int]+ enable-advanced-permissions? :- :boolean+ enable-tenants? :- :boolean+ session-timeout-seconds :- [:maybe :int]](let [sql (session-with-id-query (mdb/db-type)max-age-minutes(if (seq anti-csrf-token) :full-app-embed :normal)@@ -125,21 +132,23 @@params (concat [session-key-hash] (when (seq anti-csrf-token) [anti-csrf-token]))](t2/query-one (cons sql params))))-(defn api-key-user-info+(mu/defn api-key-user-info"The user id, api key, superuser/data-analyst/group-manager flags, and locale for the active User whose ApiKeystarts with `key-prefix`, or nil if there is none."- [key-prefix enable-advanced-permissions?]+ [key-prefix :- :string+ enable-advanced-permissions? :- :boolean](t2/query-one (cons (user-data-for-api-key-prefix-query enable-advanced-permissions?) [key-prefix])))-(defn oauth-user-info+(mu/defn oauth-user-info"The user id, superuser/data-analyst/group-manager flags, and locale for the active User with `user-id`, or nil ifthere is none."- [user-id enable-advanced-permissions?]+ [user-id :- ::lib.schema.id/user+ enable-advanced-permissions? :- :boolean](t2/query-one (cons (user-data-for-id-query enable-advanced-permissions?) [user-id])))-(defn touch-session!+(mu/defn touch-session!"Set `last_active_at` of the Session with `key-hashed` to now."- [key-hashed]+ [key-hashed :- :string](t2/query-one {:update (t2/table-name :model/Session):set {:last_active_at :%now}:where [:= :key_hashed key-hashed]}))diff --git a/src/metabase/session/db.clj b/src/metabase/session/db.cljindex ae00191c010..84538285e49 100644--- a/src/metabase/session/db.clj+++ b/src/metabase/session/db.clj@@ -4,25 +4,30 @@(:require[metabase.app-db.core :as mdb][metabase.auth-identity.db :as auth-identity.db]+ [metabase.auth-identity.schema :as auth-identity.schema]+ [metabase.lib.schema.id :as lib.schema.id][metabase.tracing.core :as tracing][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn delete-session-by-key-hashed!+(mu/defn delete-session-by-key-hashed!"Delete the Session with `key-hashed`, returning the number of rows deleted."- [key-hashed]+ [key-hashed :- :string](t2/delete! :model/Session :key_hashed key-hashed))-(defn delete-sessions-for-user!+(mu/defn delete-sessions-for-user!"Delete every Session of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/Session :user_id user-id))-(defn delete-expired-sessions!+(mu/defn delete-expired-sessions!"Delete Sessions older than `max-age-minutes`, past their own `expires_at`, or (when `idle-timeout-seconds` isgiven) idle longer than `idle-timeout-seconds`. Returns the number of rows deleted."- [max-age-minutes idle-timeout-seconds]+ [max-age-minutes :- ms/PositiveInt+ idle-timeout-seconds :- [:maybe ms/PositiveInt]](let [db-type (mdb/db-type)now (h2x/current-datetime-honeysql-form db-type)oldest-allowed (h2x/add-interval-honeysql-form db-type now (- max-age-minutes) :minute)@@ -37,38 +42,41 @@(tracing/with-span :tasks "task.session-cleanup.delete" {:db/statement (tracing/best-effort-sanitize-sql hsql)}(t2/query-one hsql))))-(defn auth-identity-for-provider+(mu/defn auth-identity-for-provider"The AuthIdentity of the User with `user-id` at `provider`, or nil. See `metabase.auth-identity.db/auth-identity`,which owns the AuthIdentity table."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](auth-identity.db/auth-identity user-id provider))-(defn auth-identity-exists?+(mu/defn auth-identity-exists?"Whether the User with `user-id` has an AuthIdentity at `provider`."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](auth-identity.db/auth-identity-exists? user-id provider))-(defn set-auth-identity-credentials!+(mu/defn set-auth-identity-credentials!"Set the `credentials` of the AuthIdentity with `auth-identity-id`."- [auth-identity-id credentials]+ [auth-identity-id :- ms/PositiveInt+ credentials :- [:maybe ::auth-identity.schema/auth-identity.credentials]](t2/update! :model/AuthIdentity auth-identity-id {:credentials credentials}))-(defn auth-identity-provider+(mu/defn auth-identity-provider"The `:provider` of the AuthIdentity with `auth-identity-id`, or nil."- [auth-identity-id]+ [auth-identity-id :- ms/PositiveInt](t2/select-one [:model/AuthIdentity :provider] :id auth-identity-id))-(defn user-by-email+(mu/defn user-by-email"The id, SSO source, and active flag of the User whose email matches `email` case-insensitively, or nil."- [email]+ [email :- :string](t2/select-one [:model/User :id :sso_source :is_active] :%lower.email (u/lower-case-en email)))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User :id user-id))-(defn user-login-status+(mu/defn user-login-status"The id, active flag, last login, and tenant id of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :is_active :last_login :tenant_id] :id user-id))diff --git a/src/metabase/session/schema.clj b/src/metabase/session/schema.cljindex 2a3785e065d..f6a75c3f743 100644--- a/src/metabase/session/schema.clj+++ b/src/metabase/session/schema.clj@@ -1,4 +1,8 @@-(ns metabase.session.schema)+(ns metabase.session.schema+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(def SessionSchema"Schema for a Session."@@ -7,3 +11,26 @@[:map[:key string?][:type [:enum :normal :full-app-embed]]]])++(mr/def ::session+ "A Session as selected from the app DB: every column of `:core_session`."+ [:map {:closed true}+ [:id :string]+ [:user_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:anti_csrf_token [:maybe :string]]+ [:key_hashed :string]+ [:auth_identity_id [:maybe ms/PositiveInt]]+ [:expires_at [:maybe ms/TemporalInstant]]+ [:last_active_at [:maybe ms/TemporalInstant]]])++(mr/def ::session.update+ "What an update (or insert) of a Session accepts: every column of `:core_session` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:anti_csrf_token {:optional true} [:maybe :string]]+ [:key_hashed {:optional true} [:maybe :string]]+ [:auth_identity_id {:optional true} [:maybe ms/PositiveInt]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_active_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/settings/db.clj b/src/metabase/settings/db.cljindex e16ff494605..2c50086af4a 100644--- a/src/metabase/settings/db.clj+++ b/src/metabase/settings/db.clj@@ -3,51 +3,61 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions."(:require[metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn setting-value+(mu/defn setting-value"The stored value of the Setting with `setting-key`, or nil."- [setting-key]+ [setting-key :- [:or :keyword :string]](t2/select-one-fn :value :model/Setting :key setting-key))-(defn setting-values-by-key+(mu/defn setting-values-by-key"A map of key to stored value for every Setting."[](t2/select-fn->fn :key :value :model/Setting))-(defn insert-setting!+(mu/defn insert-setting!"Insert a Setting row for `setting-key` holding `value` and return it."- [setting-key value]+ [setting-key :- [:or :keyword :string]+ value :- :string](first (t2/insert-returning-instances! :model/Setting :key setting-key :value value)))-(defn update-setting-value!- "Set the value of the Setting with `setting-key` to `value`."- [setting-key value]+(mu/defn update-setting-value!+ "Set the value of the Setting with `setting-key` to `value`, returning the number updated."+ [setting-key :- [:or :keyword :string]+ value :- :string](t2/update! :model/Setting :key setting-key {:value value}))-(defn delete-setting!- "Delete the Setting with `setting-key`, via a raw table delete that bypasses model hooks."- [setting-key]+(mu/defn delete-setting!+ "Delete the Setting with `setting-key`, via a raw table delete that bypasses model hooks, returning the number+ deleted."+ [setting-key :- [:or :keyword :string]](t2/delete! (t2/table-name :model/Setting) :key setting-key))-(defn update-raw-setting-row!+(mu/defn update-raw-setting-row!"Set the raw `value` and `value-with-aad` of the Setting row with `setting-key`, returning the number of rowsupdated."- [setting-key value value-with-aad]+ [setting-key :- [:or :keyword :string]+ value :- [:maybe :string]+ value-with-aad :- [:maybe :string]](t2/update! :setting {:key setting-key} {:value value, :value_with_aad value-with-aad}))-(defn insert-raw-setting-row!- "Insert a raw Setting row for `setting-key` holding `value` and `value-with-aad`."- [setting-key value value-with-aad]+(mu/defn insert-raw-setting-row!+ "Insert a raw Setting row for `setting-key` holding `value` and `value-with-aad`, returning the number inserted."+ [setting-key :- [:or :keyword :string]+ value :- [:maybe :string]+ value-with-aad :- [:maybe :string]](t2/insert! (t2/table-name (t2/resolve-model :model/Setting)):key setting-key, :value value, :value_with_aad value-with-aad))-(defn current-timestamp-string+(mu/defn current-timestamp-string"The application DB's own current timestamp, as a string."[](mdb/current-timestamp-string (mdb/db-type)))-(defn update-user-settings!- "Store `settings-json` as the user-local settings of the User with `user-id`."- [user-id settings-json]+(mu/defn update-user-settings!+ "Store `settings-json` as the user-local settings of the User with `user-id`, returning the number updated."+ [user-id :- ::lib.schema.id/user+ settings-json :- :string](t2/update! :model/User user-id {:settings settings-json}))diff --git a/src/metabase/settings/schema.clj b/src/metabase/settings/schema.cljnew file mode 100644index 00000000000..ea078e44ad6--- /dev/null+++ b/src/metabase/settings/schema.clj@@ -0,0 +1,18 @@+(ns metabase.settings.schema+ "Malli schemas for the settings module."+ (:require+ [metabase.util.malli.registry :as mr]))++(mr/def ::setting+ "A Setting as selected from the app DB: every column of `:setting`."+ [:map {:closed true}+ [:key :string]+ [:value :string]+ [:value_with_aad [:maybe :string]]])++(mr/def ::setting.update+ "What an update (or insert) of a Setting accepts: every column of `:setting` except `id`, all optional."+ [:map {:closed true}+ [:key {:optional true} [:maybe :string]]+ [:value {:optional true} [:maybe :string]]+ [:value_with_aad {:optional true} [:maybe :string]]])diff --git a/src/metabase/setup/db.clj b/src/metabase/setup/db.cljindex 36cdfc266f1..004a81113f0 100644--- a/src/metabase/setup/db.clj+++ b/src/metabase/setup/db.clj@@ -2,9 +2,11 @@"Application database queries for the setup module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn users-except+(mu/defn users-except"Every User other than the one with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select :model/User {:where [:not= :id user-id]}))diff --git a/src/metabase/setup_rest/db.clj b/src/metabase/setup_rest/db.cljindex 61c2cb15c4c..e249bf3c324 100644--- a/src/metabase/setup_rest/db.clj+++ b/src/metabase/setup_rest/db.clj@@ -2,18 +2,22 @@"Application database queries for the setup REST module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn insert-superuser!+(mu/defn insert-superuser!"Insert a superuser with the given email and name and return the User instance."- [email first-name last-name]+ [email :- :string+ first-name :- [:maybe :string]+ last-name :- [:maybe :string]](t2/insert-returning-instance! :model/User:email email:first_name first-name:last_name last-name:is_superuser true))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User :id user-id))diff --git a/src/metabase/slackbot/db.clj b/src/metabase/slackbot/db.cljindex 6a2188f30af..2d6c400cb74 100644--- a/src/metabase/slackbot/db.clj+++ b/src/metabase/slackbot/db.clj@@ -2,11 +2,13 @@"Application database queries for the Slack bot module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn active-slack-connect-identity+(mu/defn active-slack-connect-identity"The user id and metadata of the newest Slack Connect AuthIdentity of an active User for `slack-user-id`, or nil."- [slack-user-id]+ [slack-user-id :- :string](t2/select-one [:model/AuthIdentity :user_id :metadata]:provider "slack-connect":provider_id slack-user-id@@ -14,23 +16,26 @@:where [:= :user.is_active true]:order-by [[:created_at :desc]]}))-(defn metabot-message-external-id+(mu/defn metabot-message-external-id"The external id of the MetabotMessage posted to `channel-id` as `slack-msg-id`, or nil."- [channel-id slack-msg-id]+ [channel-id :- :string+ slack-msg-id :- :string](t2/select-one-fn :external_id :model/MetabotMessage :channel_id channel-id :slack_msg_id slack-msg-id))-(defn assistant-messages-by-slack-ids+(mu/defn assistant-messages-by-slack-ids"The non-deleted assistant MetabotMessages of `conversation-id` posted as one of `slack-msg-ids`."- [conversation-id slack-msg-ids]+ [conversation-id :- :string+ slack-msg-ids :- [:set :string]](t2/select :model/MetabotMessage:conversation_id conversation-id:role "assistant":deleted_at nil:slack_msg_id [:in slack-msg-ids]))-(defn deleted-assistant-slack-msg-ids+(mu/defn deleted-assistant-slack-msg-ids"The Slack message ids among `slack-msg-ids` of deleted assistant MetabotMessages of `conversation-id`."- [conversation-id slack-msg-ids]+ [conversation-id :- :string+ slack-msg-ids :- [:set :string]](t2/select-fn-set :slack_msg_id:model/MetabotMessage:conversation_id conversation-id@@ -38,31 +43,32 @@:deleted_at [:not= nil]:slack_msg_id [:in slack-msg-ids]))-(defn assistant-state-messages+(mu/defn assistant-state-messages"The id, role, state, error, and finished flag of the non-deleted assistant MetabotMessages of `conversation-id`,oldest first."- [conversation-id]+ [conversation-id :- :string](t2/select [:model/MetabotMessage :id :role :state :error :finished]:conversation_id conversation-id:role "assistant":deleted_at nil{:order-by [[:created_at :asc] [:id :asc]]}))-(defn assistant-response-user-id+(mu/defn assistant-response-user-id"The id of the User who triggered the assistant MetabotMessage posted to `channel-id` as `slack-msg-id`, or nil."- [channel-id slack-msg-id]+ [channel-id :- :string+ slack-msg-id :- :string](t2/select-one-fn :user_id:model/MetabotMessage:channel_id channel-id:slack_msg_id slack-msg-id:role "assistant"))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))diff --git a/src/metabase/sql_tools/db.clj b/src/metabase/sql_tools/db.cljindex 3c50e341405..67f7f759274 100644--- a/src/metabase/sql_tools/db.clj+++ b/src/metabase/sql_tools/db.clj@@ -2,13 +2,16 @@"Application database queries for the SQL tools module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn active-visible-table-ids-by-name+(mu/defn active-visible-table-ids-by-name"Ids of the active, non-hidden Tables of Database `database-id` whose name case-insensitively matches one of`table-names`, or nil."- [database-id table-names]+ [database-id :- ::lib.schema.id/database+ table-names :- [:sequential :string]](t2/select-pks-set :model/Table{:where [:and[:= :db_id database-id]diff --git a/src/metabase/sso/db.clj b/src/metabase/sso/db.cljindex 3627b57c076..9e8490b3809 100644--- a/src/metabase/sso/db.clj+++ b/src/metabase/sso/db.clj@@ -2,37 +2,49 @@"Application database queries for the SSO module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself."(:require+ [metabase.auth-identity.schema :as auth-identity.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn user-group-ids-excluding+(mu/defn user-group-ids-excluding"The ids of the PermissionsGroups the User with `user-id` belongs to, other than `excluded-group-ids`."- [user-id excluded-group-ids]+ [user-id :- ::lib.schema.id/user+ excluded-group-ids :- [:set ms/PositiveInt]](t2/select-fn-set :group_id :model/PermissionsGroupMembership{:where [:and[:= :user_id user-id][:not-in :group_id excluded-group-ids]]}))-(defn user-group-ids-among+(mu/defn user-group-ids-among"The ids among `group-ids` of the PermissionsGroups the User with `user-id` belongs to, other than`excluded-group-ids`."- [user-id group-ids excluded-group-ids]+ [user-id :- ::lib.schema.id/user+ group-ids :- [:set ms/PositiveInt]+ excluded-group-ids :- [:set ms/PositiveInt]](t2/select-fn-set :group_id :model/PermissionsGroupMembership{:where [:and[:= :user_id user-id][:in :group_id group-ids][:not-in :group_id excluded-group-ids]]}))-(defn auth-identity-exists?+(mu/defn auth-identity-exists?"Whether the User with `user-id` has an AuthIdentity for `provider`."- [user-id provider]+ [user-id :- ::lib.schema.id/user+ provider :- :string](t2/exists? :model/AuthIdentity :user_id user-id :provider provider))-(defn insert-auth-identity!+(mu/defn insert-auth-identity!"Insert an AuthIdentity linking the User with `user-id` to `provider-id` at `provider`."- [user-id provider provider-id]+ [user-id :- ::lib.schema.id/user+ provider :- :string+ provider-id :- :string](t2/insert! :model/AuthIdentity {:user_id user-id, :provider provider, :provider_id provider-id}))-(defn set-auth-identity-metadata!+(mu/defn set-auth-identity-metadata!"Set the `metadata` of the AuthIdentity of the User with `user-id` at `provider`."- [user-id provider metadata]+ [user-id :- ::lib.schema.id/user+ provider :- :string+ metadata :- [:maybe ::auth-identity.schema/auth-identity.metadata]](t2/update! :model/AuthIdentity {:user_id user-id, :provider provider} {:metadata metadata}))diff --git a/src/metabase/sync/db.clj b/src/metabase/sync/db.cljindex 7d80eebe165..2d51533589a 100644--- a/src/metabase/sync/db.clj+++ b/src/metabase/sync/db.clj@@ -5,9 +5,14 @@[clojure.set :as set][honey.sql :as sql][metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][metabase.warehouse-schema.models.table :as table]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))(def ^:private sync-tables-clause@@ -16,34 +21,36 @@;;; ------------------------------------------------ Database -------------------------------------------------(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn attached-dwh-database+(mu/defn attached-dwh-database"The attached data warehouse Database, or nil."[](t2/select-one :model/Database :is_attached_dwh true))-(defn database-stub?+(mu/defn database-stub?"Whether the Database with `database-id` is a stub."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :is_stub :model/Database :id database-id))-(defn database-on-demand-flags+(mu/defn database-on-demand-flags"A map of Database ID to its `:is_on_demand` flag for `database-ids`."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select-pk->fn :is_on_demand :model/Database :id [:in database-ids]))-(defn synced-user-database-exists?+(mu/defn synced-user-database-exists?"Whether any non-sample, non-audit Database has completed its initial sync."[](t2/exists? :model/Database :is_sample false :is_audit false :initial_sync_status "complete"))-(defn databases-with-schedules-reducible+(mu/defn databases-with-schedules-reducible"Reducible raw Database rows whose sync schedules are the sample or default ones."- [old-sample-metadata-cron metadata-crons cache-field-values-crons]+ [old-sample-metadata-cron :- :string+ metadata-crons :- [:set :string]+ cache-field-values-crons :- [:set :string]](t2/reducible-query {:select [:*]:from [:metabase_database]:where [:or@@ -53,109 +60,125 @@[:in :metadata_sync_schedule metadata-crons][:in :cache_field_values_schedule cache-field-values-crons]]}))-(defn update-database!- "Apply `changes` to the Database with `database-id`."- [database-id changes]+(mu/defn update-database!+ "Apply `changes` to the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ changes :- ::warehouses.schema/database.update](t2/update! :model/Database database-id changes));;; ------------------------------------------------- Table --------------------------------------------------(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn table-in-database+(mu/defn table-in-database"The Table with `table-id` in the Database with `database-id`, or nil."- [database-id table-id]+ [database-id :- ::lib.schema.id/database+ table-id :- ::lib.schema.id/table](t2/select-one :model/Table :db_id database-id :id table-id))-(defn table-by-name+(mu/defn table-by-name"The Table named `table-name` in the Database with `database-id`, or nil."- [database-id table-name]+ [database-id :- ::lib.schema.id/database+ table-name :- :string](t2/select-one :model/Table :db_id database-id :name table-name))-(defn table-by-schema-and-name+(mu/defn table-by-schema-and-name"The Table named `table-name` in `schema` of the Database with `database-id`, or nil."- [database-id schema table-name]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/select-one :model/Table :db_id database-id :name table-name :schema schema))-(defn inactive-table-by-schema-and-name+(mu/defn inactive-table-by-schema-and-name"The inactive Table named `table-name` in `schema` of the Database with `database-id`, or nil."- [database-id schema table-name]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/select-one :model/Table :db_id database-id :schema schema :name table-name :active false))-(defn active-table-id-by-name+(mu/defn active-table-id-by-name"The ID of the active Table named `table-name` in the Database with `database-id`, or nil."- [database-id table-name]+ [database-id :- ::lib.schema.id/database+ table-name :- :string](t2/select-one-pk :model/Table :db_id database-id :name table-name :active true))-(defn sync-tables-by-lower-name-and-schema+(mu/defn sync-tables-by-lower-name-and-schema"The synced Tables of the Database with `database-id` whose lower-cased name and schema match."- [database-id lower-name lower-schema]+ [database-id :- ::lib.schema.id/database+ lower-name :- :string+ lower-schema :- [:maybe :string]](t2/select :model/Table:db_id database-id:%lower.name lower-name:%lower.schema lower-schema{:where sync-tables-clause}))-(defn tables-by-name+(mu/defn tables-by-name"The `columns` of the Tables of the Database with `database-id` named one of `table-names`."- [columns database-id table-names]+ [columns :- [:sequential :keyword]+ database-id :- ::lib.schema.id/database+ table-names :- [:sequential :string]](t2/select columns :db_id database-id :name [:in table-names]))-(defn tables-to-archive- "The inactive, unarchived, non-transform-target Tables of the Database with `database-id` deactivated before the- SQL expression `deactivated-before`."- [database-id deactivated-before]+(mu/defn tables-to-archive+ "The inactive, unarchived, non-transform-target Tables of the Database with `database-id` deactivated more than+ `amount` `unit`s (e.g. `-14 :day`) before the app DB's now."+ [database-id :- ::lib.schema.id/database+ amount :- :int+ unit :- :keyword](t2/select :model/Table:db_id database-id:active false:archived_at nil:transform_target false- :deactivated_at [:< deactivated-before]))+ :deactivated_at [:< (h2x/add-interval-honeysql-form (app-db/db-type) :%now amount unit)]))-(defn table-database-ids+(mu/defn table-database-ids"A map of Table ID to Database ID for `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select-pk->fn :db_id :model/Table :id [:in table-ids]))-(defn table-schemas-reducible+(mu/defn table-schemas-reducible"Reducible `:schema` rows of the Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-select [:model/Table :schema] :db_id database-id))-(defn active-table-ids-reducible+(mu/defn active-table-ids-reducible"Reducible `:id` rows of the active Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-select [:model/Table :id] :db_id database-id :active true))-(defn active-table-count+(mu/defn active-table-count"The number of active Tables in the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/count :model/Table :db_id database-id :active true))-(defn sync-table-ids+(mu/defn sync-table-ids"The IDs of the synced Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-fn-vec :id :model/Table :db_id database-id {:where sync-tables-clause}))-(defn sync-table-schemas+(mu/defn sync-table-schemas"The distinct `:schema` rows of the synced Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/query {:select-distinct [:schema]:from [:metabase_table]:where [:and sync-tables-clause [:= :db_id database-id]]}))-(defn sync-tables-count+(mu/defn sync-tables-count"The number of synced Tables in the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/count :model/Table :db_id database-id {:where sync-tables-clause}))-(defn sync-tables-reducible+(mu/defn sync-tables-reducible"Reducible synced Tables of the Database with `database-id` ordered by schema and name, optionally narrowed to`schema-names` and/or `table-names`."- [database-id schema-names table-names]+ [database-id :- ::lib.schema.id/database+ schema-names :- [:maybe [:or [:set :string] [:sequential :string]]]+ table-names :- [:maybe [:or [:set :string] [:sequential :string]]]](t2/reducible-select :model/Table:db_id database-id{:where [:and sync-tables-clause@@ -163,9 +186,9 @@(when (seq table-names) [:in :name table-names])]:order-by [[:schema :asc] [:name :asc]]}))-(defn sync-tables-by-earliest-analyzed-reducible+(mu/defn sync-tables-by-earliest-analyzed-reducible"Reducible synced Tables of the Database with `database-id` ordered by the earliest `last_analyzed` of their Fields."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-select :model/Table{:select [:t.*]:from [[(t2/table-name :model/Table) :t]]@@ -177,46 +200,51 @@:where [:and sync-tables-clause [:= :t.db_id database-id]]:order-by [[:sub.earliest_last_analyzed :asc]]}))-(defn insert-table!+(mu/defn insert-table!"Insert `table` and return the new instance."- [table]+ [table :- ::warehouse-schema.schema/table.update](t2/insert-returning-instance! :model/Table table))-(defn update-table!- "Apply `changes` to the Table with `table-id`."- [table-id changes]+(mu/defn update-table!+ "Apply `changes` to the Table with `table-id`, returning the number updated."+ [table-id :- ::lib.schema.id/table+ changes :- ::warehouse-schema.schema/table.update](t2/update! :model/Table table-id changes))-(defn update-tables!- "Apply `changes` to the Tables with `table-ids`."- [table-ids changes]+(mu/defn update-tables!+ "Apply `changes` to the Tables with `table-ids`, returning the number updated."+ [table-ids :- [:sequential ::lib.schema.id/table]+ changes :- ::warehouse-schema.schema/table.update](t2/update! :model/Table :id [:in table-ids] changes))-(defn deactivate-tables!- "Mark the active Tables among `table-ids` inactive."- [table-ids]+(mu/defn deactivate-tables!+ "Mark the active Tables among `table-ids` inactive, returning the number updated."+ [table-ids :- [:set ::lib.schema.id/table]](t2/update! :model/Table {:id [:in table-ids] :active true} {:active false}))-(defn rename-table-schema!- "Move the Tables of the Database with `database-id` from `schema` to `new-schema`."- [database-id schema new-schema]+(mu/defn rename-table-schema!+ "Move the Tables of the Database with `database-id` from `schema` to `new-schema`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ new-schema :- [:maybe :string]](t2/update! :model/Table :db_id database-id :schema schema {:schema new-schema}))-(defn archive-inactive-table!- "Archive the inactive Table with `table-id` at `archived-at` under `new-name`, returning the number of rows updated."- [table-id archived-at new-name]- (t2/update! :model/Table {:id table-id :active false} {:archived_at archived-at :name new-name}))+(mu/defn archive-inactive-table!+ "Archive the inactive Table with `table-id` now under `new-name`, returning the number of rows updated."+ [table-id :- ::lib.schema.id/table+ new-name :- :string]+ (t2/update! :model/Table {:id table-id :active false} {:archived_at :%now :name new-name}));;; ------------------------------------------------- Field --------------------------------------------------(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn fields-for-field-values+(mu/defn fields-for-field-values"The columns needed to scan FieldValues of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select [:model/Field :name :id :base_type :effective_type :coercion_strategy :semantic_type :visibility_type:table_id :has_field_values]:id [:in field-ids]))@@ -266,10 +294,13 @@(cond-> fields-to-fingerprint-base-clause(not refingerprint?) (conj (fingerprint-version-clauses version->base-types))))-(defn fields-needing-fingerprint-update+(mu/defn fields-needing-fingerprint-update"Up to `limit` active, visible Fields of the Table with `table-id` whose fingerprint needs to be re-calculated,ordered by ID. See [[needs-fingerprint-update-clause]] for the full matching criteria."- [table-id refingerprint? version->base-types limit]+ [table-id :- ::lib.schema.id/table+ refingerprint? :- :boolean+ version->base-types :- [:map-of :int [:set [:or :keyword :string]]]+ limit :- ms/PositiveInt](t2/select :model/Field{:where [:and[:= :table_id table-id]@@ -277,14 +308,14 @@:order-by [[:id :asc]]:limit limit}))-(defn field-fingerprint+(mu/defn field-fingerprint"The fingerprint of the Field with `field-id`."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :fingerprint :model/Field :id field-id))-(defn active-fields-metadata-for-table+(mu/defn active-fields-metadata-for-table"The sync metadata columns of the active Fields of the Table with `table-id`, in field order."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select [:model/Field :name :database_type :base_type :effective_type :coercion_strategy :semantic_type:parent_id :id :description :database_position :nfc_path:database_is_auto_increment :database_required@@ -294,27 +325,31 @@:active true{:order-by table/field-order-rule}))-(defn normal-fields-for-table+(mu/defn normal-fields-for-table"Up to `limit` active, normal-visibility Fields of the Table with `table-id`, ordered by ID."- [table-id limit]+ [table-id :- ::lib.schema.id/table+ limit :- ms/PositiveInt](t2/select :model/Field:table_id table-id:active true:visibility_type "normal"{:order-by [[:id :asc]], :limit limit}))-(defn inactive-fields-by-lower-name+(mu/defn inactive-fields-by-lower-name"The inactive Fields of the Table with `table-id` under `parent-id` whose lower-cased name is one of `lower-names`."- [table-id parent-id lower-names]+ [table-id :- ::lib.schema.id/table+ parent-id :- [:maybe ms/PositiveInt]+ lower-names :- [:sequential :string]](t2/select :model/Field:table_id table-id:%lower.name [:in lower-names]:parent_id parent-id:active false))-(defn incomplete-analysis-fields-for-table+(mu/defn incomplete-analysis-fields-for-table"The active, visible Fields of the Table with `table-id` fingerprinted at `fingerprint-version` but not yet analyzed."- [table-id fingerprint-version]+ [table-id :- ::lib.schema.id/table+ fingerprint-version :- :int](t2/select :model/Field:table_id table-id:active true@@ -322,18 +357,18 @@:fingerprint_version fingerprint-version:last_analyzed nil))-(defn name-field-count-for-table+(mu/defn name-field-count-for-table"The number of active, visible Fields of the Table with `table-id` whose semantic type is `:type/Name`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/count :model/Field:table_id table-id:active true:visibility_type [:not-in ["sensitive" "retired"]]:semantic_type :type/Name))-(defn unscored-fields-for-database-reducible+(mu/defn unscored-fields-for-database-reducible"Reducible active, visible Fields of the Database with `database-id` without a dimension interestingness score."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-select :model/Field{:where [:and[:= :active true]@@ -343,15 +378,17 @@:from [(t2/table-name :model/Table)]:where [:= :db_id database-id]}]]}))-(defn top-level-field-ids-by-name+(mu/defn top-level-field-ids-by-name"The IDs of the top-level Fields of the Table with `table-id` named one of `field-names`."- [table-id field-names]+ [table-id :- ::lib.schema.id/table+ field-names :- [:sequential :string]](t2/select-pks-vec :model/Field :name [:in field-names] :table_id table-id :parent_id nil))-(defn top-level-field-ids-by-schema-table-and-name-reducible+(mu/defn top-level-field-ids-by-schema-table-and-name-reducible"Reducible `:id` rows of the top-level Fields of the Database with `database-id` matching one of the`[schema table-name field-name]` triples in `schema+table+names`, with a nil schema spelled `\"__null__\"`."- [database-id schema+table+names]+ [database-id :- ::lib.schema.id/database+ schema+table+names :- [:sequential [:tuple :string :string :string]]](t2/reducible-query {:select [[:f.id]]:from [[(t2/table-name :model/Field) :f]]:inner-join [[(t2/table-name :model/Table) :t] [:= :f.table_id :t.id]]@@ -360,14 +397,14 @@[:= :t.db_id database-id][:= :parent_id nil]]}))-(defn indexed-field-ids-for-table+(mu/defn indexed-field-ids-for-table"The IDs of the Fields of the Table with `table-id` marked as indexed."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field :table_id table-id :database_indexed true))-(defn indexed-top-level-field-ids-for-database+(mu/defn indexed-top-level-field-ids-for-database"The IDs of the top-level Fields of the Database with `database-id` marked as indexed."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-pks-set :model/Field:table_id [:in ^:allow-subquery {:select [[:t.id]]:from [[(t2/table-name :model/Table) :t]]@@ -375,57 +412,65 @@:parent_id nil:database_indexed true))-(defn insert-fields!+(mu/defn insert-fields!"Insert the Field `rows` and return their IDs."- [rows]+ [rows :- [:sequential ::warehouse-schema.schema/field.update]](t2/insert-returning-pks! :model/Field rows))-(defn update-field!- "Apply `changes` to the Field with `field-id`."- [field-id changes]+(mu/defn update-field!+ "Apply `changes` to the Field with `field-id`, returning the number updated."+ [field-id :- ::lib.schema.id/field+ changes :- ::warehouse-schema.schema/field.update](t2/update! :model/Field field-id changes))-(defn update-field-by-name!- "Apply `changes` to the Field named `field-name` of the Table with `table-id`."- [table-id field-name changes]+(mu/defn update-field-by-name!+ "Apply `changes` to the Field named `field-name` of the Table with `table-id`, returning the number updated."+ [table-id :- ::lib.schema.id/table+ field-name :- :string+ changes :- ::warehouse-schema.schema/field.update](t2/update! :model/Field {:name field-name, :table_id table-id} changes))-(defn reactivate-fields!- "Mark the Fields with `field-ids` active."- [field-ids]+(mu/defn reactivate-fields!+ "Mark the Fields with `field-ids` active, returning the number updated."+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/update! :model/Field {:id [:in field-ids]} {:active true}))-(defn set-fields-fingerprint-version!- "Set the fingerprint version of the Fields with `field-ids` to `fingerprint-version`."- [field-ids fingerprint-version]+(mu/defn set-fields-fingerprint-version!+ "Set the fingerprint version of the Fields with `field-ids` to `fingerprint-version`, returning the number updated."+ [field-ids :- [:sequential ::lib.schema.id/field]+ fingerprint-version :- :int](t2/update! :model/Field :id [:in field-ids] {:fingerprint_version fingerprint-version}))-(defn set-table-fields-indexed!+(mu/defn set-table-fields-indexed!"Mark the Fields of the Table with `table-id` whose id is in `indexed-field-ids` as indexed, and all its other- Fields as not indexed."- [table-id indexed-field-ids]+ Fields as not indexed, returning the number updated."+ [table-id :- ::lib.schema.id/table+ indexed-field-ids :- [:maybe [:sequential ::lib.schema.id/field]]](t2/update! :model/Field {:table_id table-id}{:database_indexed (if (seq indexed-field-ids)[:case [:in :id indexed-field-ids] true :else false]false)}))-(defn set-top-level-fields-indexed!- "Set `database_indexed` of the top-level Fields with `field-ids` to `indexed?`."- [field-ids indexed?]+(mu/defn set-top-level-fields-indexed!+ "Set `database_indexed` of the top-level Fields with `field-ids` to `indexed?`, returning the number updated."+ [field-ids :- [:sequential ::lib.schema.id/field]+ indexed? :- :boolean](t2/update! :model/Field :parent_id nil :id [:in field-ids] {:database_indexed indexed?}))-(defn mark-incomplete-fields-analyzed-for-table!+(mu/defn mark-incomplete-fields-analyzed-for-table!"Stamp `last_analyzed` on the Fields of the Table with `table-id` fingerprinted at `fingerprint-version` but not- yet analyzed."- [table-id fingerprint-version]+ yet analyzed, returning the number updated."+ [table-id :- ::lib.schema.id/table+ fingerprint-version :- :int](t2/update! :model/Field{:table_id table-id, :fingerprint_version fingerprint-version, :last_analyzed nil}{:last_analyzed :%now}))-(defn mark-incomplete-fields-analyzed-for-database!+(mu/defn mark-incomplete-fields-analyzed-for-database!"Stamp `last_analyzed` on the Fields of the synced Tables of the Database with `database-id` fingerprinted at- `fingerprint-version` but not yet analyzed."- [database-id fingerprint-version]+ `fingerprint-version` but not yet analyzed, returning the number updated."+ [database-id :- ::lib.schema.id/database+ fingerprint-version :- :int](t2/update! :model/Field{:fingerprint_version fingerprint-version:last_analyzed nil@@ -505,11 +550,17 @@(fk-target-changed-clause pk-field-id-query)]})](sql/format q :dialect (app-db/quoting-style (app-db/db-type)))))-(defn mark-fk!+(mu/defn mark-fk!"Set the `fk_target_field_id` of the Field at `[fk-table-schema fk-table-name fk-column-name]` in the Database with`db-id` to the id of the Field at `[pk-table-schema pk-table-name pk-column-name]`, unless it already points there.Returns 1 if a Field was updated, 0 otherwise."- [db-id fk-table-schema fk-table-name fk-column-name pk-table-schema pk-table-name pk-column-name]+ [db-id :- ::lib.schema.id/database+ fk-table-schema :- [:maybe :string]+ fk-table-name :- :string+ fk-column-name :- :string+ pk-table-schema :- [:maybe :string]+ pk-table-name :- :string+ pk-column-name :- :string](t2/query-one (mark-fk-statement db-id fk-table-schema fk-table-name fk-column-namepk-table-schema pk-table-name pk-column-name)))@@ -523,10 +574,11 @@[:or [:= :data_sensitivity nil] [:= :data_sensitivity "PUBLIC"]][:= :data_sensitivity nil]))-(defn fields-to-scan-for-data-sensitivity+(mu/defn fields-to-scan-for-data-sensitivity"The active, non-retired Fields of the Table with `table-id` that the data-sensitivity classifier still has to scan(see [[data-sensitivity-to-scan-clause]]), ordered by ID."- [table-id rescan-public?]+ [table-id :- ::lib.schema.id/table+ rescan-public? :- [:maybe :boolean]](t2/select :model/Field{:where [:and[:= :table_id table-id]@@ -535,10 +587,11 @@(data-sensitivity-to-scan-clause rescan-public?)]:order-by [[:id :asc]]}))-(defn table-ids-with-fields-to-scan-for-data-sensitivity+(mu/defn table-ids-with-fields-to-scan-for-data-sensitivity"The IDs of the active Tables of the Database with `database-id` that have active, non-retired Fields thedata-sensitivity classifier still has to scan (see [[data-sensitivity-to-scan-clause]])."- [database-id rescan-public?]+ [database-id :- ::lib.schema.id/database+ rescan-public? :- [:maybe :boolean]](t2/select-fn-set :table_id :model/Field{:select [[:metabase_field.table_id :table_id]]:from [:metabase_field]@@ -551,14 +604,15 @@(data-sensitivity-to-scan-clause rescan-public?)]:group-by [:metabase_field.table_id]}))-(defn tables-by-schema-and-name-reducible+(mu/defn tables-by-schema-and-name-reducible"Reducible Tables with `table-ids`, ordered by schema and name."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/reducible-select :model/Table :id [:in table-ids] {:order-by [[:schema :asc] [:name :asc]]}))-(defn update-field-data-sensitivity!- "Set the `data_sensitivity` of the Field with `field-id` to `data-sensitivity`."- [field-id data-sensitivity]+(mu/defn update-field-data-sensitivity!+ "Set the `data_sensitivity` of the Field with `field-id` to `data-sensitivity`, returning the number updated."+ [field-id :- ::lib.schema.id/field+ data-sensitivity :- [:or :keyword :string]](t2/update! :model/Field field-id {:data_sensitivity data-sensitivity}))(def ^:private classifier-data-sensitivity-clause@@ -572,18 +626,18 @@[:= :s.field_id :metabase_field.id][:not= :s.data_sensitivity nil]]}]]])-(defn reset-classifier-data-sensitivity-for-table!+(mu/defn reset-classifier-data-sensitivity-for-table!"Clear the classifier-written `data_sensitivity` (see [[classifier-data-sensitivity-clause]]) of the Fields of theTable with `table-id`. Returns the number of Fields cleared."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/query-one {:update :metabase_field:set {:data_sensitivity nil}:where [:and [:= :table_id table-id] classifier-data-sensitivity-clause]}))-(defn reset-classifier-data-sensitivity-for-database!+(mu/defn reset-classifier-data-sensitivity-for-database!"Clear the classifier-written `data_sensitivity` (see [[classifier-data-sensitivity-clause]]) of the Fields of everyTable of the Database with `database-id`. Returns the number of Fields cleared."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/query-one {:update :metabase_field:set {:data_sensitivity nil}:where [:and@@ -594,9 +648,9 @@;;; ---------------------------------------------- FieldValues -----------------------------------------------(defn field-values-exist?+(mu/defn field-values-exist?"Whether the Field with `field-id` has FieldValues."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/exists? :model/FieldValues :field_id field-id))(defn- before-max-age-value@@ -604,14 +658,18 @@[max-age-days][:< (h2x/add-interval-honeysql-form (app-db/db-type) :%now (- max-age-days) :day)])-(defn advanced-field-values-count-before+(mu/defn advanced-field-values-count-before"The number of FieldValues of `types` for the Field with `field-id` created more than `max-age-days` days ago."- [field-id types max-age-days]+ [field-id :- ::lib.schema.id/field+ types :- [:set :keyword]+ max-age-days :- :int](t2/count :model/FieldValues :field_id field-id :type [:in types]:created_at (before-max-age-value max-age-days)))-(defn delete-advanced-field-values-before!+(mu/defn delete-advanced-field-values-before!"Delete the FieldValues of `types` for the Field with `field-id` created more than `max-age-days` days ago."- [field-id types max-age-days]+ [field-id :- ::lib.schema.id/field+ types :- [:set :keyword]+ max-age-days :- :int](t2/delete! :model/FieldValues :field_id field-id :type [:in types]:created_at (before-max-age-value max-age-days)))diff --git a/src/metabase/sync/sync_metadata/tables.clj b/src/metabase/sync/sync_metadata/tables.cljindex e22c08fc3ea..91051bd84ab 100644--- a/src/metabase/sync/sync_metadata/tables.clj+++ b/src/metabase/sync/sync_metadata/tables.clj@@ -5,7 +5,6 @@[clojure.set :as set][java-time.api :as t][medley.core :as m]- [metabase.app-db.core :as mdb][metabase.driver :as driver][metabase.driver.util :as driver.u][metabase.lib.schema.common :as lib.schema.common]@@ -365,10 +364,8 @@;; it doesn't matter much, the source of time truth is `archived_at`,;; we're just using this as a cheap namespacesuffix (str "__mbarchiv__" (.toEpochSecond (t/offset-date-time)))- threshold-expr (apply- (requiring-resolve 'metabase.util.honey-sql-2/add-interval-honeysql-form)- (mdb/db-type) :%now archive-tables-threshold)- tables-to-archive (sync.db/tables-to-archive (u/the-id database) threshold-expr)+ [amount unit] archive-tables-threshold+ tables-to-archive (sync.db/tables-to-archive (u/the-id database) amount unit)archived (atom 0)](doseq [table tables-to-archive:let [new-name (str (:name table) suffix)]]@@ -384,7 +381,7 @@;; in the extremely unlikely case that there already exists a table with our;; archived name, we let it fail from hitting the unique constraints violation;; and just report the failure- [(sync.db/archive-inactive-table! (:id table) (mi/now) new-name)]+ [(sync.db/archive-inactive-table! (:id table) new-name)](catch Throwable t[0 t]))](when (zero? did-update)diff --git a/src/metabase/task_history/db.clj b/src/metabase/task_history/db.cljindex bbed033da04..8f0ce3f8883 100644--- a/src/metabase/task_history/db.clj+++ b/src/metabase/task_history/db.clj@@ -2,27 +2,30 @@"Application database queries for the task history module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.task-history.schema :as task-history.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn database-names-by-id+(mu/defn database-names-by-id"A map of id to name for the Databases with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :name :model/Database :id [:in ids]))-(defn card-names-by-id+(mu/defn card-names-by-id"A map of id to name for the Cards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :name :model/Card :id [:in ids]))-(defn dashboard-names-by-id+(mu/defn dashboard-names-by-id"A map of id to name for the Dashboards with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :name :model/Dashboard :id [:in ids]))-(defn task-counts-for-runs+(mu/defn task-counts-for-runs"The total, successful, and failed TaskHistory counts of the TaskRuns with `run-ids`, grouped by run."- [run-ids]+ [run-ids :- [:sequential ms/PositiveInt]](t2/query {:select [:run_id[[:count :id] :task_count][[:sum [:case [:= :status (h2x/literal "success")] [:inline 1] :else [:inline 0]]] :success_count]@@ -74,33 +77,48 @@{:order-by [[sort-column sort-direction] secondary]})))-(defn task-runs+(def ^:private RunFilters+ [:map {:closed true}+ [:run-type [:maybe :string]]+ [:entity-type [:maybe :string]]+ [:entity-id [:maybe ms/PositiveInt]]+ [:status [:maybe [:or :keyword :string]]]+ [:started-at-start [:maybe ms/TemporalInstant]]+ [:started-at-end [:maybe ms/TemporalInstant]]])++(mu/defn task-runs"Up to `limit` (offset by `offset`) TaskRuns matching `filters` (see [[run-where]] for the supported keys), sortedby `sort-column`/`sort-direction`."- [filters sort-column sort-direction limit offset]+ [filters :- RunFilters+ sort-column :- [:enum :started_at :ended_at :run_type :status :entity_name :task_count]+ sort-direction :- [:enum :asc :desc]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/TaskRun(cond-> (merge (run-where filters) (runs-order-by sort-column sort-direction))limit (assoc :limit limit)offset (assoc :offset offset))))-(defn task-run-count+(mu/defn task-run-count"The number of TaskRuns matching `filters` (see [[run-where]] for the supported keys)."- [filters]+ [filters :- RunFilters](t2/count :model/TaskRun (or (run-where filters) {})))-(defn task-run+(mu/defn task-run"The TaskRun with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/TaskRun :id id))-(defn tasks-for-run+(mu/defn tasks-for-run"The TaskHistory rows of the TaskRun with `run-id`, oldest first."- [run-id]+ [run-id :- ms/PositiveInt](t2/select :model/TaskHistory :run_id run-id {:order-by [[:started_at :asc]]}))-(defn distinct-run-entities+(mu/defn distinct-run-entities"The distinct entity type and id of the TaskRuns of `run-type` started in [`started-at-start`, `started-at-end`)."- [run-type started-at-start started-at-end]+ [run-type :- :string+ started-at-start :- [:maybe ms/TemporalInstant]+ started-at-end :- [:maybe ms/TemporalInstant]](t2/query {:select-distinct [:entity_type :entity_id]:from :task_run:where (into [:and [:= :run_type run-type]]@@ -108,16 +126,16 @@[(when started-at-start [:>= :started_at started-at-start])(when started-at-end [:< :started_at started-at-end])])}))-(defn nth-newest-task-history-ended-at+(mu/defn nth-newest-task-history-ended-at"The `ended_at` of the TaskHistory row `offset` rows from the most recently ended, or nil."- [offset]+ [offset :- ms/IntGreaterThanOrEqualToZero](t2/select-one-fn :ended_at :model/TaskHistory {:limit 1:offset offset:order-by [[:ended_at :desc]]}))-(defn delete-task-history-ended-before!+(mu/defn delete-task-history-ended-before!"Delete the TaskHistory rows that ended at or before `ended-before`."- [ended-before]+ [ended-before :- ms/TemporalInstant](t2/delete! (t2/table-name :model/TaskHistory) :ended_at [:<= ended-before]))(def ^:private join-sort-columns@@ -133,11 +151,16 @@task (conj [:= :task_history.task task])status (conj [:= :task_history.status (name status)]))))-(defn task-histories+(mu/defn task-histories"Up to `limit` (offset by `offset`) TaskHistory rows, optionally narrowed to `status` and/or `task`, sorted by`sort-column` (an allow-listed column, joining to Database for `:db_name`/`:db_engine`) and `sort-direction`, with`:id desc` as a stable tiebreaker."- [status task sort-column sort-direction limit offset]+ [status :- [:maybe [:enum :started :success :failed :unknown]]+ task :- [:maybe ms/NonBlankString]+ sort-column :- [:enum :started_at :ended_at :duration :task :status :db_name :db_engine]+ sort-direction :- [:enum :asc :desc]+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]](t2/select :model/TaskHistory(cond-> (if-let [where (task-history-where status task)]{:where where}@@ -153,50 +176,55 @@limit (assoc :limit limit)offset (assoc :offset offset))))-(defn task-history-count+(mu/defn task-history-count"The number of TaskHistory rows, optionally narrowed to `status` and/or `task`."- [status task]+ [status :- [:maybe [:enum :started :success :failed :unknown]]+ task :- [:maybe ms/NonBlankString]](t2/count :model/TaskHistory (if-let [where (task-history-where status task)] {:where where} {})))-(defn distinct-task-names+(mu/defn distinct-task-names"The distinct task names of the TaskHistory rows, in alphabetical order."[](t2/select-fn-vec :task [:model/TaskHistory :task] {:group-by [:task]:order-by [:task]}))-(defn update-task-history!+(mu/defn update-task-history!"Apply `changes` to the TaskHistory row with `id`."- [id changes]+ [id :- ms/PositiveInt+ changes :- ::task-history.schema/task-history.update](t2/update! :model/TaskHistory id changes))-(defn insert-task-history!+(mu/defn insert-task-history!"Insert the TaskHistory `row` and return its id."- [row]+ [row :- ::task-history.schema/task-history.update](t2/insert-returning-pk! :model/TaskHistory row))-(defn insert-task-run!+(mu/defn insert-task-run!"Insert the TaskRun `row` and return its id."- [row]+ [row :- ::task-history.schema/task-run.update](t2/insert-returning-pk! :model/TaskRun row))-(defn task-statuses-for-run+(mu/defn task-statuses-for-run"The set of statuses of the TaskHistory rows of the TaskRun with `run-id`."- [run-id]+ [run-id :- ms/PositiveInt](t2/select-fn-set :status :model/TaskHistory :run_id run-id))-(defn finish-started-task-run!+(mu/defn finish-started-task-run!"Set the status and end time of the started TaskRun with `run-id`."- [run-id status ended-at]+ [run-id :- ms/PositiveInt+ status :- :keyword+ ended-at :- ms/TemporalInstant](t2/update! :model/TaskRun {:id run-id :status :started} {:status status, :ended_at ended-at}))-(defn heartbeat-started-task-runs!- "Touch `updated_at` of the started TaskRuns of `process-uuid`, returning the number updated."- [process-uuid updated-at]- (t2/update! :model/TaskRun {:status :started, :process_uuid process-uuid} {:updated_at updated-at}))+(mu/defn heartbeat-started-task-runs!+ "Touch `updated_at` of the started TaskRuns of `process-uuid` with the app DB's now, returning the number updated."+ [process-uuid :- :string]+ (t2/update! :model/TaskRun {:status :started, :process_uuid process-uuid} {:updated_at :%now}))-(defn mark-started-tasks-unknown!- "Set the started TaskHistory rows of the TaskRuns with `run-ids` to unknown, returning the number updated."- [run-ids ended-at]+(mu/defn mark-started-tasks-unknown!+ "Set the started TaskHistory rows of the TaskRuns with `run-ids` to unknown, ended at the app DB's now, returning+ the number updated."+ [run-ids :- [:set ms/PositiveInt]](t2/update! :model/TaskHistory{:status :started, :run_id [:in run-ids]}- {:status :unknown, :ended_at ended-at}))+ {:status :unknown, :ended_at :%now}))diff --git a/src/metabase/task_history/models/task_run.clj b/src/metabase/task_history/models/task_run.cljindex 9aba1b67471..02bf0de1b42 100644--- a/src/metabase/task_history/models/task_run.clj+++ b/src/metabase/task_history/models/task_run.clj@@ -43,6 +43,10 @@[status](assert (task-run-status (keyword status)) "Invalid task run status"))+(t2/define-before-insert :model/TaskRun+ [task-run]+ (merge {:started_at (mi/now), :updated_at (mi/now)} task-run))+(t2/define-after-insert :model/TaskRun[task-run](assert-task-run-status (:status task-run))@@ -93,16 +97,13 @@(mu/defn create-task-run! :- ms/PositiveInt"Create a new task run record. Returns the run ID."[{:keys [run_type entity_type entity_id notification_id]} :- ::TaskRunInfo]- (let [now (mi/now)]- (task-history.db/insert-task-run!- {:run_type run_type- :entity_type entity_type- :entity_id entity_id- :notification_id notification_id- :status :started- :started_at now- :updated_at now- :process_uuid config/local-process-uuid})))+ (task-history.db/insert-task-run!+ {:run_type run_type+ :entity_type entity_type+ :entity_id entity_id+ :notification_id notification_id+ :status :started+ :process_uuid config/local-process-uuid}))(mu/defn complete-task-run!"Mark a task run as complete, deriving status from child tasks.diff --git a/src/metabase/task_history/schema.clj b/src/metabase/task_history/schema.cljnew file mode 100644index 00000000000..6c3047a5948--- /dev/null+++ b/src/metabase/task_history/schema.clj@@ -0,0 +1,68 @@+(ns metabase.task-history.schema+ "Malli schemas for the task-history module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::task-history.task-details+ "The `:task_details` column of a TaskHistory, decoded."+ :map)++(mr/def ::task-history.log+ "One entry of the `:logs` column of a TaskHistory, decoded."+ :map)++(mr/def ::task-history+ "A TaskHistory as selected from the app DB: every column of `:task_history`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:task :string]+ [:db_id [:maybe ::lib.schema.id/database]]+ [:started_at ms/TemporalInstant]+ [:ended_at [:maybe ms/TemporalInstant]]+ [:duration [:maybe :int]]+ [:task_details [:maybe ::task-history.task-details]]+ [:status [:or :keyword :string]]+ [:run_id [:maybe ms/PositiveInt]]+ [:logs [:maybe [:sequential ::task-history.log]]]])++(mr/def ::task-history.update+ "What an update (or insert) of a TaskHistory accepts: every column of `:task_history` except `id`, all optional."+ [:map {:closed true}+ [:task {:optional true} [:maybe :string]]+ [:db_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:ended_at {:optional true} [:maybe ms/TemporalInstant]]+ [:duration {:optional true} [:maybe :int]]+ [:task_details {:optional true} [:maybe ::task-history.task-details]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:run_id {:optional true} [:maybe ms/PositiveInt]]+ [:logs {:optional true} [:maybe [:sequential ::task-history.log]]]])++(mr/def ::task-run+ "A TaskRun as selected from the app DB: every column of `:task_run`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:run_type [:or :keyword :string]]+ [:entity_type [:or :keyword :string]]+ [:entity_id ms/PositiveInt]+ [:started_at ms/TemporalInstant]+ [:ended_at [:maybe ms/TemporalInstant]]+ [:status [:or :keyword :string]]+ [:process_uuid :string]+ [:updated_at ms/TemporalInstant]+ [:notification_id [:maybe ms/PositiveInt]]])++(mr/def ::task-run.update+ "What an update (or insert) of a TaskRun accepts: every column of `:task_run` except `id`, all optional."+ [:map {:closed true}+ [:run_type {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_id {:optional true} [:maybe ms/PositiveInt]]+ [:started_at {:optional true} [:maybe ms/TemporalInstant]]+ [:ended_at {:optional true} [:maybe ms/TemporalInstant]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:process_uuid {:optional true} [:maybe :string]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:notification_id {:optional true} [:maybe ms/PositiveInt]]])diff --git a/src/metabase/task_history/task/task_run_heartbeat.clj b/src/metabase/task_history/task/task_run_heartbeat.cljindex 52bbb1cd431..24dbb814e8b 100644--- a/src/metabase/task_history/task/task_run_heartbeat.clj+++ b/src/metabase/task_history/task/task_run_heartbeat.clj@@ -5,7 +5,6 @@3. Mark orphaned tasks (in :started status with no heartbeat) as :unknown"(:require[metabase.config.core :as config]- [metabase.models.interface :as mi][metabase.run-tracking.core :as rt][metabase.task-history.db :as task-history.db][metabase.task.core :as task]@@ -26,7 +25,7 @@"Update updated_at for all :started runs belonging to this process."[](tracing/with-span :tasks "task.heartbeat.update" {}- (let [updated (task-history.db/heartbeat-started-task-runs! config/local-process-uuid (mi/now))]+ (let [updated (task-history.db/heartbeat-started-task-runs! config/local-process-uuid)](when (pos? updated)(log/debugf "Sent heartbeat for %d running task runs" updated))updated)))@@ -39,11 +38,10 @@[](let [orphaned (tracing/with-span :tasks "task.heartbeat.mark-orphaned-runs" {}(rt/reap-rows! {:model :model/TaskRun- :active [:= :status "started"]- :terminal {:status "abandoned" :ended_at (mi/now)}- :stale [:or- [:< :updated_at (rt/cutoff orphan-threshold-hours :hour)]- [:< :started_at (rt/cutoff max-run-duration-hours :hour)]]}))]+ :active [:status "started"]+ :terminal {:status "abandoned" :ended_at :%now}+ :stale [{:column :updated_at :age orphan-threshold-hours :unit :hour}+ {:column :started_at :age max-run-duration-hours :unit :hour}]}))](into #{} (map :id) orphaned)))(defn mark-orphaned-tasks!@@ -51,7 +49,7 @@[orphaned-run-ids](when (seq orphaned-run-ids)(tracing/with-span :tasks "task.heartbeat.mark-orphaned-tasks" {:heartbeat/orphaned-run-count (count orphaned-run-ids)}- (let [orphaned (task-history.db/mark-started-tasks-unknown! orphaned-run-ids (mi/now))]+ (let [orphaned (task-history.db/mark-started-tasks-unknown! orphaned-run-ids)](when (pos? orphaned)(log/infof "Marked %d orphaned tasks as :unknown" orphaned))orphaned))))diff --git a/src/metabase/testing_api/db.clj b/src/metabase/testing_api/db.cljindex dcbd2c2b2ac..92047a8d85e 100644--- a/src/metabase/testing_api/db.clj+++ b/src/metabase/testing_api/db.clj@@ -2,74 +2,85 @@"Application database queries for the testing API module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.metabot.schema :as metabot.schema]+ [metabase.security-center.schema :as security-center.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn set-card-last-used-at!+(mu/defn set-card-last-used-at!"Set `last_used_at` of the Card with `card-id`."- [card-id last-used-at]+ [card-id :- ::lib.schema.id/card+ last-used-at :- ms/TemporalInstant](t2/update! :model/Card :id card-id {:last_used_at last-used-at}))-(defn set-dashboard-last-viewed-at!+(mu/defn set-dashboard-last-viewed-at!"Set `last_viewed_at` of the Dashboard with `dashboard-id`."- [dashboard-id last-viewed-at]+ [dashboard-id :- ::lib.schema.id/dashboard+ last-viewed-at :- ms/TemporalInstant](t2/update! :model/Dashboard :id dashboard-id {:last_viewed_at last-viewed-at}))-(defn delete-all-security-advisories!+(mu/defn delete-all-security-advisories!"Delete every SecurityAdvisory."[](t2/delete! :model/SecurityAdvisory))-(defn insert-security-advisories!+(mu/defn insert-security-advisories!"Insert `advisories` and return the inserted SecurityAdvisory instances."- [advisories]+ [advisories :- [:sequential+ ::security-center.schema/security-advisory.update]](t2/insert-returning-instances! :model/SecurityAdvisory advisories))-(defn permissions-group-id+(mu/defn permissions-group-id"The id of the PermissionsGroup named `group-name`, or nil."- [group-name]+ [group-name :- :string](t2/select-one-pk :model/PermissionsGroup :name group-name))-(defn insert-permissions-group!+(mu/defn insert-permissions-group!"Insert a PermissionsGroup named `group-name` and return its id."- [group-name]+ [group-name :- :string](t2/insert-returning-pk! :model/PermissionsGroup {:name group-name}))-(defn group-membership-exists?+(mu/defn group-membership-exists?"Whether the User with `user-id` is a member of the PermissionsGroup with `group-id`."- [user-id group-id]+ [user-id :- ::lib.schema.id/user+ group-id :- ms/PositiveInt](t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id group-id))-(defn delete-ai-usage-logs-for-conversations!+(mu/defn delete-ai-usage-logs-for-conversations!"Delete the AiUsageLog rows for `conversation-ids`."- [conversation-ids]+ [conversation-ids :- [:sequential :string]](t2/delete! :model/AiUsageLog {:where [:in :conversation_id conversation-ids]}))-(defn delete-metabot-conversations!+(mu/defn delete-metabot-conversations!"Delete the MetabotConversations with `conversation-ids`."- [conversation-ids]+ [conversation-ids :- [:sequential :string]](t2/delete! :model/MetabotConversation {:where [:in :id conversation-ids]}))-(defn insert-metabot-conversation!+(mu/defn insert-metabot-conversation!"Insert the MetabotConversation `row`."- [row]+ [row :- ::metabot.schema/metabot-conversation.update](t2/insert! :model/MetabotConversation row))-(defn insert-metabot-message!+(mu/defn insert-metabot-message!"Insert the MetabotMessage `row`."- [row]+ [row :- ::metabot.schema/metabot-message.update](t2/insert! :model/MetabotMessage row))-(defn insert-ai-usage-log!+(mu/defn insert-ai-usage-log!"Insert the AiUsageLog `row`."- [row]+ [row :- ::metabot.schema/ai-usage-log.update](t2/insert! :model/AiUsageLog row))-(defn set-user-tenant!+(mu/defn set-user-tenant!"Set the `tenant_id` of the User with `user-id`."- [user-id tenant-id]+ [user-id :- ::lib.schema.id/user+ tenant-id :- ms/PositiveInt](t2/update! :model/User user-id {:tenant_id tenant-id}))-(defn delete-ai-usage-logs-for-user-and-source!+(mu/defn delete-ai-usage-logs-for-user-and-source!"Delete the AiUsageLog rows of the User with `user-id` from `source`, returning the number deleted."- [user-id source]+ [user-id :- ::lib.schema.id/user+ source :- :string](t2/delete! :model/AiUsageLog :user_id user-id :source source))diff --git a/src/metabase/tiles/db.clj b/src/metabase/tiles/db.cljindex 5bc5df404d6..7edb716fab7 100644--- a/src/metabase/tiles/db.clj+++ b/src/metabase/tiles/db.clj@@ -2,19 +2,21 @@"Application database queries for the tiles module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module never talks to `toucan2.core` itself."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card card-id))-(defn dashboard+(mu/defn dashboard"The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-one :model/Dashboard dashboard-id))-(defn dashcard+(mu/defn dashcard"The DashboardCard with `dashcard-id`, or nil."- [dashcard-id]+ [dashcard-id :- ::lib.schema.id/dashcard](t2/select-one :model/DashboardCard dashcard-id))diff --git a/src/metabase/timeline/db.clj b/src/metabase/timeline/db.cljindex c1d38783cca..a963e9892f1 100644--- a/src/metabase/timeline/db.clj+++ b/src/metabase/timeline/db.clj@@ -2,78 +2,90 @@"Application database queries for the timeline module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.timeline.schema :as timeline.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn insert-timeline!+(mu/defn insert-timeline!"Insert the Timeline `row` and return the inserted instance."- [row]+ [row :- (mut/select-keys ::timeline.schema/timeline.update [:name :creator_id :default :description :icon :collection_id :archived])](t2/insert-returning-instance! :model/Timeline row))-(defn timelines-in-collections+(mu/defn timelines-in-collections"The Timelines whose archived flag is `archived` in the Collections matching the Honey SQL `collection-clause`, incase-insensitive name order."- [archived collection-clause]+ [archived :- :boolean+ collection-clause :- [:maybe vector?]](t2/select :model/Timeline{:where [:and[:= :archived archived]collection-clause]:order-by [[:%lower.name :asc]]}))-(defn timeline+(mu/defn timeline"The Timeline with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/Timeline :id id))-(defn timeline-icon+(mu/defn timeline-icon"The icon of the Timeline with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one-fn :icon :model/Timeline :id id))-(defn timelines-by-id+(mu/defn timelines-by-id"A map of id to Timeline for the Timelines with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn identity :model/Timeline :id [:in ids]))-(defn timelines-for-collection+(mu/defn timelines-for-collection"The Timelines of the Collection with `collection-id` whose archived flag is `archived`."- [collection-id archived]+ [collection-id :- [:maybe ::lib.schema.id/collection]+ archived :- :boolean](t2/select :model/Timeline :collection_id collection-id :archived archived))-(defn update-timeline!- "Apply `changes` to the Timeline with `id`."- [id changes]+(mu/defn update-timeline!+ "Apply `changes` to the Timeline with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::timeline.schema/timeline.update [:name :default :description :icon :collection_id :archived])](t2/update! :model/Timeline id changes))-(defn set-timeline-events-archived!- "Set the archived flag of the TimelineEvents of the Timeline with `timeline-id`."- [timeline-id archived]+(mu/defn set-timeline-events-archived!+ "Set the archived flag of the TimelineEvents of the Timeline with `timeline-id`, returning the number updated."+ [timeline-id :- ms/PositiveInt+ archived :- :boolean](t2/update! :model/TimelineEvent {:timeline_id timeline-id} {:archived archived}))-(defn delete-timeline!- "Delete the Timeline with `id`."- [id]+(mu/defn delete-timeline!+ "Delete the Timeline with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/Timeline :id id))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id collection-id))-(defn insert-timeline-event!+(mu/defn insert-timeline-event!"Insert the TimelineEvent `row` and return the inserted instance."- [row]+ [row :- (mut/select-keys ::timeline.schema/timeline-event.update [:name :timestamp :timezone :timeline_id :creator_id :description :time_matters :icon :archived])](t2/insert-returning-instance! :model/TimelineEvent row))-(defn timeline-event+(mu/defn timeline-event"The TimelineEvent with `id`, or nil."- [id]+ [id :- ms/PositiveInt](t2/select-one :model/TimelineEvent :id id))-(defn timeline-events-for-timelines+(mu/defn timeline-events-for-timelines"The TimelineEvents of the Timelines with `timeline-ids`, unarchived only unless `all?`, and (when `start` and/or`end` are given) within that time range (respecting each event's `:time_matters` flag)."- [timeline-ids all? start end]+ [timeline-ids :- [:sequential ms/PositiveInt]+ all? :- [:maybe :boolean]+ start :- [:maybe ms/TemporalInstant]+ end :- [:maybe ms/TemporalInstant]](t2/select :model/TimelineEvent{:where [:and[:in :timeline_id timeline-ids]@@ -94,12 +106,13 @@(when end[:<= (h2x/->date :timestamp) (h2x/->date end)])]])]}))-(defn update-timeline-event!- "Apply `changes` to the TimelineEvent with `id`."- [id changes]+(mu/defn update-timeline-event!+ "Apply `changes` to the TimelineEvent with `id`, returning the number updated."+ [id :- ms/PositiveInt+ changes :- (mut/select-keys ::timeline.schema/timeline-event.update [:name :description :timestamp :time_matters :timezone :icon :timeline_id :archived])](t2/update! :model/TimelineEvent id changes))-(defn delete-timeline-event!- "Delete the TimelineEvent with `id`."- [id]+(mu/defn delete-timeline-event!+ "Delete the TimelineEvent with `id`, returning the number deleted."+ [id :- ms/PositiveInt](t2/delete! :model/TimelineEvent :id id))diff --git a/src/metabase/timeline/schema.clj b/src/metabase/timeline/schema.cljnew file mode 100644index 00000000000..f8f1165bf15--- /dev/null+++ b/src/metabase/timeline/schema.clj@@ -0,0 +1,66 @@+(ns metabase.timeline.schema+ "Malli schemas for the timeline module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::timeline+ "A Timeline as selected from the app DB: every column of `:timeline`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name :string]+ [:description [:maybe :string]]+ [:icon :string]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:archived :boolean]+ [:creator_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:default :boolean]+ [:entity_id :string]])++(mr/def ::timeline.update+ "What an update (or insert) of a Timeline accepts: every column of `:timeline` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:icon {:optional true} [:maybe :string]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:archived {:optional true} [:maybe :boolean]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:default {:optional true} [:maybe :boolean]]+ [:entity_id {:optional true} [:maybe :string]]])++(mr/def ::timeline-event+ "A TimelineEvent as selected from the app DB: every column of `:timeline_event`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:timeline_id ms/PositiveInt]+ [:name :string]+ [:description [:maybe :string]]+ [:timestamp ms/TemporalInstant]+ [:time_matters :boolean]+ [:timezone :string]+ [:icon :string]+ [:archived :boolean]+ [:creator_id ::lib.schema.id/user]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::timeline-event.update+ "What an update (or insert) of a TimelineEvent accepts: every column of `:timeline_event` except `id`, all optional."+ [:map {:closed true}+ [:timeline_id {:optional true} [:maybe ms/PositiveInt]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:time_matters {:optional true} [:maybe :boolean]]+ [:timezone {:optional true} [:maybe :string]]+ [:icon {:optional true} [:maybe :string]]+ [:archived {:optional true} [:maybe :boolean]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/transforms/canceling.clj b/src/metabase/transforms/canceling.cljindex ab2dc0cf8fc..2327c665e70 100644--- a/src/metabase/transforms/canceling.clj+++ b/src/metabase/transforms/canceling.clj@@ -55,7 +55,7 @@owning node holds a run's `cancel-chan`."[](rt/heartbeat-and-reconcile! {:model :model/TransformRun- :active [:= :is_active true]+ :active [:is_active true]:ids (keys @connections):heartbeat! transform-run/heartbeat-runs!:on-gone chan-signal-cancel!}))diff --git a/src/metabase/transforms/coordinated_run.clj b/src/metabase/transforms/coordinated_run.cljindex 0e9fad8dced..d5ffb2038df 100644--- a/src/metabase/transforms/coordinated_run.clj+++ b/src/metabase/transforms/coordinated_run.clj@@ -35,8 +35,7 @@([model run-id properties](finish-active-run! modelrun-id- (merge {:end_time :%now}- properties+ (merge properties{:status :succeeded:is_active nil}))))@@ -45,8 +44,7 @@[model run-id properties](finish-active-run! modelrun-id- (merge {:end_time :%now}- properties+ (merge properties{:status :failed:is_active nil})))@@ -58,7 +56,6 @@run-id{:status :canceled:is_active nil- :end_time :%now:message "Canceled"}))(defn cancel!@@ -77,7 +74,7 @@(defn heartbeat-runs!"Stamp `last_heartbeat = now` on the given still-active run-ids."[model run-ids]- (rt/heartbeat-ids! model [:= :is_active true] :last_heartbeat run-ids))+ (rt/heartbeat-ids! model [:is_active true] :last_heartbeat run-ids))(defn reap-orphaned-runs!"Time out active runs whose `last_heartbeat` is older than `stale-minutes` (their coordinator@@ -86,8 +83,8 @@[model type-tag stale-minutes](rt/reap-orphaned!{:model model- :active [:= :is_active true]- :stale [:< :last_heartbeat (rt/cutoff stale-minutes :minute)]+ :active [:is_active true]+ :stale [{:column :last_heartbeat :age stale-minutes :unit :minute}]:terminal {:status "timeout" :end_time :%now :is_active nil :message "Timed out: crashed"}:metrics {:total-metric :metabase-transforms/timeouts-total:latency-metric :metabase-transforms/timeout-detection-latency-ms@@ -101,7 +98,7 @@their coordinator aborts."[model active-runs-atom](rt/heartbeat-and-reconcile! {:model model- :active [:= :is_active true]+ :active [:is_active true]:ids (keys @active-runs-atom):heartbeat! #(heartbeat-runs! model %):on-gone (fn [run-id]diff --git a/src/metabase/transforms/db.clj b/src/metabase/transforms/db.cljindex 3e9c91bddab..53e423f3d28 100644--- a/src/metabase/transforms/db.clj+++ b/src/metabase/transforms/db.clj@@ -3,9 +3,14 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydration methods,and transactions."(:require+ [malli.util :as mut][medley.core :as m][metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.transforms.schema :as transforms.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(def ^:private no-active-run-clause@@ -19,109 +24,112 @@;;; ------------------------------------------------ Transform -------------------------------------------------(defn transform+(mu/defn transform"The Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/Transform :id transform-id))-(defn transforms+(mu/defn transforms"The Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:or [:set ::lib.schema.id/transform] [:sequential ::lib.schema.id/transform]]](t2/select :model/Transform :id [:in transform-ids]))-(defn transforms-of-source-types+(mu/defn transforms-of-source-types"The Transforms whose source type is one of `source-types`, optionally narrowed to `database-id`, ordered by ID."- [source-types database-id]+ [source-types :- [:set :string]+ database-id :- [:maybe ::lib.schema.id/database]](t2/select :model/Transform {:where [:and[:in :source_type source-types](when database-id [:= :source_database_id database-id])]:order-by [[:id :asc]]}))-(defn transform-dependency-rows+(mu/defn transform-dependency-rows"The ID, target, target Table ID, creation time, and table dependencies of every Transform."[](t2/select [:model/Transform :id :target :target_table_id :created_at :table_dependencies]))-(defn transform-snapshot+(mu/defn transform-snapshot"The name, entity ID, and source type of the Transform with `transform-id`."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one [:model/Transform :name :entity_id :source_type] :id transform-id))-(defn transform-summaries-by-id+(mu/defn transform-summaries-by-id"A map of ID to the ID, name, and Collection ID of the Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/select-pk->fn identity [:model/Transform :id :name :collection_id] :id [:in transform-ids]))-(defn transform-names-by-id+(mu/defn transform-names-by-id"A map of ID to name for the Transforms with `transform-ids`."- [transform-ids]+ [transform-ids :- [:sequential ::lib.schema.id/transform]](t2/select-pk->fn :name :model/Transform :id [:in transform-ids]))-(defn transform-last-checkpoint-value+(mu/defn transform-last-checkpoint-value"The last checkpoint value of the Transform with `transform-id`."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one-fn :last_checkpoint_value [:model/Transform :last_checkpoint_value] transform-id))-(defn transform-collection-id+(mu/defn transform-collection-id"The Collection ID of the Transform with `transform-id`."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one-fn :collection_id :model/Transform :id transform-id))-(defn insert-transform!+(mu/defn insert-transform!"Insert `transform` and return the new instance."- [transform]+ [transform :- ::transforms.schema/transform.update](t2/insert-returning-instance! :model/Transform transform))-(defn update-transform!+(mu/defn update-transform!"Apply `changes` to the Transform with `transform-id`."- [transform-id changes]+ [transform-id :- ::lib.schema.id/transform+ changes :- ::transforms.schema/transform.update](t2/update! :model/Transform transform-id changes))-(defn delete-transform!+(mu/defn delete-transform!"Delete the Transform with `transform-id`."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/delete! :model/Transform transform-id));;; ---------------------------------------------- Transform tags -----------------------------------------------(defn tag+(mu/defn tag"The TransformTag with `tag-id`, or nil."- [tag-id]+ [tag-id :- ms/PositiveInt](t2/select-one :model/TransformTag :id tag-id))-(defn existing-tag-ids+(mu/defn existing-tag-ids"The subset of `tag-ids` that exist."- [tag-ids]+ [tag-ids :- [:sequential ms/PositiveInt]](t2/select-fn-set :id :model/TransformTag :id [:in tag-ids]))-(defn tag-name-exists?+(mu/defn tag-name-exists?"Whether a TransformTag named `tag-name` exists."- [tag-name]+ [tag-name :- :string](t2/exists? :model/TransformTag :name tag-name))-(defn tag-name-exists-excluding?+(mu/defn tag-name-exists-excluding?"Whether a TransformTag named `tag-name` other than `tag-id` exists."- [tag-name tag-id]+ [tag-name :- :string+ tag-id :- ms/PositiveInt](t2/exists? :model/TransformTag :name tag-name :id [:not= tag-id]))-(defn transform-tag-links+(mu/defn transform-tag-links"The tag links of the Transforms with `transform-ids`, ordered by position."- [transform-ids]+ [transform-ids :- [:or [:set [:maybe ::lib.schema.id/transform]] [:sequential [:maybe ::lib.schema.id/transform]]]](t2/select :model/TransformTransformTag :transform_id [:in transform-ids] {:order-by [[:position :asc]]}))-(defn transform-tag-links-for-tags+(mu/defn transform-tag-links-for-tags"The tag ID and Transform ID of the tag links of the TransformTags with `tag-ids`."- [tag-ids]+ [tag-ids :- [:set ms/PositiveInt]](t2/select [:model/TransformTransformTag :tag_id :transform_id] :tag_id [:in tag-ids]))-(defn transform-ids-with-tags+(mu/defn transform-ids-with-tags"The IDs of the Transforms tagged with one of `tag-ids`."- [tag-ids]+ [tag-ids :- [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]](t2/select-fn-set :transform_id :model/TransformTransformTag :tag_id [:in tag-ids]))-(defn active-job-schedules-for-transforms+(mu/defn active-job-schedules-for-transforms"Rows of Transform ID and the schedule of each active TransformJob that runs it through a shared tag."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/select :model/TransformTransformTag{:select [:ttt.transform_id [:job.schedule :schedule]]:from [[:transform_transform_tag :ttt]]@@ -131,103 +139,111 @@[:in :ttt.transform_id transform-ids][:= :job.active true]]}))-(defn insert-transform-tag-links!+(mu/defn insert-transform-tag-links!"Insert the TransformTransformTag `rows`."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::transforms.schema/transform-transform-tag.update [:transform_id :tag_id :entity_id :position])]](t2/insert! :model/TransformTransformTag rows))-(defn set-transform-tag-position!+(mu/defn set-transform-tag-position!"Set the position of the tag link between the Transform with `transform-id` and the tag with `tag-id`."- [transform-id tag-id position]+ [transform-id :- ::lib.schema.id/transform+ tag-id :- ms/PositiveInt+ position :- ms/IntGreaterThanOrEqualToZero](t2/update! :model/TransformTransformTag {:transform_id transform-id, :tag_id tag-id} {:position position}))-(defn delete-transform-tag-links!+(mu/defn delete-transform-tag-links!"Delete the links between the Transform with `transform-id` and the tags with `tag-ids`."- [transform-id tag-ids]+ [transform-id :- ::lib.schema.id/transform+ tag-ids :- [:set ms/PositiveInt]](t2/delete! :model/TransformTransformTag :transform_id transform-id :tag_id [:in tag-ids]));;; ---------------------------------------------- Transform jobs -----------------------------------------------(defn job+(mu/defn job"The TransformJob with `job-id`, or nil."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-one :model/TransformJob :id job-id))-(defn active-jobs+(mu/defn active-jobs"The active TransformJobs."[](t2/select :model/TransformJob :active true))-(defn job-snapshot+(mu/defn job-snapshot"The name, entity ID, and built-in type of the TransformJob with `job-id`."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-one [:model/TransformJob :name :entity_id :built_in_type] :id job-id))-(defn job-names-by-id+(mu/defn job-names-by-id"A map of ID to name for the TransformJobs with `job-ids`."- [job-ids]+ [job-ids :- [:sequential ms/PositiveInt]](t2/select-pk->fn :name :model/TransformJob :id [:in job-ids]))-(defn activate-job!+(mu/defn activate-job!"Mark the inactive TransformJob with `job-id` active, returning the number of rows updated."- [job-id]+ [job-id :- ms/PositiveInt](t2/update! :model/TransformJob {:id job-id, :active false} {:active true}))-(defn deactivate-job!+(mu/defn deactivate-job!"Mark the active TransformJob with `job-id` inactive, returning the number of rows updated."- [job-id]+ [job-id :- ms/PositiveInt](t2/update! :model/TransformJob {:id job-id, :active true} {:active false}))-(defn job-tag-ids+(mu/defn job-tag-ids"The IDs of the tags of the TransformJob with `job-id`."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-fn-set :tag_id :model/TransformJobTransformTag :job_id job-id))-(defn job-tag-links+(mu/defn job-tag-links"The tag links of the TransformJobs with `job-ids`, ordered by position."- [job-ids]+ [job-ids :- [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]](t2/select :model/TransformJobTransformTag :job_id [:in job-ids] {:order-by [[:position :asc]]}))-(defn insert-job-tag-links!+(mu/defn insert-job-tag-links!"Insert the TransformJobTransformTag `rows`."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::transforms.schema/transform-job-transform-tag.update [:job_id :tag_id :entity_id :position])]](t2/insert! :model/TransformJobTransformTag rows))-(defn set-job-tag-position!+(mu/defn set-job-tag-position!"Set the position of the tag link between the TransformJob with `job-id` and the tag with `tag-id`."- [job-id tag-id position]+ [job-id :- ms/PositiveInt+ tag-id :- ms/PositiveInt+ position :- ms/IntGreaterThanOrEqualToZero](t2/update! :model/TransformJobTransformTag {:job_id job-id, :tag_id tag-id} {:position position}))-(defn delete-job-tag-links!+(mu/defn delete-job-tag-links!"Delete the links between the TransformJob with `job-id` and the tags with `tag-ids`."- [job-id tag-ids]+ [job-id :- ms/PositiveInt+ tag-ids :- [:set ms/PositiveInt]](t2/delete! :model/TransformJobTransformTag :job_id job-id :tag_id [:in tag-ids]));;; ---------------------------------------------- Transform runs -----------------------------------------------(defn run+(mu/defn run"The TransformRun with `run-id`, or nil."- [run-id]+ [run-id :- ms/PositiveInt](t2/select-one :model/TransformRun :id run-id))-(defn runs+(mu/defn runs"The TransformRuns with `run-ids`."- [run-ids]+ [run-ids :- [:sequential ms/PositiveInt]](t2/select :model/TransformRun :id [:in run-ids]))-(defn runs-for-transform+(mu/defn runs-for-transform"The TransformRuns of the Transform with `transform-id`, newest first."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select :model/TransformRun :transform_id transform-id {:order-by [[:start_time :desc] [:end_time :desc]]}))-(defn runs-for-job-run+(mu/defn runs-for-job-run"The TransformRuns of the TransformJobRun with `job-run-id`, oldest first."- [job-run-id]+ [job-run-id :- ms/PositiveInt](t2/select :model/TransformRun {:where [:= :job_run_id job-run-id], :order-by [[:start_time :asc]]}))-(defn runs-for-dag-run+(mu/defn runs-for-dag-run"The TransformRuns of the TransformDagRun with `dag-run-id`, oldest first."- [dag-run-id]+ [dag-run-id :- ms/PositiveInt](t2/select :model/TransformRun {:where [:= :dag_run_id dag-run-id], :order-by [[:start_time :asc]]}))(defn- paged-runs-where@@ -327,11 +343,30 @@[:end_time sort-direction nulls-sort]])[:transform_run.id sort-direction])))-(defn paged-runs+(def ^:private RunFilters+ [:map {:closed true}+ [:started-at-start [:maybe ms/TemporalInstant]]+ [:started-at-end [:maybe ms/TemporalInstant]]+ [:ended-at-start [:maybe ms/TemporalInstant]]+ [:ended-at-end [:maybe ms/TemporalInstant]]+ [:run-methods [:maybe [:sequential :string]]]+ [:transform-ids [:maybe [:or [:set ::lib.schema.id/transform] [:sequential ::lib.schema.id/transform]]]]+ [:transform-tag-ids [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]]+ [:statuses [:maybe [:sequential :string]]]+ [:user-id [:maybe ::lib.schema.id/user]]])++(mu/defn paged-runs"Up to `limit` (offset by `offset`) TransformRuns matching `filters` (see [[paged-runs-where]] for the supportedkeys), sorted by `sort-column`/`sort-direction` (translating `status`, `run-method`, and `transform-tags` sortcolumns per `status-labels`/`run-method-labels`/`tag-name-labels`)."- [filters sort-column sort-direction status-labels run-method-labels tag-name-labels limit offset]+ [filters :- RunFilters+ sort-column :- [:maybe [:or :keyword :string]]+ sort-direction :- [:maybe [:or :keyword :string]]+ status-labels :- [:map-of :string :string]+ run-method-labels :- [:map-of :string :string]+ tag-name-labels :- [:map-of :string :string]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](let [where-clause (paged-runs-where filters)join-clause (paged-runs-join sort-column)](t2/select :model/TransformRun@@ -343,14 +378,14 @@:where where-clause:left-join join-clause))))-(defn paged-run-count+(mu/defn paged-run-count"The number of TransformRuns matching `filters` (see [[paged-runs-where]] for the supported keys)."- [filters]+ [filters :- RunFilters](t2/count :model/TransformRun (m/assoc-some {} :where (paged-runs-where filters))))-(defn latest-runs-reducible+(mu/defn latest-runs-reducible"Reducible latest TransformRun of each Transform with `transform-ids`."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/reducible-select :model/TransformRun{:with [[:latest_runs^:allow-subquery@@ -365,25 +400,26 @@:from [:latest_runs]:where [:= :rn [:inline 1]]}))-(defn active-run-for-transform+(mu/defn active-run-for-transform"The active TransformRun of the Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/TransformRun :transform_id transform-id :is_active true))-(defn active-run-ids-of-parent+(mu/defn active-run-ids-of-parent"The IDs of the active TransformRuns whose `parent-column` is `parent-run-id`."- [parent-column parent-run-id]+ [parent-column :- [:enum :job_run_id :dag_run_id]+ parent-run-id :- ms/PositiveInt](t2/select-pks-vec :model/TransformRun parent-column parent-run-id :is_active true))-(defn lock-active-runs+(mu/defn lock-active-runs"The active TransformRuns among `run-ids`, locked for update."- [run-ids]+ [run-ids :- [:sequential ms/PositiveInt]](t2/select :model/TransformRun {:where [:and [:= :is_active true] [:in :id run-ids]]:for :update}))-(defn last-success-times+(mu/defn last-success-times"Rows of Transform ID and the latest `end_time` of its succeeded runs for `transform-ids`."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/select :model/TransformRun{:select [:transform_id [[:max :end_time] :last_success]]:where [:and@@ -391,19 +427,20 @@[:= :status "succeeded"]]:group-by [:transform_id]}))-(defn insert-run!+(mu/defn insert-run!"Insert `run` and return the new instance."- [run]+ [run :- ::transforms.schema/transform-run.update](t2/insert-returning-instance! :model/TransformRun run))-(defn finish-active-run!+(mu/defn finish-active-run!"Apply `changes` to the TransformRun with `run-id` if it is still active, returning the number of rows updated."- [run-id changes]+ [run-id :- ms/PositiveInt+ changes :- ::transforms.schema/transform-run.update](t2/update! :model/TransformRun :id run-id :is_active true changes))-(defn cancel-active-runs!+(mu/defn cancel-active-runs!"Mark the active TransformRuns among `run-ids` canceled because the user asked but the run could not be stopped."- [run-ids]+ [run-ids :- [:sequential ms/PositiveInt]](t2/update! :model/TransformRun:id [:in run-ids]:is_active true@@ -412,16 +449,16 @@:is_active nil:message "Canceled by user but could not guarantee run stopped."}))-(defn mark-run-canceling!+(mu/defn mark-run-canceling!"Set the status of the TransformRun with `run-id` to canceling."- [run-id]+ [run-id :- ms/PositiveInt](t2/update! :model/TransformRun :id run-id {:status "canceling"}));;; -------------------------------------------- Run cancelations ---------------------------------------------(defn insert-cancelation-for-active-run!+(mu/defn insert-cancelation-for-active-run!"Record a cancelation request for the TransformRun with `run-id` if it is active and none exists yet."- [run-id]+ [run-id :- ms/PositiveInt](t2/query-one [(str "INSERT INTO transform_run_cancelation (run_id) ""SELECT transform_run.id ""FROM transform_run "@@ -430,48 +467,52 @@"AND NOT EXISTS (SELECT 1 FROM transform_run_cancelation WHERE run_id = ?)")run-id run-id]))-(defn cancelations-reducible+(mu/defn cancelations-reducible"Reducible TransformRunCancelations."[](t2/reducible-select :model/TransformRunCancelation))-(defn cancelations-requested-before- "The run ID and request time of the TransformRunCancelations requested before `cutoff`."- [cutoff]- (t2/select [:model/TransformRunCancelation :run_id :time] :time [:< cutoff]))+(mu/defn cancelations-requested-before+ "The run ID and request time of the TransformRunCancelations requested more than `age` `unit`s ago."+ [age :- ms/PositiveInt+ unit :- :keyword]+ (t2/select [:model/TransformRunCancelation :run_id :time]+ :time [:< (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit)]))-(defn delete-cancelation-for-inactive-run!+(mu/defn delete-cancelation-for-inactive-run!"Delete the TransformRunCancelation of the TransformRun with `run-id` if that run is no longer active."- [run-id]+ [run-id :- ms/PositiveInt](t2/delete! :model/TransformRunCancelation {:where [:and [:= :run_id run-id] no-active-run-clause]}))-(defn delete-cancelations-for-inactive-runs!+(mu/defn delete-cancelations-for-inactive-runs!"Delete every TransformRunCancelation whose run is no longer active."[](t2/delete! :model/TransformRunCancelation {:where no-active-run-clause}));;; ------------------------------------------- Job and DAG runs --------------------------------------------(defn touch-active-job-run!+(mu/defn touch-active-job-run!"Stamp `updated_at` on the active TransformJobRun with `run-id`."- [run-id]+ [run-id :- ms/PositiveInt](t2/update! :model/TransformJobRun :id run-id :is_active true {:updated_at :%now}))-(defn touch-active-dag-run!+(mu/defn touch-active-dag-run!"Stamp `updated_at` on the active TransformDagRun with `run-id`."- [run-id]+ [run-id :- ms/PositiveInt](t2/update! :model/TransformDagRun :id run-id :is_active true {:updated_at :%now}))-(defn finish-active-job-run!+(mu/defn finish-active-job-run!"Apply `changes` to the TransformJobRun with `run-id` if it is still active, returning the number of rowsupdated."- [run-id changes]+ [run-id :- ms/PositiveInt+ changes :- ::transforms.schema/transform-job-run.update](t2/update! :model/TransformJobRun :id run-id :is_active true changes))-(defn finish-active-dag-run!+(mu/defn finish-active-dag-run!"Apply `changes` to the TransformDagRun with `run-id` if it is still active, returning the number of rowsupdated."- [run-id changes]+ [run-id :- ms/PositiveInt+ changes :- ::transforms.schema/transform-dag-run.update](t2/update! :model/TransformDagRun :id run-id :is_active true changes))(defn- job-run-where@@ -496,10 +537,18 @@[[:start_time sort-direction][:end_time sort-direction nulls-sort]])))-(defn job-runs+(mu/defn job-runs"Up to `limit` (offset by `offset`) TransformJobRuns, optionally narrowed to `job-id`, `status`, `run-method`, andstarted in [`started-at-start`, `started-at-end`), sorted by `sort-column`/`sort-direction`."- [job-id status run-method started-at-start started-at-end sort-column sort-direction limit offset]+ [job-id :- [:maybe ms/PositiveInt]+ status :- [:maybe :string]+ run-method :- [:maybe :string]+ started-at-start :- [:maybe ms/TemporalInstant]+ started-at-end :- [:maybe ms/TemporalInstant]+ sort-column :- [:maybe [:or :keyword :string]]+ sort-direction :- [:maybe [:or :keyword :string]]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](t2/select :model/TransformJobRun(cond-> {:order-by (job-run-order-by sort-column sort-direction):offset offset@@ -507,18 +556,22 @@(job-run-where job-id status run-method started-at-start started-at-end)(assoc :where (job-run-where job-id status run-method started-at-start started-at-end)))))-(defn job-run-count+(mu/defn job-run-count"The number of TransformJobRuns, optionally narrowed to `job-id`, `status`, `run-method`, and started in[`started-at-start`, `started-at-end`)."- [job-id status run-method started-at-start started-at-end]+ [job-id :- [:maybe ms/PositiveInt]+ status :- [:maybe :string]+ run-method :- [:maybe :string]+ started-at-start :- [:maybe ms/TemporalInstant]+ started-at-end :- [:maybe ms/TemporalInstant]](t2/count :model/TransformJobRun(if-let [where (job-run-where job-id status run-method started-at-start started-at-end)]{:where where}{})))-(defn latest-job-runs-reducible+(mu/defn latest-job-runs-reducible"Reducible latest TransformJobRun of each TransformJob with `job-ids`."- [job-ids]+ [job-ids :- [:set ms/PositiveInt]](t2/reducible-select :model/TransformJobRun{:with [[:ranked_runs^:allow-subquery@@ -533,15 +586,16 @@:from [:ranked_runs]:where [:= :rn [:inline 1]]}))-(defn active-job-run-for-job+(mu/defn active-job-run-for-job"The active TransformJobRun of the TransformJob with `job-id`, or nil."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-one :model/TransformJobRun :job_id job-id :is_active true))-(defn failed-cron-job-runs-between+(mu/defn failed-cron-job-runs-between"The job ID, start time, and message of the cron TransformJobRuns that failed or timed out in `[start, end)`,oldest first."- [start end]+ [start :- ms/TemporalInstant+ end :- ms/TemporalInstant](t2/select [:model/TransformJobRun :job_id :start_time :message]{:where [:and[:= :run_method "cron"]@@ -550,19 +604,19 @@[:< :start_time end]]:order-by [[:start_time :asc]]}))-(defn insert-job-run!+(mu/defn insert-job-run!"Insert `job-run` and return the new instance."- [job-run]+ [job-run :- ::transforms.schema/transform-job-run.update](t2/insert-returning-instance! :model/TransformJobRun job-run))-(defn active-dag-run-for-transform+(mu/defn active-dag-run-for-transform"The active TransformDagRun seeded from the Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/TransformDagRun :source_transform_id transform-id :is_active true))-(defn insert-dag-run!+(mu/defn insert-dag-run!"Insert `dag-run` and return the new instance."- [dag-run]+ [dag-run :- ::transforms.schema/transform-dag-run.update](t2/insert-returning-instance! :model/TransformDagRun dag-run));;; ------------------------------------------ Root run listing ------------------------------------------@@ -666,13 +720,23 @@[[:start_time sort-direction][:end_time sort-direction nulls-sort]])))-(defn root-run-summaries-page+(mu/defn root-run-summaries-page"Up to `limit` (offset by `offset`) root-run summary rows -- see [[metabase.transforms.run-listing]] -- of `types`(a subset of `#{:job :dag :transform}`, or all three when empty), optionally narrowed to `statuses`,`run-methods`, started in [`started-at-start`, `started-at-end`), ended in [`ended-at-start`, `ended-at-end`),and/or touching one of `transform-ids`, sorted by `sort-column`/`sort-direction`."- [types statuses run-methods started-at-start started-at-end ended-at-start ended-at-end transform-ids- sort-column sort-direction limit offset]+ [types :- [:maybe [:sequential :keyword]]+ statuses :- [:maybe [:sequential :string]]+ run-methods :- [:maybe [:sequential :string]]+ started-at-start :- [:maybe ms/TemporalInstant]+ started-at-end :- [:maybe ms/TemporalInstant]+ ended-at-start :- [:maybe ms/TemporalInstant]+ ended-at-end :- [:maybe ms/TemporalInstant]+ transform-ids :- [:maybe [:sequential ::lib.schema.id/transform]]+ sort-column :- [:maybe [:or :keyword :string]]+ sort-direction :- [:maybe [:or :keyword :string]]+ limit :- ms/PositiveInt+ offset :- ms/IntGreaterThanOrEqualToZero](let [where (root-run-summaries-where statuses run-methods started-at-start started-at-end ended-at-startended-at-end)base (cond-> {:from [[(union-subquery types transform-ids) :runs]]}@@ -682,78 +746,85 @@:limit limit:offset offset}))))-(defn root-run-summaries-count+(mu/defn root-run-summaries-count"The number of root-run summary rows matching the same filters as [[root-run-summaries-page]]."- [types statuses run-methods started-at-start started-at-end ended-at-start ended-at-end transform-ids]+ [types :- [:maybe [:sequential :keyword]]+ statuses :- [:maybe [:sequential :string]]+ run-methods :- [:maybe [:sequential :string]]+ started-at-start :- [:maybe ms/TemporalInstant]+ started-at-end :- [:maybe ms/TemporalInstant]+ ended-at-start :- [:maybe ms/TemporalInstant]+ ended-at-end :- [:maybe ms/TemporalInstant]+ transform-ids :- [:maybe [:sequential ::lib.schema.id/transform]]](let [where (root-run-summaries-where statuses run-methods started-at-start started-at-end ended-at-startended-at-end)base (cond-> {:from [[(union-subquery types transform-ids) :runs]]}where (assoc :where where))](:count (first (t2/query (merge base {:select [[[:count :*] :count]]}))))))-(defn app-db-now+(mu/defn app-db-now"The current time according to the application database."[](:now (t2/query-one {:select [[(h2x/current-datetime-honeysql-form (mdb/db-type)) :now]]})));;; ----------------------------------------------- Other models ------------------------------------------------(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table table-id))-(defn databases+(mu/defn databases"The Databases with `database-ids`."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select :model/Database :id [:in database-ids]))-(defn database-exists?+(mu/defn database-exists?"Whether a Database with `database-id` exists."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn table-indexes-for-transforms+(mu/defn table-indexes-for-transforms"The TableIndexes of the Transforms with `transform-ids`, ordered by index name."- [transform-ids]+ [transform-ids :- [:set ::lib.schema.id/transform]](t2/select :model/TableIndex :transform_id [:in transform-ids] {:order-by [[:index_name :asc]]}))-(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field field-id))-(defn field-exists?+(mu/defn field-exists?"Whether a Field with `field-id` exists."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/exists? :model/Field :id field-id))-(defn active-field-ids-by-name+(mu/defn active-field-ids-by-name"A map of name to ID for the active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn->fn :name :id [:model/Field :name :id] :table_id table-id :active true))-(defn active-users+(mu/defn active-users"The active Users with `user-ids`."- [user-ids]+ [user-ids :- [:sequential ::lib.schema.id/user]](t2/select :model/User :id [:in user-ids] :is_active true))-(defn active-admins+(mu/defn active-admins"The active superusers."[](t2/select :model/User :is_superuser true :is_active true))-(defn user-summaries-by-id+(mu/defn user-summaries-by-id"A map of ID to the ID, email, and names of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))diff --git a/src/metabase/transforms/models/dag_run.clj b/src/metabase/transforms/models/dag_run.cljindex bfbda744096..f63ef82c60b 100644--- a/src/metabase/transforms/models/dag_run.clj+++ b/src/metabase/transforms/models/dag_run.clj@@ -17,6 +17,16 @@(derive :model/TransformDagRun :metabase/model)(derive :model/TransformDagRun :hook/timestamped?)+(def ^:private terminal-statuses+ #{:succeeded :failed :canceled :timeout})++(t2/define-before-update :model/TransformDagRun+ [run]+ (let [changes (t2/changes run)]+ (cond-> run+ (and (contains? terminal-statuses (keyword (:status changes))) (not (contains? changes :end_time)))+ (assoc :end_time (mi/now)))))+(t2/deftransforms :model/TransformDagRun{:status mi/transform-keyword:direction mi/transform-keyword})diff --git a/src/metabase/transforms/models/job_run.clj b/src/metabase/transforms/models/job_run.cljindex ca8173334ed..4883596de59 100644--- a/src/metabase/transforms/models/job_run.clj+++ b/src/metabase/transforms/models/job_run.clj@@ -15,6 +15,16 @@(derive :model/TransformJobRun :metabase/model)(derive :model/TransformJobRun :hook/timestamped?)+(def ^:private terminal-statuses+ #{:succeeded :failed :canceled :timeout})++(t2/define-before-update :model/TransformJobRun+ [run]+ (let [changes (t2/changes run)]+ (cond-> run+ (and (contains? terminal-statuses (keyword (:status changes))) (not (contains? changes :end_time)))+ (assoc :end_time (mi/now)))))+(t2/deftransforms :model/TransformJobRun{:status mi/transform-keyword:run_method mi/transform-keyword})diff --git a/src/metabase/transforms/models/transform.clj b/src/metabase/transforms/models/transform.cljindex 81d1a27172b..d2e5eff9b3d 100644--- a/src/metabase/transforms/models/transform.clj+++ b/src/metabase/transforms/models/transform.clj@@ -13,7 +13,6 @@[metabase.permissions.core :as perms][metabase.remote-sync.core :as remote-sync][metabase.search.core :as search.core]- [metabase.search.ingestion :as search][metabase.search.spec :as search.spec][metabase.transforms-base.interface :as transforms-base.i][metabase.transforms-base.util :as transforms-base.u]@@ -543,7 +542,7 @@:python (:body source-data)nil)](when query-text- (subs query-text 0 (min (count query-text) search/max-searchable-value-length)))))+ (subs query-text 0 (min (count query-text) search.core/max-searchable-value-length)))))(defn transforms-with-tags"Returns all transforms associated with the given tag IDs.diff --git a/src/metabase/transforms/models/transform_run.clj b/src/metabase/transforms/models/transform_run.cljindex 03d8a7c382e..40a0ec843de 100644--- a/src/metabase/transforms/models/transform_run.clj+++ b/src/metabase/transforms/models/transform_run.clj@@ -2,7 +2,6 @@(:require[metabase.analytics-interface.core :as analytics][metabase.api.common :as api]- [metabase.app-db.core :as mdb][metabase.collections.models.collection.root :as collection.root][metabase.events.core :as events][metabase.models.interface :as mi]@@ -12,7 +11,6 @@[metabase.transforms.models.transform-run-cancelation :as cancel][metabase.transforms.models.util :as transforms.models.u][metabase.util :as u]- [metabase.util.honey-sql-2 :as h2x][metabase.util.i18n :refer [tru]][metabase.util.log :as log][methodical.core :as methodical]@@ -27,6 +25,16 @@(derive :model/TransformRun :metabase/model)+(def ^:private terminal-statuses+ #{:succeeded :failed :canceled :timeout})++(t2/define-before-update :model/TransformRun+ [run]+ (let [changes (t2/changes run)]+ (cond-> run+ (and (contains? terminal-statuses (keyword (:status changes))) (not (contains? changes :end_time)))+ (assoc :end_time (mi/now)))))+(t2/deftransforms :model/TransformRun{:status mi/transform-keyword:run_method mi/transform-keyword})@@ -83,8 +91,7 @@([run-id properties](u/prog1 (transforms.db/finish-active-run! run-id(merge properties- {:end_time :%now- :status :succeeded+ {:status :succeeded:is_active nil}))(cancel/delete-cancelation! run-id))))@@ -93,8 +100,7 @@[run-id properties](u/prog1 (transforms.db/finish-active-run! run-id(merge properties- {:end_time :%now- :status :failed+ {:status :failed:is_active nil}))(cancel/delete-cancelation! run-id)))@@ -105,8 +111,7 @@([run-id properties](u/prog1 (transforms.db/finish-active-run! run-id(merge properties- {:end_time :%now- :status :canceled+ {:status :canceled:is_active nil}))(cancel/delete-cancelation! run-id))))@@ -128,8 +133,7 @@([run-id properties](u/prog1 (transforms.db/finish-active-run! run-id(merge properties- {:end_time :%now- :message "Timed out"+ {:message "Timed out":status :timeout:is_active nil}))(cancel/delete-cancelation! run-id)@@ -145,8 +149,8 @@(let [end-time (OffsetDateTime/now ZoneOffset/UTC)reaped (rt/reap-orphaned!{:model :model/TransformRun- :active [:= :is_active true]- :stale [:< stale-column (rt/cutoff age unit)]+ :active [:is_active true]+ :stale [{:column stale-column :age age :unit unit}]:terminal {:status "timeout" :end_time :%now :is_active nil :message message}:metrics {:total-metric :metabase-transforms/timeouts-total:latency-metric :metabase-transforms/timeout-detection-latency-ms@@ -171,7 +175,7 @@(defn heartbeat-runs!"Stamp `last_heartbeat = now` on the given still-active `run-ids`."[run-ids]- (rt/heartbeat-ids! :model/TransformRun [:= :is_active true] :last_heartbeat run-ids))+ (rt/heartbeat-ids! :model/TransformRun [:is_active true] :last_heartbeat run-ids))(defn reap-orphaned-runs!"Time out active runs whose `last_heartbeat` is older than `stale-minutes` (their owning process is@@ -190,9 +194,8 @@the lock set and are not reported."[age unit](t2/with-transaction [_conn]- (let [cutoff (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit)- times (into {} (map (juxt :run_id :time))- (transforms.db/cancelations-requested-before cutoff))+ (let [times (into {} (map (juxt :run_id :time))+ (transforms.db/cancelations-requested-before age unit))locked (when (seq times)(transforms.db/lock-active-runs (keys times)))](when (seq locked)diff --git a/src/metabase/transforms/schema.clj b/src/metabase/transforms/schema.cljindex 2e3906e5a44..24b3d52950e 100644--- a/src/metabase/transforms/schema.clj+++ b/src/metabase/transforms/schema.clj@@ -5,6 +5,7 @@[metabase.lib.schema.id :as lib.schema.id][metabase.transforms-base.util :as transforms-base.u][metabase.util.date-2 :as u.date]+ [metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]))@@ -88,10 +89,263 @@(mr/def ::id pos-int?)+(mr/def ::transform.source+ "The `:source` column of a Transform, decoded."+ :map)++(mr/def ::transform.target+ "The `:target` column of a Transform, decoded."+ :map)++(mr/def ::transform.table-dependency+ "One entry of the `:table_dependencies` column of a Transform, decoded."+ :map)+(mr/def ::transform- [:map- [:id ::id]- [:description {:optional true} [:maybe :string]]- [:name :string]- [:source [:ref ::transform-source]]- [:target [:ref ::transform-target]]])+ "A Transform as selected from the app DB: every column of `:transform`."+ [:map {:closed true}+ [:id ::lib.schema.id/transform]+ [:name :string]+ [:description [:maybe :string]]+ [:source ::transform.source]+ [:target ::transform.target]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:source_type [:or :keyword :string]]+ [:creator_id ::lib.schema.id/user]+ [:source_database_id [:maybe ::lib.schema.id/database]]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:owner_user_id [:maybe ::lib.schema.id/user]]+ [:owner_email [:maybe :string]]+ [:target_db_id [:maybe ::lib.schema.id/database]]+ [:last_checkpoint_value [:maybe :string]]+ [:target_table_id [:maybe ::lib.schema.id/table]]+ [:table_dependencies [:maybe [:sequential ::transform.table-dependency]]]])++(mr/def ::transform.update+ "What an update (or insert) of a Transform accepts: every column of `:transform` except `id`, all optional, plus `:run_trigger` consumed by the model's hooks."+ [:map {:closed true}+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:source {:optional true} [:maybe ::transform.source]]+ [:target {:optional true} [:maybe ::transform.target]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:source_database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:owner_user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:owner_email {:optional true} [:maybe :string]]+ [:target_db_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:last_checkpoint_value {:optional true} [:maybe :string]]+ [:target_table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:table_dependencies {:optional true} [:maybe [:sequential ::transform.table-dependency]]]+ [:run_trigger {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::transform-dag-run+ "A TransformDagRun as selected from the app DB: every column of `:transform_dag_run`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_transform_id [:maybe ::lib.schema.id/transform]]+ [:source_transform_name [:maybe :string]]+ [:source_transform_entity_id [:maybe :string]]+ [:direction [:or :keyword :string]]+ [:transform_count [:maybe :int]]+ [:status [:or :keyword :string]]+ [:is_active [:maybe :boolean]]+ [:start_time ms/TemporalInstant]+ [:end_time [:maybe ms/TemporalInstant]]+ [:message [:maybe :string]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:last_heartbeat ms/TemporalInstant]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++(mr/def ::transform-dag-run.update+ "What an update (or insert) of a TransformDagRun accepts: every column of `:transform_dag_run` except `id`, all optional."+ [:map {:closed true}+ [:source_transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:source_transform_name {:optional true} [:maybe :string]]+ [:source_transform_entity_id {:optional true} [:maybe :string]]+ [:direction {:optional true} [:maybe [:or :keyword :string]]]+ [:transform_count {:optional true} [:maybe :int]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:start_time {:optional true} [:maybe ms/TemporalInstant]]+ [:end_time {:optional true} [:maybe ms/TemporalInstant]]+ [:message {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:last_heartbeat {:optional true} [:maybe ms/TemporalInstant]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::transform-job+ "A TransformJob as selected from the app DB: every column of `:transform_job`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name [:or :string mu/localized-string-schema]]+ [:description [:maybe [:or :string mu/localized-string-schema]]]+ [:schedule :string]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:built_in_type [:maybe [:or :keyword :string]]]+ [:ui_display_type [:or :keyword :string]]+ [:active :boolean]])++(mr/def ::transform-job.update+ "What an update (or insert) of a TransformJob accepts: every column of `:transform_job` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe [:or :string mu/localized-string-schema]]]+ [:description {:optional true} [:maybe [:or :string mu/localized-string-schema]]]+ [:schedule {:optional true} [:maybe :string]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:built_in_type {:optional true} [:maybe [:or :keyword :string]]]+ [:ui_display_type {:optional true} [:maybe [:or :keyword :string]]]+ [:active {:optional true} [:maybe :boolean]]])++(mr/def ::transform-job-run+ "A TransformJobRun as selected from the app DB: every column of `:transform_job_run`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:job_id ms/PositiveInt]+ [:run_method [:or :keyword :string]]+ [:status [:or :keyword :string]]+ [:is_active [:maybe :boolean]]+ [:start_time ms/TemporalInstant]+ [:end_time [:maybe ms/TemporalInstant]]+ [:message [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:last_heartbeat ms/TemporalInstant]+ [:job_name [:maybe :string]]+ [:job_entity_id [:maybe :string]]])++(mr/def ::transform-job-run.update+ "What an update (or insert) of a TransformJobRun accepts: every column of `:transform_job_run` except `id`, all optional."+ [:map {:closed true}+ [:job_id {:optional true} [:maybe ms/PositiveInt]]+ [:run_method {:optional true} [:maybe [:or :keyword :string]]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:start_time {:optional true} [:maybe ms/TemporalInstant]]+ [:end_time {:optional true} [:maybe ms/TemporalInstant]]+ [:message {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_heartbeat {:optional true} [:maybe ms/TemporalInstant]]+ [:job_name {:optional true} [:maybe :string]]+ [:job_entity_id {:optional true} [:maybe :string]]])++(mr/def ::transform-job-transform-tag+ "A TransformJobTransformTag as selected from the app DB: every column of `:transform_job_transform_tag`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:job_id ms/PositiveInt]+ [:tag_id ms/PositiveInt]+ [:entity_id :string]+ [:position :int]])++(mr/def ::transform-job-transform-tag.update+ "What an update (or insert) of a TransformJobTransformTag accepts: every column of `:transform_job_transform_tag` except `id`, all optional."+ [:map {:closed true}+ [:job_id {:optional true} [:maybe ms/PositiveInt]]+ [:tag_id {:optional true} [:maybe ms/PositiveInt]]+ [:entity_id {:optional true} [:maybe :string]]+ [:position {:optional true} [:maybe :int]]])++(mr/def ::transform-run+ "A TransformRun as selected from the app DB: every column of `:transform_run`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:transform_id [:maybe ::lib.schema.id/transform]]+ [:run_method [:or :keyword :string]]+ [:status [:or :keyword :string]]+ [:is_active [:maybe :boolean]]+ [:start_time ms/TemporalInstant]+ [:end_time [:maybe ms/TemporalInstant]]+ [:message [:maybe :string]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:transform_name [:maybe :string]]+ [:transform_entity_id [:maybe :string]]+ [:checkpoint_filter_field_id [:maybe ::lib.schema.id/field]]+ [:checkpoint_lo_value [:maybe :string]]+ [:checkpoint_hi_value [:maybe :string]]+ [:metered_as [:maybe :string]]+ [:last_heartbeat ms/TemporalInstant]+ [:job_run_id [:maybe ms/PositiveInt]]+ [:dag_run_id [:maybe ms/PositiveInt]]])++(mr/def ::transform-run.update+ "What an update (or insert) of a TransformRun accepts: every column of `:transform_run` except `id`, all optional."+ [:map {:closed true}+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:run_method {:optional true} [:maybe [:or :keyword :string]]]+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:start_time {:optional true} [:maybe ms/TemporalInstant]]+ [:end_time {:optional true} [:maybe ms/TemporalInstant]]+ [:message {:optional true} [:maybe :string]]+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:transform_name {:optional true} [:maybe :string]]+ [:transform_entity_id {:optional true} [:maybe :string]]+ [:checkpoint_filter_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:checkpoint_lo_value {:optional true} [:maybe :string]]+ [:checkpoint_hi_value {:optional true} [:maybe :string]]+ [:metered_as {:optional true} [:maybe :string]]+ [:last_heartbeat {:optional true} [:maybe ms/TemporalInstant]]+ [:job_run_id {:optional true} [:maybe ms/PositiveInt]]+ [:dag_run_id {:optional true} [:maybe ms/PositiveInt]]])++(mr/def ::transform-run-cancelation+ "A TransformRunCancelation as selected from the app DB: every column of `:transform_run_cancelation`."+ [:map {:closed true}+ [:run_id ms/PositiveInt]+ [:time ms/TemporalInstant]])++(mr/def ::transform-run-cancelation.update+ "What an update (or insert) of a TransformRunCancelation accepts: every column of `:transform_run_cancelation` except `id`, all optional."+ [:map {:closed true}+ [:run_id {:optional true} [:maybe ms/PositiveInt]]+ [:time {:optional true} [:maybe ms/TemporalInstant]]])++(mr/def ::transform-tag+ "A TransformTag as selected from the app DB: every column of `:transform_tag`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name [:or :string mu/localized-string-schema]]+ [:entity_id :string]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:built_in_type [:maybe [:or :keyword :string]]]])++(mr/def ::transform-tag.update+ "What an update (or insert) of a TransformTag accepts: every column of `:transform_tag` except `id`, all optional."+ [:map {:closed true}+ [:name {:optional true} [:maybe [:or :string mu/localized-string-schema]]]+ [:entity_id {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:built_in_type {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::transform-transform-tag+ "A TransformTransformTag as selected from the app DB: every column of `:transform_transform_tag`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:transform_id ::lib.schema.id/transform]+ [:tag_id ms/PositiveInt]+ [:entity_id :string]+ [:position :int]])++(mr/def ::transform-transform-tag.update+ "What an update (or insert) of a TransformTransformTag accepts: every column of `:transform_transform_tag` except `id`, all optional."+ [:map {:closed true}+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:tag_id {:optional true} [:maybe ms/PositiveInt]]+ [:entity_id {:optional true} [:maybe :string]]+ [:position {:optional true} [:maybe :int]]])diff --git a/src/metabase/transforms_base/db.clj b/src/metabase/transforms_base/db.cljindex 16a301a882b..ae3549e91bb 100644--- a/src/metabase/transforms_base/db.clj+++ b/src/metabase/transforms_base/db.clj@@ -2,94 +2,116 @@"Application database queries for the transforms base module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn transform-source+(mu/defn transform-source"The source of the Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one-fn :source [:model/Transform :id :source] transform-id))-(defn transforms-for-ordering+(mu/defn transforms-for-ordering"The id, name, target, target Table id, source Database id, and table dependencies of every Transformexcluding [[transform-id]]."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select [:model/Transform :id :name :target :target_table_id :source_database_id :table_dependencies]:id [:not= transform-id]))-(defn transform+(mu/defn transform"The Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/Transform transform-id))-(defn update-transform!+(mu/defn update-transform!"Apply `changes` to the Transform with `transform-id`."- [transform-id changes]+ [transform-id :- ::lib.schema.id/transform+ changes :- [:map {:closed true}+ [:last_checkpoint_value {:optional true} [:maybe :string]]+ [:target_table_id {:optional true} [:maybe ::lib.schema.id/table]]]](t2/update! :model/Transform transform-id changes))-(defn update-transform-run!+(mu/defn update-transform-run!"Apply `changes` to the TransformRun with `run-id`."- [run-id changes]+ [run-id :- ms/PositiveInt+ changes :- [:map {:closed true}+ [:checkpoint_filter_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:checkpoint_lo_value {:optional true} [:maybe :string]]+ [:checkpoint_hi_value {:optional true} [:maybe :string]]]](t2/update! :model/TransformRun run-id changes))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn field-table-id+(mu/defn field-table-id"The Table id of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :table_id :model/Field field-id))-(defn target-table+(mu/defn target-table"The Table named `table-name` in `schema` of the Database with `database-id` also matching the key-value`conditions`, or nil."- [database-id schema table-name & conditions]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string+ & conditions :- [:* :some]](apply t2/select-one :model/Table :db_id database-id :schema schema :name table-name conditions))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table table-id))-(defn table-for-transform+(mu/defn table-for-transform"The Table owned by the Transform with `transform-id`, or nil."- [transform-id]+ [transform-id :- ::lib.schema.id/transform](t2/select-one :model/Table :transform_id transform-id))-(defn update-table!+(mu/defn update-table!"Apply `changes` to the Table with `table-id`."- [table-id changes]+ [table-id :- ::lib.schema.id/table+ changes :- [:map {:closed true}+ [:schema {:optional true} [:maybe :string]]+ [:active {:optional true} [:maybe :boolean]]+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]]](t2/update! :model/Table table-id changes))-(defn mark-table-index-failed!+(mu/defn mark-table-index-failed!"Mark the TableIndex named `index-name` of the Transform with `transform-id` failed with `error-message`."- [transform-id index-name error-message]+ [transform-id :- ::lib.schema.id/transform+ index-name :- :string+ error-message :- :string](t2/update! :model/TableIndex:transform_id transform-id :index_name index-name{:status :failed :error_message error-message :last_executed_at :%now}))-(defn delete-table-indexes!+(mu/defn delete-table-indexes!"Delete the TableIndexes with `ids`."- [ids]+ [ids :- [:sequential ms/PositiveInt]](t2/delete! :model/TableIndex :id [:in ids]))-(defn update-table-indexes!- "Apply `changes` to the TableIndexes with `ids`."- [ids changes]- (t2/update! :model/TableIndex :id [:in ids] changes))+(mu/defn update-table-indexes!+ "Apply `changes` to the TableIndexes with `ids`, stamping `last_executed_at` with the app DB's now."+ [ids :- [:sequential ms/PositiveInt]+ changes :- [:map {:closed true}+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:error_message {:optional true} [:maybe :string]]]]+ (t2/update! :model/TableIndex :id [:in ids] (merge {:last_executed_at :%now} changes)))-(defn active-field-names-for-table+(mu/defn active-field-names-for-table"The names of the active Fields of the Table with `table-id`, in position order."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn-vec :name [:model/Field :name :position]:table_id table-id :active true{:order-by [[:position :asc]]}))-(defn table-refs-matching+(mu/defn table-refs-matching"The id, Database id, schema, and name of the Tables matching any of `refs` (each a `[db-id schema table-name]`triple; `schema` may be nil)."- [refs]+ [refs :- [:sequential [:tuple ms/PositiveInt [:maybe :string] :string]]](t2/select [:model/Table :id :db_id :schema :name]{:where (into [:or](map (fn [[db-id schema table-name]]@@ -101,7 +123,7 @@[:= :name table-name]]))refs)}))-(defn table-refs+(mu/defn table-refs"The id, Database id, schema, and name of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Table :id :db_id :schema :name] :id [:in table-ids]))diff --git a/src/metabase/transforms_base/util.clj b/src/metabase/transforms_base/util.cljindex d7683338b53..cc8904e4486 100644--- a/src/metabase/transforms_base/util.clj+++ b/src/metabase/transforms_base/util.clj@@ -506,8 +506,9 @@(defn target-table"Load the `target` table of a transform from the database specified by `database-id`."[database-id target & kv-args]- (some-> (apply transforms-base.db/target-table database-id (:schema target) (:name target) kv-args)- (t2/hydrate :db)))+ (when database-id+ (some-> (apply transforms-base.db/target-table database-id (:schema target) (:name target) kv-args)+ (t2/hydrate :db))))(defn target-table-exists?"Test if the target table of a transform already exists."@@ -677,8 +678,7 @@(if (= :delete-row status)(transforms-base.db/delete-table-indexes! (map :id rows))(transforms-base.db/update-table-indexes! (map :id rows)- (cond-> {:status status- :last_executed_at :%now}+ (cond-> {:status status}(= status :succeeded)(assoc :error_message nil)(= status :failed)diff --git a/src/metabase/transforms_rest/db.clj b/src/metabase/transforms_rest/db.cljindex 755ea560730..be51fbb47f1 100644--- a/src/metabase/transforms_rest/db.clj+++ b/src/metabase/transforms_rest/db.clj@@ -2,102 +2,111 @@"Application database queries for the transforms-rest module. Every function here is a direct Toucan 2 callwith no additional logic, so no other namespace in the module runs a query itself."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.transforms.schema :as transforms.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]));;; ------------------------------------------------- Transforms --------------------------------------------------(defn transforms-by-id+(mu/defn transforms-by-id"A map of id to Transform, for every Transform."[](t2/select-pk->fn identity :model/Transform))-(defn reset-checkpoint!+(mu/defn reset-checkpoint!"Clear the stored checkpoint value of the Transform with `id`."- [id]+ [id :- ::lib.schema.id/transform](t2/update! :model/Transform id {:last_checkpoint_value nil}));;; ---------------------------------------------------- Tags -------------------------------------------------------(defn existing-tag-ids+(mu/defn existing-tag-ids"The subset of `tag-ids` that are ids of existing TransformTags."- [tag-ids]+ [tag-ids :- [:sequential ms/PositiveInt]](set (t2/select-pks-vec :model/TransformTag :id [:in tag-ids])))-(defn tag+(mu/defn tag"The TransformTag with `tag-id`, or nil."- [tag-id]+ [tag-id :- ms/PositiveInt](t2/select-one :model/TransformTag :id tag-id))-(defn insert-tag!+(mu/defn insert-tag!"Insert a TransformTag named `name` and return the inserted instance."- [name]+ [name :- :string](t2/insert-returning-instance! :model/TransformTag {:name name}))-(defn update-tag!+(mu/defn update-tag!"Set the name of the TransformTag with `tag-id`."- [tag-id name]+ [tag-id :- ms/PositiveInt+ name :- :string](t2/update! :model/TransformTag tag-id {:name name}))-(defn delete-tag!+(mu/defn delete-tag!"Delete the TransformTag with `tag-id`."- [tag-id]+ [tag-id :- ms/PositiveInt](t2/delete! :model/TransformTag :id tag-id))-(defn tags+(mu/defn tags"Every TransformTag, in name order."[](t2/select :model/TransformTag {:order-by [[:name :asc]]}));;; ---------------------------------------------------- Jobs -------------------------------------------------------(defn insert-job!+(mu/defn insert-job!"Insert the TransformJob `job-data` and return the inserted instance."- [job-data]+ [job-data :- ::transforms.schema/transform-job.update](t2/insert-returning-instance! :model/TransformJob job-data))-(defn insert-job-tags!+(mu/defn insert-job-tags!"Insert the TransformJobTransformTag `rows`."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::transforms.schema/transform-job-transform-tag.update [:job_id :tag_id :entity_id :position])]](t2/insert! :model/TransformJobTransformTag rows))-(defn jobs-with-active-flag+(mu/defn jobs-with-active-flag"The TransformJobs whose `:active` flag is `active`."- [active]+ [active :- :boolean](t2/select :model/TransformJob :active active))-(defn job+(mu/defn job"The TransformJob with `job-id`, or nil."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-one :model/TransformJob :id job-id))-(defn job-pk+(mu/defn job-pk"The primary key of the TransformJob with `job-id`, or nil."- [job-id]+ [job-id :- ms/PositiveInt](t2/select-one-pk :model/TransformJob :id job-id))-(defn update-job!+(mu/defn update-job!"Apply `updates` to the TransformJob with `job-id`."- [job-id updates]+ [job-id :- ms/PositiveInt+ updates :- (mut/select-keys ::transforms.schema/transform-job.update [:name :description :schedule :ui_display_type])](t2/update! :model/TransformJob job-id updates))-(defn delete-job!+(mu/defn delete-job!"Delete the TransformJob with `job-id`."- [job-id]+ [job-id :- ms/PositiveInt](t2/delete! :model/TransformJob :id job-id))-(defn jobs+(mu/defn jobs"Every TransformJob, newest first."[](t2/select :model/TransformJob {:order-by [[:created_at :desc]]}))-(defn job-run+(mu/defn job-run"The TransformJobRun with `run-id` belonging to the TransformJob with `job-id`, or nil."- [run-id job-id]+ [run-id :- ms/PositiveInt+ job-id :- ms/PositiveInt](t2/select-one :model/TransformJobRun :id run-id :job_id job-id));;; -------------------------------------------------- DAG runs ------------------------------------------------------(defn dag-run+(mu/defn dag-run"The TransformDagRun with `run-id`, or nil."- [run-id]+ [run-id :- ms/PositiveInt](t2/select-one :model/TransformDagRun :id run-id))diff --git a/src/metabase/typed_schemas/db.clj b/src/metabase/typed_schemas/db.cljindex 71a15fc597a..b16eae76ff9 100644--- a/src/metabase/typed_schemas/db.clj+++ b/src/metabase/typed_schemas/db.clj@@ -3,11 +3,14 @@additional logic, so the rest of the module never talks to `toucan2.core` itself."(:require[metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn destination-database-ids+(mu/defn destination-database-ids"The ids among `database-ids` of Databases that are routing destinations."- [database-ids]+ [database-ids :- [:set ::lib.schema.id/database]](t2/select-fn-set :id :model/Database :id [:in database-ids] :router_database_id [:not= nil]))(defn- scope-filter-clause@@ -20,10 +23,12 @@;; no row has id -1: a resolved-but-empty scope matches no rows[:= column -1])))-(defn cards-ordered-by-name+(mu/defn cards-ordered-by-name"The readable, non-archived Cards of `card-type` among `database-ids` and/or `collection-ids` (either nil forunscoped), in name then id order."- [card-type database-ids collection-ids]+ [card-type :- [:enum :model :question :metric]+ database-ids :- [:maybe [:set ::lib.schema.id/database]]+ collection-ids :- [:maybe [:sequential ::lib.schema.id/collection]]](t2/select :model/Card{:where [:and[:= :type (name card-type)]@@ -33,46 +38,47 @@(scope-filter-clause collection-ids :collection_id)]:order-by [[:name :asc] [:id :asc]]}))-(defn field-ids-and-table-ids+(mu/defn field-ids-and-table-ids"The id and Table id of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :table_id] :id [:in field-ids]))-(defn card-dimensions+(mu/defn card-dimensions"The dimensions and dimension mappings of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :dimensions :dimension_mappings] :id card-id))-(defn table-names+(mu/defn table-names"The id, name, and display name of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :display_name] :id [:in table-ids]))-(defn model-actions+(mu/defn model-actions"The id, model id, name, and type of the unarchived non-HTTP Actions of the model Cards with `model-ids`."- [model-ids]+ [model-ids :- [:set ms/PositiveInt]](t2/select [:model/Action :id :model_id :name :type]:model_id [:in model-ids]:archived false:type [:not= "http"]))-(defn field-table-id+(mu/defn field-table-id"The Table id of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :table_id :model/Field :id field-id))-(defn active-tables-in-scope+(mu/defn active-tables-in-scope"The active Tables among `database-ids` and/or `table-ids` (either nil for unscoped), in name then id order."- [database-ids table-ids]+ [database-ids :- [:maybe [:set ::lib.schema.id/database]]+ table-ids :- [:maybe [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]]](t2/select :model/Table{:where [:and [:= :active true](scope-filter-clause database-ids :db_id)(scope-filter-clause table-ids :id)]:order-by [[:name :asc] [:id :asc]]}))-(defn published-library-tables-in-collections+(mu/defn published-library-tables-in-collections"The active, published Tables in `collection-ids` (nil for unscoped), in name then id order."- [collection-ids]+ [collection-ids :- [:maybe [:set ::lib.schema.id/collection]]](t2/select :model/Table{:where [:and[:= :active true]@@ -80,32 +86,32 @@(scope-filter-clause collection-ids :collection_id)]:order-by [[:name :asc] [:id :asc]]}))-(defn measure-definition+(mu/defn measure-definition"The definition of the Measure with `measure-id`, or nil."- [measure-id]+ [measure-id :- ::lib.schema.id/measure](t2/select-one-fn :definition :model/Measure :id measure-id))-(defn measure-definitions+(mu/defn measure-definitions"The id and definition of the Measures with `measure-ids`."- [measure-ids]+ [measure-ids :- [:sequential ::lib.schema.id/measure]](t2/select [:model/Measure :id :definition] :id [:in measure-ids]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one :model/Database :id database-id))-(defn databases-named+(mu/defn databases-named"The Databases named `database-name`."- [database-name]+ [database-name :- :string](t2/select :model/Database :name database-name))-(defn collections+(mu/defn collections"The Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:set ::lib.schema.id/collection]](t2/select :model/Collection :id [:in collection-ids]))-(defn collections-by-entity-ids+(mu/defn collections-by-entity-ids"The Collections with `entity-ids`."- [entity-ids]+ [entity-ids :- [:set :string]](t2/select :model/Collection :entity_id [:in entity-ids]))diff --git a/src/metabase/upload/db.clj b/src/metabase/upload/db.cljindex 4f3ecf1f41f..1edf2e9b837 100644--- a/src/metabase/upload/db.clj+++ b/src/metabase/upload/db.clj@@ -2,48 +2,54 @@"Application database queries for the upload module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.humanization :as humanization]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn current-database+(mu/defn current-database"The Database being used for uploads, or nil."[](t2/select-one :model/Database :uploads_enabled true))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn database-is-attached-dwh?+(mu/defn database-is-attached-dwh?"Whether the Database with `database-id` is an attached data warehouse."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :is_attached_dwh :model/Database database-id))-(defn disable-uploads-for-all-databases!+(mu/defn disable-uploads-for-all-databases!"Disable uploads on every Database that has them enabled."[](t2/update! :model/Database :uploads_enabled true {:uploads_enabled false:uploads_schema_name nil:uploads_table_prefix nil}))-(defn enable-uploads-for-database!+(mu/defn enable-uploads-for-database!"Enable uploads on the Database with `database-id` into `schema-name` with `table-prefix`."- [database-id schema-name table-prefix]+ [database-id :- ::lib.schema.id/database+ schema-name :- [:maybe :string]+ table-prefix :- [:maybe :string]](t2/update! :model/Database database-id {:uploads_enabled true:uploads_schema_name schema-name:uploads_table_prefix table-prefix}))-(defn active-fields-for-table+(mu/defn active-fields-for-table"The active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :active true))-(defn set-field-display-names!+(mu/defn set-field-display-names!"Set the display name of each Field of the Table with `table-id` whose lower-cased name is a key of`name->display-name` to the corresponding value, unless its current display name no longer matches theautomatic humanization of its name (i.e. it was manually edited, in which case it is left alone)."- [table-id name->display-name]+ [table-id :- ::lib.schema.id/table+ name->display-name :- [:map-of :string :string]];; A raw update rather than `t2/update!`, which produces an invalid query for certain versions of PostgreSQL:;; SELECT * FROM "metabase_field" WHERE "id" AND ("table_id" = ?) AND ... (argument of AND must be type boolean).(t2/query {:update (t2/table-name :model/Field)@@ -60,53 +66,55 @@[:= :table_id table-id][:in [:lower :name] (keys name->display-name)]]}))-(defn mark-table-upload!+(mu/defn mark-table-upload!"Flag the Table with `table-id` as an authoritative, writable upload table."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/update! :model/Table table-id {:is_upload true:data_authority :authoritative:data_source :upload:is_writable true}))-(defn set-field-display-name!+(mu/defn set-field-display-name!"Set the display name of the Field with `field-id`."- [field-id display-name]+ [field-id :- ::lib.schema.id/field+ display-name :- :string](t2/update! :model/Field field-id {:display_name display-name}))-(defn unarchived-models-for-table+(mu/defn unarchived-models-for-table"The id, query, and schema of the unarchived model Cards of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select [:model/Card :id :dataset_query :card_schema]:table_id table-id:type :model:archived false))-(defn card-query-and-metadata+(mu/defn card-query-and-metadata"The query, result metadata, and schema of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one [:model/Card :dataset_query :result_metadata :card_schema] card-id))-(defn set-card-result-metadata!+(mu/defn set-card-result-metadata!"Set the result metadata of the Card with `card-id`."- [card-id result-metadata]+ [card-id :- ::lib.schema.id/card+ result-metadata :- [:maybe ::queries.schema/card.result-metadata]](t2/update! :model/Card card-id {:result_metadata result-metadata}))-(defn deactivate-table!+(mu/defn deactivate-table!"Mark the Table with `table-id` inactive."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/update! :model/Table :id table-id {:active false}))-(defn archive-cards-for-table!+(mu/defn archive-cards-for-table!"Archive the unarchived Cards of the Table with `table-id`, returning their ids."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/update-returning-pks! :model/Card {:table_id table-id :archived false} {:archived true}))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one :model/Table :id table-id))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))diff --git a/src/metabase/usage_metadata/db.clj b/src/metabase/usage_metadata/db.cljindex 0c97025d6ba..ae31c62be7b 100644--- a/src/metabase/usage_metadata/db.clj+++ b/src/metabase/usage_metadata/db.clj@@ -2,121 +2,133 @@"Application database queries for the usage metadata module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [malli.util :as mut]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.usage-metadata.schema :as usage-metadata.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn query-execution-hash-counts+(mu/defn query-execution-hash-counts"The `:hash` and execution count `:n` of the QueryExecutions started at or after `started-at` and before`started-before`, grouped by hash."- [started-at started-before]+ [started-at :- ms/TemporalInstant+ started-before :- ms/TemporalInstant](t2/select [:model/QueryExecution :hash [:%count.* :n]]{:where [:and[:>= :started_at started-at][:< :started_at started-before]]:group-by [:hash]}))-(defn raw-field-fingerprint+(mu/defn raw-field-fingerprint"The stored fingerprint of the Field with `field-id`, as it sits in the table, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :fingerprint :metabase_field :id field-id))-(defn queries-reducible+(mu/defn queries-reducible"A reducible over `conn` of the hash and query of the Queries with `query-hashes`."- [conn query-hashes]+ [conn :- (ms/InstanceOfClass java.sql.Connection)+ query-hashes :- [:sequential bytes?]](t2/reducible-select :conn conn [:model/Query :query_hash :query] :query_hash [:in query-hashes]))-(defn delete-segment-rollups-before!+(mu/defn delete-segment-rollups-before!"Delete the SourceSegmentDaily rollup rows bucketed before `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceSegmentDaily :bucket_date [:< bucket-date]))-(defn delete-segment-rollups-for-day!+(mu/defn delete-segment-rollups-for-day!"Delete the SourceSegmentDaily rollup rows bucketed on `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceSegmentDaily :bucket_date bucket-date))-(defn insert-segment-rollups!+(mu/defn insert-segment-rollups!"Insert `rows` into SourceSegmentDaily."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::usage-metadata.schema/source-segment-daily.update [:source_type :source_id :ownership_mode :field_id :predicate :bucket_date :count])]](t2/insert! :model/SourceSegmentDaily rows))-(defn delete-segment-composite-rollups-before!+(mu/defn delete-segment-composite-rollups-before!"Delete the SourceSegmentCompositeDaily rollup rows bucketed before `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceSegmentCompositeDaily :bucket_date [:< bucket-date]))-(defn delete-segment-composite-rollups-for-day!+(mu/defn delete-segment-composite-rollups-for-day!"Delete the SourceSegmentCompositeDaily rollup rows bucketed on `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceSegmentCompositeDaily :bucket_date bucket-date))-(defn insert-segment-composite-rollups!+(mu/defn insert-segment-composite-rollups!"Insert `rows` into SourceSegmentCompositeDaily."- [rows]+ [rows :- [:sequential+ ::usage-metadata.schema/source-segment-composite-daily.update]](t2/insert! :model/SourceSegmentCompositeDaily rows))-(defn delete-metric-rollups-before!+(mu/defn delete-metric-rollups-before!"Delete the SourceMetricDaily rollup rows bucketed before `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceMetricDaily :bucket_date [:< bucket-date]))-(defn delete-metric-rollups-for-day!+(mu/defn delete-metric-rollups-for-day!"Delete the SourceMetricDaily rollup rows bucketed on `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceMetricDaily :bucket_date bucket-date))-(defn insert-metric-rollups!+(mu/defn insert-metric-rollups!"Insert `rows` into SourceMetricDaily."- [rows]+ [rows :- [:sequential+ ::usage-metadata.schema/source-metric-daily.update]](t2/insert! :model/SourceMetricDaily rows))-(defn delete-dimension-rollups-before!+(mu/defn delete-dimension-rollups-before!"Delete the SourceDimensionDaily rollup rows bucketed before `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceDimensionDaily :bucket_date [:< bucket-date]))-(defn delete-dimension-rollups-for-day!+(mu/defn delete-dimension-rollups-for-day!"Delete the SourceDimensionDaily rollup rows bucketed on `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceDimensionDaily :bucket_date bucket-date))-(defn insert-dimension-rollups!+(mu/defn insert-dimension-rollups!"Insert `rows` into SourceDimensionDaily."- [rows]+ [rows :- [:sequential+ ::usage-metadata.schema/source-dimension-daily.update]](t2/insert! :model/SourceDimensionDaily rows))-(defn delete-dimension-profile-rollups-before!+(mu/defn delete-dimension-profile-rollups-before!"Delete the SourceDimensionProfileDaily rollup rows bucketed before `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceDimensionProfileDaily :bucket_date [:< bucket-date]))-(defn delete-dimension-profile-rollups-for-day!+(mu/defn delete-dimension-profile-rollups-for-day!"Delete the SourceDimensionProfileDaily rollup rows bucketed on `bucket-date`."- [bucket-date]+ [bucket-date :- ms/TemporalInstant](t2/delete! :model/SourceDimensionProfileDaily :bucket_date bucket-date))-(defn insert-dimension-profile-rollups!+(mu/defn insert-dimension-profile-rollups!"Insert `rows` into SourceDimensionProfileDaily."- [rows]+ [rows :- [:sequential+ ::usage-metadata.schema/source-dimension-profile-daily.update]](t2/insert! :model/SourceDimensionProfileDaily rows))-(defn field-names+(mu/defn field-names"The id, name, and display name of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select [:model/Field :id :name :display_name] :id [:in field-ids]))-(defn table-names+(mu/defn table-names"The id, name, display name, Database id, and schema of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Table :id :name :display_name :db_id :schema] :id [:in table-ids]))-(defn table-database-ids+(mu/defn table-database-ids"The id and Database id of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Table :id :db_id] :id [:in table-ids]))-(defn card-names+(mu/defn card-names"The id and name of the Cards with `card-ids`."- [card-ids]+ [card-ids :- [:set ::lib.schema.id/card]](t2/select [:model/Card :id :name] :id [:in card-ids]))(defn- grouped-rollup-where@@ -127,10 +139,13 @@bucket-start (conj [:>= :bucket_date bucket-start])bucket-end (conj [:<= :bucket_date bucket-end])))-(defn grouped-segment-rows+(mu/defn grouped-segment-rows"The summed `source_segment_daily` counts optionally narrowed to `source-type`, `source-id`, and bucketed between`bucket-start` and `bucket-end`, grouped by source, field, and predicate, largest first."- [source-type source-id bucket-start bucket-end]+ [source-type :- [:maybe :keyword]+ source-id :- [:maybe ms/PositiveInt]+ bucket-start :- [:maybe ms/TemporalInstant]+ bucket-end :- [:maybe ms/TemporalInstant]](t2/select [:model/SourceSegmentDaily:source_type:source_id@@ -141,10 +156,13 @@:group-by [:source_type :source_id :field_id :predicate]:order-by [[:total_count :desc]]}))-(defn grouped-metric-rows+(mu/defn grouped-metric-rows"The summed `source_metric_daily` counts optionally narrowed to `source-type`, `source-id`, and bucketed between`bucket-start` and `bucket-end`, grouped by source and aggregation, largest first."- [source-type source-id bucket-start bucket-end]+ [source-type :- [:maybe :keyword]+ source-id :- [:maybe ms/PositiveInt]+ bucket-start :- [:maybe ms/TemporalInstant]+ bucket-end :- [:maybe ms/TemporalInstant]](t2/select [:model/SourceMetricDaily:source_type:source_id@@ -157,10 +175,13 @@:group-by [:source_type :source_id :agg_type :agg_field_id :temporal_field_id :temporal_unit]:order-by [[:total_count :desc]]}))-(defn grouped-dimension-rows+(mu/defn grouped-dimension-rows"The summed `source_dimension_daily` counts optionally narrowed to `source-type`, `source-id`, and bucketedbetween `bucket-start` and `bucket-end`, grouped by source, field, unit, and binning, largest first."- [source-type source-id bucket-start bucket-end]+ [source-type :- [:maybe :keyword]+ source-id :- [:maybe ms/PositiveInt]+ bucket-start :- [:maybe ms/TemporalInstant]+ bucket-end :- [:maybe ms/TemporalInstant]](t2/select [:model/SourceDimensionDaily:source_type:source_id@@ -172,10 +193,13 @@:group-by [:source_type :source_id :field_id :temporal_unit :binning]:order-by [[:total_count :desc]]}))-(defn grouped-composite-rows+(mu/defn grouped-composite-rows"The summed `source_segment_composite_daily` counts optionally narrowed to `source-type`, `source-id`, andbucketed between `bucket-start` and `bucket-end`, grouped by source and clause, largest first."- [source-type source-id bucket-start bucket-end]+ [source-type :- [:maybe :keyword]+ source-id :- [:maybe ms/PositiveInt]+ bucket-start :- [:maybe ms/TemporalInstant]+ bucket-end :- [:maybe ms/TemporalInstant]](t2/select [:model/SourceSegmentCompositeDaily:source_type:source_id@@ -187,10 +211,13 @@:group-by [:source_type :source_id :clause :atom_fingerprints :atom_count]:order-by [[:total_count :desc]]}))-(defn grouped-profile-rows+(mu/defn grouped-profile-rows"The summed `source_dimension_profile_daily` counts optionally narrowed to `source-type`, `source-id`, andbucketed between `bucket-start` and `bucket-end`, grouped by source, field, and observation, largest first."- [source-type source-id bucket-start bucket-end]+ [source-type :- [:maybe :keyword]+ source-id :- [:maybe ms/PositiveInt]+ bucket-start :- [:maybe ms/TemporalInstant]+ bucket-end :- [:maybe ms/TemporalInstant]](t2/select [:model/SourceDimensionProfileDaily:source_type:source_id@@ -207,14 +234,14 @@:group-by [:source_type :source_id :field_id :source_basis :observation_type :observation_value]:order-by [[:total_count :desc]]}))-(defn unarchived-segments+(mu/defn unarchived-segments"The id, Table id, and definition of the unarchived Segments, optionally narrowed to `table-id`."- [table-id]+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select [:model/Segment :id :table_id :definition]{:where (cond-> [:and [:= :archived false]]table-id (conj [:= :table_id table-id]))}))-(defn unarchived-metric-cards+(mu/defn unarchived-metric-cards"The id, Database id, query, and schema of the unarchived metric Cards."[](t2/select [:model/Card :id :database_id :dataset_query :card_schema] :type "metric" :archived false))diff --git a/src/metabase/usage_metadata/schema.clj b/src/metabase/usage_metadata/schema.cljindex 569e4042c6f..a953d5d509e 100644--- a/src/metabase/usage_metadata/schema.clj+++ b/src/metabase/usage_metadata/schema.clj@@ -4,7 +4,9 @@These are the shape contracts pinned at the `metabase.usage-metadata.core` boundary andenforced inside `metabase.usage-metadata.insights` producers."(:require- [metabase.util.malli.registry :as mr]))+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))(mr/def ::source-type [:enum :table :card])@@ -110,3 +112,128 @@:any]]][:count {:description "Number of executions in the window that surfaced this observation for this field."}pos-int?]])++(mr/def ::source-dimension-daily+ "A SourceDimensionDaily as selected from the app DB: every column of `:source_dimension_daily`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_type [:or :keyword :string]]+ [:source_id ms/PositiveInt]+ [:ownership_mode [:or :keyword :string]]+ [:field_id ::lib.schema.id/field]+ [:temporal_unit [:maybe [:or :keyword :string]]]+ [:binning [:maybe :string]]+ [:bucket_date ms/TemporalInstant]+ [:count :int]])++(mr/def ::source-dimension-daily.update+ "What an update (or insert) of a SourceDimensionDaily accepts: every column of `:source_dimension_daily` except `id`, all optional."+ [:map {:closed true}+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:ownership_mode {:optional true} [:maybe [:or :keyword :string]]]+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:temporal_unit {:optional true} [:maybe [:or :keyword :string]]]+ [:binning {:optional true} [:maybe :string]]+ [:bucket_date {:optional true} [:maybe ms/TemporalInstant]]+ [:count {:optional true} [:maybe :int]]])++(mr/def ::source-dimension-profile-daily+ "A SourceDimensionProfileDaily as selected from the app DB: every column of `:source_dimension_profile_daily`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_type [:or :keyword :string]]+ [:source_id ms/PositiveInt]+ [:field_id ::lib.schema.id/field]+ [:source_basis [:or :keyword :string]]+ [:observation_type [:or :keyword :string]]+ [:observation_value [:maybe :string]]+ [:bucket_date ms/TemporalInstant]+ [:count :int]])++(mr/def ::source-dimension-profile-daily.update+ "What an update (or insert) of a SourceDimensionProfileDaily accepts: every column of `:source_dimension_profile_daily` except `id`, all optional."+ [:map {:closed true}+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:source_basis {:optional true} [:maybe [:or :keyword :string]]]+ [:observation_type {:optional true} [:maybe [:or :keyword :string]]]+ [:observation_value {:optional true} [:maybe :string]]+ [:bucket_date {:optional true} [:maybe ms/TemporalInstant]]+ [:count {:optional true} [:maybe :int]]])++(mr/def ::source-metric-daily+ "A SourceMetricDaily as selected from the app DB: every column of `:source_metric_daily`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_type [:maybe [:or :keyword :string]]]+ [:source_id [:maybe ms/PositiveInt]]+ [:ownership_mode [:or :keyword :string]]+ [:agg_type [:or :keyword :string]]+ [:agg_field_id [:maybe ::lib.schema.id/field]]+ [:temporal_field_id [:maybe ::lib.schema.id/field]]+ [:temporal_unit [:maybe [:or :keyword :string]]]+ [:bucket_date ms/TemporalInstant]+ [:count :int]])++(mr/def ::source-metric-daily.update+ "What an update (or insert) of a SourceMetricDaily accepts: every column of `:source_metric_daily` except `id`, all optional."+ [:map {:closed true}+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:ownership_mode {:optional true} [:maybe [:or :keyword :string]]]+ [:agg_type {:optional true} [:maybe [:or :keyword :string]]]+ [:agg_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:temporal_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:temporal_unit {:optional true} [:maybe [:or :keyword :string]]]+ [:bucket_date {:optional true} [:maybe ms/TemporalInstant]]+ [:count {:optional true} [:maybe :int]]])++(mr/def ::source-segment-composite-daily+ "A SourceSegmentCompositeDaily as selected from the app DB: every column of `:source_segment_composite_daily`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_type [:maybe [:or :keyword :string]]]+ [:source_id [:maybe ms/PositiveInt]]+ [:ownership_mode [:or :keyword :string]]+ [:clause :string]+ [:atom_fingerprints :string]+ [:atom_count :int]+ [:bucket_date ms/TemporalInstant]+ [:count :int]])++(mr/def ::source-segment-composite-daily.update+ "What an update (or insert) of a SourceSegmentCompositeDaily accepts: every column of `:source_segment_composite_daily` except `id`, all optional."+ [:map {:closed true}+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:ownership_mode {:optional true} [:maybe [:or :keyword :string]]]+ [:clause {:optional true} [:maybe :string]]+ [:atom_fingerprints {:optional true} [:maybe :string]]+ [:atom_count {:optional true} [:maybe :int]]+ [:bucket_date {:optional true} [:maybe ms/TemporalInstant]]+ [:count {:optional true} [:maybe :int]]])++(mr/def ::source-segment-daily+ "A SourceSegmentDaily as selected from the app DB: every column of `:source_segment_daily`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:source_type [:maybe [:or :keyword :string]]]+ [:source_id [:maybe ms/PositiveInt]]+ [:ownership_mode [:or :keyword :string]]+ [:field_id [:maybe ::lib.schema.id/field]]+ [:predicate :string]+ [:bucket_date ms/TemporalInstant]+ [:count :int]])++(mr/def ::source-segment-daily.update+ "What an update (or insert) of a SourceSegmentDaily accepts: every column of `:source_segment_daily` except `id`, all optional."+ [:map {:closed true}+ [:source_type {:optional true} [:maybe [:or :keyword :string]]]+ [:source_id {:optional true} [:maybe ms/PositiveInt]]+ [:ownership_mode {:optional true} [:maybe [:or :keyword :string]]]+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:predicate {:optional true} [:maybe :string]]+ [:bucket_date {:optional true} [:maybe ms/TemporalInstant]]+ [:count {:optional true} [:maybe :int]]])diff --git a/src/metabase/user_key_value/db.clj b/src/metabase/user_key_value/db.cljindex c9393409438..9eeda5357dc 100644--- a/src/metabase/user_key_value/db.clj+++ b/src/metabase/user_key_value/db.clj@@ -2,35 +2,53 @@"Application database queries for the user key-value module. Every function here is a direct Toucan 2 call with noadditional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn user-key-value+(mu/defn user-key-value"The UserKeyValue of the User with `user-id` for `k` in `namespace`, or nil."- [user-id namespace k]+ [user-id :- ::lib.schema.id/user+ namespace :- :string+ k :- :string](t2/select-one :model/UserKeyValue :user_id user-id :namespace namespace :key k))-(defn update-user-key-value!- "Set the value and expiry of the UserKeyValue of the User with `user-id` for `k` in `namespace`."- [user-id namespace k value expires-at]+(mu/defn update-user-key-value!+ "Set the value and expiry of the UserKeyValue of the User with `user-id` for `k` in `namespace`, returning the+ number updated."+ [user-id :- ::lib.schema.id/user+ namespace :- :string+ k :- :string+ value :- :string+ expires-at :- [:maybe ms/TemporalInstant]](t2/update! :model/UserKeyValue :user_id user-id :namespace namespace :key k {:value value, :expires_at expires-at}))-(defn insert-user-key-value!- "Insert a UserKeyValue for the User with `user-id`."- [user-id namespace k value expires-at]+(mu/defn insert-user-key-value!+ "Insert a UserKeyValue for the User with `user-id`, returning the number inserted."+ [user-id :- ::lib.schema.id/user+ namespace :- :string+ k :- :string+ value :- :string+ expires-at :- [:maybe ms/TemporalInstant]](t2/insert! :model/UserKeyValue {:user_id user-id:namespace namespace:key k:value value:expires_at expires-at}))-(defn delete-user-key-value!- "Delete the UserKeyValue of the User with `user-id` for `k` in `namespace`."- [user-id namespace k]+(mu/defn delete-user-key-value!+ "Delete the UserKeyValue of the User with `user-id` for `k` in `namespace`, returning the number deleted."+ [user-id :- ::lib.schema.id/user+ namespace :- :string+ k :- :string](t2/delete! :model/UserKeyValue :namespace namespace :user_id user-id :key k))-(defn unexpired-user-key-value+(mu/defn unexpired-user-key-value"The unexpired UserKeyValue of the User with `user-id` for `k` in `namespace`, or nil."- [user-id namespace k]+ [user-id :- ::lib.schema.id/user+ namespace :- :string+ k :- :string](t2/select-one :model/UserKeyValue{:where [:and[:= :user_id user-id]@@ -40,9 +58,10 @@[:>= :expires_at :%now][:= :expires_at nil]]]}))-(defn unexpired-user-key-values+(mu/defn unexpired-user-key-values"The unexpired UserKeyValues of the User with `user-id` in `namespace`."- [user-id namespace]+ [user-id :- ::lib.schema.id/user+ namespace :- :string](t2/select :model/UserKeyValue{:where [:and[:= :user_id user-id]diff --git a/src/metabase/user_key_value/schema.clj b/src/metabase/user_key_value/schema.cljnew file mode 100644index 00000000000..3a5012a5a22--- /dev/null+++ b/src/metabase/user_key_value/schema.clj@@ -0,0 +1,29 @@+(ns metabase.user-key-value.schema+ "Malli schemas for the user-key-value module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::user-key-value+ "A UserKeyValue as selected from the app DB: every column of `:user_key_value`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:namespace :string]+ [:key :string]+ [:value [:maybe :string]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:expires_at [:maybe ms/TemporalInstant]]])++(mr/def ::user-key-value.update+ "What an update (or insert) of a UserKeyValue accepts: every column of `:user_key_value` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:namespace {:optional true} [:maybe :string]]+ [:key {:optional true} [:maybe :string]]+ [:value {:optional true} [:maybe :string]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]])diff --git a/src/metabase/users/db.clj b/src/metabase/users/db.cljindex e51387eb2d9..f6ed073e639 100644--- a/src/metabase/users/db.clj+++ b/src/metabase/users/db.clj@@ -3,9 +3,14 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[honey.sql.helpers :as sql.helpers]+ [malli.util :as mut][metabase.api.common :as api]+ [metabase.lib.schema.id :as lib.schema.id][metabase.permissions.core :as perms]+ [metabase.users.schema :as users.schema][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))(defn- status-clause@@ -62,25 +67,11 @@[honeysql-map sort](apply sql.helpers/order-by honeysql-map (sort-order-by sort)))-(defn filter-clauses- "Honeysql clauses for filtering on users.-- Options:- :status - filter by status (\"active\", \"deactivated\", \"all\")- :query - text search on first_name, last_name, email- :group-ids - filter by permissions group membership- :user-ids - filter to just these user ids- :include-deactivated - legacy alias for status=all- :is-data-analyst? - filter by data analyst status (true/false)- :can-access-data-studio? - filter by Data Studio access (analysts, superusers, or users with table metadata perms)- :tenant-filter - restrict `:tenant_id`: a tenant id, `:all` (no restriction), `:external` (any- non-nil tenant), or nil (no tenant); omit the key entirely for no restriction- :sort - `:first-name` or `:last-name`, adds an ORDER BY; omit for none- :limit - pagination limit- :offset - pagination offset"+(mu/defn filter-clauses+ "Honeysql clauses for filtering on users. See [[metabase.users.schema/user-filters]] for the accepted options."[{:keys [status query group-ids user-ids include-deactivated is-data-analyst? can-access-data-studio? sortlimit offset]- :as options}]+ :as options} :- ::users.schema/user-filters](cond-> {}true (sql.helpers/where [:= :core_user.type "personal"])true (sql.helpers/where (status-clause status include-deactivated))@@ -110,61 +101,63 @@(some? limit) (sql.helpers/limit limit)(some? offset) (sql.helpers/offset offset)))-(defn set-user-last-login-now!+(mu/defn set-user-last-login-now!"Set `last_login` of the User with `user-id` to now."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/update! :model/User user-id {:last_login :%now}))-(defn user-settings+(mu/defn user-settings"The `:settings` of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :settings [:model/User :settings] :id user-id))-(defn delete-pulse-channel-recipients-for-user!+(mu/defn delete-pulse-channel-recipients-for-user!"Delete every PulseChannelRecipient of the User with `user-id`."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/delete! :model/PulseChannelRecipient :user_id user-id))-(defn group-membership-exists?+(mu/defn group-membership-exists?"Whether the User with `user-id` is a member of the PermissionsGroup with `group-id`."- [group-id user-id]+ [group-id :- ms/PositiveInt+ user-id :- ::lib.schema.id/user](t2/exists? :model/PermissionsGroupMembership :group_id group-id :user_id user-id))-(defn user-group-ids+(mu/defn user-group-ids"The ids of the PermissionsGroups the User with `user-id` belongs to."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id user-id))-(defn group-memberships-for-users+(mu/defn group-memberships-for-users"The user id, group id (as `:id`), and group manager flag of the PermissionsGroupMemberships of the Users with`user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select [:model/PermissionsGroupMembership :user_id [:group_id :id] :is_group_manager] :user_id [:in user-ids]))-(defn user-group-ids-for-users+(mu/defn user-group-ids-for-users"The user id and group id of the PermissionsGroupMemberships of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select [:model/PermissionsGroupMembership :user_id :group_id] :user_id [:in user-ids]))-(defn user-count+(mu/defn user-count"The number of Users."[](t2/count :model/User))-(defn tenant-collection-ids+(mu/defn tenant-collection-ids"A map of Tenant id to tenant Collection id for the Tenants with `tenant-ids`."- [tenant-ids]+ [tenant-ids :- [:set ms/PositiveInt]](t2/select-pk->fn :tenant_collection_id :model/Tenant :id [:in tenant-ids]))-(defn insert-user!+(mu/defn insert-user!"Insert the User `row` and return the inserted instance."- [row]+ [row :- ::users.schema/user.update](t2/insert-returning-instance! :model/User row))-(defn same-groups-user-ids+(mu/defn same-groups-user-ids"The `:user_id`s of the Users sharing a PermissionsGroup other than the one with `all-users-group-id` with the Userwith `user-id`."- [user-id all-users-group-id]+ [user-id :- ::lib.schema.id/user+ all-users-group-id :- ms/PositiveInt](t2/query {:select-distinct [:permissions_group_membership.user_id]:from [:permissions_group_membership]:where [:in :permissions_group_membership.group_id@@ -174,9 +167,12 @@:where [:and [:= :permissions_group_membership.user_id user-id][:not= :permissions_group_membership.group_id all-users-group-id]]}]}))-(defn delete-user-parameter-values!+(mu/defn delete-user-parameter-values!"Delete the UserParameterValues identified by the `:user_id`, `:dashboard_id`, and `:parameter_id` of `parameters`."- [parameters]+ [parameters :- [:sequential [:map+ [:user_id ::lib.schema.id/user]+ [:dashboard_id ::lib.schema.id/dashboard]+ [:parameter_id :string]]]](t2/delete! :model/UserParameterValue{:where (into [:or] (for [p parameters][:and@@ -184,31 +180,37 @@[:= :dashboard_id (:dashboard_id p)][:= :parameter_id (:parameter_id p)]]))}))-(defn insert-user-parameter-values!+(mu/defn insert-user-parameter-values!"Insert the UserParameterValue `rows`."- [rows]+ [rows :- [:sequential+ (mut/select-keys ::users.schema/user-parameter-value.update [:user_id :dashboard_id :parameter_id :value])]](t2/insert! :model/UserParameterValue rows))-(defn user-parameter-values-for-dashboards+(mu/defn user-parameter-values-for-dashboards"The UserParameterValues of the User with `user-id` for the Dashboards with `dashboard-ids`."- [user-id dashboard-ids]+ [user-id :- ::lib.schema.id/user+ dashboard-ids :- [:sequential ::lib.schema.id/dashboard]](t2/select :model/UserParameterValue :dashboard_id [:in dashboard-ids] :user_id user-id))-(defn database-exists?+(mu/defn database-exists?"Whether a Database with `database-id` exists."- [database-id]+ [database-id :- :int](t2/exists? :model/Database :id database-id))-(defn admin-or-self-visible-user+(mu/defn admin-or-self-visible-user"The User with `id`, with the given `columns`, or nil. When `type` and/or `is-active?` are given (non-nil), alsorequires `:type` and/or `:is_active` to match."- [columns id & {:keys [type is-active?]}]+ [columns :- [:sequential :keyword]+ id :- ::lib.schema.id/user+ & {:keys [type is-active?]} :- [:maybe [:map {:closed true}+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:is-active? {:optional true} [:maybe :boolean]]]]](apply t2/select-one (into [:model/User] columns):id id(concat (when type [:type type])(when (some? is-active?) [:is_active is-active?]))))-(defn user-email-exists?+(mu/defn user-email-exists?"Whether a User whose lower-cased email is `lower-case-email` exists."- [lower-case-email]+ [lower-case-email :- :string](t2/exists? :model/User :%lower.email lower-case-email))diff --git a/src/metabase/users/schema.clj b/src/metabase/users/schema.cljindex d664b7a74c6..b3380a7a38c 100644--- a/src/metabase/users/schema.clj+++ b/src/metabase/users/schema.clj@@ -1,6 +1,7 @@(ns metabase.users.schema(:require[clojure.string :as str]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.i18n :refer [deferred-tru]][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -51,3 +52,133 @@[:type [:enum "dashboard" "question"]][:id ms/PositiveInt][:name ms/NonBlankString]])++(mr/def ::user.settings+ "The `:settings` column of a User, decoded."+ :map)++(mr/def ::user+ "A User as selected from the app DB: every column of `:core_user` that the model selects by default, plus `:common_name` added by the model's after-select hook."+ [:map {:closed true}+ [:id ::lib.schema.id/user]+ [:email :string]+ [:first_name [:maybe :string]]+ [:last_name [:maybe :string]]+ [:date_joined ms/TemporalInstant]+ [:last_login [:maybe ms/TemporalInstant]]+ [:is_superuser :boolean]+ [:is_qbnewb :boolean]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:is_data_analyst :boolean]+ [:common_name {:optional true} [:maybe :string]]])++(mr/def ::user.full+ "A User as selected from the app DB with every column of `:core_user`, not only the default ones, plus `:common_name` added by the model's after-select hook."+ [:map {:closed true}+ [:id ::lib.schema.id/user]+ [:email :string]+ [:first_name [:maybe :string]]+ [:last_name [:maybe :string]]+ [:password [:maybe :string]]+ [:password_salt [:maybe :string]]+ [:date_joined ms/TemporalInstant]+ [:last_login [:maybe ms/TemporalInstant]]+ [:is_superuser :boolean]+ [:is_active :boolean]+ [:reset_token [:maybe :string]]+ [:reset_triggered [:maybe :int]]+ [:is_qbnewb :boolean]+ [:login_attributes [:maybe LoginAttributes]]+ [:updated_at [:maybe ms/TemporalInstant]]+ [:sso_source [:maybe [:or :keyword :string]]]+ [:locale [:maybe :string]]+ [:is_datasetnewb :boolean]+ [:settings [:maybe ::user.settings]]+ [:type [:or :keyword :string]]+ [:entity_id :string]+ [:deactivated_at [:maybe ms/TemporalInstant]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:jwt_attributes [:maybe LoginAttributes]]+ [:deactivated_with_tenant [:maybe :boolean]]+ [:is_data_analyst :boolean]+ [:common_name {:optional true} [:maybe :string]]])++(mr/def ::user.update+ "What an update (or insert) of a User accepts: every column of `:core_user` except `id`, all optional."+ [:map {:closed true}+ [:email {:optional true} [:maybe :string]]+ [:first_name {:optional true} [:maybe :string]]+ [:last_name {:optional true} [:maybe :string]]+ [:password {:optional true} [:maybe :string]]+ [:password_salt {:optional true} [:maybe :string]]+ [:date_joined {:optional true} [:maybe ms/TemporalInstant]]+ [:last_login {:optional true} [:maybe ms/TemporalInstant]]+ [:is_superuser {:optional true} [:maybe :boolean]]+ [:is_active {:optional true} [:maybe :boolean]]+ [:reset_token {:optional true} [:maybe :string]]+ [:reset_triggered {:optional true} [:maybe :int]]+ [:is_qbnewb {:optional true} [:maybe :boolean]]+ [:login_attributes {:optional true} [:maybe LoginAttributes]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:sso_source {:optional true} [:maybe [:or :keyword :string]]]+ [:locale {:optional true} [:maybe :string]]+ [:is_datasetnewb {:optional true} [:maybe :boolean]]+ [:settings {:optional true} [:maybe ::user.settings]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:entity_id {:optional true} [:maybe :string]]+ [:deactivated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:jwt_attributes {:optional true} [:maybe LoginAttributes]]+ [:deactivated_with_tenant {:optional true} [:maybe :boolean]]+ [:is_data_analyst {:optional true} [:maybe :boolean]]])++(mr/def ::user-filters+ "Options accepted by `metabase.users.db/filter-clauses` (and, by extension, any db.clj function that filters+ Users on the caller's behalf).++ Keys:+ :status - filter by status (\"active\", \"deactivated\", \"all\")+ :query - text search on first_name, last_name, email+ :group-ids - filter by permissions group membership+ :user-ids - filter to just these user ids+ :include-deactivated - legacy alias for status=all+ :is-data-analyst? - filter by data analyst status (true/false)+ :can-access-data-studio? - filter by Data Studio access (analysts, superusers, or users with table metadata perms)+ :tenant-filter - restrict `:tenant_id`: a tenant id, `:all` (no restriction), `:external` (any+ non-nil tenant), or nil (no tenant); omit the key entirely for no restriction+ :sort - `:first-name` or `:last-name`, adds an ORDER BY; omit for none+ :limit - pagination limit+ :offset - pagination offset"+ [:map {:closed true}+ [:status {:optional true} [:maybe [:or :keyword :string]]]+ [:query {:optional true} [:maybe :string]]+ [:group-ids {:optional true} [:maybe [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]]]+ [:user-ids {:optional true} [:maybe [:or [:set ::lib.schema.id/user] [:sequential ::lib.schema.id/user]]]]+ [:include-deactivated {:optional true} [:maybe :boolean]]+ [:is-data-analyst? {:optional true} [:maybe :boolean]]+ [:can-access-data-studio? {:optional true} [:maybe :boolean]]+ [:tenant-filter {:optional true} [:maybe [:or ms/PositiveInt [:enum :all :external]]]]+ [:sort {:optional true} [:maybe [:enum :first-name :last-name]]]+ [:limit {:optional true} [:maybe ms/PositiveInt]]+ [:offset {:optional true} [:maybe ms/IntGreaterThanOrEqualToZero]]])++(mr/def ::user-parameter-value.value+ "The `:value` column of a UserParameterValue, decoded."+ [:or :string number? :boolean [:sequential [:maybe [:or :string number? :boolean]]]])++(mr/def ::user-parameter-value+ "A UserParameterValue as selected from the app DB: every column of `:user_parameter_value`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id ::lib.schema.id/user]+ [:parameter_id :string]+ [:value [:maybe ::user-parameter-value.value]]+ [:dashboard_id [:maybe ::lib.schema.id/dashboard]]])++(mr/def ::user-parameter-value.update+ "What an update (or insert) of a UserParameterValue accepts: every column of `:user_parameter_value` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:parameter_id {:optional true} [:maybe :string]]+ [:value {:optional true} [:maybe ::user-parameter-value.value]]+ [:dashboard_id {:optional true} [:maybe ::lib.schema.id/dashboard]]])diff --git a/src/metabase/users_rest/api.clj b/src/metabase/users_rest/api.cljindex a2ae956a455..13d04bd3885 100644--- a/src/metabase/users_rest/api.clj+++ b/src/metabase/users_rest/api.clj@@ -200,19 +200,19 @@(= tenancy :all) :all(= tenancy :external) :external:else nil)- clauses (user/filter-clauses {:status status- :query query- :group-ids (when group_id [group_id])- :include-deactivated include_deactivated- :is-data-analyst? is_data_analyst- :can-access-data-studio? can_access_data_studio- :tenant-filter tenant-filter- :sort :first-name- :limit (request/limit)- :offset (request/offset)})]+ filters {:status status+ :query query+ :group-ids (when group_id [group_id])+ :include-deactivated include_deactivated+ :is-data-analyst? is_data_analyst+ :can-access-data-studio? can_access_data_studio+ :tenant-filter tenant-filter+ :sort :first-name+ :limit (request/limit)+ :offset (request/offset)}]{:data (cond-> (users-rest.db/users-with-columns(user-visible-columns)- clauses)+ filters);; For admins also include the IDs of Users' Personal Collectionsapi/*is-superuser?*(t2/hydrate :personal_collection_id :tenant_collection_id)@@ -224,7 +224,7 @@;; multiple groupsgroup_iddistinct)- :total (-> (users-rest.db/distinct-user-count (users/filter-clauses-without-paging clauses))+ :total (-> (users-rest.db/distinct-user-count filters)first:count):limit (request/limit)@@ -246,18 +246,18 @@(not api/*is-superuser?*) (:tenant_id @api/*current-user*)(not (perms/use-tenants)) nil:else :all))- (all [] (let [clauses (user/filter-clauses {:tenant-filter (recipient-tenant-filter)- :sort :last-name})]- {:data (users-rest.db/users-with-columns (user-visible-columns) clauses)- :total (users-rest.db/user-count (users/filter-clauses-without-paging clauses))+ (all [] (let [filters {:tenant-filter (recipient-tenant-filter)+ :sort :last-name}]+ {:data (users-rest.db/users-with-columns (user-visible-columns) filters)+ :total (users-rest.db/user-count filters):limit (request/limit):offset (request/offset)}))(within-group [] (let [user-ids (user/same-groups-user-ids api/*current-user-id*)- clauses (user/filter-clauses {:tenant-filter (recipient-tenant-filter)- :user-ids user-ids- :sort :last-name})]- {:data (users-rest.db/users-with-columns (user-visible-columns) clauses)- :total (users-rest.db/user-count (users/filter-clauses-without-paging clauses))+ filters {:tenant-filter (recipient-tenant-filter)+ :user-ids user-ids+ :sort :last-name}]+ {:data (users-rest.db/users-with-columns (user-visible-columns) filters)+ :total (users-rest.db/user-count filters):limit (request/limit):offset (request/offset)}))](conddiff --git a/src/metabase/users_rest/db.clj b/src/metabase/users_rest/db.cljindex 4a1fbbe159b..e9b2f369cf1 100644--- a/src/metabase/users_rest/db.clj+++ b/src/metabase/users_rest/db.clj@@ -3,41 +3,57 @@additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require[metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi]+ [metabase.users.core :as users]+ [metabase.users.models.user :as user]+ [metabase.users.schema :as users.schema][metabase.util :as u]+ [metabase.util.malli :as mu][toucan2.core :as t2]))-(defn rename-collection!+(def ^:private PersonalUserChanges+ "The keys callers pass to [[update-personal-user!]]."+ [:map {:closed true}+ [:is_active {:optional true} :boolean]+ [:is_qbnewb {:optional true} :boolean]+ [:is_datasetnewb {:optional true} :boolean]])++(mu/defn rename-collection!"Set the name of the Collection with `collection-id`."- [collection-id collection-name]+ [collection-id :- ::lib.schema.id/collection+ collection-name :- :string](t2/update! :model/Collection collection-id {:name collection-name}))-(defn users-with-columns- "The `columns` of the Users selected by the Honey SQL `query`."- [columns query]- (t2/select (into [:model/User] columns) query))--(defn user-count- "The number of Users matching the Honey SQL `query`."- [query]- (t2/count :model/User query))--(defn distinct-user-count- "The `:count` of distinct Users matching the Honey SQL `clauses`."- [clauses]+(mu/defn users-with-columns+ "The `columns` of the Users matching `filters` (see [[metabase.users.schema/user-filters]])."+ [columns :- [:sequential :keyword]+ filters :- ::users.schema/user-filters]+ (t2/select (into [:model/User] columns) (user/filter-clauses filters)))++(mu/defn user-count+ "The number of Users matching `filters` (see [[metabase.users.schema/user-filters]]), ignoring any `:sort`,+ `:limit`, or `:offset` in `filters`."+ [filters :- ::users.schema/user-filters]+ (t2/count :model/User (users/filter-clauses-without-paging (user/filter-clauses filters))))++(mu/defn distinct-user-count+ "The `:count` of distinct Users matching `filters` (see [[metabase.users.schema/user-filters]]), ignoring any+ `:sort`, `:limit`, or `:offset` in `filters`."+ [filters :- ::users.schema/user-filters](t2/query (merge {:select [[[:count [:distinct :core_user.id]] :count]]:from :core_user}- clauses)))+ (users/filter-clauses-without-paging (user/filter-clauses filters)))))-(defn user-sso-source+(mu/defn user-sso-source"The SSO source of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one-fn :sso_source :model/User :id user-id))-(defn has-visible-card?+(mu/defn has-visible-card?"Whether an unarchived, non-internal Card visible to the current user exists, optionally narrowed to `card-type`(nil for any type)."- [card-type]+ [card-type :- [:maybe :string]](t2/exists? :model/Card{:where (into [:and[:= :archived false]@@ -45,7 +61,7 @@(mi/exclude-internal-content-hsql :model/Card)](when card-type [[:= :type card-type]]))}))-(defn has-visible-dashboard?+(mu/defn has-visible-dashboard?"Whether an unarchived, non-internal Dashboard visible to the current user exists."[](t2/exists? :model/Dashboard@@ -54,17 +70,19 @@(collection/visible-collection-filter-clause)(mi/exclude-internal-content-hsql :model/Dashboard)]}))-(defn first-login+(mu/defn first-login"The timestamp of the earliest LoginHistory of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/LoginHistory :timestamp] :user_id user-id {:order-by [[:timestamp :asc]]}))-(defn dashboard- "The Dashboard with `dashboard-id`, or nil."- [dashboard-id]+(mu/defn dashboard+ "The Dashboard with `dashboard-id`, or nil. `dashboard-id` may be nil (e.g. when no custom homepage dashboard is+ configured) or a stale/invalid id (e.g. a deleted custom homepage Dashboard) that matches no Dashboard, in which+ case the result is nil."+ [dashboard-id :- [:maybe :int]](t2/select-one :model/Dashboard :id dashboard-id))-(defn writable-collection-exists?+(mu/defn writable-collection-exists?"Whether the current user can write to at least one Collection (excluding the Trash and archived items)."[](t2/exists? :model/Collection@@ -74,44 +92,47 @@:include-archived-items :exclude:permission-level :write})}))-(defn other-user-with-email-exists?+(mu/defn other-user-with-email-exists?"Whether a User other than `user-id` has an email matching `email` case-insensitively."- [email user-id]+ [email :- :string+ user-id :- ::lib.schema.id/user](t2/exists? :model/User, :%lower.email (u/lower-case-en email), :id [:not= user-id]))-(defn update-user!+(mu/defn update-user!"Apply `changes` to the User with `user-id`."- [user-id changes]+ [user-id :- ::lib.schema.id/user+ changes :- ::users.schema/user.update](t2/update! :model/User user-id changes))-(defn user+(mu/defn user"The User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one :model/User :id user-id))-(defn personal-user-columns+(mu/defn personal-user-columns"The id, email, name, active flag, SSO source, and tenant id of the personal User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :email :first_name :last_name :is_active :sso_source :tenant_id]:type :personal:id user-id))-(defn active-personal-user-login-columns+(mu/defn active-personal-user-login-columns"The id, email, and last login of the active personal User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :email :last_login], :id user-id, :type :personal, :is_active true))-(defn user-active-and-type+(mu/defn user-active-and-type"The id, active flag, and type of the User with `user-id`, or nil."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/select-one [:model/User :id :is_active :type] :id user-id))-(defn user-exists?+(mu/defn user-exists?"Whether a User with `user-id` exists."- [user-id]+ [user-id :- ::lib.schema.id/user](t2/exists? :model/User :id user-id))-(defn update-personal-user!+(mu/defn update-personal-user!"Apply `changes` to the personal User with `user-id`, returning the number of rows updated."- [user-id changes]+ [user-id :- ::lib.schema.id/user+ changes :- PersonalUserChanges](t2/update! :model/User user-id {:type :personal} changes))diff --git a/src/metabase/util/malli/schema.clj b/src/metabase/util/malli/schema.cljindex c96b9c97ecb..1a23d6a47f7 100644--- a/src/metabase/util/malli/schema.clj+++ b/src/metabase/util/malli/schema.clj@@ -292,6 +292,14 @@"Schema for a valid list of values for a field, in contexts where the field can have a remapped field."[:sequential [:or RemappedFieldValue NonRemappedFieldValue]])+(def FieldValue+ "One stored value of a Field, as kept in the `field_values.values` and `human_readable_values` columns."+ [:maybe [:or :string number? :boolean]])++(def FieldValues+ "The stored values of a Field: the decoded `field_values.values` or `human_readable_values` column."+ [:sequential FieldValue])+(def FieldValuesResult"Schema for a value result of fetching the values for a field, in contexts where the field can have a remapped field."[:mapdiff --git a/src/metabase/view_log/db.clj b/src/metabase/view_log/db.cljindex 444f7eb0ea8..711e13b1528 100644--- a/src/metabase/view_log/db.clj+++ b/src/metabase/view_log/db.clj@@ -3,6 +3,10 @@additional logic, so the rest of the module never talks to `toucan2.core` itself."(:require[java-time.api :as t]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.view-log.schema :as view-log.schema][toucan2.core :as t2]))(defn- increment-view-counts-of-model!@@ -16,42 +20,45 @@count->ids))]}:where [:in :id (apply concat (vals count->ids))]}))-(defn increment-card-view-counts!+(def ^:private count->ids-schema+ [:map-of ms/PositiveInt [:sequential ms/PositiveInt]])++(mu/defn increment-card-view-counts!"Add, for each `[count ids]` entry of `count->ids`, `count` to the `view_count` of the Cards with `ids`."- [count->ids]+ [count->ids :- count->ids-schema](increment-view-counts-of-model! :model/Card count->ids))-(defn increment-dashboard-view-counts!+(mu/defn increment-dashboard-view-counts!"Add, for each `[count ids]` entry of `count->ids`, `count` to the `view_count` of the Dashboards with `ids`."- [count->ids]+ [count->ids :- count->ids-schema](increment-view-counts-of-model! :model/Dashboard count->ids))-(defn increment-table-view-counts!+(mu/defn increment-table-view-counts!"Add, for each `[count ids]` entry of `count->ids`, `count` to the `view_count` of the Tables with `ids`."- [count->ids]+ [count->ids :- count->ids-schema](increment-view-counts-of-model! :model/Table count->ids))-(defn increment-document-view-counts!+(mu/defn increment-document-view-counts!"Add, for each `[count ids]` entry of `count->ids`, `count` to the `view_count` of the Documents with `ids`."- [count->ids]+ [count->ids :- count->ids-schema](increment-view-counts-of-model! :model/Document count->ids))-(defn insert-view-logs!- "Insert the ViewLog rows `views`."- [views]+(mu/defn insert-view-logs!+ "Insert the ViewLog rows `views`, returning the number inserted."+ [views :- [:sequential ::view-log.schema/view-log.update]](t2/insert! :model/ViewLog views))-(defn card-type+(mu/defn card-type"The `:type` of the Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one-fn :type :model/Card :id card-id))-(defn update-dashboards-last-viewed-at!+(mu/defn update-dashboards-last-viewed-at!"Move `last_viewed_at` of each Dashboard in `dashboard-id->timestamp` forward to its timestamp, without touching`updated_at`, via a raw update that bypasses Toucan 2 model hooks (specifically the :hook/search-indexafter-update; the search index can tolerate staleness on this field, catching up on the next re-index cycle orwhen the dashboard is edited)."- [dashboard-id->timestamp]+ [dashboard-id->timestamp :- [:map-of ms/PositiveInt ms/TemporalInstant]](t2/query {:update (t2/table-name :model/Dashboard):set {:last_viewed_at (into [:case](mapcat (fn [[id timestamp]]diff --git a/src/metabase/view_log/schema.clj b/src/metabase/view_log/schema.cljnew file mode 100644index 00000000000..25779d59a45--- /dev/null+++ b/src/metabase/view_log/schema.clj@@ -0,0 +1,57 @@+(ns metabase.view-log.schema+ "Malli schemas for the view-log module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::view-log.metadata+ "The `:metadata` column of a ViewLog, decoded."+ :map)++(mr/def ::view-log+ "A ViewLog as selected from the app DB: every column of `:view_log`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:model [:or :keyword :string]]+ [:model_id [:maybe :int]]+ [:timestamp ms/TemporalInstant]+ [:metadata [:maybe ::view-log.metadata]]+ [:has_access [:maybe :boolean]]+ [:context [:maybe [:or :keyword :string]]]+ [:embedding_client [:maybe :string]]+ [:embedding_sdk_version [:maybe :string]]+ [:auth_method [:maybe [:or :keyword :string]]]+ [:tenant_id [:maybe ms/PositiveInt]]+ [:embedding_hostname [:maybe :string]]+ [:embedding_path [:maybe :string]]+ [:user_agent [:maybe :string]]+ [:ip_address [:maybe :string]]+ [:sanitized_user_agent [:maybe :string]]+ [:embedding_route [:maybe :string]]+ [:metabase_version [:maybe :string]]+ [:embedding_client_identifier [:maybe :string]]])++(mr/def ::view-log.update+ "What an update (or insert) of a ViewLog accepts: every column of `:view_log` except `id`, all optional."+ [:map {:closed true}+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:model {:optional true} [:maybe [:or :keyword :string]]]+ [:model_id {:optional true} [:maybe :int]]+ [:timestamp {:optional true} [:maybe ms/TemporalInstant]]+ [:metadata {:optional true} [:maybe ::view-log.metadata]]+ [:has_access {:optional true} [:maybe :boolean]]+ [:context {:optional true} [:maybe [:or :keyword :string]]]+ [:embedding_client {:optional true} [:maybe :string]]+ [:embedding_sdk_version {:optional true} [:maybe :string]]+ [:auth_method {:optional true} [:maybe [:or :keyword :string]]]+ [:tenant_id {:optional true} [:maybe ms/PositiveInt]]+ [:embedding_hostname {:optional true} [:maybe :string]]+ [:embedding_path {:optional true} [:maybe :string]]+ [:user_agent {:optional true} [:maybe :string]]+ [:ip_address {:optional true} [:maybe :string]]+ [:sanitized_user_agent {:optional true} [:maybe :string]]+ [:embedding_route {:optional true} [:maybe :string]]+ [:metabase_version {:optional true} [:maybe :string]]+ [:embedding_client_identifier {:optional true} [:maybe :string]]])diff --git a/src/metabase/warehouse_schema/db.clj b/src/metabase/warehouse_schema/db.cljindex 5eff57a4c7a..f8704c65bf9 100644--- a/src/metabase/warehouse_schema/db.clj+++ b/src/metabase/warehouse_schema/db.clj@@ -3,65 +3,73 @@no additional logic, so the rest of the module only touches `toucan2.core` for model definitions, hydrationmethods, and transactions."(:require+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.db :as models.db]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema][toucan2.core :as t2]))(def field-order-rule"How should we order fields."[[:position :asc] [:%lower.name :asc]])-;;; ------------------------------------------------- Field -------------------------------------------------+;;; ------------------------------------------------- ::warehouse-schema.schema/field --------------------------------------------------(defn field- "The Field with `field-id`, or nil."- [field-id]+(mu/defn field+ "The ::warehouse-schema.schema/field with `field-id`, or nil."+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))-(defn field-in-path- "The Field named by the last of `field-names` (each nested inside the previous, bottom-most first) under+(mu/defn field-in-path+ "The ::warehouse-schema.schema/field named by the last of `field-names` (each nested inside the previous, bottom-most first) under`table-id`, or nil. See `metabase.models.db/field-in-path`, which owns the shared query."- [table-id field-names]+ [table-id :- [:maybe ::lib.schema.id/table]+ field-names :- [:sequential :string]](models.db/field-in-path table-id field-names))-(defn fields+(mu/defn fields"The Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select :model/Field :id [:in field-ids]))-(defn fields-by-id- "A map of ID to Field for `field-ids`."- [field-ids]+(mu/defn fields-by-id+ "A map of ID to ::warehouse-schema.schema/field for `field-ids`."+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select-fn->fn :id identity :model/Field :id [:in field-ids]))-(defn field-table-id-rows- "The ID and Table ID of the Fields with `field-ids`."- [field-ids]+(mu/defn field-table-id-rows+ "The ID and ::warehouse-schema.schema/table ID of the Fields with `field-ids`."+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select [:model/Field :id :table_id] :id [:in field-ids]))-(defn field-table-id- "The Table ID of the Field with `field-id`."- [field-id]+(mu/defn field-table-id+ "The ::warehouse-schema.schema/table ID of the ::warehouse-schema.schema/field with `field-id`."+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :table_id :model/Field :id field-id))-(defn field-id-by-name- "The ID of the Field named `field-name` under `parent-id` in the Table with `table-id`, or nil."- [table-id parent-id field-name]+(mu/defn field-id-by-name+ "The ID of the ::warehouse-schema.schema/field named `field-name` under `parent-id` in the ::warehouse-schema.schema/table with `table-id`, or nil."+ [table-id :- ::lib.schema.id/table+ parent-id :- [:maybe ms/PositiveInt]+ field-name :- :string](t2/select-one-pk :model/Field :name field-name :parent_id parent-id :table_id table-id))-(defn field-values-eligibility- "The columns deciding whether the Field with `field-id` should have FieldValues, or nil."- [field-id]+(mu/defn field-values-eligibility+ "The columns deciding whether the ::warehouse-schema.schema/field with `field-id` should have FieldValues, or nil."+ [field-id :- ::lib.schema.id/field](t2/select-one [:model/Field :base_type :visibility_type :has_field_values :preview_display] :id field-id))-(defn field-ids-for-table- "The IDs of the Fields of the Table with `table-id`."- [table-id]+(mu/defn field-ids-for-table+ "The IDs of the Fields of the ::warehouse-schema.schema/table with `table-id`."+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field {:where [:= :table_id table-id]}))-(defn active-field-ids-for-table- "The IDs of the active Fields of the Table with `table-id`."- [table-id]+(mu/defn active-field-ids-for-table+ "The IDs of the active Fields of the ::warehouse-schema.schema/table with `table-id`."+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field :table_id table-id :active true))(defn- field-order-order-by@@ -78,32 +86,33 @@:database [[:database_position :asc]]:alphabetical [[:%lower.name :asc]]))-(defn field-ids-for-table-ordered- "The ids of the Fields of the Table with `table-id`, ordered per `field-order` (`:custom`, `:smart`, `:database`,+(mu/defn field-ids-for-table-ordered+ "The ids of the Fields of the ::warehouse-schema.schema/table with `table-id`, ordered per `field-order` (`:custom`, `:smart`, `:database`,or `:alphabetical`)."- [table-id field-order]+ [table-id :- ::lib.schema.id/table+ field-order :- [:enum :custom :smart :database :alphabetical]](t2/select [:model/Field :id] :table_id table-id {:order-by (field-order-order-by field-order)}))-(defn active-fields-for-tables+(mu/defn active-fields-for-tables"The active, unretired Fields of the Tables with `table-ids`, in field order."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Field:active true:table_id [:in table-ids]:visibility_type [:not= "retired"]{:order-by field-order-rule}))-(defn pk-field-ids-by-table- "A map of Table ID to the ID of its visible primary key Field for `table-ids`."- [table-ids]+(mu/defn pk-field-ids-by-table+ "A map of ::warehouse-schema.schema/table ID to the ID of its visible primary key ::warehouse-schema.schema/field for `table-ids`."+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select-fn->fn :table_id :id :model/Field:table_id [:in table-ids]:semantic_type (app-db/isa :type/PK):visibility_type [:not-in ["sensitive" "retired"]]))-(defn fk-source-field-ids-without-user-settings- "The `:id` rows of the Fields targeting the Field with `field-id` that have no FieldUserSettings row."- [field-id]+(mu/defn fk-source-field-ids-without-user-settings+ "The `:id` rows of the Fields targeting the ::warehouse-schema.schema/field with `field-id` that have no FieldUserSettings row."+ [field-id :- ::lib.schema.id/field](t2/query {:select [:id]:from [:metabase_field]:where [:and@@ -112,80 +121,87 @@:from [:metabase_field_user_settings]:where [:= :metabase_field_user_settings.field_id :metabase_field.id]}]]]}))-(defn update-field!- "Apply `changes` to the Field with `field-id`."- [field-id changes]+(mu/defn update-field!+ "Apply `changes` to the ::warehouse-schema.schema/field with `field-id`, returning the number updated."+ [field-id :- ::lib.schema.id/field+ changes :- (mut/select-keys ::warehouse-schema.schema/field.update [:position :custom_position])](t2/update! :model/Field field-id changes))-(defn clear-fk-targets-to-field!- "Clear the FK semantic type and target of every Field targeting the Field with `field-id`."- [field-id]+(mu/defn clear-fk-targets-to-field!+ "Clear the FK semantic type and target of every ::warehouse-schema.schema/field targeting the ::warehouse-schema.schema/field with `field-id`, returning the number+ updated."+ [field-id :- ::lib.schema.id/field](t2/update! :model/Field {:fk_target_field_id field-id} {:semantic_type nil, :fk_target_field_id nil}))-(defn delete-child-fields!- "Delete the Fields nested under the Field with `parent-id`."- [parent-id]+(mu/defn delete-child-fields!+ "Delete the Fields nested under the ::warehouse-schema.schema/field with `parent-id`, returning the number deleted."+ [parent-id :- ms/PositiveInt](t2/delete! :model/Field :parent_id parent-id))-(defn delete-fields-for-table!- "Delete the Fields of the Table with `table-id`."- [table-id]+(mu/defn delete-fields-for-table!+ "Delete the Fields of the ::warehouse-schema.schema/table with `table-id`, returning the number deleted."+ [table-id :- ::lib.schema.id/table](t2/delete! :model/Field :table_id table-id));;; -------------------------------------------- FieldUserSettings ---------------------------------------------(defn field-user-settings- "The FieldUserSettings of the Field with `field-id`, or nil."- [field-id]+(mu/defn field-user-settings+ "The FieldUserSettings of the ::warehouse-schema.schema/field with `field-id`, or nil (also for a nil `field-id`, e.g. a ::warehouse-schema.schema/field not yet+ inserted)."+ [field-id :- [:maybe ::lib.schema.id/field]](t2/select-one :model/FieldUserSettings :field_id field-id))-(defn field-user-settings-exist?- "Whether the Field with `field-id` has a FieldUserSettings row."- [field-id]+(mu/defn field-user-settings-exist?+ "Whether the ::warehouse-schema.schema/field with `field-id` has a FieldUserSettings row."+ [field-id :- ::lib.schema.id/field](t2/exists? :model/FieldUserSettings field-id))-(defn user-edited-field-ids-for-table- "The IDs of the Fields of the Table with `table-id` that have a FieldUserSettings row."- [table-id]+(mu/defn user-edited-field-ids-for-table+ "The IDs of the Fields of the ::warehouse-schema.schema/table with `table-id` that have a FieldUserSettings row."+ [table-id :- ::lib.schema.id/table](t2/select-fn-set :field_id :model/FieldUserSettings{:join [[:metabase_field :f] [:= :f.id :field_id]]:where [:= :f.table_id table-id]}))-(defn insert-field-user-settings!- "Insert one FieldUserSettings map or a sequence of them."- [rows]+(mu/defn insert-field-user-settings!+ "Insert one FieldUserSettings map or a sequence of them, returning the number inserted."+ [rows :- [:or (mut/select-keys ::warehouse-schema.schema/field-user-settings.update [:field_id :created_at :updated_at :semantic_type :description :display_name :visibility_type :fk_target_field_id :has_field_values :effective_type :coercion_strategy :caveats :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity]) [:sequential (mut/select-keys ::warehouse-schema.schema/field-user-settings.update [:field_id :created_at :updated_at :semantic_type :description :display_name :visibility_type :fk_target_field_id :has_field_values :effective_type :coercion_strategy :caveats :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity])]]](t2/insert! :model/FieldUserSettings rows))-(defn update-field-user-settings!- "Apply `changes` to the FieldUserSettings of the Field with `field-id`."- [field-id changes]+(mu/defn update-field-user-settings!+ "Apply `changes` to the FieldUserSettings of the ::warehouse-schema.schema/field with `field-id`, returning the number updated."+ [field-id :- ::lib.schema.id/field+ changes :- (mut/select-keys ::warehouse-schema.schema/field-user-settings.update [:field_id :created_at :updated_at :semantic_type :description :display_name :visibility_type :fk_target_field_id :has_field_values :effective_type :coercion_strategy :caveats :points_of_interest :nfc_path :json_unfolding :settings :data_sensitivity])](t2/update! :model/FieldUserSettings field-id changes))-(defn clear-user-settings-fk-targets-to-field!- "Clear the user-set FK semantic type and target of every Field targeting the Field with `field-id`."- [field-id]+(mu/defn clear-user-settings-fk-targets-to-field!+ "Clear the user-set FK semantic type and target of every ::warehouse-schema.schema/field targeting the ::warehouse-schema.schema/field with `field-id`, returning the+ number updated."+ [field-id :- ::lib.schema.id/field](t2/update! :model/FieldUserSettings {:fk_target_field_id field-id} {:semantic_type nil, :fk_target_field_id nil}));;; ---------------------------------------------- FieldValues -----------------------------------------------(defn field-values-of-type- "The FieldValues of `type` with `hash-key` for the Field with `field-id`."- [field-id type hash-key]+(mu/defn field-values-of-type+ "The FieldValues of `type` with `hash-key` for the ::warehouse-schema.schema/field with `field-id`."+ [field-id :- ::lib.schema.id/field+ type :- [:enum :full :sandbox :impersonation :linked-filter :advanced]+ hash-key :- [:maybe :string]](t2/select :model/FieldValues :field_id field-id :type type :hash_key hash-key))-(defn full-field-values-for-fields+(mu/defn full-field-values-for-fields"The full FieldValues of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select :model/FieldValues :field_id [:in field-ids] :type :full :hash_key nil))-(defn full-field-values-rows- "The Field ID and values of the full FieldValues of the Field with `field-id`."- [field-id]+(mu/defn full-field-values-rows+ "The ::warehouse-schema.schema/field ID and values of the full FieldValues of the ::warehouse-schema.schema/field with `field-id`."+ [field-id :- ::lib.schema.id/field](t2/select [:model/FieldValues :field_id :values] :field_id field-id :type :full))-(defn full-field-values-for-tables- "The Field ID, values, and Table ID of the full FieldValues of the normal Fields of the Tables with `table-ids`."- [table-ids]+(mu/defn full-field-values-for-tables+ "The ::warehouse-schema.schema/field ID, values, and ::warehouse-schema.schema/table ID of the full FieldValues of the normal Fields of the Tables with `table-ids`."+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/FieldValues :field_id :values :field.table_id]{:join [[:metabase_field :field] [:= :metabase_fieldvalues.field_id :field.id]]:where [:and@@ -193,10 +209,10 @@[:= :field.visibility_type "normal"][:= :metabase_fieldvalues.type "full"]]}))-(defn full-field-values-with-human-readable-values- "The values and human-readable values of the full FieldValues of the Field with `field-id` if it has+(mu/defn full-field-values-with-human-readable-values+ "The values and human-readable values of the full FieldValues of the ::warehouse-schema.schema/field with `field-id` if it hashuman-readable values, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one [:model/FieldValues :values :human_readable_values]{:where [:and[:= :type "full"]@@ -204,160 +220,170 @@[:not= :human_readable_values nil][:not= :human_readable_values "{}"]]}))-(defn field-values-last-used-at- "The latest `last_used_at` of any FieldValues of the Field with `field-id`."- [field-id]+(mu/defn field-values-last-used-at+ "The latest `last_used_at` of any FieldValues of the ::warehouse-schema.schema/field with `field-id`."+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :max-last-used-at [:model/FieldValues [[:max :last_used_at] :max-last-used-at]]{:where [:= :field_id field-id]}))-(defn full-field-values-by-field+(mu/defn full-field-values-by-field"The `columns` of the full FieldValues of the Fields with `field-ids`."- [columns field-ids]+ [columns :- [:or :keyword [:sequential :keyword]]+ field-ids :- [:set ::lib.schema.id/field]](t2/select columns :field_id [:in field-ids] :type :full))-(defn dimensions-for-fields+(mu/defn dimensions-for-fields"The Dimensions of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select :model/Dimension :field_id [:in field-ids]))-(defn update-field-values!- "Apply `changes` to the FieldValues with `field-values-id`."- [field-values-id changes]+(mu/defn update-field-values!+ "Apply `changes` to the FieldValues with `field-values-id`, returning the number updated."+ [field-values-id :- ms/PositiveInt+ changes :- (mut/select-keys ::warehouse-schema.schema/field-values.update [:has_more_values :values :human_readable_values])](t2/update! :model/FieldValues field-values-id changes))-(defn find-or-insert-full-field-values!- "The full FieldValues of the Field with `field-id`, inserting one with `has-more-values`, `values`, and no+(mu/defn find-or-insert-full-field-values!+ "The full FieldValues of the ::warehouse-schema.schema/field with `field-id`, inserting one with `has-more-values`, `values`, and no`human_readable_values` if none exists yet."- [field-id has-more-values values]+ [field-id :- ::lib.schema.id/field+ has-more-values :- :boolean+ values :- [:maybe ms/FieldValues]](app-db/select-or-insert! :model/FieldValues {:field_id field-id, :type :full}(constantly {:has_more_values has-more-values:values values:human_readable_values nil})))-(defn touch-field-values!- "Stamp `last_used_at` on the FieldValues with `field-values-id`."- [field-values-id]+(mu/defn touch-field-values!+ "Stamp `last_used_at` on the FieldValues with `field-values-id`, returning the number updated."+ [field-values-id :- ms/PositiveInt](t2/update! :model/FieldValues field-values-id {:last_used_at :%now}))-(defn delete-field-values!- "Delete the FieldValues with `field-values-ids`."- [field-values-ids]+(mu/defn delete-field-values!+ "Delete the FieldValues with `field-values-ids`, returning the number deleted."+ [field-values-ids :- [:sequential ms/PositiveInt]](t2/delete! :model/FieldValues :id [:in field-values-ids]))-(defn delete-field-values-for-field!- "Delete every FieldValues of the Field with `field-id`."- [field-id]+(mu/defn delete-field-values-for-field!+ "Delete every FieldValues of the ::warehouse-schema.schema/field with `field-id`, returning the number deleted."+ [field-id :- ::lib.schema.id/field](t2/delete! :model/FieldValues :field_id field-id))-(defn delete-field-values-of-types!- "Delete the FieldValues of `types` of the Field with `field-id`."- [field-id types]+(mu/defn delete-field-values-of-types!+ "Delete the FieldValues of `types` of the ::warehouse-schema.schema/field with `field-id`, returning the number deleted."+ [field-id :- ::lib.schema.id/field+ types :- [:set :keyword]](t2/delete! :model/FieldValues :field_id field-id :type [:in types]))-;;; ------------------------------------------------- Table -------------------------------------------------+;;; ------------------------------------------------- ::warehouse-schema.schema/table --------------------------------------------------(defn table- "The Table with `table-id`, or nil."- [table-id]+(mu/defn table+ "The ::warehouse-schema.schema/table with `table-id`, or nil."+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select-one :model/Table :id table-id))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:or [:set ::lib.schema.id/table] [:sequential ::lib.schema.id/table]]](t2/select :model/Table :id [:in table-ids]))-(defn table-by-name- "The Table named `table-name` in `schema` of the Database with `database-id`, or nil."- [database-id schema table-name]+(mu/defn table-by-name+ "The ::warehouse-schema.schema/table named `table-name` in `schema` of the ::warehouses.schema/database with `database-id`, or nil."+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-name :- :string](t2/select-one :model/Table :name table-name :db_id database-id :schema schema))-(defn table-name-and-schema- "The name and schema of the Table with `table-id`."- [table-id]+(mu/defn table-name-and-schema+ "The name and schema of the ::warehouse-schema.schema/table with `table-id`."+ [table-id :- ::lib.schema.id/table](t2/select-one [:model/Table :name :schema] :id table-id))-(defn table-database-id- "The Database ID of the Table with `table-id`."- [table-id]+(mu/defn table-database-id+ "The ::warehouses.schema/database ID of the ::warehouse-schema.schema/table with `table-id`."+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table table-id))-(defn update-table!- "Apply `changes` to the Table with `table-id`."- [table-id changes]+(mu/defn update-table!+ "Apply `changes` to the ::warehouse-schema.schema/table with `table-id`, returning the number updated."+ [table-id :- ::lib.schema.id/table+ changes :- (mut/select-keys ::warehouse-schema.schema/table.update [:field_order])](t2/update! :model/Table table-id changes))-(defn unarchived-segments-for-tables+(mu/defn unarchived-segments-for-tables"The unarchived Segments of the Tables with `table-ids`, ordered by name."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Segment :table_id [:in table-ids] :archived false {:order-by [[:name :asc]]}))-(defn segment-ids-for-table- "The IDs of the Segments of the Table with `table-id`, excluding archived ones when `skip-archived?`."- [table-id skip-archived?]+(mu/defn segment-ids-for-table+ "The IDs of the Segments of the ::warehouse-schema.schema/table with `table-id`, excluding archived ones when `skip-archived?`."+ [table-id :- ::lib.schema.id/table+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Segment {:where [:and [:= :table_id table-id] (when skip-archived? [:not :archived])]}))-(defn unarchived-measures-for-tables+(mu/defn unarchived-measures-for-tables"The unarchived Measures of the Tables with `table-ids`, ordered by name."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Measure :table_id [:in table-ids] :archived false {:order-by [[:name :asc]]}))-(defn measure-ids-for-table- "The IDs of the Measures of the Table with `table-id`, excluding archived ones when `skip-archived?`."- [table-id skip-archived?]+(mu/defn measure-ids-for-table+ "The IDs of the Measures of the ::warehouse-schema.schema/table with `table-id`, excluding archived ones when `skip-archived?`."+ [table-id :- ::lib.schema.id/table+ skip-archived? :- [:maybe :boolean]](t2/select-pks-set :model/Measure {:where [:and [:= :table_id table-id] (when skip-archived? [:not :archived])]}))-(defn unarchived-metric-cards-for-tables+(mu/defn unarchived-metric-cards-for-tables"The unarchived metric Cards of the Tables with `table-ids`, ordered by name."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Card :table_id [:in table-ids] :archived false :type :metric {:order-by [[:name :asc]]}))-(defn transforms-by-id+(mu/defn transforms-by-id"A map of ID to Transform for `transform-ids`."- [transform-ids]+ [transform-ids :- [:sequential ::lib.schema.id/transform]](t2/select-fn->fn :id identity :model/Transform :id [:in transform-ids]))-;;; ------------------------------------------------ Database ------------------------------------------------+;;; ------------------------------------------------ ::warehouses.schema/database -------------------------------------------------(defn database- "The Database with `database-id`, or nil."- [database-id]+(mu/defn database+ "The ::warehouses.schema/database with `database-id`, or nil."+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one :model/Database :id database-id))-(defn databases-by-id- "A map of ID to Database for `database-ids`."- [database-ids]+(mu/defn databases-by-id+ "A map of ID to ::warehouses.schema/database for `database-ids`."+ [database-ids :- [:set ::lib.schema.id/database]](t2/select-pk->fn identity :model/Database :id [:in database-ids]))-(defn database-engine- "The engine of the Database with `database-id`."- [database-id]+(mu/defn database-engine+ "The engine of the ::warehouses.schema/database with `database-id`."+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :engine :model/Database :id database-id))-(defn database-name- "The name of the Database with `database-id`."- [database-id]+(mu/defn database-name+ "The name of the ::warehouses.schema/database with `database-id`."+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one-fn :name :model/Database :id database-id))-(defn database-id-by-name- "The ID of the Database named `database-name`, or nil."- [database-name]+(mu/defn database-id-by-name+ "The ID of the ::warehouses.schema/database named `database-name`, or nil."+ [database-name :- :string](t2/select-one-pk :model/Database :name database-name));;; ---------------------------------------------- Other models -----------------------------------------------(defn collections+(mu/defn collections"The Collections with `collection-ids`."- [collection-ids]+ [collection-ids :- [:sequential ::lib.schema.id/collection]](t2/select :model/Collection :id [:in collection-ids]))-(defn user-summaries-by-id+(mu/defn user-summaries-by-id"A map of ID to the ID, email, and names of the Users with `user-ids`."- [user-ids]+ [user-ids :- [:set ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(defn cards-with-moderated-status+(mu/defn cards-with-moderated-status"The query-metadata columns of the Cards with `card-ids`, with their latest moderation status."- [card-ids]+ [card-ids :- [:or [:set ::lib.schema.id/card] [:sequential ::lib.schema.id/card]]](t2/select :model/Card{:select [:c.id :c.dataset_query :c.result_metadata :c.name:c.description :c.collection_id :c.database_id :c.typediff --git a/src/metabase/warehouse_schema/schema.clj b/src/metabase/warehouse_schema/schema.cljindex 03dfe9057fd..6c32cc1a45d 100644--- a/src/metabase/warehouse_schema/schema.clj+++ b/src/metabase/warehouse_schema/schema.clj@@ -1,13 +1,268 @@(ns metabase.warehouse-schema.schema(:require[metabase.lib.schema.id :as lib.schema.id]- [metabase.util.malli.registry :as mr]))+ [metabase.lib.schema.metadata.fingerprint :as lib.schema.metadata.fingerprint]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::dimension+ "A Dimension as selected from the app DB: every column of `:dimension`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:field_id ::lib.schema.id/field]+ [:name :string]+ [:type [:or :keyword :string]]+ [:human_readable_field_id [:maybe ::lib.schema.id/field]]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:entity_id :string]])++(mr/def ::dimension.update+ "What an update (or insert) of a Dimension accepts: every column of `:dimension` except `id`, all optional."+ [:map {:closed true}+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:name {:optional true} [:maybe :string]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:human_readable_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:entity_id {:optional true} [:maybe :string]]])++(mr/def ::field.settings+ "The `:settings` column of a Field, decoded."+ :map)++(mr/def ::field+ "A Field as selected from the app DB: every column of `:metabase_field`."+ [:map {:closed true}+ [:id ::lib.schema.id/field]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:name :string]+ [:base_type [:or :keyword :string]]+ [:semantic_type [:maybe [:or :keyword :string]]]+ [:active :boolean]+ [:description [:maybe :string]]+ [:preview_display :boolean]+ [:position :int]+ [:table_id ::lib.schema.id/table]+ [:parent_id [:maybe ms/PositiveInt]]+ [:display_name [:maybe :string]]+ [:visibility_type [:or :keyword :string]]+ [:fk_target_field_id [:maybe ::lib.schema.id/field]]+ [:last_analyzed [:maybe ms/TemporalInstant]]+ [:points_of_interest [:maybe :string]]+ [:caveats [:maybe :string]]+ [:fingerprint [:maybe ::lib.schema.metadata.fingerprint/fingerprint]]+ [:fingerprint_version :int]+ [:database_type [:or :keyword :string]]+ [:has_field_values [:maybe [:or :keyword :string]]]+ [:settings [:maybe ::field.settings]]+ [:database_position :int]+ [:custom_position :int]+ [:effective_type [:maybe [:or :keyword :string]]]+ [:coercion_strategy [:maybe [:or :keyword :string]]]+ [:nfc_path [:maybe [:sequential [:or :string :keyword]]]]+ [:database_required :boolean]+ [:json_unfolding :boolean]+ [:database_is_auto_increment :boolean]+ [:database_indexed [:maybe :boolean]]+ [:database_partitioned [:maybe :boolean]]+ [:is_defective_duplicate {:optional true} :boolean]+ [:unique_field_helper {:optional true} [:maybe :int]]+ [:database_is_pk [:maybe :boolean]]+ [:database_is_nullable [:maybe :boolean]]+ [:database_is_generated [:maybe :boolean]]+ [:database_default [:maybe :string]]+ [:dimension_interestingness [:maybe number?]]+ [:data_sensitivity [:maybe [:or :keyword :string]]]])++(mr/def ::field.update+ "What an update (or insert) of a Field accepts: every column of `:metabase_field` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:base_type {:optional true} [:maybe [:or :keyword :string]]]+ [:semantic_type {:optional true} [:maybe [:or :keyword :string]]]+ [:active {:optional true} [:maybe :boolean]]+ [:description {:optional true} [:maybe :string]]+ [:preview_display {:optional true} [:maybe :boolean]]+ [:position {:optional true} [:maybe :int]]+ [:table_id {:optional true} [:maybe ::lib.schema.id/table]]+ [:parent_id {:optional true} [:maybe ms/PositiveInt]]+ [:display_name {:optional true} [:maybe :string]]+ [:visibility_type {:optional true} [:maybe [:or :keyword :string]]]+ [:fk_target_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:last_analyzed {:optional true} [:maybe ms/TemporalInstant]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:caveats {:optional true} [:maybe :string]]+ [:fingerprint {:optional true} [:maybe ::lib.schema.metadata.fingerprint/fingerprint]]+ [:fingerprint_version {:optional true} [:maybe :int]]+ [:database_type {:optional true} [:maybe [:or :keyword :string]]]+ [:has_field_values {:optional true} [:maybe [:or :keyword :string]]]+ [:settings {:optional true} [:maybe ::field.settings]]+ [:database_position {:optional true} [:maybe :int]]+ [:custom_position {:optional true} [:maybe :int]]+ [:effective_type {:optional true} [:maybe [:or :keyword :string]]]+ [:coercion_strategy {:optional true} [:maybe [:or :keyword :string]]]+ [:nfc_path {:optional true} [:maybe [:sequential [:or :string :keyword]]]]+ [:database_required {:optional true} [:maybe :boolean]]+ [:json_unfolding {:optional true} [:maybe :boolean]]+ [:database_is_auto_increment {:optional true} [:maybe :boolean]]+ [:database_indexed {:optional true} [:maybe :boolean]]+ [:database_partitioned {:optional true} [:maybe :boolean]]+ [:is_defective_duplicate {:optional true} [:maybe :boolean]]+ [:database_is_pk {:optional true} [:maybe :boolean]]+ [:database_is_nullable {:optional true} [:maybe :boolean]]+ [:database_is_generated {:optional true} [:maybe :boolean]]+ [:database_default {:optional true} [:maybe :string]]+ [:dimension_interestingness {:optional true} [:maybe number?]]+ [:data_sensitivity {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::field-user-settings.settings+ "The `:settings` column of a FieldUserSettings, decoded."+ :map)++(mr/def ::field-user-settings+ "A FieldUserSettings as selected from the app DB: every column of `:metabase_field_user_settings`."+ [:map {:closed true}+ [:field_id ::lib.schema.id/field]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:semantic_type [:maybe [:or :keyword :string]]]+ [:description [:maybe :string]]+ [:display_name [:maybe :string]]+ [:visibility_type [:maybe [:or :keyword :string]]]+ [:fk_target_field_id [:maybe ::lib.schema.id/field]]+ [:has_field_values [:maybe [:or :keyword :string]]]+ [:effective_type [:maybe [:or :keyword :string]]]+ [:coercion_strategy [:maybe [:or :keyword :string]]]+ [:caveats [:maybe :string]]+ [:points_of_interest [:maybe :string]]+ [:nfc_path [:maybe [:sequential [:or :string :keyword]]]]+ [:json_unfolding [:maybe :boolean]]+ [:settings [:maybe ::field-user-settings.settings]]+ [:data_sensitivity [:maybe [:or :keyword :string]]]])++(mr/def ::field-user-settings.update+ "What an update (or insert) of a FieldUserSettings accepts: every column of `:metabase_field_user_settings` except `id`, all optional."+ [:map {:closed true}+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:semantic_type {:optional true} [:maybe [:or :keyword :string]]]+ [:description {:optional true} [:maybe :string]]+ [:display_name {:optional true} [:maybe :string]]+ [:visibility_type {:optional true} [:maybe [:or :keyword :string]]]+ [:fk_target_field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:has_field_values {:optional true} [:maybe [:or :keyword :string]]]+ [:effective_type {:optional true} [:maybe [:or :keyword :string]]]+ [:coercion_strategy {:optional true} [:maybe [:or :keyword :string]]]+ [:caveats {:optional true} [:maybe :string]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:nfc_path {:optional true} [:maybe [:sequential [:or :string :keyword]]]]+ [:json_unfolding {:optional true} [:maybe :boolean]]+ [:settings {:optional true} [:maybe ::field-user-settings.settings]]+ [:data_sensitivity {:optional true} [:maybe [:or :keyword :string]]]])++(mr/def ::field-values+ "A FieldValues as selected from the app DB: every column of `:metabase_fieldvalues`."+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:values [:maybe ms/FieldValues]]+ [:human_readable_values [:maybe ms/FieldValues]]+ [:field_id ::lib.schema.id/field]+ [:has_more_values [:maybe :boolean]]+ [:type [:or :keyword :string]]+ [:hash_key [:maybe :string]]+ [:last_used_at ms/TemporalInstant]])++(mr/def ::field-values.update+ "What an update (or insert) of a FieldValues accepts: every column of `:metabase_fieldvalues` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:values {:optional true} [:maybe ms/FieldValues]]+ [:human_readable_values {:optional true} [:maybe ms/FieldValues]]+ [:field_id {:optional true} [:maybe ::lib.schema.id/field]]+ [:has_more_values {:optional true} [:maybe :boolean]]+ [:type {:optional true} [:maybe [:or :keyword :string]]]+ [:hash_key {:optional true} [:maybe :string]]+ [:last_used_at {:optional true} [:maybe ms/TemporalInstant]]])(mr/def ::table- "Schema for an instance of a `:model/Table`."- [:map- [:id ::lib.schema.id/table]- [:name :string]- [:description {:optional true} [:maybe :string]]- [:schema {:optional true} [:maybe :string]]- [:db_id ::lib.schema.id/database]])+ "A Table as selected from the app DB: every column of `:metabase_table`."+ [:map {:closed true}+ [:id ::lib.schema.id/table]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:name :string]+ [:description [:maybe :string]]+ [:entity_type [:maybe [:or :keyword :string]]]+ [:active :boolean]+ [:db_id ::lib.schema.id/database]+ [:display_name [:maybe :string]]+ [:visibility_type [:maybe [:or :keyword :string]]]+ [:schema [:maybe :string]]+ [:points_of_interest [:maybe :string]]+ [:caveats [:maybe :string]]+ [:show_in_getting_started :boolean]+ [:field_order [:or :keyword :string]]+ [:initial_sync_status [:or :keyword :string]]+ [:is_upload :boolean]+ [:database_require_filter [:maybe :boolean]]+ [:estimated_row_count [:maybe :int]]+ [:view_count :int]+ [:is_defective_duplicate {:optional true} :boolean]+ [:unique_table_helper {:optional true} [:maybe :string]]+ [:deactivated_at [:maybe ms/TemporalInstant]]+ [:archived_at [:maybe ms/TemporalInstant]]+ [:is_writable [:maybe :boolean]]+ [:data_authority [:or :keyword :string]]+ [:data_source [:maybe [:or :keyword :string]]]+ [:data_layer [:maybe [:or :keyword :string]]]+ [:owner_email [:maybe :string]]+ [:owner_user_id [:maybe ::lib.schema.id/user]]+ [:collection_id [:maybe ::lib.schema.id/collection]]+ [:is_published :boolean]+ [:transform_id [:maybe ::lib.schema.id/transform]]+ [:transform_target :boolean]])++(mr/def ::table.update+ "What an update (or insert) of a Table accepts: every column of `:metabase_table` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:entity_type {:optional true} [:maybe [:or :keyword :string]]]+ [:active {:optional true} [:maybe :boolean]]+ [:db_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:display_name {:optional true} [:maybe :string]]+ [:visibility_type {:optional true} [:maybe [:or :keyword :string]]]+ [:schema {:optional true} [:maybe :string]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:caveats {:optional true} [:maybe :string]]+ [:show_in_getting_started {:optional true} [:maybe :boolean]]+ [:field_order {:optional true} [:maybe [:or :keyword :string]]]+ [:initial_sync_status {:optional true} [:maybe [:or :keyword :string]]]+ [:is_upload {:optional true} [:maybe :boolean]]+ [:database_require_filter {:optional true} [:maybe :boolean]]+ [:estimated_row_count {:optional true} [:maybe :int]]+ [:view_count {:optional true} [:maybe :int]]+ [:is_defective_duplicate {:optional true} [:maybe :boolean]]+ [:deactivated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:archived_at {:optional true} [:maybe ms/TemporalInstant]]+ [:is_writable {:optional true} [:maybe :boolean]]+ [:data_authority {:optional true} [:maybe [:or :keyword :string]]]+ [:data_source {:optional true} [:maybe [:or :keyword :string]]]+ [:data_layer {:optional true} [:maybe [:or :keyword :string]]]+ [:owner_email {:optional true} [:maybe :string]]+ [:owner_user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:collection_id {:optional true} [:maybe ::lib.schema.id/collection]]+ [:is_published {:optional true} [:maybe :boolean]]+ [:transform_id {:optional true} [:maybe ::lib.schema.id/transform]]+ [:transform_target {:optional true} [:maybe :boolean]]])diff --git a/src/metabase/warehouse_schema_rest/db.clj b/src/metabase/warehouse_schema_rest/db.cljindex 5fea0e2944c..560f7979204 100644--- a/src/metabase/warehouse_schema_rest/db.clj+++ b/src/metabase/warehouse_schema_rest/db.clj@@ -3,13 +3,19 @@additional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require[clojure.string :as str]+ [malli.util :as mut][metabase.app-db.core :as app-db]+ [metabase.lib.schema.id :as lib.schema.id][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouse-schema.schema :as warehouse-schema.schema][toucan2.core :as t2]))-(defn field-and-target-database-ids+(mu/defn field-and-target-database-ids"The `:source_db_id` and `:target_db_id` of the Fields with `source-field-id` and `target-field-id`."- [source-field-id target-field-id]+ [source-field-id :- ::lib.schema.id/field+ target-field-id :- ::lib.schema.id/field](t2/query {:select [[:source_t.db_id :source_db_id][:target_t.db_id :target_db_id]]:from [[(t2/table-name :model/Field) :sf]]@@ -18,60 +24,76 @@[(t2/table-name :model/Table) :target_t] [:= :tf.table_id :target_t.id]]:where [:= :sf.id source-field-id]}))-(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))-(defn update-field!+(mu/defn update-field!"Apply `changes` to the Field with `field-id`."- [field-id changes]+ [field-id :- ::lib.schema.id/field+ changes :- ::warehouse-schema.schema/field.update](t2/update! :model/Field field-id changes))-(defn set-nested-fields-active!+(mu/defn set-nested-fields-active!"Set the active flag of the Fields of the Table with `table-id` whose NFC path matches the SQL LIKE`nfc-path-pattern`, returning the number updated."- [table-id nfc-path-pattern active?]+ [table-id :- ::lib.schema.id/table+ nfc-path-pattern :- :string+ active? :- :boolean](t2/update! :model/Field :table_id table-id :nfc_path [:like nfc-path-pattern] {:active active?}))-(defn dimension-for-field+(mu/defn dimension-for-field"The Dimension of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Dimension :field_id field-id))-(defn insert-dimension!+(mu/defn insert-dimension!"Insert the Dimension `row`."- [row]+ [row :- (mut/select-keys ::warehouse-schema.schema/dimension.update [:field_id :type :name :human_readable_field_id])](t2/insert! :model/Dimension row))-(defn update-dimension!+(mu/defn update-dimension!"Apply `changes` to the Dimension with `id`."- [id changes]+ [id :- ::lib.schema.id/dimension+ changes :- (mut/select-keys ::warehouse-schema.schema/dimension.update [:type :name :human_readable_field_id])](t2/update! :model/Dimension id changes))-(defn rename-dimension-for-field!+(mu/defn rename-dimension-for-field!"Set the name of the Dimension of the Field with `field-id`."- [field-id dimension-name]+ [field-id :- ::lib.schema.id/field+ dimension-name :- :string](t2/update! :model/Dimension :field_id field-id {:name dimension-name}))-(defn delete-dimension!+(mu/defn delete-dimension!"Delete the Dimension with `id`."- [id]+ [id :- ::lib.schema.id/dimension](t2/delete! :model/Dimension :id id))-(defn delete-dimensions-for-field!+(mu/defn delete-dimensions-for-field!"Delete the Dimensions of the Field with `field-id`."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/delete! :model/Dimension :field_id field-id))-(defn matching-tables+(mu/defn matching-tables"The Tables (active, or with `transform_target` when `include-transform-targets?`) matching `term` (a glob patternusing `*` as a wildcard, matched against `:name` and `:display_name`), optionally narrowed to `visibility-type`,`data-layer`, `data-source`, `owner-user-id`, and/or `owner-email`; restricted to ownerless Tables when`orphan-only?`, published Tables when `published-only?`, and (when `check-unused?`) Tables with no dependents,ordered by name."[{:keys [term visibility-type data-layer data-source owner-user-id owner-email orphan-only? published-only?- check-unused? include-transform-targets?]}]+ check-unused? include-transform-targets?]}+ :- [:map {:closed true}+ [:term {:optional true} [:maybe :string]]+ [:visibility-type {:optional true} [:maybe :string]]+ [:data-layer {:optional true} [:maybe [:or :keyword :string]]]+ [:data-source {:optional true} [:maybe [:or :keyword :string]]]+ [:owner-user-id {:optional true} [:maybe :int]]+ [:owner-email {:optional true} [:maybe :string]]+ [:orphan-only? {:optional true} [:maybe :boolean]]+ [:published-only? {:optional true} [:maybe :boolean]]+ [:check-unused? {:optional true} [:maybe :boolean]]+ [:include-transform-targets? {:optional true} [:maybe :boolean]]]](let [db-type (app-db/db-type)glob (fn [escaped](-> escaped@@ -108,60 +130,63 @@[:= :d.to_entity_type "table"]]}]))](t2/select :model/Table {:where where, :order-by [[:name :asc]]})))-(defn tables-by-ids+(mu/defn tables-by-ids"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn table+(mu/defn table"The Table with `table-id`, or nil."- [table-id]+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select-one :model/Table :id table-id))-(defn update-table!+(mu/defn update-table!"Apply `changes` to the Table with `table-id`."- [table-id changes]+ [table-id :- ::lib.schema.id/table+ changes :- ::warehouse-schema.schema/table.update](t2/update! :model/Table table-id changes))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database database-id))-(defn non-destination-database+(mu/defn non-destination-database"The Database with `database-id` if it is not a routing destination, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id :router_database_id nil))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- [:maybe ::lib.schema.id/collection]](t2/select-one :model/Collection :id collection-id))-(defn active-unretired-field-ids-for-table+(mu/defn active-unretired-field-ids-for-table"The ids of the active, unretired Fields of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field, :table_id table-id, :visibility_type [:not= "retired"], :active true))-(defn field-ids-for-table+(mu/defn field-ids-for-table"The ids of the Fields of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-pks-set :model/Field :table_id table-id))-(defn active-fields-targeting+(mu/defn active-fields-targeting"The active Fields whose FK target is one of `field-ids`."- [field-ids]+ [field-ids :- [:sequential ::lib.schema.id/field]](t2/select :model/Field, :fk_target_field_id [:in field-ids], :active true))-(defn delete-field-values-for-fields!+(mu/defn delete-field-values-for-fields!"Delete the FieldValues of the Fields with `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/delete! (t2/table-name :model/FieldValues) :field_id [:in field-ids]))-(defn update-or-insert-full-field-values!+(mu/defn update-or-insert-full-field-values!"Update the full FieldValues of the Field with `field-id` to have `values` and `human-readable-values`, insertingone if none exists yet. Returns the number of rows affected."- [field-id values human-readable-values]+ [field-id :- ::lib.schema.id/field+ values :- [:maybe ms/FieldValues]+ human-readable-values :- [:maybe ms/FieldValues]](app-db/update-or-insert! :model/FieldValues {:field_id field-id, :type :full}(constantly {:values values:human_readable_values human-readable-values})))diff --git a/src/metabase/warehouses/db.clj b/src/metabase/warehouses/db.cljindex 4c1a38881c3..f016f959bf0 100644--- a/src/metabase/warehouses/db.clj+++ b/src/metabase/warehouses/db.clj@@ -3,44 +3,49 @@additional logic, so no other namespace in the module runs a query itself (model definitions still use `toucan2.core`)."(:require[metabase.app-db.core :as mdb]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli :as mu]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn router-database-id+(mu/defn router-database-id"The router Database id of the Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one-fn :router_database_id :model/Database :id database-id))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- [:maybe ::lib.schema.id/database]](t2/select-one :model/Database :id database-id))-(defn non-destination-database+(mu/defn non-destination-database"The Database with `database-id` if it is not a routing destination, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id :router_database_id nil))-(defn databases+(mu/defn databases"The Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select :model/Database :id [:in database-ids]))-(defn database-by-name+(mu/defn database-by-name"The Database named `database-name`, or nil."- [database-name]+ [database-name :- :string](t2/select-one :model/Database :name database-name))-(defn set-database-details!- "Set the connection details of the Database with `database-id`."- [database-id details]+(mu/defn set-database-details!+ "Set the connection details of the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ details :- ::warehouses.schema/database.details](t2/update! :model/Database database-id {:details details}))-(defn set-database-provider-name!- "Set the provider name of the Database with `database-id`."- [database-id provider-name]+(mu/defn set-database-provider-name!+ "Set the provider name of the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ provider-name :- :string](t2/update! :model/Database database-id {:provider_name provider-name}))-(defn health-check-candidate-ids+(mu/defn health-check-candidate-ids"The `:id` of the lowest-id non-audit, non-sample, non-destination Database of each engine."[](t2/query {:select [[:%min.id :id]]@@ -55,14 +60,14 @@[database-id]^:allow-subquery {:from [(t2/table-name :model/Table)], :select [:id], :where [:= :db_id database-id]})-(defn fields-exist-for-database?+(mu/defn fields-exist-for-database?"Whether any Field belongs to a Table of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Field :table_id [:in (table-ids-of-database-query database-id)]))-(defn delete-childless-fields-for-database!+(mu/defn delete-childless-fields-for-database!"Delete the childless Fields of the Tables of the Database with `database-id`, returning the number deleted."- [database-id]+ [database-id :- ::lib.schema.id/database](let [table-ids-query (table-ids-of-database-query database-id)extra-clause (if (= (mdb/db-type) :mysql);; double-wrapped subquery to work around the MySQL restriction on selecting from the@@ -82,60 +87,60 @@[:in :table_id table-ids-query]extra-clause]})))-(defn delete-cards-for-database-returning-ids-reducible+(mu/defn delete-cards-for-database-returning-ids-reducible"A reducible that deletes the Cards of the Database with `database-id` and yields their `:id`s (Postgres only)."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-query {:delete-from (t2/table-name :model/Card):where [:= :database_id database-id]:returning [:id]}))-(defn card-ids-for-database-reducible+(mu/defn card-ids-for-database-reducible"A reducible of the `:id`s of the Cards of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/reducible-query {:from [(t2/table-name :model/Card)]:select [:id]:where [:= :database_id database-id]}))-(defn delete-cards-for-database!+(mu/defn delete-cards-for-database!"Delete the Cards of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/query {:delete-from (t2/table-name :model/Card):where [:= :database_id database-id]}))-(defn disable-uploads-for-all-databases!- "Disable uploads on every Database that has them enabled."+(mu/defn disable-uploads-for-all-databases!+ "Disable uploads on every Database that has them enabled, returning the number updated."[](t2/update! :model/Database :uploads_enabled true {:uploads_enabled false :uploads_table_prefix nil :uploads_schema_name nil}))-(defn active-tables-for-database+(mu/defn active-tables-for-database"The active Tables of the Database with `database-id`, in case-insensitive display name order."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select :model/Table :db_id database-id :active true {:order-by [[:%lower.display_name :asc]]}))-(defn active-tables-for-databases+(mu/defn active-tables-for-databases"The active Tables of the Databases with `database-ids`, in database then display name order."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select :model/Table:db_id [:in database-ids]:active true{:order-by [[:db_id :asc] [:%lower.display_name :asc]]}))-(defn active-table-ids-for-database+(mu/defn active-table-ids-for-database"The ids of the active Tables of the Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-pks-set :model/Table, :db_id database-id, :active true))-(defn pk-fields-for-tables+(mu/defn pk-fields-for-tables"The primary-key Fields of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Field, :table_id [:in table-ids], :semantic_type (mdb/isa :type/PK)))-(defn databases-reducible+(mu/defn databases-reducible"A reducible of the Databases matching the Honey SQL `where` clause."- [where]+ [where :- [:maybe vector?]](t2/reducible-select :model/Database {:where where}))-(defn table-database-id+(mu/defn table-database-id"The Database id of the Table with `table-id`, or nil."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-one-fn :db_id :model/Table, :id table-id))diff --git a/src/metabase/warehouses/schema.clj b/src/metabase/warehouses/schema.cljnew file mode 100644index 00000000000..03985a41e4e--- /dev/null+++ b/src/metabase/warehouses/schema.clj@@ -0,0 +1,98 @@+(ns metabase.warehouses.schema+ "Malli schemas for the warehouses module."+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::database.details+ "The `:details` column of a Database, decoded."+ :map)++(mr/def ::database.settings+ "The `:settings` column of a Database, decoded."+ :map)++(mr/def ::database.dbms-version+ "The `:dbms_version` column of a Database, decoded."+ :map)++(mr/def ::database.write-data-details+ "The `:write_data_details` column of a Database, decoded."+ :map)++(mr/def ::database.admin-details+ "The `:admin_details` column of a Database, decoded."+ :map)++(mr/def ::database+ "A Database as selected from the app DB: every column of `:metabase_database`, plus `:features` added by the model's after-select hook."+ [:map {:closed true}+ [:id ::lib.schema.id/database]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]+ [:name :string]+ [:description [:maybe :string]]+ [:details ::database.details]+ [:engine [:or :keyword :string]]+ [:is_sample :boolean]+ [:is_full_sync :boolean]+ [:points_of_interest [:maybe :string]]+ [:caveats [:maybe :string]]+ [:metadata_sync_schedule :string]+ [:cache_field_values_schedule [:maybe :string]]+ [:timezone [:maybe :string]]+ [:is_on_demand :boolean]+ [:auto_run_queries :boolean]+ [:refingerprint [:maybe :boolean]]+ [:cache_ttl [:maybe :int]]+ [:initial_sync_status [:or :keyword :string]]+ [:creator_id [:maybe ::lib.schema.id/user]]+ [:settings [:maybe ::database.settings]]+ [:dbms_version [:maybe ::database.dbms-version]]+ [:is_audit :boolean]+ [:uploads_enabled :boolean]+ [:uploads_schema_name [:maybe :string]]+ [:uploads_table_prefix [:maybe :string]]+ [:is_attached_dwh :boolean]+ [:router_database_id [:maybe ::lib.schema.id/database]]+ [:provider_name [:maybe :string]]+ [:write_data_details [:maybe ::database.write-data-details]]+ [:admin_details [:maybe ::database.admin-details]]+ [:is_stub :boolean]+ [:features {:optional true} [:maybe [:set :keyword]]]])++(mr/def ::database.update+ "What an update (or insert) of a Database accepts: every column of `:metabase_database` except `id`, all optional."+ [:map {:closed true}+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:updated_at {:optional true} [:maybe ms/TemporalInstant]]+ [:name {:optional true} [:maybe :string]]+ [:description {:optional true} [:maybe :string]]+ [:details {:optional true} [:maybe ::database.details]]+ [:engine {:optional true} [:maybe [:or :keyword :string]]]+ [:is_sample {:optional true} [:maybe :boolean]]+ [:is_full_sync {:optional true} [:maybe :boolean]]+ [:points_of_interest {:optional true} [:maybe :string]]+ [:caveats {:optional true} [:maybe :string]]+ [:metadata_sync_schedule {:optional true} [:maybe :string]]+ [:cache_field_values_schedule {:optional true} [:maybe :string]]+ [:timezone {:optional true} [:maybe :string]]+ [:is_on_demand {:optional true} [:maybe :boolean]]+ [:auto_run_queries {:optional true} [:maybe :boolean]]+ [:refingerprint {:optional true} [:maybe :boolean]]+ [:cache_ttl {:optional true} [:maybe :int]]+ [:initial_sync_status {:optional true} [:maybe [:or :keyword :string]]]+ [:creator_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:settings {:optional true} [:maybe ::database.settings]]+ [:dbms_version {:optional true} [:maybe ::database.dbms-version]]+ [:is_audit {:optional true} [:maybe :boolean]]+ [:uploads_enabled {:optional true} [:maybe :boolean]]+ [:uploads_schema_name {:optional true} [:maybe :string]]+ [:uploads_table_prefix {:optional true} [:maybe :string]]+ [:is_attached_dwh {:optional true} [:maybe :boolean]]+ [:router_database_id {:optional true} [:maybe ::lib.schema.id/database]]+ [:provider_name {:optional true} [:maybe :string]]+ [:write_data_details {:optional true} [:maybe ::database.write-data-details]]+ [:admin_details {:optional true} [:maybe ::database.admin-details]]+ [:is_stub {:optional true} [:maybe :boolean]]])diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 55af4910680..4e41f1bdf58 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -202,7 +202,7 @@(mu/defn- source-query-cards"Fetch the Cards that can be used as source queries (e.g. presented as virtual tables)."- [card-type :- ::queries.schema/card-type+ [card-type :- ::queries.schema/card.type& {:keys [collection-scope xform], :or {xform identity}}](when-let [ids-of-dbs-that-support-source-queries (not-empty (ids-of-dbs-that-support-source-queries))](transduce@@ -216,20 +216,20 @@(mu/defn- source-query-cards-exist?"Truthy if a single Card that can be used as a source query exists."- [card-type :- ::queries.schema/card-type]+ [card-type :- ::queries.schema/card.type](seq (source-query-cards card-type :xform (take 1))))(mu/defn- cards-virtual-tables"Return a sequence of 'virtual' Table metadata for eligible Cards.(This takes the Cards from `source-query-cards` and returns them in a format suitable for consumption by the QueryBuilder.)"- [card-type :- ::queries.schema/card-type+ [card-type :- ::queries.schema/card.type& {:keys [include-fields?]}](schema.table/cards->virtual-tables (source-query-cards card-type):include-fields? include-fields?))(mu/defn- saved-cards-virtual-db-metadata- [card-type :- ::queries.schema/card-type+ [card-type :- ::queries.schema/card.type& {:keys [include-tables? include-fields?]}](when (lib-be/enable-nested-queries)(cond-> {:name (trs "Saved Questions")@@ -304,10 +304,8 @@filter-by-data-access? (not (or include-editable-data-model?exclude-uneditable-details?filter-on-router-database-id))- user-info {:user-id api/*current-user-id*- :is-superuser? (mi/superuser?)- :is-data-analyst? api/*is-data-analyst?*}- dbs (warehouses-rest.db/databases-where user-info filter-by-data-access? filter-on-router-database-id+ dbs (warehouses-rest.db/databases-where api/*current-user-id* (mi/superuser?) api/*is-data-analyst?*+ filter-by-data-access? filter-on-router-database-idinclude-analytics?);; everything below walks the list one database at a time_ (perms/prime-database-perms-cache {:db-ids (into #{} (map :id) dbs)})]diff --git a/src/metabase/warehouses_rest/db.clj b/src/metabase/warehouses_rest/db.cljindex b741540f44f..0842c03d068 100644--- a/src/metabase/warehouses_rest/db.clj+++ b/src/metabase/warehouses_rest/db.clj@@ -5,14 +5,18 @@[clojure.string :as str][metabase.app-db.core :as mdb][metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [metabase.warehouses.schema :as warehouses.schema][toucan2.core :as t2]))-(defn active-visible-tables-for-databases+(mu/defn active-visible-tables-for-databases"The active, visible Tables of the Databases with `database-ids`, in schema then display name order."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/select :model/Table:active true:db_id [:in database-ids]@@ -20,9 +24,9 @@{:order-by [[:%lower.schema :asc][:%lower.display_name :asc]]}))-(defn active-visible-schemas-for-databases+(mu/defn active-visible-schemas-for-databases"The distinct Database id and schema of the active, visible Tables of the Databases with `database-ids`."- [database-ids]+ [database-ids :- [:sequential ::lib.schema.id/database]](t2/query {:select-distinct [:db_id :schema]:from [(t2/table-name :model/Table)]:where [:and@@ -30,17 +34,19 @@[:= :active true][:= :visibility_type nil]]}))-(defn database-engines+(mu/defn database-engines"The id and engine of every Database."[](t2/select [:model/Database :id :engine]))-(defn source-query-cards-reducible+(mu/defn source-query-cards-reducible"A reducible of the Cards of `card-type` (also including \"metric\" Cards) in the Databases with `database-ids`visible to the current user that can be used as source queries, with their moderation status, in case-insensitivename order. `collection-scope` further restricts by collection: `nil` applies no collection restriction, `:root`restricts to Cards with no collection, and a collection of ids restricts to Cards in those collections."- [card-type database-ids collection-scope]+ [card-type :- [:or :keyword :string]+ database-ids :- [:set ::lib.schema.id/database]+ collection-scope :- [:maybe [:or [:= :root] [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]]](t2/reducible-query {:select [:name :description :database_id :dataset_query :id :collection_id:result_metadata :type :source_card_id :card_schema[^:allow-subquery {:select [:status]@@ -65,14 +71,20 @@(collection/visible-collection-filter-clause)]:order-by [[:%lower.name :asc]]}))-(defn databases-where- "The Databases visible to the user described by `user-info` (a map of `:user-id`/`:is-superuser?`/- `:is-data-analyst?`), in name then engine order. Excludes stub Databases and, unless `include-analytics?`, the- audit Database. Restricted to Databases routed from `router-database-id` when given, otherwise to non-routed- Databases. When `filter-by-data-access?` is true, further restricted to Databases the user can query, manage, or- edit the metadata of."- [user-info filter-by-data-access? router-database-id include-analytics?]- (let [base-where [:and+(mu/defn databases-where+ "The Databases visible to the user with `user-id` (`is-superuser?`/`is-data-analyst?` further widen visibility), in+ name then engine order. Excludes stub Databases and, unless `include-analytics?`, the audit Database. Restricted+ to Databases routed from `router-database-id` when given, otherwise to non-routed Databases. When+ `filter-by-data-access?` is true, further restricted to Databases the user can query, manage, or edit the+ metadata of."+ [user-id :- ::lib.schema.id/user+ is-superuser? :- :boolean+ is-data-analyst? :- :boolean+ filter-by-data-access? :- :boolean+ router-database-id :- [:maybe ::lib.schema.id/database]+ include-analytics? :- :boolean]+ (let [user-info {:user-id user-id :is-superuser? is-superuser? :is-data-analyst? is-data-analyst?}+ base-where [:and[:= :is_stub false](when-not include-analytics?[:= :is_audit false])@@ -89,25 +101,28 @@(t2/select :model/Database {:order-by [:%lower.name :%lower.engine]:where where})))-(defn database-exists?+(mu/defn database-exists?"Whether a Database with `database-id` exists."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id))-(defn non-destination-database-exists?+(mu/defn non-destination-database-exists?"Whether a Database with `database-id` that is not a routing destination exists."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :id database-id :router_database_id nil))-(defn destination-database-exists-for-router?+(mu/defn destination-database-exists-for-router?"Whether the Database with `database-id` has routing destinations."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/exists? :model/Database :router_database_id database-id))-(defn autocomplete-tables+(mu/defn autocomplete-tables"Up to `limit` id, Database id, schema, and name rows of the active, visible Tables of the Database with- `database-id` whose lower-cased name matches the SQL LIKE `like-pattern`, in name order."- [database-id like-pattern limit]+ `database-id` whose lower-cased name matches the SQL LIKE `like-pattern` (a Honey SQL LIKE right-hand side, see+ `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in name order."+ [database-id :- ::lib.schema.id/database+ like-pattern :- [:or :string vector?]+ limit :- ms/PositiveInt](t2/select [:model/Table :id :db_id :schema :name]{:where [:and [:= :db_id database-id][:= :active true]@@ -149,11 +164,13 @@(and (empty? search-id) (not-empty search-name))[:like [:lower :report_card.name] (h2x/like-substring search-name)])))-(defn autocomplete-cards+(mu/defn autocomplete-cards"Up to 50 unarchived Cards of the Database with `database-id` matching `search-card-slug` (see[[autocomplete-cards-search-clause]]), with their Collection name, models first then newest first. Dashboardquestions are excluded unless `include-dashboard-questions?`."- [database-id search-card-slug include-dashboard-questions?]+ [database-id :- ::lib.schema.id/database+ search-card-slug :- :string+ include-dashboard-questions? :- [:maybe :boolean]](t2/select [:model/Card :id :type :database_id :name :collection_id[:collection.name :collection_name] :card_schema]{:where [:and@@ -170,10 +187,13 @@[:report_card.id :desc]] ; sort by most recently created after sorting by type:limit 50}))-(defn autocomplete-fields+(mu/defn autocomplete-fields"Up to `limit` name, type, id, and Table of the active, non-sensitive Fields of active Tables of the Database with- `database-id` whose lower-cased name matches the SQL LIKE `like-pattern`, in field then table name order."- [database-id like-pattern limit]+ `database-id` whose lower-cased name matches the SQL LIKE `like-pattern` (a Honey SQL LIKE right-hand side, see+ `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in field then table name order."+ [database-id :- ::lib.schema.id/database+ like-pattern :- [:or :string vector?]+ limit :- ms/PositiveInt];; NOTE: measuring showed that this query performance is improved ~4x when adding trgm index in pgsql and ~10x when;; adding a index on `lower(metabase_field.name)` for ordering (trgm index having on impact on queries with index).;; Pgsql now has an index on that (see migration `v49.2023-01-24T12:00:00`) as other dbms do not support indexes on@@ -190,56 +210,57 @@[:= :table.id :metabase_field.table_id]]]:limit limit}))-(defn table-ids-for-database+(mu/defn table-ids-for-database"The ids of the Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-fn-set :id :model/Table, :db_id database-id))-(defn non-sensitive-fields-for-tables+(mu/defn non-sensitive-fields-for-tables"The id, name, display name, Table id, and types of the non-sensitive Fields of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select [:model/Field :id :name :display_name :table_id :base_type :semantic_type]:table_id [:in table-ids]:visibility_type [:not-in ["sensitive" "retired"]]))-(defn insert-database!+(mu/defn insert-database!"Insert the Database `row` and return the inserted instance."- [row]+ [row :- ::warehouses.schema/database.update](t2/insert-returning-instance! :model/Database row))-(defn sample-database+(mu/defn sample-database"The sample Database, or nil."[](t2/select-one :model/Database :is_sample true))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn update-database!- "Apply `changes` to the Database with `database-id`."- [database-id changes]+(mu/defn update-database!+ "Apply `changes` to the Database with `database-id`, returning the number updated."+ [database-id :- ::lib.schema.id/database+ changes :- ::warehouses.schema/database.update](t2/update! :model/Database database-id changes))-(defn delete-destination-databases!- "Delete the routing destination Databases of the Database with `router-database-id`."- [router-database-id]+(mu/defn delete-destination-databases!+ "Delete the routing destination Databases of the Database with `router-database-id`, returning the number deleted."+ [router-database-id :- ::lib.schema.id/database](t2/delete! :model/Database :router_database_id router-database-id))-(defn delete-database!- "Delete the Database with `database-id`."- [database-id]+(mu/defn delete-database!+ "Delete the Database with `database-id`, returning the number deleted."+ [database-id :- ::lib.schema.id/database](t2/delete! :model/Database :id database-id))-(defn mark-tables-sync-complete!- "Mark the initial sync of the Tables with `table-ids` complete."- [table-ids]+(mu/defn mark-tables-sync-complete!+ "Mark the initial sync of the Tables with `table-ids` complete, returning the number updated."+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/update! :model/Table {:id [:in table-ids]} {:initial_sync_status "complete"}))-(defn delete-field-values-for-database!- "Delete the FieldValues of every Field of the Database with `database-id`."- [database-id]+(mu/defn delete-field-values-for-database!+ "Delete the FieldValues of every Field of the Database with `database-id`, returning the number deleted."+ [database-id :- ::lib.schema.id/database](t2/query-one {:delete-from :metabase_fieldvalues:where [:in :field_id^:allow-subquery {:select [:f.id]@@ -247,16 +268,17 @@:right-join [[:metabase_table :t] [:= :f.table_id :t.id]]:where [:= :t.db_id database-id]}]}))-(defn active-tables-for-database+(mu/defn active-tables-for-database"The active Tables of the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select :model/Table :db_id database-id :active true))-(defn active-table-schemas+(mu/defn active-table-schemas"The distinct schemas of the active Tables of the Database with `database-id`, in schema order. When`include-hidden?` is false, restricted to Tables with no `visibility_type` (a non-nil value means the Table ishidden -- see [[metabase.warehouse-schema.models.table/visibility-types]])."- [database-id include-hidden?]+ [database-id :- ::lib.schema.id/database+ include-hidden? :- :boolean](let [clauses (cond-> [](not include-hidden?) (conj [:= :visibility_type nil]))](t2/select-fn-set :schema :model/Table :db_id database-id :active true@@ -264,18 +286,20 @@(when clauses{:where (into [:and] clauses)})))))-(defn active-tables-in-schema+(mu/defn active-tables-in-schema"The active Tables in `schema` of the Database with `database-id`, in display name order."- [database-id schema]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/select :model/Table:db_id database-id:schema schema:active true{:order-by [[:display_name :asc]]}))-(defn active-visible-tables-in-schema+(mu/defn active-visible-tables-in-schema"The active, visible Tables in `schema` of the Database with `database-id`, in display name order."- [database-id schema]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/select :model/Table:db_id database-id:schema schema@@ -283,9 +307,9 @@:visibility_type nil{:order-by [[:display_name :asc]]}))-(defn collection-ids-named+(mu/defn collection-ids-named"The ids of the Collections named `collection-name`, or nil."- [collection-name]+ [collection-name :- :string](t2/select-pks-set :model/Collection :name collection-name))(defn- card-usage-count-subquery@@ -296,10 +320,10 @@[:= :database_id database-id][:= :type type-str]]})-(defn database-usage-counts+(mu/defn database-usage-counts"A single row with the count of Questions (`:question`), Models (`:dataset`), Metrics (`:metric`), Segments(`:segment`), and Transforms (`:transform`) that use the Database with `database-id`."- [database-id]+ [database-id :- ::lib.schema.id/database](mdb/query{:select [:*]:from [[(card-usage-count-subquery database-id :question "question") :question]diff --git a/src/metabase/xrays/db.clj b/src/metabase/xrays/db.cljindex 22367f4cd3b..b1dab7b6918 100644--- a/src/metabase/xrays/db.clj+++ b/src/metabase/xrays/db.clj@@ -2,31 +2,42 @@"Application database queries for the x-rays module. Every function here is a direct Toucan 2 call with noadditional logic, so the rest of the module only touches `toucan2.core` for hydration."(:require+ [metabase.collections.schema :as collections.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.queries.schema :as queries.schema]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(defn database+(mu/defn database"The Database with `database-id`, or nil."- [database-id]+ [database-id :- ::lib.schema.id/database](t2/select-one :model/Database :id database-id))-(defn table- "The Table with `table-id`, or nil."- [table-id]+(mu/defn table+ "The Table with `table-id`, or nil. `table-id` may be nil (some callers pass a Card's or Metric's possibly-absent+ table id), in which case this returns nil."+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select-one :model/Table :id table-id))-(defn tables+(mu/defn tables"The Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Table :id [:in table-ids]))-(defn tables-in-schema- "The Tables in `schema` of the Database with `database-id`."- [database-id schema]+(mu/defn tables-in-schema+ "The Tables in `schema` of the Database with `database-id`. `schema` may be nil (some drivers have no+ schema concept)."+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/select :model/Table :db_id database-id :schema schema))-(defn sibling-tables- "The active, visible Tables in `schema` of the Database with `database-id` other than `table-id`."- [database-id schema table-id]+(mu/defn sibling-tables+ "The active, visible Tables in `schema` of the Database with `database-id` other than `table-id`. `schema`+ may be nil (some drivers have no schema concept)."+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]+ table-id :- ::lib.schema.id/table](t2/select :model/Table:db_id database-id:schema schema@@ -34,10 +45,11 @@:visibility_type nil:active true))-(defn candidate-tables-with-field-stats+(mu/defn candidate-tables-with-field-stats"The id, schema, name, entity type, Database, field count, and list-likeness of the active, visible Tables of theDatabase with `database-id` (optionally narrowed to `schema`) that have at least one non-key Field."- [database-id schema]+ [database-id :- ::lib.schema.id/database+ schema :- [:maybe :string]](t2/select [:model/Table :id :schema :display_name :entity_type :db_id[:ts.count :num-fields][[:and@@ -64,192 +76,202 @@[:= :active true]]schema (conj [:= :schema schema]))}))-(defn field+(mu/defn field"The Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :model/Field :id field-id))-(defn field-name+(mu/defn field-name"The name of the Field with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one-fn :name :model/Field :id field-id))-(defn metadata-column+(mu/defn metadata-column"The `:metadata/column` with `field-id`, or nil."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select-one :metadata/column :id field-id))-(defn fields-targeting+(mu/defn fields-targeting"The Fields whose FK target is the Field with `field-id`."- [field-id]+ [field-id :- ::lib.schema.id/field](t2/select :model/Field :fk_target_field_id field-id))-(defn fk-fields-for-tables+(mu/defn fk-fields-for-tables"The FK Fields of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:set ::lib.schema.id/table]](t2/select :model/Field :fk_target_field_id [:not= nil] :table_id [:in table-ids]))-(defn active-fk-fields-for-table+(mu/defn active-fk-fields-for-table"The active FK Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select :model/Field :table_id table-id :fk_target_field_id [:not= nil] :active true))-(defn fk-target-field-ids-for-table+(mu/defn fk-target-field-ids-for-table"The FK target Field ids of the active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn-set :fk_target_field_id :model/Field:table_id table-id:fk_target_field_id [:not= nil]:active true))-(defn active-field-ids-for-table+(mu/defn active-field-ids-for-table"The ids of the active Fields of the Table with `table-id`."- [table-id]+ [table-id :- ::lib.schema.id/table](t2/select-fn-set :id :model/Field :table_id table-id :active true))-(defn table-ids-of-fields-targeting+(mu/defn table-ids-of-fields-targeting"The Table ids of the active Fields whose FK target is one of `field-ids`."- [field-ids]+ [field-ids :- [:set ::lib.schema.id/field]](t2/select-fn-set :table_id :model/Field :fk_target_field_id [:in field-ids] :active true))-(defn visible-fields-for-tables+(mu/defn visible-fields-for-tables"The active, normally visible, previewable Fields of the Tables with `table-ids`."- [table-ids]+ [table-ids :- [:sequential ::lib.schema.id/table]](t2/select :model/Field:table_id [:in table-ids]:visibility_type "normal":preview_display true:active true))-(defn other-visible-fields-in-table+(mu/defn other-visible-fields-in-table"The active, normally visible Fields of the Table with `table-id` other than `field-id`."- [table-id field-id]+ [table-id :- [:maybe ::lib.schema.id/table]+ field-id :- [:maybe ::lib.schema.id/field]](t2/select :model/Field:table_id table-id:id [:not= field-id]:visibility_type "normal":active true))-(defn card+(mu/defn card"The Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id))-(defn model-card+(mu/defn model-card"The model Card with `card-id`, or nil."- [card-id]+ [card-id :- ::lib.schema.id/card](t2/select-one :model/Card :id card-id :type :model))-(defn cards-in-collection+(mu/defn cards-in-collection"The Cards in the Collection with `collection-id`."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select :model/Card :collection_id collection-id))-(defn unarchived-cards-for-table-of-types- "The unarchived Cards of the Table with `table-id` whose type is one of `card-types`."- [table-id card-types]+(mu/defn unarchived-cards-for-table-of-types+ "The unarchived Cards of the Table with `table-id` whose type is one of `card-types`. `table-id` may be nil+ (a native-query or nested-query Card has no resolved table id)."+ [table-id :- [:maybe ::lib.schema.id/table]+ card-types :- [:sequential :keyword]](t2/select :model/Card :table_id table-id :type [:in card-types] :archived false))-(defn unarchived-metrics-for-table- "The unarchived metric Cards of the Table with `table-id`."- [table-id]+(mu/defn unarchived-metrics-for-table+ "The unarchived metric Cards of the Table with `table-id`. `table-id` may be nil (the caller's Table is+ itself sometimes absent)."+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select :model/Card :table_id table-id :type :metric :archived false))-(defn insert-card!+(mu/defn insert-card!"Insert the Card `card` and return the inserted instance."- [card]+ [card :- ::queries.schema/card.update](t2/insert-returning-instance! :model/Card card))-(defn delete-cards-in-collection!- "Delete the Cards in the Collection with `collection-id`."- [collection-id]+(mu/defn delete-cards-in-collection!+ "Delete the Cards in the Collection with `collection-id`, returning the number deleted."+ [collection-id :- ::lib.schema.id/collection](t2/delete! :model/Card :collection_id collection-id))-(defn segment+(mu/defn segment"The Segment with `segment-id`, or nil."- [segment-id]+ [segment-id :- ::lib.schema.id/segment](t2/select-one :model/Segment :id segment-id))-(defn unarchived-segments-for-table- "The unarchived Segments of the Table with `table-id`."- [table-id]+(mu/defn unarchived-segments-for-table+ "The unarchived Segments of the Table with `table-id`. `table-id` may be nil (the caller's Table is+ itself sometimes absent)."+ [table-id :- [:maybe ::lib.schema.id/table]](t2/select :model/Segment :table_id table-id :archived false))-(defn collection+(mu/defn collection"The Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one :model/Collection :id collection-id))-(defn collection-location-columns+(mu/defn collection-location-columns"The location and id of the Collection with `collection-id`, or nil."- [collection-id]+ [collection-id :- ::lib.schema.id/collection](t2/select-one [:model/Collection :location :id] :id collection-id))-(defn collection-id-by-name-and-location+(mu/defn collection-id-by-name-and-location"The id of the Collection named `collection-name` at `location`, or nil."- [collection-name location]+ [collection-name :- :string+ location :- :string](t2/select-one-pk :model/Collection :name collection-name :location location))-(defn automagic-dashboards-collection+(mu/defn automagic-dashboards-collection"The unarchived automatically generated dashboards Collection at `location`, or nil."- [location]+ [location :- :string](t2/select-one :model/Collection:name "Automatically Generated Dashboards":archived false:location location))-(defn insert-collection!+(mu/defn insert-collection!"Insert the Collection `row` and return the inserted instance."- [row]+ [row :- ::collections.schema/collection.update](t2/insert-returning-instance! :model/Collection row))-(defn insert-collection-returning-pk!+(mu/defn insert-collection-returning-pk!"Insert the Collection `row` and return its id."- [row]+ [row :- ::collections.schema/collection.update](t2/insert-returning-pk! :model/Collection row))-(defn dashboards+(mu/defn dashboards"The Dashboards with `dashboard-ids`."- [dashboard-ids]+ [dashboard-ids :- [:set ::lib.schema.id/dashboard]](t2/select :model/Dashboard :id [:in dashboard-ids]))-(defn recently-edited-dashboard-ids-for-user+(mu/defn recently-edited-dashboard-ids-for-user"The ids of the Dashboards the User with `user-id` has revised, most recent first."- [user-id]+ [user-id :- [:maybe ::lib.schema.id/user]](t2/select-fn-set :model_id :model/Revision:model "Dashboard":user_id user-id{:order-by [[:timestamp :desc]]}))-(defn dashboard-ids-for-card- "The Dashboard ids of the DashboardCards showing the Card with `card-id`."- [card-id]+(mu/defn dashboard-ids-for-card+ "The Dashboard ids of the DashboardCards showing the Card with `card-id`. `card-id` may be nil (an ad-hoc+ query has no saved Card id), in which case this returns nil."+ [card-id :- [:maybe ::lib.schema.id/card]](t2/select-fn-set :dashboard_id :model/DashboardCard :card_id card-id))-(defn card-ids-for-dashboard+(mu/defn card-ids-for-dashboard"The Card ids of the DashboardCards of the Dashboard with `dashboard-id`."- [dashboard-id]+ [dashboard-id :- ::lib.schema.id/dashboard](t2/select-fn-set :card_id :model/DashboardCard :dashboard_id dashboard-id))-(defn other-card-ids-on-dashboards+(mu/defn other-card-ids-on-dashboards"The Card ids other than `card-id` of the DashboardCards of the Dashboards with `dashboard-ids`."- [dashboard-ids card-id]+ [dashboard-ids :- [:sequential ::lib.schema.id/dashboard]+ card-id :- ::lib.schema.id/card](t2/select-fn-set :card_id :model/DashboardCard :dashboard_id [:in dashboard-ids] :card_id [:not= card-id]))-(defn dashcard-card-and-dashboard-ids+(mu/defn dashcard-card-and-dashboard-ids"The Card and Dashboard ids of the DashboardCards, optionally narrowed to `card-ids` and/or excluding`excluded-dashboard-ids`."- [card-ids excluded-dashboard-ids]+ [card-ids :- [:maybe [:sequential ::lib.schema.id/card]]+ excluded-dashboard-ids :- [:maybe [:sequential ::lib.schema.id/dashboard]]](apply t2/select [:model/DashboardCard :card_id :dashboard_id](concat (when (seq card-ids) [:card_id [:in card-ids]])(when (seq excluded-dashboard-ids) [:dashboard_id [:not-in excluded-dashboard-ids]]))))-(defn model-index+(mu/defn model-index"The ModelIndex with `model-index-id`, or nil."- [model-index-id]+ [model-index-id :- ms/PositiveInt](t2/select-one :model/ModelIndex model-index-id))-(defn model-index-value+(mu/defn model-index-value"The ModelIndexValue of the ModelIndex with `model-index-id` for the model primary key `model-pk`, or nil."- [model-index-id model-pk]+ [model-index-id :- ms/PositiveInt+ model-pk :- :int](t2/select-one :model/ModelIndexValue :model_index_id model-index-id :model_pk model-pk))diff --git a/test/metabase/collections_rest/api_test.clj b/test/metabase/collections_rest/api_test.cljindex 670ee9563c7..08a140eafad 100644--- a/test/metabase/collections_rest/api_test.clj+++ b/test/metabase/collections_rest/api_test.clj@@ -6,6 +6,7 @@[clojure.string :as str][clojure.test :refer :all][metabase.collections-rest.api :as api.collection]+ [metabase.collections-rest.children-query :as children-query][metabase.collections.models.collection :as collection][metabase.collections.models.collection-test :as collection-test][metabase.collections.test-utils :refer [personal-collection with-library-not-synced without-library]]@@ -1607,7 +1608,7 @@[:type :asc :nulls-first][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:official-collections-first? true} app-db))))))+ (children-query/children-sort-clause {:official-collections-first? true} app-db))))))(deftest ^:parallel children-sort-clause-test-2(testing "Sorting by last-edited-at"@@ -1617,7 +1618,7 @@[:last_edit_timestamp :asc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :last-edited-at+ (children-query/children-sort-clause {:sort-column :last-edited-at:sort-direction :asc:official-collections-first? true} :mysql)))))@@ -1629,7 +1630,7 @@[:last_edit_timestamp :asc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :last-edited-at+ (children-query/children-sort-clause {:sort-column :last-edited-at:sort-direction :asc:official-collections-first? true} :postgres)))))@@ -1643,7 +1644,7 @@[:last_edit_first_name :asc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :last-edited-by+ (children-query/children-sort-clause {:sort-column :last-edited-by:sort-direction :asc:official-collections-first? true} :postgres)))))@@ -1657,7 +1658,7 @@[:last_edit_first_name :asc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :last-edited-by+ (children-query/children-sort-clause {:sort-column :last-edited-by:sort-direction :asc:official-collections-first? true} :mysql)))))@@ -1668,7 +1669,7 @@[:model_ranking :asc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :model+ (children-query/children-sort-clause {:sort-column :model:sort-direction :asc:official-collections-first? true} :postgres)))))@@ -1679,7 +1680,7 @@[:model_ranking :desc][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :model+ (children-query/children-sort-clause {:sort-column :model:sort-direction :desc:official-collections-first? true} :mysql)))))@@ -1691,7 +1692,7 @@[:%lower.description :asc :nulls-last][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :description+ (children-query/children-sort-clause {:sort-column :description:sort-direction :asc:official-collections-first? true} :postgres))))(testing "descending"@@ -1700,7 +1701,7 @@[:%lower.description :desc :nulls-last][:%lower.name :asc][:id :asc]]- (api.collection/children-sort-clause {:sort-column :description+ (children-query/children-sort-clause {:sort-column :description:sort-direction :desc:official-collections-first? true} :postgres))))))diff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.cljindex 8829c925f12..5b7b58faa93 100644--- a/test/metabase/queries_rest/api/card_test.clj+++ b/test/metabase/queries_rest/api/card_test.clj@@ -861,7 +861,7 @@type-schema (resolve-schema (get body-properties "type"))result-metadata-schema (resolve-schema (get body-properties "result_metadata"))](testing 'type- (is (=? {:oneOf [{:$ref "#/components/schemas/metabase.queries.schema.card-type"} {:type :null}]}+ (is (=? {:oneOf [{:$ref "#/components/schemas/metabase.queries.schema..card.type"} {:type :null}]}type-schema)))(testing 'result_metadata(is (=? {:oneOf [{:$ref "#/components/schemas/metabase.lib.schema.metadata..card.result-metadata"} {:type :null}]}diff --git a/test/metabase/run_tracking/core_test.clj b/test/metabase/run_tracking/core_test.cljindex f7e3374bda0..1540f2cc63b 100644--- a/test/metabase/run_tracking/core_test.clj+++ b/test/metabase/run_tracking/core_test.clj@@ -52,8 +52,8 @@:model/TransformRun {fresh :id} {:transform_id t2 :status :started :is_active true:run_method :manual :start_time (minutes-ago 10):last_heartbeat (minutes-ago 1)}]- (let [reaped (rt/reap-rows! {:model :model/TransformRun :active [:= :is_active true]- :stale [:< :last_heartbeat (rt/cutoff 5 :minute)]+ (let [reaped (rt/reap-rows! {:model :model/TransformRun :active [:is_active true]+ :stale [{:column :last_heartbeat :age 5 :unit :minute}]:terminal {:status "timeout" :is_active nil :end_time :%now:message "reaped"}})](is (= [stale] (mapv :id reaped)))@@ -62,8 +62,8 @@(is (= "reaped" (t2/select-one-fn :message :model/TransformRun :id stale)))(is (= :started (t2/select-one-fn :status :model/TransformRun :id fresh))))(testing "second sweep is empty — the row is no longer active"- (is (empty? (rt/reap-rows! {:model :model/TransformRun :active [:= :is_active true]- :stale [:< :last_heartbeat (rt/cutoff 5 :minute)]+ (is (empty? (rt/reap-rows! {:model :model/TransformRun :active [:is_active true]+ :stale [{:column :last_heartbeat :age 5 :unit :minute}]:terminal {:status "timeout" :is_active nil :end_time :%now}}))))))))(deftest reap-rows!-or-stale-test@@ -73,9 +73,9 @@:model/TransformRun {old-start :id} {:transform_id tid :status :started :is_active true:run_method :manual :start_time (minutes-ago 120):last_heartbeat (minutes-ago 1)}]- (let [reaped (rt/reap-rows! {:model :model/TransformRun :active [:= :is_active true]- :stale [:or [:< :last_heartbeat (rt/cutoff 5 :minute)]- [:< :start_time (rt/cutoff 60 :minute)]]+ (let [reaped (rt/reap-rows! {:model :model/TransformRun :active [:is_active true]+ :stale [{:column :last_heartbeat :age 5 :unit :minute}+ {:column :start_time :age 60 :unit :minute}]:terminal {:status "timeout" :is_active nil :end_time :%now}})](is (= [old-start] (mapv :id reaped)))(is (= :timeout (t2/select-one-fn :status :model/TransformRun :id old-start))))))))@@ -88,8 +88,8 @@:model/TaskRun {fresh :id} {:run_type :sync :entity_type :database :entity_id 1:status :started :started_at (minutes-ago 1):updated_at (minutes-ago 1) :process_uuid "test"}]- (let [reaped (rt/reap-rows! {:model :model/TaskRun :active [:= :status "started"]- :stale [:< :updated_at (rt/cutoff 60 :minute)]+ (let [reaped (rt/reap-rows! {:model :model/TaskRun :active [:status "started"]+ :stale [{:column :updated_at :age 60 :unit :minute}]:terminal {:status "abandoned"}})](is (= [stale] (mapv :id reaped)))(is (= :abandoned (t2/select-one-fn :status :model/TaskRun :id stale)))@@ -111,10 +111,10 @@:model/TransformRun {done :id} {:transform_id t1 :status :succeeded :is_active nil:run_method :manual :start_time (minutes-ago 10):last_heartbeat (minutes-ago 10)}]- (rt/heartbeat-ids! :model/TransformRun [:= :is_active true] :last_heartbeat [beat done])+ (rt/heartbeat-ids! :model/TransformRun [:is_active true] :last_heartbeat [beat done])(let [recent? (fn [id] (.isAfter ^OffsetDateTime (t2/select-one-fn :last_heartbeat :model/TransformRun :id id)(minutes-ago 1)))](is (recent? beat) "a passed, still-active id is stamped")(is (not (recent? skip)) "an id not in the list is left to go stale")(is (not (recent? done)) "an inactive id is not stamped (active guard)")- (is (nil? (rt/heartbeat-ids! :model/TransformRun [:= :is_active true] :last_heartbeat [])) "empty id list is a no-op")))))+ (is (nil? (rt/heartbeat-ids! :model/TransformRun [:is_active true] :last_heartbeat [])) "empty id list is a no-op")))))diff --git a/test/metabase/search/appdb/scoring_test.clj b/test/metabase/search/appdb/scoring_test.cljindex 159a1e3b4db..136593fcf58 100644--- a/test/metabase/search/appdb/scoring_test.clj+++ b/test/metabase/search/appdb/scoring_test.clj@@ -5,6 +5,7 @@[clojure.string :as str][clojure.test :refer :all][metabase.app-db.core :as mdb]+ [metabase.search.appdb.core :as search.appdb.core][metabase.search.appdb.index :as search.index][metabase.search.appdb.scoring :as scoring][metabase.search.appdb.specialization.api :as specialization]@@ -41,7 +42,7 @@(defn search-results**[search-string raw-ctx]- (memoize/memo-clear! #'scoring/view-count-percentiles)+ (memoize/memo-clear! #'search.appdb.core/view-count-percentiles)(search.tu/search-results search-string (assoc raw-ctx :search-engine "appdb")))(defn search-results*@@ -79,7 +80,8 @@;; Remove db-specific fields(remove #{:search-vector :query}))(vals (scoring/scorers {:search-engine :search.engine/appdb- :search-string ""})))+ :search-string ""}+ {})))(set (cons :model (keys search.spec/attr-types))))))));; ---- index-ony rankers ----diff --git a/test/metabase/search/ingestion_test.clj b/test/metabase/search/ingestion_test.cljindex 29139c24a2a..e5c5d00af06 100644--- a/test/metabase/search/ingestion_test.clj+++ b/test/metabase/search/ingestion_test.clj@@ -10,6 +10,7 @@[metabase.search.core :as search][metabase.search.engine :as search.engine][metabase.search.ingestion :as search.ingestion]+ [metabase.search.ingestion.query :as search.ingestion.query][metabase.search.spec :as search.spec][metabase.search.test-util :as search.tu][metabase.test :as mt]@@ -157,11 +158,11 @@(deftest search-term-columns-test(testing "search-term-columns with vector format"(is (= #{:name :description}- (set (#'search.ingestion/search-term-columns [:name :description])))))+ (set (#'search.ingestion.query/search-term-columns [:name :description])))))(testing "search-term-columns with map format"(is (= #{:name :description}- (set (#'search.ingestion/search-term-columns {:name identity- :description nil}))))))+ (set (#'search.ingestion.query/search-term-columns {:name identity+ :description nil}))))))(deftest search-items-count-test(testing "search-items-count returns correct count with various searchable items"diff --git a/enterprise/backend/test/metabase_enterprise/security_center/schema_test.clj b/test/metabase/security_center/schema_test.cljsimilarity index 83%rename from enterprise/backend/test/metabase_enterprise/security_center/schema_test.cljrename to test/metabase/security_center/schema_test.cljindex 23174951c9a..d497b7448ae 100644--- a/enterprise/backend/test/metabase_enterprise/security_center/schema_test.clj+++ b/test/metabase/security_center/schema_test.clj@@ -1,7 +1,7 @@-(ns metabase-enterprise.security-center.schema-test+(ns metabase.security-center.schema-test(:require[clojure.test :refer :all]- [metabase-enterprise.security-center.schema :as schema]+ [metabase.security-center.schema :as schema][metabase.util.malli.registry :as mr]))(set! *warn-on-reflection* true)