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
Refuse invalid data app definitions in generate (#86020)
Alexander Polyankine6355ebb9ec9
diff --git a/e2e/support/assets/data-apps/data_apps/teams/src/metabase.schema.ts b/e2e/support/assets/data-apps/data_apps/teams/src/metabase.schema.tsindex 7714c6e7b4d..34988df772b 100644--- a/e2e/support/assets/data-apps/data_apps/teams/src/metabase.schema.ts+++ b/e2e/support/assets/data-apps/data_apps/teams/src/metabase.schema.ts@@ -6,64 +6,58 @@ const tables = {name: "Scoreboard Actions",fields: {createdAt: {- /* metadata: { "displayName": "Created At" } */+ /* metadata: { "displayName": "Created At", "baseType": "type/DateTimeWithLocalTZ" } */type: "column",name: "created_at",- sourceName: "scoreboard_actions",+ slug: "created_at",jsType: "Date",fieldId: 2167,tableId: 215,- baseType: "type/DateTimeWithLocalTZ",},id: {- /* metadata: { "displayName": "ID", "semanticType": "type/PK" } */+ /* metadata: { "displayName": "ID", "baseType": "type/Integer", "semanticType": "type/PK" } */type: "column",name: "id",- sourceName: "scoreboard_actions",+ slug: "id",jsType: "number",fieldId: 2163,tableId: 215,- baseType: "type/Integer",},score: {- /* metadata: { "displayName": "Score" } */+ /* metadata: { "displayName": "Score", "baseType": "type/Integer" } */type: "column",name: "score",- sourceName: "scoreboard_actions",+ slug: "score",jsType: "number",fieldId: 2165,tableId: 215,- baseType: "type/Integer",},status: {- /* metadata: { "displayName": "Status" } */+ /* metadata: { "displayName": "Status", "baseType": "type/Text" } */type: "column",name: "status",- sourceName: "scoreboard_actions",+ slug: "status",jsType: "string",fieldId: 2166,tableId: 215,- baseType: "type/Text",},teamName: {- /* metadata: { "displayName": "Team Name" } */+ /* metadata: { "displayName": "Team Name", "baseType": "type/Text" } */type: "column",name: "team_name",- sourceName: "scoreboard_actions",+ slug: "team_name",jsType: "string",fieldId: 2164,tableId: 215,- baseType: "type/Text",},updatedAt: {- /* metadata: { "displayName": "Updated At" } */+ /* metadata: { "displayName": "Updated At", "baseType": "type/DateTimeWithLocalTZ" } */type: "column",name: "updated_at",- sourceName: "scoreboard_actions",+ slug: "updated_at",jsType: "Date",fieldId: 2168,tableId: 215,- baseType: "type/DateTimeWithLocalTZ",},},},diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.cljindex 9e249569b72..077b84e3d78 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj@@ -10,12 +10,13 @@[metabase-enterprise.data-apps.generate.actions :as generate.actions][metabase-enterprise.data-apps.generate.queries :as generate.queries][metabase-enterprise.data-apps.generate.schema :as generate.schema]+ [metabase-enterprise.data-apps.generate.util :as generate.util][metabase-enterprise.data-apps.group-access :as data-app.group-access][metabase.api-scope.data-app :as api-scope][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.api.routes.common :refer [+auth]]- [metabase.lib.schema.test-spec]+ [metabase.data-apps.schema :as data-apps.schema][metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json]@@ -99,21 +100,35 @@[x](walk/postwalk #(cond-> % (map? %) (update-keys (comp keyword u/->kebab-case-en name))) x))+(defn- definitions-schema+ "Request schema for definitions by export name."+ [definition-schema]+ [:and+ {:decode/normalize (fn [definitions]+ (cond-> definitions+ (map? definitions)+ (update-vals #(api.macros/decode-and-validate-params :body definition-schema %))))}+ (ms/string-keyed-map definition-schema)+ [:fn {:error/message "export names whose files have distinct paths"}+ (fn [definitions]+ (apply distinct? nil (map generate.util/resource-path (keys definitions))))]])+(def ^:private QueryDefinitions"The `defineQuery` exports of a data app's `metabase.queries.ts`, by export name: each one's entity ID, and itsdefinition as a one-stage test query."- [:map-of :string [:map {:closed true}- [:entityId ms/NanoIdString]- [:query [:and {:decode/api kebab-case-keys} :metabase.lib.schema.test-spec/test-query-spec]]]])+ (definitions-schema+ [:map {:closed true}+ [:entityId ms/NanoIdString]+ [:query [:and {:decode/api kebab-case-keys} ::data-apps.schema/query-spec]]]))(def ^:private ActionDefinitions"The `defineAction` exports of a data app's `metabase.actions.ts`, by export name: implicit actions of `kind` on atable."- [:map-of :string [:map {:closed true}- [:entityId ms/NanoIdString]- [:kind [:enum "row/create" "row/update" "row/delete"]]- [:table [:map {:closed true}- [:id ms/PositiveInt]]]]])+ (definitions-schema+ [:merge+ [:map {:closed true}+ [:entityId ms/NanoIdString]]+ ::data-apps.schema/action-spec]))(def ^:private AddGroupsRequest[:map {:closed true}diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/metric.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/metric.cljindex d3a9e354b1c..b546e879db4 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/metric.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/metric.clj@@ -108,32 +108,23 @@(defn- dimension-schema"Returns the schema for a metric dimension."- ([dimension metric-id]- (dimension-schema dimension metric-id (dimension-table-id dimension) {} {}))- ([dimension- metric-id- table-id- table-key-by-id- table-source-name-by-id]- (let [field-id (dimension-field-id dimension)- dimension-id (or (:id dimension) field-id)- column (if (:sources dimension)- (persisted-dimension->column dimension)- dimension)]- (m/assoc-some- (assoc (schemas.common/column-schema column)- :type "column"- :key (schemas.common/generated-key (:name column) dimension-id)- :id (str dimension-id))- :sourceName (when (integer? table-id)- (get table-source-name-by-id table-id))- :fieldId (when (integer? field-id) field-id)- :tableId (when (integer? table-id) table-id)- :sourceFieldId (when (integer? (:source-field-id dimension))- (:source-field-id dimension))- :metricId metric-id- :keyDisambiguator (when (integer? table-id)- (get table-key-by-id table-id))))))+ [dimension table-id table-key-by-id]+ (let [field-id (dimension-field-id dimension)+ dimension-id (or (:id dimension) field-id)+ column (if (:sources dimension)+ (persisted-dimension->column dimension)+ dimension)]+ (m/assoc-some+ (assoc (schemas.common/column-schema column)+ :type "column"+ :key (schemas.common/generated-key (:name column) dimension-id)+ :id (str dimension-id))+ :fieldId (when (integer? field-id) field-id)+ :tableId (when (integer? table-id) table-id)+ :sourceFieldId (when (integer? (:source-field-id dimension))+ (:source-field-id dimension))+ :keyDisambiguator (when (integer? table-id)+ (get table-key-by-id table-id)))))(defn- mapping-source-field-id"Returns the mapped source field id for a metric dimension mapping."@@ -177,14 +168,6 @@[id (schemas.common/pascal-case (schemas.common/generated-key (or display_name name) id))]))(into {}))))-(defn- table-source-names- "Returns table names emitted as metric dimension source names."- [table-rows]- (when (seq table-rows)- (->> table-rows- (map (juxt :id :name))- (into {}))))-(defn- source-table-schema"Returns the metric source table identity from portable table metadata."[[database-name schema-name table-name]]@@ -228,18 +211,13 @@(defn- metric-dimension-schemas"Returns dimension schemas and mapped table ids for a metric."- [metric-id details source-card-id-value]+ [details source-card-id-value](let [dimension-table-id-pairs (metric-dimensions-with-table-ids details source-card-id-value)table-ids (->> dimension-table-id-pairs (keep second) (filter integer?) distinct)table-rows (table-source-rows table-ids)table-key-by-id (table-key-disambiguators table-rows)- table-source-name-by-id (table-source-names table-rows)dimension-schemas (mapv (fn [[dimension table-id]]- (dimension-schema dimension- metric-id- table-id- table-key-by-id- table-source-name-by-id))+ (dimension-schema dimension table-id table-key-by-id))dimension-table-id-pairs)]{:dimension-schemas dimension-schemas:mapped-table-ids (->> dimension-schemas (keep :tableId) distinct sort vec)}))@@ -251,14 +229,13 @@(let [result-column (or (some-> (metric-result-column card) schemas.common/column-schema)(fallback-metric-column details))source-card-id-value (source-card-id card)- {:keys [dimension-schemas mapped-table-ids]} (metric-dimension-schemas id details source-card-id-value)]+ {:keys [dimension-schemas mapped-table-ids]} (metric-dimension-schemas details source-card-id-value)](m/assoc-some{:type "metric":key (schemas.common/generated-key name id):id id:name name:columns [result-column]}- :databaseId (:database_id card):sourceTableId (source-table-id card):sourceCardId source-card-id-value:entityId portable_entity_iddiff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/render.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/render.cljindex 687ba776865..cdb8cfaa9bf 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/render.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/render.clj@@ -28,19 +28,17 @@(def ^:private schema-render-policy{:table {:runtime [:type :id :name :fields :segments :measures]:metadata [:entityId :description :databaseName :schemaName :tableName]}- :field {:runtime [:type :name :sourceName :jsType :fieldId :tableId- :baseType :effectiveType :defaultTemporalBucket]- :metadata [:displayName :description :semanticType :unit]}+ :field {:runtime [:type :name :slug :jsType :fieldId :tableId]+ :metadata [:displayName :description :baseType :effectiveType :semanticType :unit]}:segment {:runtime [:type :id :tableId :name]:metadata [:entityId :description]}:measure {:runtime [:type :id :tableId :name :columns]:metadata [:entityId :description]}- :metric {:runtime [:type :id :name :databaseId :sourceTableId :sourceCardId+ :metric {:runtime [:type :id :name :sourceTableId :sourceCardId:mappedTableIds :columns :dimensions]:metadata [:entityId :description :filters :verified :sourceTable]}- :metric-dimension {:runtime [:type :id :fieldId :metricId :tableId :sourceName :sourceFieldId- :name :jsType :baseType :effectiveType :defaultTemporalBucket]- :metadata [:displayName :description :semanticType :unit]}+ :metric-dimension {:runtime [:type :id :fieldId :tableId :sourceFieldId :name :jsType]+ :metadata [:displayName :description :baseType :effectiveType :semanticType :unit]}:column {:runtime [:type :name :jsType]:metadata [:displayName :description :baseType :effectiveType :semanticType :unit]}})@@ -275,8 +273,7 @@Raw dimensions are kept under `\"fields\"`, or `\"metricFields\"` if compactedfields already use `\"fields\"` as their output key."[dimensions compact-keys compact-fields]- (when-let [raw-fields (not-empty (update-vals (apply dissoc dimensions compact-keys)- #(dissoc % :metricId)))]+ (when-let [raw-fields (not-empty (apply dissoc dimensions compact-keys))](let [group-key (if (contains? compact-fields "fields")"metricFields""fields")]diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/table.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/table.cljindex ddac68ac7e2..368e8dbe1e4 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/table.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/table.clj@@ -7,7 +7,8 @@[metabase.audit-app.core :as audit][metabase.lib-be.core :as lib-be][metabase.metabot.core :as metabot]- [metabase.premium-features.core :as premium-features]))+ [metabase.premium-features.core :as premium-features]+ [metabase.util :as u]))(set! *warn-on-reflection* true)@@ -20,21 +21,18 @@(defn field-schema"Returns the schema for a table field."([field]- (field-schema field nil nil))- ([field source-name]- (field-schema field source-name nil))- ([{:keys [id field_id] :as field} source-name table-id]+ (field-schema field nil))+ ([{:keys [id field_id] :as field} table-id](let [field-id (or id field_id)table-id (or table-id (:table_id field) (:table-id field) (table-by-field-id field-id))](m/assoc-some(assoc (schemas.common/column-schema field):type "column":key (schemas.common/generated-key (:name field) field-id)- :id field-id)- :sourceName source-name+ :id field-id+ :slug (u/slugify (:name field))):fieldId (when (integer? field-id) field-id)- :tableId (when (integer? table-id) table-id)- :defaultTemporalBucket (:unit field)))))+ :tableId (when (integer? table-id) table-id)))))(defn- without-unavailable-tables"`tables` without any backed by a routing destination database, and without the audit database's while the audit@@ -140,7 +138,7 @@:name (or display_name name):databaseName database_name:tableName name- :fields (schemas.common/keyed-map (map #(field-schema % name id) fields))}+ :fields (schemas.common/keyed-map (map #(field-schema % id) fields))}:entityId portable_entity_id:description description:schemaName database_schemadiff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/util.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/util.cljindex d9f21ea39ca..fcd3edeca44 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/util.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/util.clj@@ -5,8 +5,13 @@(set! *warn-on-reflection* true)+(defn resource-path+ "The path of the file for the export named `export-name` in the app's collection directory."+ [export-name]+ (str (serialization/slugify-name export-name) ".yaml"))+(defn resource-file"The file of the extracted `entity` named `export-name`, at its path in the app's collection directory."[export-name entity]- {:path (str (serialization/slugify-name export-name) ".yaml")+ {:path (resource-path export-name):contents (serialization/entity-yaml entity)})diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.cljindex edbdff01c18..4965792c688 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj@@ -319,3 +319,108 @@(testing "a user sees it once their group can read the collection"(group-access/add-groups! (t2/select-one :model/DataApp :name "demo") [(:id (perms/all-users-group))])(is (some #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps")))))))++;;; --------------------------------------------------- Generate ---------------------------------------------------++(def ^:private collection-entity-id "AppCollection12345678")++(deftest generate-schema-test+ (mt/with-premium-features #{:data-apps}+ (data-apps.tu/with-published-tables! [(mt/id :venues)]+ (testing "a non-superuser cannot generate an app's files"+ (mt/user-http-request :rasta :get 403 "ee/apps/generate/schema"))+ (let [[{:keys [path contents]} :as files] (mt/user-http-request :crowberto :get 200 "ee/apps/generate/schema")]+ (is (= 1 (count files)))+ (is (= "src/metabase.schema.ts" path))+ (testing "the schema holds the published tables with their fields, and the Library's metrics"+ (is (str/includes? contents "const tables = {"))+ (is (str/includes? contents (str "id: " (mt/id :venues))))+ (is (str/includes? contents (str "fieldId: " (mt/id :venues :name))))+ (is (str/includes? contents "slug: \"category_id\""))+ (is (str/includes? contents "const metrics = {")))+ (testing "and no actions"+ (is (not (str/includes? contents "actions"))))))))++(deftest generate-queries-test+ (mt/with-premium-features #{:data-apps}+ (data-apps.tu/with-published-tables! [(mt/id :venues)]+ (let [definition {:entityId "VenuesByCategory12345"+ :query {:stages [{:source {:type "table" :id (mt/id :venues) :name "Venues"}+ :aggregations [{:type "operator" :operator "count" :args []}]+ :breakouts [{:type "column"+ :name "CATEGORY_ID"+ :fieldId (mt/id :venues :category_id)+ :displayName "Category ID"+ :jsType "number"}]}]}}+ [{:keys [path contents]} :as files]+ (mt/user-http-request :crowberto :post 200 "ee/apps/generate/queries"+ {:collection collection-entity-id+ :queries {:venuesByCategory definition}})]+ (is (= 1 (count files)))+ (testing "a question is named after its export and filed in the app's collection by its entity ID"+ (is (= "venuesbycategory.yaml" path))+ (is (str/includes? contents "name: venuesByCategory\n"))+ (is (str/includes? contents "entity_id: VenuesByCategory12345\n"))+ (is (str/includes? contents (str "collection_id: " collection-entity-id "\n"))))+ (testing "its query is the definition's, with portable references"+ (is (str/includes? contents "- count"))+ (is (str/includes? contents "- CATEGORY_ID")))))))++(defn- venues-definition+ "A definition of a question on venues with entity ID `entity-id`, with `stage` merged into its one stage."+ [entity-id stage]+ {:entityId entity-id+ :query {:stages [(merge {:source {:type "table" :id (mt/id :venues) :name "Venues"}} stage)]}})++(deftest generate-queries-refuses-invalid-definitions-test+ (mt/with-premium-features #{:data-apps}+ (data-apps.tu/with-published-tables! [(mt/id :venues)]+ (testing "a definition that doesn't match its schema"+ (mt/user-http-request :crowberto :post 400 "ee/apps/generate/queries"+ {:collection collection-entity-id+ :queries {:venues (venues-definition "Venues123456789012345" {:limit "five"})+ :valid (venues-definition "Valid1234567890123456" {})}}))+ (testing "export names whose files have the same path"+ (mt/user-http-request :crowberto :post 400 "ee/apps/generate/queries"+ {:collection collection-entity-id+ :queries {:venueList (venues-definition "VenueList123456789012" {})+ :VenueList (venues-definition "VenueList223456789012" {})}})))))++(deftest generate-actions-refuses-invalid-definitions-test+ (mt/with-premium-features #{:data-apps}+ (testing "a definition that doesn't match its schema"+ (mt/user-http-request :crowberto :post 400 "ee/apps/generate/actions"+ {:collection collection-entity-id+ :actions {:updateVenue {:entityId "UpdateVenue1234567890"+ :kind "row/upsert"+ :table {:id (mt/id :venues)}}+ :valid {:entityId "Valid1234567890123456"+ :kind "row/update"+ :table {:id (mt/id :venues)}}}}))+ (testing "export names whose files have the same path"+ (mt/user-http-request :crowberto :post 400 "ee/apps/generate/actions"+ {:collection collection-entity-id+ :actions {:updateVenue {:entityId "UpdateVenue1234567890"+ :kind "row/update"+ :table {:id (mt/id :venues)}}+ :UpdateVenue {:entityId "UpdateVenue2234567890"+ :kind "row/update"+ :table {:id (mt/id :venues)}}}}))))++(deftest generate-actions-test+ (mt/with-premium-features #{:data-apps}+ (let [[{:keys [path contents]} :as files]+ (mt/user-http-request :crowberto :post 200 "ee/apps/generate/actions"+ {:collection collection-entity-id+ :actions {:updateVenue {:entityId "UpdateVenue1234567890"+ :kind "row/update"+ :table {:id (mt/id :venues) :name "Venues"}}}})]+ (is (= 1 (count files)))+ (testing "an implicit action is named after its export and filed in the app's collection by its entity ID"+ (is (= "updatevenue.yaml" path))+ (is (str/includes? contents "name: updateVenue\n"))+ (is (str/includes? contents "entity_id: UpdateVenue1234567890\n"))+ (is (str/includes? contents (str "collection_id: " collection-entity-id "\n")))+ (is (str/includes? contents "type: implicit\n"))+ (is (str/includes? contents "kind: row/update"))+ (is (str/includes? contents "- VENUES"))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate_test.cljdeleted file mode 100644index 8df0d8e6ed1..00000000000--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate_test.clj+++ /dev/null@@ -1,69 +0,0 @@-(ns metabase-enterprise.data-apps.generate-test- (:require- [clojure.string :as str]- [clojure.test :refer :all]- [metabase-enterprise.data-apps.test-util :as data-apps.tu]- [metabase.test :as mt]))--(set! *warn-on-reflection* true)--(def ^:private collection-entity-id "AppCollection12345678")--(deftest generate-schema-test- (mt/with-premium-features #{:data-apps}- (data-apps.tu/with-published-tables! [(mt/id :venues)]- (testing "a non-superuser cannot generate an app's files"- (mt/user-http-request :rasta :get 403 "ee/apps/generate/schema"))- (let [[{:keys [path contents]} :as files] (mt/user-http-request :crowberto :get 200 "ee/apps/generate/schema")]- (is (= 1 (count files)))- (is (= "src/metabase.schema.ts" path))- (testing "the schema holds the published tables with their fields, and the Library's metrics"- (is (str/includes? contents "const tables = {"))- (is (str/includes? contents (str "id: " (mt/id :venues))))- (is (str/includes? contents (str "fieldId: " (mt/id :venues :name))))- (is (str/includes? contents "const metrics = {")))- (testing "and no actions"- (is (not (str/includes? contents "actions"))))))))--(deftest generate-queries-test- (mt/with-premium-features #{:data-apps}- (data-apps.tu/with-published-tables! [(mt/id :venues)]- (let [definition {:entityId "VenuesByCategory12345"- :query {:stages [{:source {:type "table" :id (mt/id :venues) :name "Venues"}- :aggregations [{:type "operator" :operator "count" :args []}]- :breakouts [{:type "column"- :name "CATEGORY_ID"- :fieldId (mt/id :venues :category_id)- :displayName "Category ID"- :jsType "number"}]}]}}- [{:keys [path contents]} :as files]- (mt/user-http-request :crowberto :post 200 "ee/apps/generate/queries"- {:collection collection-entity-id- :queries {:venuesByCategory definition}})]- (is (= 1 (count files)))- (testing "a question is named after its export and filed in the app's collection by its entity ID"- (is (= "venuesbycategory.yaml" path))- (is (str/includes? contents "name: venuesByCategory\n"))- (is (str/includes? contents "entity_id: VenuesByCategory12345\n"))- (is (str/includes? contents (str "collection_id: " collection-entity-id "\n"))))- (testing "its query is the definition's, with portable references"- (is (str/includes? contents "- count"))- (is (str/includes? contents "- CATEGORY_ID")))))))--(deftest generate-actions-test- (mt/with-premium-features #{:data-apps}- (let [[{:keys [path contents]} :as files]- (mt/user-http-request :crowberto :post 200 "ee/apps/generate/actions"- {:collection collection-entity-id- :actions {:updateVenue {:entityId "UpdateVenue1234567890"- :kind "row/update"- :table {:id (mt/id :venues) :name "Venues"}}}})]- (is (= 1 (count files)))- (testing "an implicit action is named after its export and filed in the app's collection by its entity ID"- (is (= "updatevenue.yaml" path))- (is (str/includes? contents "name: updateVenue\n"))- (is (str/includes? contents "entity_id: UpdateVenue1234567890\n"))- (is (str/includes? contents (str "collection_id: " collection-entity-id "\n")))- (is (str/includes? contents "type: implicit\n"))- (is (str/includes? contents "kind: row/update"))- (is (str/includes? contents "- VENUES"))))))diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/data-schema.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/data-schema.tsindex 85a6bc76599..441d36a6fb5 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/data-schema.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/data-schema.ts@@ -7,6 +7,7 @@ import type {SchemaColumn,SchemaJavaScriptType,SegmentSchema,+ TableFieldSchema,TableSchema,} from "embedding-sdk-shared/lib/create-metabase-query/schema";import type { DatasetColumn, RowValues } from "metabase-types/api";@@ -19,6 +20,7 @@ export type {SchemaColumn,SchemaJavaScriptType,SegmentSchema,+ TableFieldSchema,TableSchema,};diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/index.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/index.tsindex 8d459d5aacf..69f42ffaecc 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/index.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/index.ts@@ -5,7 +5,6 @@ export type {ActionDefinition,ActionKindFromDefinition,ActionParametersFromDefinition,- ActionResultForBulk,ActionResultForCreate,ActionResultForDelete,ActionResultForKind,diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/types.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/types.tsindex 9d63d8d3215..67660ca1418 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/types.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/types.ts@@ -12,22 +12,22 @@import type { SdkActionId } from "embedding-sdk-bundle/types/action";import type {- FieldSchema,RowValue,SchemaJavaScriptType,+ TableFieldSchema,TableSchema,} from "../data-schema";/**- * Flat public kind union. Maps onto the backend's namespaced- * `row/*` + `bulk/*` `implicitKind` and the `query` `type` value, but- * exposes a simpler five-value surface to callers: `create` / `update` /- * `delete` always refer to a single row, `bulk` covers any bulk variant,- * and `sql` covers custom SQL actions (the backend's `query`-type action).+ * Flat public kind union. Maps onto the backend's namespaced `row/*`+ * `implicitKind` and the `query` `type` value, but exposes a simpler+ * four-value surface to callers: `create` / `update` / `delete` always refer+ * to a single row, and `sql` covers custom SQL actions (the backend's+ * `query`-type action).** @category useAction*/-export type ActionKind = "create" | "update" | "delete" | "bulk" | "sql";+export type ActionKind = "create" | "update" | "delete" | "sql";/*** Response from a single-row create — the inserted row.@@ -56,18 +56,6 @@ export type ActionResultForDelete = {"rows-deleted": readonly RowValue[];};-/**- * Response from any bulk variant — a success flag plus optional counts.- *- * @category useAction- */-export type ActionResultForBulk = {- success: boolean;- "rows-created"?: number;- "rows-updated"?: number;- "rows-deleted"?: number;-};-/*** Response from a custom SQL action — the affected row count.*@@ -89,7 +77,6 @@ export type AnyActionResult =| ActionResultForCreate| ActionResultForUpdate| ActionResultForDelete- | ActionResultForBulk| ActionResultForSql;/**@@ -131,11 +118,9 @@ export type ActionResultForKind<TKind extends ActionKind | undefined> =? ActionResultForUpdate: TKind extends "delete"? ActionResultForDelete- : TKind extends "bulk"- ? ActionResultForBulk- : TKind extends "sql"- ? ActionResultForSql- : AnyActionResult;+ : TKind extends "sql"+ ? ActionResultForSql+ : AnyActionResult;// ============================================================================// Data App schema-derived helpers@@ -187,19 +172,20 @@ type ActionParamValue<TParam extends { jsType?: SchemaJavaScriptType }> =: RowValue | null;/**- * An implicit action's parameters: its table's columns, by name, each typed- * by its `jsType` and optional, since the database decides what a row needs.+ * An implicit action's parameters: its table's columns, by their `slug`, each+ * typed by its `jsType` and optional, since the database decides what a row+ * needs.** @category useAction* @notExported ActionParamValue*/export type TableActionParameters<TTable> = TTable extends {- fields: infer TFields extends Record<string, FieldSchema>;+ fields: infer TFields extends Record<string, TableFieldSchema>;}? {- [TKey in keyof TFields as Lowercase<- TFields[TKey]["name"]- >]?: ActionParamValue<TFields[TKey]>;+ [TKey in keyof TFields as TFields[TKey]["slug"]]?: ActionParamValue<+ TFields[TKey]+ >;}: Record<string, unknown>;diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/fixtures.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/fixtures.tsindex 7be38bba9fc..853200b999e 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/fixtures.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/fixtures.ts@@ -8,7 +8,7 @@ const column = <TName extends string,TDisplayName extends string,TJsType extends string,- TOptions extends object = object,+ const TOptions extends object = object,>(fieldId: TFieldId,tableId: TTableId,@@ -90,12 +90,16 @@ export const TEST_SCHEMA = {type: "table" as const,id: 1,fields: {- id: column(100, 1, "ID", "ID", "number"),+ id: column(100, 1, "ID", "ID", "number", { slug: "id" }),createdAt: column(103, 1, "CREATED_AT", "Created At", "Date", {- baseType: "type/DateTime",+ slug: "created_at",+ }),+ amount: column(102, 1, "AMOUNT", "Amount", "number", {+ slug: "amount",+ }),+ status: column(101, 1, "STATUS", "Status", "string", {+ slug: "status",}),- amount: column(102, 1, "AMOUNT", "Amount", "number"),- status: column(101, 1, "STATUS", "Status", "string"),},segments: {completed: { type: "segment" as const, id: 11, tableId: 1 },@@ -114,7 +118,7 @@ export const TEST_SCHEMA = {type: "table" as const,id: 2,fields: {- price: column(201, 2, "PRICE", "Price", "number"),+ price: column(201, 2, "PRICE", "Price", "number", { slug: "price" }),},segments: {active: { type: "segment" as const, id: 12, tableId: 2 },diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-invalid.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-invalid.tsindex d661feae9a5..493205acc0a 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-invalid.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-invalid.ts@@ -71,6 +71,27 @@ const CreateOrder = defineAction({table: TEST_SCHEMA.tables.orders,});+const REVIEWS_TABLE = {+ type: "table",+ id: 3,+ fields: {+ reviewId: {+ type: "column",+ fieldId: 300,+ tableId: 3,+ name: "Review ID",+ slug: "review_id",+ jsType: "number",+ },+ },+} as const;++const CreateReview = defineAction({+ entityId: "createReviewEntity",+ kind: "row/create",+ table: REVIEWS_TABLE,+});+// --------// Compile-time contracts that must **fail** type-checking.//@@ -271,6 +292,9 @@ function InvalidTypeFixtures() {// @ts-expect-error a parameter value is typed by its `jsType`useAction(CreateOrder).execute({ status: 1 });+ // @ts-expect-error a parameter is keyed by its column's slug, not its lowercased name+ useAction(CreateReview).execute({ "review id": 1 });+// @ts-expect-error `result` is discriminated by the action's kindvoid useAction(CreateOrder).result?.["rows-updated"];diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-valid.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-valid.tsindex 461870f7af3..15445ec0799 100644--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-valid.ts+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/type-contract-valid.ts@@ -45,6 +45,27 @@ const CreateOrder = defineAction({const _actionEntityId: "createOrderEntity" = CreateOrder.entityId;+const REVIEWS_TABLE = {+ type: "table",+ id: 3,+ fields: {+ reviewId: {+ type: "column",+ fieldId: 300,+ tableId: 3,+ name: "Review ID",+ slug: "review_id",+ jsType: "number",+ },+ },+} as const;++const CreateReview = defineAction({+ entityId: "createReviewEntity",+ kind: "row/create",+ table: REVIEWS_TABLE,+});+const UpdateOrder = defineAction({entityId: "updateOrderEntity",kind: "row/update",@@ -181,6 +202,9 @@ function ValidTypeFixtures() {void updateOrder.execute({ id: 1 });+ // A parameter is keyed by its column's slug, not its name.+ void useDataAppAction(CreateReview).execute({ review_id: 1 });+// The SDK hook takes a plain definition object, and a `defineAction` export too.const sdkCreateOrder = useAction({entityId: "createOrderEntity",diff --git a/enterprise/frontend/src/embedding-sdk-package/index.ts b/enterprise/frontend/src/embedding-sdk-package/index.tsindex 37be1a5f137..b3e1f10652d 100644--- a/enterprise/frontend/src/embedding-sdk-package/index.ts+++ b/enterprise/frontend/src/embedding-sdk-package/index.ts@@ -33,7 +33,6 @@ export { useAction } from "./hooks/public/use-action";export type {ActionExecuteError,ActionKind,- ActionResultForBulk,ActionResultForCreate,ActionResultForDelete,ActionResultForKind,diff --git a/frontend/src/embedding-sdk-shared/lib/create-metabase-query/schema.ts b/frontend/src/embedding-sdk-shared/lib/create-metabase-query/schema.tsindex c8956c7067e..4bde6e7de7e 100644--- a/frontend/src/embedding-sdk-shared/lib/create-metabase-query/schema.ts+++ b/frontend/src/embedding-sdk-shared/lib/create-metabase-query/schema.ts@@ -11,8 +11,6 @@ export type SchemaColumn = {description?: string;displayName?: string;jsType?: SchemaJavaScriptType;- baseType?: string;- effectiveType?: string;};/**@@ -21,12 +19,15 @@ export type SchemaColumn = {export type FieldSchema = SchemaColumn & {type: "column";id?: string | number;- sourceName?: string;fieldId?: number;tableId?: number;sourceFieldId?: number;};+export type TableFieldSchema = FieldSchema & {+ slug: string;+};+export type SegmentSchema<TTableId extends number = number> = {type: "segment";id: number;@@ -44,7 +45,7 @@ export type TableSchema = {type: "table";id: number;columns?: readonly SchemaColumn[];- fields?: Record<string, FieldSchema>;+ fields?: Record<string, TableFieldSchema>;segments?: Record<string, SegmentSchema>;measures?: Record<string, MeasureSchema>;};@@ -52,7 +53,6 @@ export type TableSchema = {export type MetricSchema = {type: "metric";id: number;- databaseId?: number;sourceTableId?: number;sourceCardId?: number;mappedTableIds?: readonly number[];diff --git a/src/metabase/actions/execution.clj b/src/metabase/actions/execution.cljindex 8abd2f1116b..7f2083900b1 100644--- a/src/metabase/actions/execution.clj+++ b/src/metabase/actions/execution.clj@@ -139,7 +139,7 @@:row/delete :model.row/delete})(def ^:private ImplicitActionKind- [:enum :model.row/create :model.row/update :model.row/delete :bulk/create :bulk/update :bulk/delete])+ [:enum :model.row/create :model.row/update :model.row/delete])(mu/defn- build-implicit-query :- [:map[:query ::mbql.s/Query]diff --git a/src/metabase/actions/schema.clj b/src/metabase/actions/schema.cljindex 060c470c139..ca43359fbe1 100644--- a/src/metabase/actions/schema.clj+++ b/src/metabase/actions/schema.clj@@ -93,10 +93,7 @@:description (deferred-tru "Unsupported implicit action kind")}:row/create:row/update- :row/delete- :bulk/create- :bulk/update- :bulk/delete])+ :row/delete])(def ^:private implicit-action-entries[[:kind {:optional true} [:maybe ::implicit-action.kind]]diff --git a/src/metabase/data_apps/schema.clj b/src/metabase/data_apps/schema.cljindex 05f8d857b5b..e4c3262620d 100644--- a/src/metabase/data_apps/schema.clj+++ b/src/metabase/data_apps/schema.clj@@ -2,6 +2,10 @@"Malli schemas for the data-apps module."(:require[clojure.string :as str]+ [malli.util :as mut]+ [metabase.actions.schema :as actions.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.test-spec :as lib.schema.test-spec][metabase.util :as u][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]))@@ -130,3 +134,23 @@[:name ::slug][:display_name ::display-name][:bundle_path ::bundle-path]]])++(mr/def ::query-stage-spec+ "A stage of a Lib test query spec with only what a data app's question may hold, as the card hooks check it: a table+ source, and no joins or custom expressions."+ [:merge+ (mut/select-keys ::lib.schema.test-spec/test-stage-spec [:fields :filters :aggregations :breakouts :order-bys :limit])+ [:map {:closed true}+ [:source [:ref ::lib.schema.test-spec/test-table-source-spec]]]])++(mr/def ::query-spec+ "A Lib test query spec of a data app's question: one stage."+ [:map {:closed true}+ [:stages [:tuple [:ref ::query-stage-spec]]]])++(mr/def ::action-spec+ "An action of a data app: an implicit action of `kind` on a table."+ [:map {:closed true}+ [:kind ::actions.schema/implicit-action.kind]+ [:table [:map {:closed true}+ [:id ::lib.schema.id/table]]]])diff --git a/src/metabase/driver/sql_jdbc/actions.clj b/src/metabase/driver/sql_jdbc/actions.cljindex ccbe32b9c4e..19ec4f052f5 100644--- a/src/metabase/driver/sql_jdbc/actions.clj+++ b/src/metabase/driver/sql_jdbc/actions.clj@@ -706,7 +706,7 @@[action context inputs](table-row-delete! action context inputs))-;;;; `bulk/update`+;;;; `:table.row/update`(mu/defn- check-row-has-all-pk-columns"Return a 400 if `row` doesn't have all the required PK columns."diff --git a/src/metabase/lib/query/test_spec.cljc b/src/metabase/lib/query/test_spec.cljcindex e41447df06e..eb44971ddd0 100644--- a/src/metabase/lib/query/test_spec.cljc+++ b/src/metabase/lib/query/test_spec.cljc@@ -215,7 +215,11 @@:operator {:lib/type :mbql/expression-parts:operator (:operator expression-spec):options {}- :args (mapv #(expression-spec->expression-parts query stage-number % available-columns)+ :args (into []+ (mapcat (fn [{arg-type :type, value :value, :as arg}]+ (if (and (= arg-type :literal) (sequential? value))+ value+ [(expression-spec->expression-parts query stage-number arg available-columns)])))(:args expression-spec))}))(mu/defn- named-expression-spec? :- :booleandiff --git a/src/metabase/lib/schema/test_spec.cljc b/src/metabase/lib/schema/test_spec.cljcindex c8b21cc9853..936a121a261 100644--- a/src/metabase/lib/schema/test_spec.cljc+++ b/src/metabase/lib/schema/test_spec.cljc@@ -89,6 +89,14 @@[:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :literal]][:value [:ref ::literal/literal]]])+(mr/def ::test-literal-arg-spec+ "A literal argument of an operator: a value, or several values that are each an argument, as in `[:= x 4 5]`."+ [:map {:closed true}+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :literal]]+ [:value [:or+ [:ref ::literal/literal]+ [:sequential {:min 1} [:ref ::literal/literal]]]]])+(mr/def ::test-operator-spec;; TODO(@romeovs): be more specific here and limit to all valid operators?keyword?)@@ -112,7 +120,7 @@[:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case:dispatch (comp keyword :type)}[:column [:ref ::test-column-spec]]- [:literal [:ref ::test-literal-expression-spec]]+ [:literal [:ref ::test-literal-arg-spec]][:operator [:ref ::test-operator-expression-spec]][:segment [:ref ::test-segment-spec]][:measure [:ref ::test-measure-spec]]diff --git a/test/metabase/data_apps/schema_test.clj b/test/metabase/data_apps/schema_test.cljindex 6520cea0d1e..54e1abec7f2 100644--- a/test/metabase/data_apps/schema_test.clj+++ b/test/metabase/data_apps/schema_test.clj@@ -86,3 +86,23 @@(deftest bundle-test(is (valid? ::data-apps.schema/bundle (byte-array 3)))(is (not (valid? ::data-apps.schema/bundle (byte-array (inc data-apps.schema/max-bundle-bytes))))))++(deftest query-spec-test+ (let [stage {:source {:type :table :id 1}}]+ (testing "a query spec has one stage on a table"+ (is (valid? ::data-apps.schema/query-spec {:stages [stage]})))+ (testing "a query spec of several stages, or none, is rejected"+ (doseq [stages [[] [stage stage]]]+ (is (not (valid? ::data-apps.schema/query-spec {:stages stages})) (str "should reject: " (pr-str stages)))))+ (testing "a stage on a card is rejected"+ (is (not (valid? ::data-apps.schema/query-spec {:stages [{:source {:type :card :id 1}}]}))))+ (testing "a stage holds no joins or custom expressions"+ (doseq [k [:joins :expressions]]+ (is (not (valid? ::data-apps.schema/query-spec {:stages [(assoc stage k [])]})) (str "should reject: " k))))))++(deftest action-spec-test+ (testing "an action spec is an implicit action of a known kind on a table"+ (is (valid? ::data-apps.schema/action-spec {:kind "row/update" :table {:id 1}})))+ (testing "an unknown kind, or a table without an ID, is rejected"+ (doseq [bad [{:kind "row/upsert" :table {:id 1}} {:kind "row/update" :table {}}]]+ (is (not (valid? ::data-apps.schema/action-spec bad)) (str "should reject: " (pr-str bad))))))diff --git a/test/metabase/lib/query/test_spec_test.cljc b/test/metabase/lib/query/test_spec_test.cljcindex f1153165daa..a775f48a61a 100644--- a/test/metabase/lib/query/test_spec_test.cljc+++ b/test/metabase/lib/query/test_spec_test.cljc@@ -100,6 +100,21 @@(is (=? [[:> {} [:field {} (meta/id :venues :price)] 3]](lib/filters query))))))+(deftest ^:parallel test-query-with-multiple-values-filter-test+ (testing "a literal argument with several values passes each one as an argument"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :filters [{:type :operator+ :operator :=+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value [3 4]}]}]}]})]+ (is (=? [[:= {} [:field {} (meta/id :venues :price)] 3 4]]+ (lib/filters query))))))+(deftest ^:parallel test-query-with-aggregations-test(testing "test-query adds aggregations to the query"(let [query (lib.query.test-spec/test-query