Skip to content

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

Data Apps improvements (#85183)

Alexander Polyankinf23f31361c17
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 49922d2c0cf..4e43d5c155d 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3828,36 +3828,27 @@
:uses #{actions
api
api-scope
- audit-app
collections
events
lib
- lib.be
lib.schema
lib.types
- metabot
- metrics
models
permissions
premium-features
- queries
remote-sync
request
settings
- system
users
util
util.graph
warehouse-schema-overlay
- enterprise/dependencies
- enterprise/serialization}
+ enterprise/dependencies}
:model-imports #{:model/Action
:model/Card
:model/Collection
:model/DataPermissions
- :model/Database
:model/Field
- :model/Measure
:model/Permissions
:model/Table}
:model-exports #{:model/DataApp}}
diff --git a/bin/embedding-sdk/generate-sdk-package-files.js b/bin/embedding-sdk/generate-sdk-package-files.js
index 5ba42272b0e..1ea5dfb6a78 100644
--- a/bin/embedding-sdk/generate-sdk-package-files.js
+++ b/bin/embedding-sdk/generate-sdk-package-files.js
@@ -12,10 +12,6 @@ function generateSdkPackage() {
maybeCommitHash = maybeCommitHash.slice(0, 7);
}
- const mainPackageJsonContent = JSON.parse(
- fs.readFileSync(path.resolve("./package.json"), "utf-8"),
- );
-
const sdkPackageTemplateJson = fs.readFileSync(
path.resolve(
"./enterprise/frontend/src/embedding-sdk-package/package.template.json",
@@ -38,15 +34,6 @@ function generateSdkPackage() {
version: maybeCommitHash
? `${sdkPackageTemplateJsonContent.version}-${todayDate}-${maybeCommitHash}`
: sdkPackageTemplateJsonContent.version,
-
- // Runtime dependencies of the CLI for data apps, not pre-bundled.
- dependencies: {
- // Compiles app definitions and local imports for the CLI to evaluate.
- esbuild: mainPackageJsonContent.devDependencies.esbuild,
-
- // Parses and edits TypeScript source files during resource sync.
- typescript: mainPackageJsonContent.dependencies.typescript,
- },
};
const mergedContentString = JSON.stringify(mergedContent, null, 2);
diff --git a/bin/embedding-sdk/test-sdk-package.mjs b/bin/embedding-sdk/test-sdk-package.mjs
index fb97651519a..c5c92bd0771 100644
--- a/bin/embedding-sdk/test-sdk-package.mjs
+++ b/bin/embedding-sdk/test-sdk-package.mjs
@@ -1,4 +1,3 @@
-import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import console from "node:console";
import fs from "node:fs";
@@ -34,7 +33,7 @@ try {
),
);
- // Install the packed sdk and runtime dependencies
+ // Install the packed sdk
execFileSync(
"npm",
[
@@ -47,46 +46,6 @@ try {
NPM_COMMAND_OPTIONS,
);
- const installedPackage = path.join(
- SMOKE_TEST_PATH,
- "node_modules/@metabase/embedding-sdk-react",
- );
-
- const manifest = JSON.parse(
- fs.readFileSync(path.join(installedPackage, "package.json"), "utf8"),
- );
-
- // Check that the esbuild module the CLI bundles definition files with is installed and can be used
- execFileSync(
- process.execPath,
- [
- "--eval",
- `
- const { createRequire } = require("node:module");
- const sdkRequire = createRequire(${JSON.stringify(path.join(installedPackage, "package.json"))});
-
- sdkRequire("esbuild").transformSync("const value: number = 1", { loader: "ts" });
- `,
- ],
- NPM_COMMAND_OPTIONS,
- );
-
- for (const command of ["write-resources", "check-resources"]) {
- const output = execFileSync(
- process.execPath,
- [
- path.join(installedPackage, manifest.bin),
- "data-apps",
- command,
- "--help",
- ],
- NPM_COMMAND_OPTIONS,
- );
-
- assert.match(output, new RegExp(`Usage: .*${command}`));
- assert.match(output, /--app-root/);
- }
-
console.log("SDK package smoke test passed.");
} finally {
fs.rmSync(SMOKE_TEST_PATH, { recursive: true, force: true });
diff --git a/e2e/support/assets/data-apps/isolation/src/probes.ts b/e2e/support/assets/data-apps/isolation/src/probes.ts
index 2f9202ba16d..ccacbec0c7f 100644
--- a/e2e/support/assets/data-apps/isolation/src/probes.ts
+++ b/e2e/support/assets/data-apps/isolation/src/probes.ts
@@ -441,7 +441,7 @@ export const createProbes = (
u: string,
) => Promise<unknown>;
- dynamicImport(`${env.instanceUrl}/api/apps/isolation/bundle`)
+ dynamicImport(`${env.instanceUrl}/api/ee/apps/isolation/bundle`)
.then(() => report("reached:dynamic-import-evaluated"))
.catch((err) => report(`isolated:${describeError(err)}`));
},
diff --git a/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx b/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx
index 3f2f935350b..701913c043c 100644
--- a/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx
+++ b/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx
@@ -198,15 +198,10 @@ function NativeQueryPage() {
}
function Actions() {
- const { actionId, actionEntityId, actionParams } = getTestEnv();
- // A data app names its actions by definition. This fixture has no
- // collection files, so the copy the definition points at is the authored action.
+ const { actionEntityId, actionParams } = getTestEnv();
const action = useAction(
- actionId
- ? defineAction({
- action: { id: actionId, parameters: [] },
- copiedActionEntityId: actionEntityId,
- })
+ actionEntityId
+ ? defineAction({ action: { id: actionEntityId, parameters: [] } })
: null,
);
const [output, setOutput] = useState("idle");
diff --git a/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx b/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx
index 37fe315eba6..747e0b041d3 100644
--- a/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx
+++ b/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx
@@ -4,10 +4,9 @@ import { useState } from "react";
import { CreateScore } from "../actions/orders.action";
/**
- * Passes a source-controlled definition to `useAction` rather than a bare ID, so
- * a production build executes the copy its `copiedActionEntityId` names, the one
- * the app's own group is permitted to run. The spec that drives this app writes
- * `actions/orders.action.ts` and the app's collection files before publishing it.
+ * Passes a typed-schema definition to `useAction`, which executes the action in
+ * the app's collection by its entity ID. The spec that drives this app writes
+ * `actions/orders.action.ts` after creating the action.
*/
export default function App() {
const action = useAction(CreateScore);
diff --git a/e2e/support/assets/data-apps/synced-app/src/App.tsx b/e2e/support/assets/data-apps/synced-app/src/App.tsx
index 59f5861dcf8..12b8ef6d458 100644
--- a/e2e/support/assets/data-apps/synced-app/src/App.tsx
+++ b/e2e/support/assets/data-apps/synced-app/src/App.tsx
@@ -3,8 +3,7 @@ import { useMetabaseQuery } from "@metabase/embedding-sdk-react/data-app";
import { OrdersCount } from "../queries/orders.query";
/**
- * Consumes a source-controlled query rather than an inline one, so a production
- * build runs the saved question its `savedQuestionEntityId` names. The spec that
+ * Runs the saved question its typed-schema definition names. The spec that
* drives this app writes `queries/orders.query.ts` and the app's collection files
* before publishing it.
*/
diff --git a/e2e/support/helpers/build-data-app-fixture.mjs b/e2e/support/helpers/build-data-app-fixture.mjs
index 32a9a0bc397..1d18213b3fe 100644
--- a/e2e/support/helpers/build-data-app-fixture.mjs
+++ b/e2e/support/helpers/build-data-app-fixture.mjs
@@ -61,11 +61,5 @@ await build({
...buildInfoDefine,
"process.env.IS_EMBEDDING_SDK": '"true"',
},
- // A fixture's definitions come from `testEnv` or from files a spec writes, so
- // the resource check that guards an author's build is covered by the resource
- // specs instead. `dataAppConfig` nests its plugins in an array, so flatten
- // before filtering.
- plugins: dataAppConfig()
- .plugins.flat(Infinity)
- .filter((plugin) => plugin?.name !== "metabase-resource-check"),
+ plugins: dataAppConfig().plugins,
});
diff --git a/e2e/support/helpers/data-app-test-env.ts b/e2e/support/helpers/data-app-test-env.ts
index 4b6be4443f7..3b529682411 100644
--- a/e2e/support/helpers/data-app-test-env.ts
+++ b/e2e/support/helpers/data-app-test-env.ts
@@ -9,17 +9,9 @@
* runtime through its own `getTestEnv()`.
*/
export type DataAppTestEnv = {
- /**
- * A deployed app runs the card its query was published as, so every query the
- * fixture renders names one by entity ID. The swap drops the static clauses the
- * card already contains, so each card must match its whole authored query.
- */
- scalarQuery: {
- source: TableSource;
- aggregations: CountAggregation[];
- savedQuestionEntityId: string;
- };
- questionQuery: { source: TableSource; savedQuestionEntityId: string };
+ /** Every query the fixture renders is a saved question, named by entity ID. */
+ scalarQuery: { source: CardSource };
+ questionQuery: { source: CardSource };
/**
* `/download-question` page: the id of a saved CHART question (so a `.png`
* export is offered). Passed in because the id tracks the Cypress snapshot.
@@ -44,21 +36,14 @@ export type DataAppTestEnv = {
};
/**
* `/query-states` page: a deliberately invalid query (a card that does not
- * exist), so the hook resolves to an `error` state rather than to the refusal
- * a query without a saved question would raise; the page also exercises
+ * exist), so the hook resolves to an `error` state; the page also exercises
* `refetch`.
*/
- errorQuery?: { source: TableSource; savedQuestionEntityId: string };
- /**
- * `/actions` page: the id of the action the spec creates and `useAction`
- * executes, so it can't be hard-coded in the app. Left out to exercise the "no
- * action id" path, where the hook must not request anything.
- */
- actionId?: number;
+ errorQuery?: { source: CardSource };
/**
- * `/actions` page: the entity ID of that action. A deployed app runs the copy
- * its definition names by entity ID; this fixture has no collection files, so the
- * "copy" is the action itself.
+ * `/actions` page: the entity ID of the action the spec creates and
+ * `useAction` executes, so it can't be hard-coded in the app. Left out to
+ * exercise the "no action" path, where the hook must not request anything.
*/
actionEntityId?: string;
/** `/actions` page: the parameters the page passes to `execute()`. */
@@ -72,7 +57,4 @@ export type DataAppTestEnv = {
nativeQuery?: { database: number; query: string };
};
-// The queries use the SDK's `source` API (`{ type: "table", id }`); the database
-// is resolved from the SDK's metadata store, so no databaseId is passed.
-type TableSource = { type: "table"; id: number };
-type CountAggregation = { type: "operator"; operator: "count"; args: [] };
+type CardSource = { type: "card"; entityId: string };
diff --git a/e2e/support/helpers/e2e-data-app-helpers.ts b/e2e/support/helpers/e2e-data-app-helpers.ts
index 2454670e8f0..1c6e9fb1827 100644
--- a/e2e/support/helpers/e2e-data-app-helpers.ts
+++ b/e2e/support/helpers/e2e-data-app-helpers.ts
@@ -1,9 +1,7 @@
-import { nanoid } from "@reduxjs/toolkit";
import yaml from "js-yaml";
import { USERS, USER_GROUPS, WRITABLE_DB_ID } from "e2e/support/cypress_data";
import * as Urls from "metabase/urls/data-apps";
-import { NANOID_LENGTH } from "metabase-types/api";
import type {
Collection,
CollectionId,
@@ -12,18 +10,15 @@ import type {
DataApp,
Group,
GroupInfo,
- RemoteSyncTask,
WritebackAction,
} from "metabase-types/api";
-import { isObject } from "metabase-types/guards";
-import { createApiKey, createTestNativeQuery, updateSetting } from "./api";
+import { createTestNativeQuery, updateSetting } from "./api";
import type { DataAppTestEnv } from "./data-app-test-env";
import { getIframeBody } from "./e2e-embedding-helpers";
import {
LOCAL_GIT_PATH,
commitToRepo,
- configureGit,
configureGitAndPullChanges,
copySyncedCollectionFixture,
setupGitSync,
@@ -80,7 +75,7 @@ export const mockDataApp = <TestEnv = DataAppTestEnv>(
// the entrypoint checks for app access before serving its HTML
// we need to have a real app in the database - mocking the endpoint isn't enough
cy.request({
- url: `/api/apps/${slug}`,
+ url: `/api/ee/apps/${slug}`,
failOnStatusCode: false,
}).then(({ status }) => {
if (status === 200) {
@@ -90,16 +85,16 @@ export const mockDataApp = <TestEnv = DataAppTestEnv>(
expect(status).to.equal(404);
cy.log("register the data app");
- cy.request("POST", "/api/apps", {
+ cy.request("POST", "/api/ee/apps", {
name: slug,
display_name: displayName,
bundle_path: "dist/index.js",
allowed_hosts: allowedHosts,
- bundle: "",
+ enabled: true,
});
cy.log("assign the data app to all users group");
- cy.request("POST", `/api/apps/${slug}/groups`, {
+ cy.request("POST", `/api/ee/apps/${slug}/groups`, {
group_ids: [USER_GROUPS.ALL_USERS_GROUP],
});
});
@@ -118,13 +113,13 @@ export const mockDataApp = <TestEnv = DataAppTestEnv>(
allowed_hosts: allowedHosts,
});
- cy.intercept("GET", "/api/apps/repo-status", {
+ cy.intercept("GET", "/api/ee/apps/repo-status", {
configured: true,
});
- cy.intercept("GET", "/api/apps", [app]);
- cy.intercept({ method: "GET", pathname: `/api/apps/${slug}` }, app);
+ cy.intercept("GET", "/api/ee/apps", [app]);
+ cy.intercept({ method: "GET", pathname: `/api/ee/apps/${slug}` }, app);
cy.intercept(
- { method: "GET", pathname: `/api/apps/${slug}/bundle` },
+ { method: "GET", pathname: `/api/ee/apps/${slug}/bundle` },
{
statusCode: 200,
headers: {
@@ -324,32 +319,6 @@ export function createDataAppScoreboardAction({
);
}
-/**
- * The dev host app is a real vite data app with the published SDK installed, so
- * its CLI and build are the ones an author actually runs.
- */
-export const dataAppHostAppRoot = () =>
- `${Cypress.config("projectRoot")}/${DATA_APP_DEV_HOST_APP_DIR}`;
-
-/**
- * Clears what the resource specs write into the host app. They drive the same
- * checked-in directory, so each has to start from a clean tree.
- */
-export function resetDataAppHostAppSources() {
- const appRoot = dataAppHostAppRoot();
-
- return cy.task("removeDataAppPaths", {
- paths: [
- `${appRoot}/queries`,
- `${appRoot}/actions`,
- `${appRoot}/collections`,
- ],
- });
-}
-
-/** A new entity ID, as `representations generate-entity-id` makes one for an author. */
-export const newEntityId = () => nanoid(NANOID_LENGTH);
-
/**
* A table as serialized YAML references it: database, schema, and table name.
* The schema is null for a database without schemas.
@@ -418,60 +387,6 @@ const resourceCard = ({
"serdes/meta": serdesMeta("Card", entityId, name),
});
-/**
- * What `POST /api/apps/generate/resources` answers for the actions `copies` name: the
- * file of each copy, by position, for the app collection `collection`.
- */
-export function serializeDataAppActions(
- copies: Array<{ sourceActionId: number; entityId: string }>,
- collection: string,
-) {
- return cy
- .request<{
- actions: Array<{ file: string; yaml: string } | { error: string }>;
- }>("POST", "/api/apps/generate/resources", {
- actions: copies.map(({ sourceActionId, entityId }) => ({
- action_id: sourceActionId,
- entity_id: entityId,
- collection_id: collection,
- })),
- })
- .then(({ body }) =>
- body.actions.map((answer) => {
- if ("error" in answer) {
- throw new Error(answer.error);
- }
-
- return answer;
- }),
- );
-}
-
-/**
- * The copies of the actions `copies` name, as an author writes them into the
- * app's collection: what Metabase serializes for each source action, with the
- * copy's entity ID, in the app's `collection`.
- */
-export function serializeDataAppActionCopies(
- copies: Array<{ sourceActionId: number; entityId: string }>,
- collection: string,
-) {
- return serializeDataAppActions(copies, collection).then((files) =>
- cy.wrap(
- files.map(({ yaml: text }): ResourceEntity => {
- const entity = yaml.load(text);
-
- if (!isObject(entity)) {
- throw new Error(`Generated an action copy that isn't a map: ${text}`);
- }
-
- return entity;
- }),
- { log: false },
- ),
- );
-}
-
/**
* The YAML an author writes for an app's collection, in the Metabase
* representation format: plain data for `writeDataAppResources`, read from
@@ -525,82 +440,24 @@ export function writeDataAppResources(
});
}
-/** Declares one `defineAction` per action, as the generated schema names it, with the ID of its copy. */
+/** Declares one `defineAction` per action, as the app's typed schema does, by the action's entity ID. */
export function declareDataAppActions(
appRoot: string,
- actions: Array<{
- exportName: string;
- sourceActionId: number;
- copiedActionEntityId: string;
- }>,
+ actions: Array<{ exportName: string; actionEntityId: string }>,
) {
return cy.task("writeDataAppFiles", {
files: {
[`${appRoot}/actions/orders.action.ts`]: [
'import { defineAction } from "@metabase/embedding-sdk-react/data-app";',
...actions.map(
- ({ exportName, sourceActionId, copiedActionEntityId }) =>
- `export const ${exportName} = defineAction({ copiedActionEntityId: "${copiedActionEntityId}", action: { id: ${sourceActionId}, parameters: [] } });`,
+ ({ exportName, actionEntityId }) =>
+ `export const ${exportName} = defineAction({ action: { id: "${actionEntityId}", parameters: [] } });`,
),
].join("\n"),
},
});
}
-/** Declares one `defineQuery` per entry, as an app author would, with the ID of its saved question. */
-export function declareDataAppQueries(
- appRoot: string,
- declarations: Array<{
- name: string;
- tableId: number;
- savedQuestionEntityId: string;
- metricId?: number;
- }>,
-) {
- return cy.task("writeDataAppFiles", {
- files: {
- [`${appRoot}/queries/orders.query.ts`]: [
- 'import { defineQuery } from "@metabase/embedding-sdk-react/data-app";',
- ...declarations.map(
- ({ name, tableId, savedQuestionEntityId, metricId }) => {
- const clauses =
- metricId === undefined
- ? ""
- : `, aggregations: [{ type: "metric", id: ${metricId} }]`;
- return `export const ${name} = defineQuery({ savedQuestionEntityId: "${savedQuestionEntityId}", source: { type: "table", id: ${tableId} }${clauses} });`;
- },
- ),
- ].join("\n"),
- },
- });
-}
-
-/**
- * Runs the data app CLI the host app has installed, the one an author runs:
- * `embedding-sdk-react data-apps <command>`. `check-resources` never calls Metabase; `write-resources`
- * reaches it through `env` (see `dataAppCliEnv`).
- */
-export function runDataAppCli(command: string, env?: Record<string, string>) {
- return cy.exec(
- `cd "${dataAppHostAppRoot()}" && ./node_modules/.bin/embedding-sdk-react data-apps ${command}`,
- { failOnNonZeroExit: false, timeout: 60_000, env },
- );
-}
-
-/**
- * The instance and an admin API key a data-app command reaches Metabase with,
- * as the environment variables `.env.local` would otherwise hold.
- */
-export function dataAppCliEnv() {
- return createApiKey(
- `data-app-cli-e2e-${Date.now()}`,
- USER_GROUPS.ADMIN_GROUP,
- ).then(({ body }) => ({
- DATA_APP_MB_URL: String(Cypress.config("baseUrl")),
- DATA_APP_MB_API_KEY: body.unmasked_key,
- }));
-}
-
export const copySyncedDataAppsFixture = () =>
cy.task("copyDirectory", {
source: `${Cypress.config("projectRoot")}/e2e/support/assets/example_synced_data_apps`,
@@ -638,20 +495,6 @@ export type SyncedDataApp = DataApp & {
resource_collection_id: number;
};
-/** The host app's checked-in `data_app.yaml`, as serialization reads it. */
-export const DATA_APP_HOST_APP_MANIFEST = `version: 1
-name: Vite 6 Data App
-slug: vite-6-data-app-host-app
-path: ./dist/index.js
-allowed_hosts:
- - https://allowed.data-app.test
-entity_id: qxpaPkU_WRE2ZQu0cmpqD
-serdes/meta:
-- model: DataApp
- id: qxpaPkU_WRE2ZQu0cmpqD
- label: vite-6-data-app-host-app
-`;
-
const isSyncedDataApp = (app: DataApp): app is SyncedDataApp =>
typeof app.resource_collection_id === "number";
@@ -704,7 +547,7 @@ export function publishDataApp(
commitDataApp(appRoot, slug, options);
configureGitAndPullChanges("read-write");
- return cy.request<DataApp[]>("/api/apps").then(({ body: apps }) => {
+ return cy.request<DataApp[]>("/api/ee/apps").then(({ body: apps }) => {
const app = apps.find(({ name }) => name === slug);
if (!app) {
@@ -720,61 +563,6 @@ export function publishDataApp(
});
}
-const IMPORT_POLL_LIMIT = 120;
-
-/** Yields the error of the import that is running or just ran, once it fails. */
-function waitForImportError(retries = 0): Cypress.Chainable<string> {
- if (retries > IMPORT_POLL_LIMIT) {
- throw new Error("The import did not fail in time.");
- }
-
- return cy
- .request<RemoteSyncTask | null>("/api/ee/remote-sync/current-task")
- .then(({ body }) => {
- if (body?.sync_task_type === "import" && body.status === "errored") {
- return cy.wrap(body.error_message ?? "", { log: false });
- }
-
- if (body?.sync_task_type === "import" && body.status === "successful") {
- throw new Error(
- "The import succeeded, but its resources should have been refused.",
- );
- }
-
- cy.wait(500);
- return waitForImportError(retries + 1);
- });
-}
-
-/**
- * Publishes an app whose resource files the pull is expected to refuse, and
- * yields the pull's error. A refused file fails the whole pull, so nothing of
- * the app loads.
- */
-export function publishDataAppExpectingRefusal(
- appRoot: string,
- slug: string,
- options?: { initializeRepo?: boolean },
-) {
- commitDataApp(appRoot, slug, options);
- configureGit("read-write");
- cy.request("POST", "/api/ee/remote-sync/import", { expected_branch: "main" });
-
- return waitForImportError();
-}
-
-/**
- * Runs the host app's own production build. The SDK's `metabase-resource-check`
- * plugin runs on `buildStart`, so this is what refuses to bundle an app whose
- * collection files don't back its definitions.
- */
-export function buildDataAppHostApp() {
- return cy.exec(`cd "${dataAppHostAppRoot()}" && npm run build`, {
- failOnNonZeroExit: false,
- timeout: 180_000,
- });
-}
-
/** Create a group and assign it to a data app by slug. */
export function assignTestGroupToDataApp(slug: string) {
cy.request<GroupInfo>("POST", "/api/permissions/group", {
@@ -784,7 +572,7 @@ export function assignTestGroupToDataApp(slug: string) {
.as("dataAppTestGroup");
cy.get<GroupInfo>("@dataAppTestGroup").then(({ id }) =>
- cy.request("POST", `/api/apps/${slug}/groups`, { group_ids: [id] }),
+ cy.request("POST", `/api/ee/apps/${slug}/groups`, { group_ids: [id] }),
);
return cy.get<GroupInfo>("@dataAppTestGroup").its("id");
@@ -872,53 +660,3 @@ function waitForDataAppDevServerEnv(
);
});
}
-
-/**
- * The `/* metadata: {...} *\/` block of the generated typed-schema entry that
- * opens with `entry` (its first match): the one at the entry's own depth, not
- * a nested entity's.
- */
-export function typedSchemaMetadata(
- body: string,
- entry: string,
-): Record<string, unknown> {
- const lines = body.split("\n");
- const start = lines.findIndex((line) => line.trim() === entry);
- if (start === -1) {
- throw new Error(`The typed schema has no entry \`${entry}\`.`);
- }
- const indent = lines[start].search(/\S/);
- // The entry's closing brace, so a sibling's block is never mistaken for its own.
- const end = lines.findIndex(
- (line, index) =>
- index > start &&
- line.search(/\S/) === indent &&
- line.trimStart().startsWith("}"),
- );
- const blockStart = lines.findIndex(
- (line, index) =>
- index > start &&
- index < end &&
- line.search(/\S/) === indent + 2 &&
- line.trimStart().startsWith("/* metadata: "),
- );
- if (blockStart === -1) {
- throw new Error(`The typed schema has no metadata for \`${entry}\`.`);
- }
- const blockEnd = lines.findIndex(
- (line, index) => index >= blockStart && line.trimEnd().endsWith(" */"),
- );
- const block = lines
- .slice(blockStart, blockEnd + 1)
- .join("\n")
- .trim();
-
- const metadata: unknown = JSON.parse(
- block.slice("/* metadata: ".length, block.length - " */".length),
- );
- if (!isObject(metadata)) {
- throw new Error(`The metadata of \`${entry}\` is not an object.`);
- }
-
- return metadata;
-}
diff --git a/e2e/test-component/scenarios/data-apps/diagnostics-toolbar.cy.spec.tsx b/e2e/test-component/scenarios/data-apps/diagnostics-toolbar.cy.spec.tsx
index 0769fe36fd1..cb3d1515dda 100644
--- a/e2e/test-component/scenarios/data-apps/diagnostics-toolbar.cy.spec.tsx
+++ b/e2e/test-component/scenarios/data-apps/diagnostics-toolbar.cy.spec.tsx
@@ -240,6 +240,7 @@ describe("scenarios > data-apps > dev diagnostics toolbar", () => {
it("renders the manifest status the feed carries", () => {
serveDiagnosticsFeed([], {
manifest: {
+ exists: true,
name: "Demo",
bundlePath: "dist/index.js",
bundlePathExists: false,
diff --git a/e2e/test-host-app/data-apps/resources-actions.cy.spec.ts b/e2e/test-host-app/data-apps/resources-actions.cy.spec.ts
deleted file mode 100644
index cb23c3047bd..00000000000
--- a/e2e/test-host-app/data-apps/resources-actions.cy.spec.ts
+++ /dev/null
@@ -1,181 +0,0 @@
-import { USERS, WRITABLE_DB_ID } from "e2e/support/cypress_data";
-import type { WritebackAction } from "metabase-types/api";
-
-const { H } = cy;
-
-const TEST_TABLE = "scoreboard_actions";
-
-/** The app is published under its directory's name. */
-const APP_SLUG = "vite-6-data-app-host-app";
-
-const APP_ROOT = () => H.dataAppHostAppRoot();
-const MANIFEST_FILE = () => `${APP_ROOT()}/data_app.yaml`;
-
-const COLLECTION = "hostAppCollection0002";
-
-const SCORE = { team_name: "Data App FC", score: 7 };
-
-type Copy = { source: WritebackAction; entityId: string; exportName: string };
-
-/**
- * Loads actions into an app the way an author does: the source actions exist
- * in Metabase, belonging to no model, and the author copies what Metabase
- * serializes for them into the app's collection with new entity IDs. A
- * repository pull is what creates the copies.
- */
-describe(
- "Embedding SDK: data-app resources (actions)",
- { tags: ["@external", "@actions"] },
- () => {
- beforeEach(() => {
- H.restore("postgres-writable");
- cy.signInAsAdmin();
- H.activateToken("bleeding-edge");
-
- H.resetTestTable({ type: "postgres", table: TEST_TABLE });
- H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: TEST_TABLE });
- H.setActionsEnabledForDB(WRITABLE_DB_ID);
-
- // The specs share a checked-in host app, so start from a clean tree.
- H.resetDataAppHostAppSources();
- cy.writeFile(
- MANIFEST_FILE(),
- `${H.DATA_APP_HOST_APP_MANIFEST}collection: ${COLLECTION}\n`,
- );
- });
-
- // Leave the checked-in host app as it was.
- after(() => {
- H.resetDataAppHostAppSources();
- cy.writeFile(MANIFEST_FILE(), H.DATA_APP_HOST_APP_MANIFEST);
- });
-
- /** Declares `copies` and writes them, changed by `edit`, into the app's collection. */
- const writeCopies = (
- copies: Copy[],
- edit: (copy: Record<string, unknown>) => Record<string, unknown> = (
- copy,
- ) => copy,
- ) => {
- H.declareDataAppActions(
- APP_ROOT(),
- copies.map(({ source, entityId, exportName }) => ({
- exportName,
- sourceActionId: source.id,
- copiedActionEntityId: entityId,
- })),
- );
-
- return H.serializeDataAppActionCopies(
- copies.map(({ source, entityId }) => ({
- sourceActionId: source.id,
- entityId,
- })),
- COLLECTION,
- ).then((actions) =>
- H.writeDataAppResources(APP_ROOT(), {
- collection: H.dataAppRepresentations.collection(
- COLLECTION,
- "Data App: Vite 6 Data App",
- ),
- actions: actions.map(edit),
- }),
- );
- };
-
- /** Two source actions that belong to no model, with a copy of each written. */
- const copyTwoActions = () =>
- H.createDataAppScoreboardAction({ name: "Add team" }).then((add) =>
- H.createDataAppScoreboardAction({ name: "Add rival" }).then((rival) => {
- const copies: [Copy, Copy] = [
- { source: add, entityId: H.newEntityId(), exportName: "AddTeam" },
- {
- source: rival,
- entityId: H.newEntityId(),
- exportName: "AddRival",
- },
- ];
-
- writeCopies(copies);
-
- return cy.wrap(copies, { log: false });
- }),
- );
-
- /** Which of the copies with `entityIds` Metabase holds. */
- const copiesLoaded = (entityIds: string[]) =>
- cy
- .request<WritebackAction[]>("/api/action")
- .then(({ body }) =>
- body
- .map((action) => action.entity_id)
- .filter((entityId) => entityIds.includes(entityId)),
- );
-
- it("lets the app's group execute a published copy but not the action it was copied from", () => {
- copyTwoActions().then(([{ source, entityId }]) => {
- // A user whose groups read no collection: the copy is reachable through
- // the app's group alone, and the source action, in the root collection,
- // through none of them.
- H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) => {
- H.assignTestGroupToDataApp(app.name).then((groupId) => {
- H.addUserToGroup(groupId, USERS.nocollection.email);
- });
- });
-
- cy.signIn("nocollection");
- cy.request({
- method: "POST",
- url: `/api/action/${entityId}/execute`,
- body: { parameters: SCORE },
- })
- .its("status")
- .should("be.oneOf", [200, 204]);
-
- cy.request({
- method: "POST",
- url: `/api/action/${source.id}/execute`,
- body: { parameters: SCORE },
- failOnStatusCode: false,
- })
- .its("status")
- .should("eq", 403);
- });
- });
-
- it("deletes a copied action on the next pull once its file is gone", () => {
- copyTwoActions().then(([kept, removed]) => {
- const entityIds = [kept.entityId, removed.entityId];
-
- H.publishDataApp(APP_ROOT(), APP_SLUG);
- copiesLoaded(entityIds).should("have.members", entityIds);
-
- writeCopies([kept]);
- H.publishDataApp(APP_ROOT(), APP_SLUG, { initializeRepo: false });
-
- copiesLoaded(entityIds).should("deep.equal", [kept.entityId]);
- });
- });
-
- it("refuses a copy of an action that belongs to a model, and loads none of them", () => {
- copyTwoActions().then((copies) => {
- const [first] = copies;
-
- // A data app runs only actions that belong to no model.
- writeCopies(copies, (copy) =>
- copy.entity_id === first.entityId
- ? { ...copy, model_id: H.newEntityId() }
- : copy,
- );
-
- H.publishDataAppExpectingRefusal(APP_ROOT(), APP_SLUG).then((error) => {
- expect(error).to.contain("must belong to no model");
- copiesLoaded(copies.map(({ entityId }) => entityId)).should(
- "deep.equal",
- [],
- );
- });
- });
- });
- },
-);
diff --git a/e2e/test-host-app/data-apps/resources.cy.spec.ts b/e2e/test-host-app/data-apps/resources.cy.spec.ts
deleted file mode 100644
index 386457be851..00000000000
--- a/e2e/test-host-app/data-apps/resources.cy.spec.ts
+++ /dev/null
@@ -1,302 +0,0 @@
-import yaml from "js-yaml";
-
-import { USERS } from "e2e/support/cypress_data";
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-import type { PortableTable } from "e2e/support/helpers";
-import type { Card, CollectionItem } from "metabase-types/api";
-
-const { H } = cy;
-const { ORDERS_ID } = SAMPLE_DATABASE;
-
-/** The app is published under its directory's name. */
-const APP_SLUG = "vite-6-data-app-host-app";
-
-const APP_ROOT = () => H.dataAppHostAppRoot();
-const MANIFEST_FILE = () => `${APP_ROOT()}/data_app.yaml`;
-
-const COLLECTION = "hostAppCollection0001";
-
-const ORDERS_TABLE: PortableTable = ["Sample Database", "PUBLIC", "ORDERS"];
-
-const collection = () =>
- H.dataAppRepresentations.collection(COLLECTION, "Data App: Vite 6 Data App");
-
-/** The saved question an author writes for a plain `Orders` definition. */
-const ordersQuestion = (entityId: string, stage = {}) =>
- H.dataAppRepresentations.card({
- entityId,
- name: "Orders",
- type: "question",
- collection: COLLECTION,
- table: ORDERS_TABLE,
- stage,
- });
-
-const metricAggregation = (metric: string) => ({
- aggregation: [["metric", { "lib/uuid": crypto.randomUUID() }, metric]],
-});
-
-/**
- * Runs the app's resources end to end against the dev host app, a real vite
- * data app with the published SDK installed: the CLI an author runs, the build
- * guard, and the repository pull that loads what the author wrote.
- */
-describe("Embedding SDK: data-app resources (queries)", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- H.activateToken("bleeding-edge");
-
- // The specs share a checked-in host app, so start from a clean tree.
- H.resetDataAppHostAppSources();
- cy.writeFile(
- MANIFEST_FILE(),
- `${H.DATA_APP_HOST_APP_MANIFEST}collection: ${COLLECTION}\n`,
- );
- });
-
- // Leave the checked-in host app as it was.
- after(() => {
- H.resetDataAppHostAppSources();
- cy.writeFile(MANIFEST_FILE(), H.DATA_APP_HOST_APP_MANIFEST);
- });
-
- describe("the CLI", () => {
- it("writes the saved question Metabase builds from a definition into the collection's folder", () => {
- const question = H.newEntityId();
- H.declareDataAppQueries(APP_ROOT(), [
- { name: "Orders", tableId: ORDERS_ID, savedQuestionEntityId: question },
- ]);
- H.writeDataAppResources(APP_ROOT(), { collection: collection() });
-
- H.dataAppCliEnv().then((env) =>
- H.runDataAppCli("write-resources", env).then(({ exitCode, stdout }) => {
- expect(exitCode, stdout).to.eq(0);
- const [, written] = stdout.match(/^Wrote (.+)$/m) ?? [];
- expect(written).to.match(
- /^collections\/data_apps\/data_app__[^\/]+\/orders\.yaml$/,
- );
- expect(stdout.trim().split("\n")).to.have.length(1);
-
- cy.readFile(`${APP_ROOT()}/${written}`).then((text) => {
- expect(yaml.load(text)).to.deep.include({
- entity_id: question,
- collection_id: COLLECTION,
- name: "Orders",
- type: "question",
- display: "table",
- dataset_query: {
- "lib/type": "mbql/query",
- database: "Sample Database",
- stages: [
- {
- "lib/type": "mbql.stage/mbql",
- "source-table": ["Sample Database", "PUBLIC", "ORDERS"],
- },
- ],
- },
- "serdes/meta": [{ model: "Card" }],
- });
- });
- }),
- );
- });
-
- it("checks that the resources back every definition, listing every problem", () => {
- const question = H.newEntityId();
- const stale = H.newEntityId();
- H.declareDataAppQueries(APP_ROOT(), [
- { name: "Orders", tableId: ORDERS_ID, savedQuestionEntityId: question },
- ]);
-
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(question)],
- });
- H.runDataAppCli("check-resources").then(({ exitCode, stdout }) => {
- expect(exitCode).to.eq(0);
- expect(stdout).to.contain(
- "The app's collection files back every definition.",
- );
- });
-
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(stale)],
- });
- H.runDataAppCli("check-resources").then(({ exitCode, stderr }) => {
- expect(exitCode).not.to.eq(0);
- expect(stderr).to.contain(
- `queries/orders.query.ts:Orders names saved question ${question}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- expect(stderr).to.contain(
- "is a resource that is not referenced anywhere.",
- );
- });
- });
- });
-
- it("publishes the saved question with a pull, readable by the app's group alone", () => {
- const question = H.newEntityId();
- H.declareDataAppQueries(APP_ROOT(), [
- { name: "Orders", tableId: ORDERS_ID, savedQuestionEntityId: question },
- ]);
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(question)],
- });
- H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) => {
- cy.request<Card>(`/api/card/${question}`).then(({ body: card }) => {
- expect(card.collection_id).to.eq(app.resource_collection_id);
- expect(card.name).to.eq("Orders");
-
- H.assignTestGroupToDataApp(app.name).then((groupId) => {
- H.addUserToGroup(groupId, USERS.normal.email);
- });
-
- cy.signInAsNormalUser();
- cy.request(`/api/card/${question}`)
- .its("body.id")
- .should("eq", card.id);
-
- cy.signIn("nocollection");
- cy.request({ url: `/api/card/${question}`, failOnStatusCode: false })
- .its("status")
- .should("eq", 403);
- });
- });
- });
-
- it("publishes a copy of the metric a query uses, readable by the app's group alone", () => {
- H.createQuestion({
- name: "Orders count",
- type: "metric",
- query: { "source-table": ORDERS_ID, aggregation: [["count"]] },
- }).then(({ body: metric }) => {
- const question = H.newEntityId();
- const metricCopy = H.newEntityId();
-
- H.declareDataAppQueries(APP_ROOT(), [
- {
- name: "OrdersCount",
- tableId: ORDERS_ID,
- metricId: metric.id,
- savedQuestionEntityId: question,
- },
- ]);
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [
- ordersQuestion(question, metricAggregation(metricCopy)),
- H.dataAppRepresentations.card({
- entityId: metricCopy,
- name: "Orders count",
- type: "metric",
- collection: COLLECTION,
- table: ORDERS_TABLE,
- stage: {
- aggregation: [["count", { "lib/uuid": crypto.randomUUID() }]],
- },
- }),
- ],
- });
- H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) => {
- cy.request<{ data: CollectionItem[] }>(
- `/api/collection/${app.resource_collection_id}/items?models=metric`,
- ).then(({ body: { data: metrics } }) => {
- expect(metrics.map(({ name }) => name)).to.deep.eq(["Orders count"]);
- const [copy] = metrics;
- expect(copy.id, "a copy, not the source metric").not.to.eq(metric.id);
-
- H.assignTestGroupToDataApp(app.name).then((groupId) => {
- H.addUserToGroup(groupId, USERS.normal.email);
- });
-
- cy.signInAsNormalUser();
- cy.request(`/api/card/${copy.id}`).its("status").should("eq", 200);
- cy.request<Card>(`/api/card/${question}`).then(({ body: card }) => {
- cy.request("POST", `/api/card/${card.id}/query`)
- .its("body.data.rows.0.0")
- .should("be.greaterThan", 0);
- });
-
- cy.signIn("nocollection");
- cy.request({ url: `/api/card/${copy.id}`, failOnStatusCode: false })
- .its("status")
- .should("eq", 403);
- });
- });
- });
- });
-
- it("refuses resources that read a card outside the app, and loads none of them", () => {
- H.createQuestion({
- name: "Orders count",
- type: "metric",
- query: { "source-table": ORDERS_ID, aggregation: [["count"]] },
- }).then(({ body: metric }) => {
- const question = H.newEntityId();
-
- H.declareDataAppQueries(APP_ROOT(), [
- {
- name: "OrdersCount",
- tableId: ORDERS_ID,
- metricId: metric.id,
- savedQuestionEntityId: question,
- },
- ]);
- // The question reads the source metric instead of a copy in the app.
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(question, metricAggregation(metric.entity_id))],
- });
-
- H.publishDataAppExpectingRefusal(APP_ROOT(), APP_SLUG).then((error) => {
- expect(error).to.contain(
- `references Card ${metric.entity_id}, which is not one of the app's resources`,
- );
- cy.request({ url: `/api/card/${question}`, failOnStatusCode: false })
- .its("status")
- .should("eq", 404);
- });
- });
- });
-
- describe("the build guard", () => {
- /** `cy.exec` reports the command's output; vite prints this only on success. */
- const expectBuildToSucceed = () =>
- H.buildDataAppHostApp().should((result) => {
- expect(`${result.stdout}${result.stderr}`).to.contain("built in");
- });
-
- // `metabase-resource-check` runs on buildStart and never calls Metabase, so
- // an app whose definitions name missing resources is refused before it can
- // be bundled.
- it("refuses to build when a definition's saved question is missing", () => {
- const question = H.newEntityId();
- H.declareDataAppQueries(APP_ROOT(), [
- { name: "Orders", tableId: ORDERS_ID, savedQuestionEntityId: question },
- ]);
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(question)],
- });
- expectBuildToSucceed();
-
- H.writeDataAppResources(APP_ROOT(), { collection: collection() });
- H.buildDataAppHostApp().should((result) => {
- const output = `${result.stdout}${result.stderr}`;
- expect(output, "the build is refused").to.contain(
- `names saved question ${question}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- expect(output).not.to.contain("built in");
- });
-
- H.writeDataAppResources(APP_ROOT(), {
- collection: collection(),
- cards: [ordersQuestion(question)],
- });
- expectBuildToSucceed();
- });
- });
-});
diff --git a/e2e/test/scenarios/data-apps/actions.cy.spec.ts b/e2e/test/scenarios/data-apps/actions.cy.spec.ts
index 8cbd59063e6..12ae16a6629 100644
--- a/e2e/test/scenarios/data-apps/actions.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/actions.cy.spec.ts
@@ -36,7 +36,6 @@ describe(
displayName: APP_DISPLAY_NAME,
testEnv: {
...TEST_ENV,
- actionId: action.id,
actionEntityId: action.entity_id,
actionParams,
},
@@ -111,7 +110,6 @@ describe(
displayName: APP_DISPLAY_NAME,
testEnv: {
...TEST_ENV,
- actionId: action.id,
actionEntityId: action.entity_id,
actionParams: { team_name: "Data App FC", score: 7 },
},
diff --git a/e2e/test/scenarios/data-apps/admin.cy.spec.ts b/e2e/test/scenarios/data-apps/admin.cy.spec.ts
index d2f825c905e..851c8236657 100644
--- a/e2e/test/scenarios/data-apps/admin.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/admin.cy.spec.ts
@@ -53,8 +53,8 @@ describe("scenarios > data apps > admin management", () => {
});
it("dismisses the promo banner and keeps it hidden across a reload", () => {
- cy.intercept("GET", "/api/apps/repo-status", { configured: true });
- cy.intercept("GET", "/api/apps", []);
+ cy.intercept("GET", "/api/ee/apps/repo-status", { configured: true });
+ cy.intercept("GET", "/api/ee/apps", []);
cy.intercept(
"PUT",
"/api/user-key-value/namespace/user_acknowledgement/key/data-apps-admin-settings-banner",
@@ -86,11 +86,11 @@ describe("scenarios > data apps > admin management", () => {
function markAppOutdated() {
H.pullExampleDataApps();
- cy.request<DataApp>("GET", `/api/apps/${OUTDATED_APP}`)
+ cy.request<DataApp>("GET", `/api/ee/apps/${OUTDATED_APP}`)
.its("body.display_name")
.as("outdatedAppName");
- cy.intercept("GET", "/api/apps", (req) => {
+ cy.intercept("GET", "/api/ee/apps", (req) => {
req.continue((res) => {
res.body = res.body.map((app: DataApp) =>
app.name === OUTDATED_APP
@@ -100,7 +100,7 @@ describe("scenarios > data apps > admin management", () => {
});
});
cy.intercept(
- { method: "GET", pathname: `/api/apps/${OUTDATED_APP}` },
+ { method: "GET", pathname: `/api/ee/apps/${OUTDATED_APP}` },
(req) => {
req.continue((res) => {
res.body = { ...res.body, version: 1, outdated: true };
diff --git a/e2e/test/scenarios/data-apps/group-access-warnings.cy.spec.ts b/e2e/test/scenarios/data-apps/group-access-warnings.cy.spec.ts
index 8f8ab93a183..d2ea12e2bb4 100644
--- a/e2e/test/scenarios/data-apps/group-access-warnings.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/group-access-warnings.cy.spec.ts
@@ -91,9 +91,10 @@ describe("scenarios > data apps > group access warnings (EMB-2416)", () => {
H.modifyPermission(GROUP_NAME, DATA_ACCESS_PERMISSION_INDEX, "Can view");
H.savePermissions();
- cy.intercept("GET", `/api/apps/${APP_NAME}/group-permission-warnings`).as(
- "groupWarnings",
- );
+ cy.intercept(
+ "GET",
+ `/api/ee/apps/${APP_NAME}/group-permission-warnings`,
+ ).as("groupWarnings");
cy.go("back");
cy.location("pathname").should(
diff --git a/e2e/test/scenarios/data-apps/group-access.cy.spec.ts b/e2e/test/scenarios/data-apps/group-access.cy.spec.ts
index 6497439bbbd..3324ee96a81 100644
--- a/e2e/test/scenarios/data-apps/group-access.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/group-access.cy.spec.ts
@@ -43,11 +43,13 @@ describe("scenarios > data apps > group access (EMB-2415)", () => {
H.commitToRepo("Add data apps for testing group accesses");
H.configureGitAndPullChanges("read-write");
- cy.request("GET", `/api/apps/${APP_NAME}`).its("body").should("include", {
- name: APP_NAME,
- display_name: APP_DISPLAY_NAME,
- enabled: true,
- });
+ cy.request("GET", `/api/ee/apps/${APP_NAME}`)
+ .its("body")
+ .should("include", {
+ name: APP_NAME,
+ display_name: APP_DISPLAY_NAME,
+ enabled: true,
+ });
cy.request("GET", `/api/permissions/group/${COLLECTION_GROUP_ID}`)
.its("body.name")
@@ -123,7 +125,7 @@ describe("scenarios > data apps > group access (EMB-2415)", () => {
});
function visitHome() {
- cy.intercept("GET", "/api/apps?available=true").as("availableApps");
+ cy.intercept("GET", "/api/ee/apps?available=true").as("availableApps");
cy.visit("/");
cy.wait("@availableApps");
diff --git a/e2e/test/scenarios/data-apps/helpers/index.ts b/e2e/test/scenarios/data-apps/helpers/index.ts
index 9cf5c588974..b60a78d640d 100644
--- a/e2e/test/scenarios/data-apps/helpers/index.ts
+++ b/e2e/test/scenarios/data-apps/helpers/index.ts
@@ -1,33 +1,15 @@
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
ORDERS_COUNT_QUESTION_ENTITY_ID,
ORDERS_QUESTION_ENTITY_ID,
} from "e2e/support/cypress_sample_instance_data";
import type { DataAppTestEnv } from "e2e/support/helpers";
-// These live here (co-located with the specs) rather than in
-// `e2e/support/helpers/e2e-data-app-helpers.ts`: that file is part of the
-// Cypress support bundle, and importing `cypress_sample_database` there would
-// pull the git-ignored `cypress_sample_database.json` into the snapshot-creation
-// build — before that file is generated. Specs are bundled only after it exists.
-
-const { ORDERS_ID } = SAMPLE_DATABASE;
-
-const source = { type: "table" as const, id: ORDERS_ID };
-
-/**
- * The `testEnv` the fixture's Overview page reads (Orders count + question).
- *
- * Both carry a `savedQuestionEntityId`, because a deployed app runs the card its
- * query was published as — a bare table source is refused. Each id names a
- * snapshot question equivalent to the authored query, since the swap drops the
- * static clauses the card already contains.
- */
+/** The `testEnv` the fixture's Overview page reads (Orders count + question). */
export const DATA_APP_TEST_ENV: DataAppTestEnv = {
scalarQuery: {
- source,
- aggregations: [{ type: "operator", operator: "count", args: [] }],
- savedQuestionEntityId: ORDERS_COUNT_QUESTION_ENTITY_ID,
+ source: { type: "card", entityId: ORDERS_COUNT_QUESTION_ENTITY_ID },
+ },
+ questionQuery: {
+ source: { type: "card", entityId: ORDERS_QUESTION_ENTITY_ID },
},
- questionQuery: { source, savedQuestionEntityId: ORDERS_QUESTION_ENTITY_ID },
};
diff --git a/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts b/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts
index 6a56b7f1e90..694f811f69f 100644
--- a/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts
@@ -1,5 +1,3 @@
-import yaml from "js-yaml";
-
import { USERS, WRITABLE_DB_ID } from "e2e/support/cypress_data";
const { H } = cy;
@@ -15,14 +13,8 @@ const COLLECTION = "syncedActionsAppColl1";
const APP_ROOT = () =>
`${Cypress.config("projectRoot")}/e2e/support/assets/data-apps/${APP_SLUG}`;
-/**
- * The action half of the production path. Outside the dev preview
- * `toExecutableActionId` runs `copiedActionEntityId`, the copy in the app's
- * collection, because that is the only action an app's viewers may execute.
- * The copy reaches Metabase through the app's collection files and a repository pull.
- */
describe(
- "scenarios > data apps > resources in production (actions)",
+ "scenarios > data apps > resources from a repository (actions)",
{ tags: ["@external", "@actions"] },
() => {
const removeTestFiles = () =>
@@ -47,72 +39,32 @@ describe(
});
/**
- * Declares an action that belongs to no model, writes the copy an author
- * writes from its serialization, publishes the app, and yields it with the copy's
- * entity ID.
+ * Publishes the app, creates an action in its collection, and declares it
+ * by its entity ID, as the app's typed schema does.
*/
- const publishApp = () =>
- H.createDataAppScoreboardAction().then((source) => {
- const actionCopy = H.newEntityId();
-
- H.declareDataAppActions(APP_ROOT(), [
- {
- exportName: "CreateScore",
- sourceActionId: source.id,
- copiedActionEntityId: actionCopy,
- },
- ]);
-
- return H.serializeDataAppActionCopies(
- [{ sourceActionId: source.id, entityId: actionCopy }],
- COLLECTION,
- )
- .then((actions) =>
- H.writeDataAppResources(APP_ROOT(), {
- collection: H.dataAppRepresentations.collection(
- COLLECTION,
- "Data App: Synced Actions App",
- ),
- actions,
- }),
- )
- .then(() => H.publishDataApp(APP_ROOT(), APP_SLUG))
- .then((app) => cy.wrap({ app, actionCopy }, { log: false }));
- });
-
- it("serializes the action an author copies, and executes the app's copy rather than the authored action, for a member of the app's group", () => {
- cy.log("serialize an action with no model, as its file holds it");
- H.createDataAppScoreboardAction().then((source) => {
- // Executing the action reads this, so the copy must keep it.
- cy.request("PUT", `/api/action/${source.id}`, {
- visualization_settings: {
- fields: { score: { id: "score", hidden: true, defaultValue: 0 } },
- },
- });
-
- const copy = H.newEntityId();
-
- H.serializeDataAppActions(
- [{ sourceActionId: source.id, entityId: copy }],
+ const publishApp = () => {
+ H.writeDataAppResources(APP_ROOT(), {
+ collection: H.dataAppRepresentations.collection(
COLLECTION,
- ).then(([file]) => {
- expect(file.file).to.eq("add_team.yaml");
-
- const entity = yaml.load(file.yaml);
- expect(entity).to.deep.include({
- entity_id: copy,
- collection_id: COLLECTION,
- type: "query",
- });
- expect(entity).not.to.have.property("model_id");
- expect(entity)
- .to.have.nested.property("visualization_settings.fields.score")
- .that.deep.include({ hidden: true, defaultValue: 0 });
- });
+ "Data App: Synced Actions App",
+ ),
});
- cy.log("execute the app's copy of a separate action");
- publishApp().then(({ app, actionCopy }) => {
+ return H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) =>
+ H.createDataAppScoreboardAction({
+ collectionId: app.resource_collection_id,
+ }).then((action) => {
+ H.declareDataAppActions(APP_ROOT(), [
+ { exportName: "CreateScore", actionEntityId: action.entity_id },
+ ]);
+
+ return cy.wrap({ app, action }, { log: false });
+ }),
+ );
+ };
+
+ it("executes the action in the app's collection for a member of the app's group", () => {
+ publishApp().then(({ app, action }) => {
H.assignTestGroupToDataApp(app.name).then((groupId) => {
H.addUserToGroup(groupId, USERS.normal.email);
});
@@ -129,7 +81,7 @@ describe(
cy.wait("@execute")
.its("request.url")
- .should("contain", `/api/action/${actionCopy}/execute`);
+ .should("contain", `/api/action/${action.entity_id}/execute`);
});
});
},
diff --git a/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts b/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts
index 5d6930a1db7..13af64e3b2e 100644
--- a/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts
@@ -1,10 +1,8 @@
-import { SAMPLE_DB_ID, USERS } from "e2e/support/cypress_data";
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { USERS } from "e2e/support/cypress_data";
import type { PortableTable } from "e2e/support/helpers";
import type { Card } from "metabase-types/api";
const { H } = cy;
-const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
const APP_SLUG = "synced-app";
const APP_DISPLAY_NAME = "Synced App";
@@ -18,32 +16,12 @@ const ORDERS_TABLE: PortableTable = ["Sample Database", "PUBLIC", "ORDERS"];
const APP_ROOT = () =>
`${Cypress.config("projectRoot")}/e2e/support/assets/data-apps/${APP_SLUG}`;
-/**
- * The definition as source control holds it. `id` is the sample database's
- * ORDERS table, written from the same constant, so a snapshot change fails
- * loudly rather than silently.
- */
+/** The definition the app's typed schema holds for its saved question. */
const DECLARATION = [
- "import {",
- " aggregations,",
- " breakout,",
- " defineQuery,",
- '} from "@metabase/embedding-sdk-react/data-app";',
- "",
- "const OrdersUserId = {",
- ' type: "column" as const,',
- ` fieldId: ${ORDERS.USER_ID},`,
- ` tableId: ${ORDERS_ID},`,
- ' name: "USER_ID",',
- ' displayName: "User ID",',
- ' jsType: "number",',
- "};",
+ 'import { defineQuery } from "@metabase/embedding-sdk-react/data-app";',
"",
"export const OrdersCount = defineQuery({",
- ` savedQuestionEntityId: "${QUESTION}",`,
- ` source: { type: "table", id: ${ORDERS_ID} },`,
- " aggregations: [aggregations.count()],",
- " breakouts: [breakout(OrdersUserId)],",
+ ` source: { type: "card", entityId: "${QUESTION}" },`,
"});",
"",
].join("\n");
@@ -68,13 +46,10 @@ const ORDERS_COUNT_QUESTION = H.dataAppRepresentations.card({
});
/**
- * What a shipped data app actually does: outside the dev preview
- * `isDataAppDev()` is false, so the SDK runs the saved question the definition
- * names, the only resource an app's viewers are permitted to read, rather than
- * the authored source. The question reaches Metabase the way it does for an
- * author: written into the app's collection files, committed, and loaded by a repository pull.
+ * An app imported from a repository runs the saved question its typed schema
+ * names, which the pull loaded into the app's collection.
*/
-describe("scenarios > data apps > resources in production", () => {
+describe("scenarios > data apps > resources from a repository", () => {
const removeTestFiles = () =>
cy.task("removeDataAppPaths", {
paths: [`${APP_ROOT()}/queries`, `${APP_ROOT()}/collections`],
@@ -112,7 +87,7 @@ describe("scenarios > data apps > resources in production", () => {
),
);
- it("runs the saved question rather than the authored table query, and both return the same rows", () => {
+ it("runs the saved question its definition names", () => {
publishApp().then(({ cardId }) => {
cy.intercept("POST", "/api/dataset").as("dataset");
H.mockDataApp(APP_SLUG, { displayName: APP_DISPLAY_NAME });
@@ -129,34 +104,6 @@ describe("scenarios > data apps > resources in production", () => {
cy.wait("@dataset").then(({ request }) => {
const [stage] = request.body.stages ?? [];
expect(stage?.["source-card"], "runs the saved question").to.eq(cardId);
- expect(stage?.["source-table"], "not the authored table").to.eq(
- undefined,
- );
- });
-
- // The swap is only safe if the saved question the author wrote returns what
- // the definition does. The dev preview runs the definition; production runs
- // the card. A deployed app cannot run the definition at all, so the two sides
- // are captured separately rather than side by side.
- cy.log("the saved question returns the same rows as the authored query");
- cy.request("POST", "/api/dataset", {
- type: "query",
- database: SAMPLE_DB_ID,
- query: {
- "source-table": ORDERS_ID,
- aggregation: [["count"]],
- breakout: [["field", ORDERS.USER_ID, null]],
- },
- }).then(({ body: authored }) => {
- cy.request("POST", `/api/card/${cardId}/query`).then(
- ({ body: published }) => {
- expect(published.data.rows).to.deep.eq(authored.data.rows);
- expect(
- published.data.rows[0][1],
- "a match on two empty results would be vacuous",
- ).to.be.greaterThan(0);
- },
- );
});
});
});
diff --git a/e2e/test/scenarios/data-apps/sdk.cy.spec.ts b/e2e/test/scenarios/data-apps/sdk.cy.spec.ts
index 277f46fd9d7..f1dbab4e84d 100644
--- a/e2e/test/scenarios/data-apps/sdk.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/sdk.cy.spec.ts
@@ -1,4 +1,3 @@
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { ORDERS_BY_YEAR_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";
import {
DATA_APP_DISPLAY_NAME as APP_DISPLAY_NAME,
@@ -8,8 +7,6 @@ import {
import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";
-const { ORDERS_ID } = SAMPLE_DATABASE;
-
const { H } = cy;
describe("scenarios > data apps > SDK runtime", () => {
@@ -26,11 +23,9 @@ describe("scenarios > data apps > SDK runtime", () => {
displayName: APP_DISPLAY_NAME,
testEnv: {
...TEST_ENV,
- // A card that doesn't exist, so the query resolves to an error rather
- // than to the refusal a query without a saved question would raise.
+ // A card that doesn't exist, so the query resolves to an error.
errorQuery: {
- source: { type: "table", id: ORDERS_ID },
- savedQuestionEntityId: "missingQuestionEntity",
+ source: { type: "card", entityId: "missingQuestionEntity" },
},
},
});
diff --git a/e2e/test/scenarios/data-apps/semantic-schema.cy.spec.ts b/e2e/test/scenarios/data-apps/semantic-schema.cy.spec.ts
deleted file mode 100644
index 99a2c61f7f3..00000000000
--- a/e2e/test/scenarios/data-apps/semantic-schema.cy.spec.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-import type { Collection } from "metabase-types/api";
-
-const { H } = cy;
-const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
-
-type LibraryResponse = Collection & {
- effective_children?: Collection[];
-};
-
-const METRIC_DESCRIPTION = "Revenue from completed orders";
-
-describe("scenarios > data apps > semantic schema", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- H.activateToken("bleeding-edge");
-
- H.createLibrary().then(
- ({ body: library }: Cypress.Response<LibraryResponse>) => {
- const metricsCollection = library.effective_children?.find(
- ({ type }) => type === "library-metrics",
- );
-
- H.publishTables({ table_ids: [ORDERS_ID] });
- H.createQuestion({
- name: "Revenue",
- description: METRIC_DESCRIPTION,
- type: "metric",
- query: {
- "source-table": ORDERS_ID,
- aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
- filter: [">=", ["field", ORDERS.CREATED_AT, null], "2025-01-01"],
- },
- }).then(({ body: metric }) => {
- // `createQuestion` saves a question and only then sets the metric
- // type, and the Metrics collection accepts nothing but metrics.
- cy.request("PUT", `/api/card/${metric.id}`, {
- collection_id: metricsCollection?.id,
- });
- });
- },
- );
- });
-
- it("tells the author about the filter a metric applies", () => {
- cy.request<string>("GET", "/api/apps/generate/schemas").then(({ body }) => {
- const metadata = H.typedSchemaMetadata(body, "revenue: {");
-
- expect(metadata.description).to.eq(METRIC_DESCRIPTION);
- expect(metadata.filters).to.satisfy(
- (filters: unknown) =>
- Array.isArray(filters) &&
- filters.some(
- (filter) =>
- typeof filter === "string" && filter.includes("Created At"),
- ),
- );
- });
- });
-});
diff --git a/e2e/test/scenarios/data-apps/sync.cy.spec.ts b/e2e/test/scenarios/data-apps/sync.cy.spec.ts
index 5c142863b90..5e00406a37a 100644
--- a/e2e/test/scenarios/data-apps/sync.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/sync.cy.spec.ts
@@ -34,18 +34,18 @@ describe("scenarios > data apps > repo sync", () => {
cy.findByTestId("data-app-list-item-second-app").should("exist");
});
- cy.request("GET", "/api/apps").then(({ body: apps }) => {
+ cy.request("GET", "/api/ee/apps").then(({ body: apps }) => {
expect(apps.map((app: { name: string }) => app.name).sort()).to.deep.eq([
"good",
"second-app",
]);
});
- cy.request("/api/apps/good/bundle").its("status").should("eq", 200);
+ cy.request("/api/ee/apps/good/bundle").its("status").should("eq", 200);
// The good app's collection files were loaded with it: its collection, and
// the saved question in it, addressed by the entity IDs the files carry.
- cy.request<DataApp>("/api/apps/good").then(({ body: app }) => {
+ cy.request<DataApp>("/api/ee/apps/good").then(({ body: app }) => {
cy.request("/api/collection/goodAppCollection0000")
.its("body.id")
.should("eq", app.resource_collection_id);
@@ -67,12 +67,12 @@ describe("scenarios > data apps > repo sync", () => {
H.commitToRepo("Remove the good app from the repo");
H.configureGitAndPullChanges("read-write");
- cy.request("GET", "/api/apps").then(({ body: apps }) => {
+ cy.request("GET", "/api/ee/apps").then(({ body: apps }) => {
expect(apps.map((app: { name: string }) => app.name)).to.deep.eq([
"second-app",
]);
});
- cy.request({ url: "/api/apps/good", failOnStatusCode: false })
+ cy.request({ url: "/api/ee/apps/good", failOnStatusCode: false })
.its("status")
.should("eq", 404);
cy.request({
diff --git a/e2e/test/scenarios/data-apps/user-access.cy.spec.ts b/e2e/test/scenarios/data-apps/user-access.cy.spec.ts
index ea5837cc535..8203a63c4e0 100644
--- a/e2e/test/scenarios/data-apps/user-access.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/user-access.cy.spec.ts
@@ -43,7 +43,7 @@ describe("scenarios > data apps > authentication", () => {
ALLOWED_HOST,
);
});
- cy.request(`/api/apps/${SYNCED_APP_SLUG}/bundle`).then((res) => {
+ cy.request(`/api/ee/apps/${SYNCED_APP_SLUG}/bundle`).then((res) => {
expect(
String(res.headers["x-metabase-data-app-allowed-hosts"]),
).to.contain(ALLOWED_HOST);
@@ -64,11 +64,11 @@ describe("scenarios > data apps > authentication", () => {
followRedirect: false,
}).as("appPage");
cy.request({
- url: `/api/apps/${SYNCED_APP_SLUG}/bundle`,
+ url: `/api/ee/apps/${SYNCED_APP_SLUG}/bundle`,
failOnStatusCode: false,
}).as("bundle");
cy.request({
- url: `/api/apps/${SYNCED_APP_SLUG}`,
+ url: `/api/ee/apps/${SYNCED_APP_SLUG}`,
failOnStatusCode: false,
}).as("metadata");
diff --git a/e2e/test/scenarios/data-apps/viewing.cy.spec.ts b/e2e/test/scenarios/data-apps/viewing.cy.spec.ts
index fc9e1e7c1f4..647e5e78f37 100644
--- a/e2e/test/scenarios/data-apps/viewing.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/viewing.cy.spec.ts
@@ -167,7 +167,7 @@ describe("scenarios > data apps > viewing & routing", () => {
// The metadata endpoint refuses an outdated app to non-admins with this
// exact shape (see `check-not-outdated` in data_apps/api.clj).
cy.intercept(
- { method: "GET", pathname: `/api/apps/${APP_NAME}` },
+ { method: "GET", pathname: `/api/ee/apps/${APP_NAME}` },
{
statusCode: 409,
body: {
diff --git a/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts b/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts
index e4a99e598d3..6982d0a8daf 100644
--- a/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts
+++ b/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts
@@ -1,9 +1,7 @@
import { USERS, WRITABLE_DB_ID } from "e2e/support/cypress_data";
-import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import type { PortableTable } from "e2e/support/helpers";
const { H } = cy;
-const { ORDERS_ID } = SAMPLE_DATABASE;
const DATA_APPS_ANALYTICS_PATH = "/monitor/ai-auditing/data-apps";
@@ -62,13 +60,18 @@ function writeQuestionAppResources(root: string) {
function publishQuestionApp() {
const root = appRoot(QUESTION_APP.slug);
- H.declareDataAppQueries(root, [
- {
- name: "OrdersCount",
- tableId: ORDERS_ID,
- savedQuestionEntityId: QUESTION_APP.question,
+ cy.task("writeDataAppFiles", {
+ files: {
+ [`${root}/queries/orders.query.ts`]: [
+ 'import { defineQuery } from "@metabase/embedding-sdk-react/data-app";',
+ "",
+ "export const OrdersCount = defineQuery({",
+ ` source: { type: "card", entityId: "${QUESTION_APP.question}" },`,
+ "});",
+ "",
+ ].join("\n"),
},
- ]);
+ });
writeQuestionAppResources(root);
return H.publishDataApp(root, QUESTION_APP.slug);
}
@@ -78,35 +81,25 @@ function publishQuestionApp() {
function publishActionsApp() {
const root = appRoot(ACTIONS_APP.slug);
- return H.createDataAppScoreboardAction().then((source) => {
- const actionCopy = H.newEntityId();
-
- H.declareDataAppActions(root, [
- {
- exportName: "CreateScore",
- sourceActionId: source.id,
- copiedActionEntityId: actionCopy,
- },
- ]);
-
- return H.serializeDataAppActionCopies(
- [{ sourceActionId: source.id, entityId: actionCopy }],
+ H.writeDataAppResources(root, {
+ collection: H.dataAppRepresentations.collection(
ACTIONS_APP.collection,
- )
- .then((actions) =>
- H.writeDataAppResources(root, {
- collection: H.dataAppRepresentations.collection(
- ACTIONS_APP.collection,
- `Data App: ${ACTIONS_APP.displayName}`,
- ),
- actions,
- }),
- )
- .then(() => writeQuestionAppResources(root))
- .then(() =>
- H.publishDataApp(root, ACTIONS_APP.slug, { initializeRepo: false }),
- );
+ `Data App: ${ACTIONS_APP.displayName}`,
+ ),
});
+ writeQuestionAppResources(root);
+
+ return H.publishDataApp(root, ACTIONS_APP.slug, {
+ initializeRepo: false,
+ }).then((app) =>
+ H.createDataAppScoreboardAction({
+ collectionId: app.resource_collection_id,
+ }).then((action) =>
+ H.declareDataAppActions(root, [
+ { exportName: "CreateScore", actionEntityId: action.entity_id },
+ ]),
+ ),
+ );
}
// The first query against the audit view can take a while to settle, and the page's other
@@ -180,7 +173,7 @@ describe(
cy.log(
"The page names the apps from the installed list, which the app mocks replaced",
);
- cy.intercept("GET", "/api/apps", (req) => {
+ cy.intercept("GET", "/api/ee/apps", (req) => {
req.continue();
});
diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
index 7431b81a000..2cd5615be0c 100644
--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
@@ -51,7 +51,6 @@
[metabase-enterprise.upload-management.api]
[metabase.api.macros :as api.macros]
[metabase.api.util.handlers :as handlers]
- [metabase.request.core :as request]
[metabase.util.i18n :refer [deferred-tru]]))
(comment metabase-enterprise.advanced-config.api.logs/keep-me)
@@ -100,10 +99,6 @@
TODO -- Please fix them! See #22687"
{"/moderation-review" metabase-enterprise.content-verification.api.routes/routes
- ;; Data-app bundle hosting. Naughty because the FE route lives at `/apps/:slug`
- ;; (and `/api/apps/...`), NOT `/api/ee/...` — `/app/*` is reserved for static
- ;; assets, so we keep the public path stable here. Superuser-only inside the handler.
- (str "/" request/data-app-url-segment) (premium-handler metabase-enterprise.data-apps.api/routes :data-apps)
"/mt" metabase-enterprise.sandbox.api.routes/sandbox-routes
"/table" metabase-enterprise.sandbox.api.routes/sandbox-table-routes})
@@ -120,6 +115,7 @@
(handlers/route-map-handler
{"/mcp-permissions" metabase-enterprise.mcp.api.permissions/routes})
(premium-handler metabase-enterprise.metabot.api.routes/routes :ai-controls))
+ "/apps" (premium-handler metabase-enterprise.data-apps.api/routes :data-apps)
"/audit-app" (premium-handler metabase-enterprise.audit-app.api.routes/routes :audit-app)
"/billing" metabase-enterprise.billing.api.routes/routes
"/content-diagnostics" (premium-handler metabase-enterprise.content-diagnostics.api/routes :content-diagnostics)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/README.md b/enterprise/backend/src/metabase_enterprise/data_apps/README.md
deleted file mode 100644
index cb8ad126499..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/README.md
+++ /dev/null
@@ -1,169 +0,0 @@
-# Data apps (backend)
-
-A **data app** is a JS bundle, authored in a git repository, that Metabase serves and runs inside a
-Near Membrane sandbox in the browser. This README covers the backend half: how apps are stored, created,
-serialized, and served. The sandbox, the app runtime, and the host
-iframe live on the frontend and are out of scope here.
-
-## What an app is
-
-`:model/DataApp` is a regular serdes entity. A row holds the app's manifest fields — `name` (the
-slug, served at `/apps/<slug>`), `display_name`, `description`, the data app contract `version`, the
-`bundle_path`, and the `allowed_hosts` its sandboxed bundle may `fetch`/XHR — plus the bundle bytes
-themselves, cached in the app DB so serving never reads a repository.
-
-The model's hooks hold its invariants: every write is normalized and validated against the column
-schemas in `schema.clj`, `bundle_hash` always matches `bundle`, an inserted app gets the collection
-it owns (see Permissions), and deleting an app deletes its collection and group assignments while
-preserving the assigned permission groups. Its default fields leave the bundle out, so listing apps
-never drags bundles out of the DB; `db.clj`'s `data-app-bundle` reads it explicitly.
-
-An app with a lower `version` than this Metabase serves is outdated — badged for admins, hidden
-from everyone else, and a 409 to open.
-
-## Serialization
-
-A serialized app is a `data_app.yaml` in its own directory under `data_apps/`, with its bundle as a
-plain file next to it at its `path`. Its resource collection is a collection of the `data-apps`
-namespace, serialized like any collection under `collections/`:
-
-```
-data_apps/
- sales/
- data_app.yaml # serdes/meta, entity_id, slug, name, description, version, path, allowed_hosts, collection
- dist/index.js # the bundle
-collections/
- data_apps/
- data_app__sales.yaml # the app's resource collection (namespace: data-apps)
- data_app__sales/
- *.yaml # the saved questions, metric copies, and action copies the app runs
-```
-
-Collections of the `data-apps` namespace are written under `collections/data_apps/`, as the
-`transforms` and `snippets` namespaces have their folders.
-
-The YAML keeps the keys a hand-written manifest uses: `slug` is the `name` column, `name` the
-`display_name`, `path` the `bundle_path`, and `collection` the entity ID of the app's resource
-collection, which the app depends on and so loads after. The bundle travels as a serdes _resource
-file_: the entity carries it in `:serdes/resources` on export, the storage writers put it next to
-the YAML, and ingestion reads the paths `serdes/resource-paths` returns back in. A resource path
-must stay inside the entity's directory.
-
-`enabled` is admin-owned and never leaves the instance; group assignments also stay local to the
-instance, and `bundle_hash` is recomputed from the bundle on import.
-A targeted export of an app brings its collection and what it holds along (`serdes/descendants`).
-
-An import matches an app by `entity_id` and reasserts the app's resources; a manifest whose slug an app
-made on the instance holds is refused. A manifest that names a collection the repository lacks, or one
-other than the collection the app already owns, fails to load. It is a no-op without the
-`:data-apps` feature.
-
-Remote sync treats data apps like any other entity, globally rather than per collection, and their
-collections like any namespace's: in scope for import, cleanup and export by their namespace. Before
-an import loads anything, `resource_validation.clj` checks every manifest with its collection and
-the cards and actions in it, and fails the pull naming the file that a load couldn't take as the
-author meant it. Because the bundle is a separate file, a pull that changes only a bundle, or an
-export that touches an app, takes the full rather than the incremental path. An app's directory
-also holds its source, which serialization doesn't own, so exports replace only the YAML and
-resource files in `data_apps/`.
-
-An author deletes an app by deleting its directory and its collection's files under
-`collections/data_apps/` in one commit: the pull deletes the app, and the app's `before-delete`
-hook deletes the collection with what it holds. A commit that deletes the directory but keeps the
-collection's files still deletes the app and its collection; the next export removes the files,
-and until then a full pull loads them back as a collection no app owns. That collection is read-only
-like an app's, so only deleting its files from the repository removes it.
-
-## Serving
-
-Routes are mounted at `/api/apps` (`api.clj`). Not `/app/*` — the server reserves that for static
-assets (`metabase.server.routes/static-files-handler`).
-
-- `GET /api/apps` — list; `?available=true` filters to enabled apps.
-- `GET /api/apps/:slug` — metadata for one enabled app.
-- `GET /api/apps/:slug/bundle` — the cached bytes, with a content-hash ETag and `If-None-Match`
- → 304. Carries `X-Metabase-Data-App-Allowed-Hosts`, which the iframe reads to configure its
- sandbox fetch allowlist.
-- `GET /api/apps/sandbox-host` — the empty document loaded as the Near-Membrane realm iframe,
- carrying the CSP that confines `'unsafe-eval'` to that realm.
-- `POST /api/apps` — create an app from its manifest fields and bundle text (superuser).
-- `PUT /api/apps/:slug` — update manifest fields or the bundle, or toggle `enabled` (superuser).
-- `DELETE /api/apps/:slug` — drop a row, its bundle, and its owned resources (superuser).
-- `GET /api/apps/repo-status` — whether a repo is connected (superuser).
-- `POST /api/apps/generate/app` — an app's `data_app.yaml` and its collection's file, each at its path
- from the repository root, with new entity IDs, or with the `entity_id` and `collection_id` an existing
- app passes (`generate/app.clj`; superuser).
-- `GET /api/apps/generate/schemas` — the TypeScript module an app's definitions are written against: the
- root libraries' tables and metrics, and the query actions that belong to no model
- (`generate/schemas.clj`; superuser).
-- `POST /api/apps/generate/resources` — the files of an app's collection, each a file name and the YAML a
- remote-sync export writes: a saved question per `defineQuery` definition, a copy of each action, and a
- copy of each metric the queries aggregate (`generate/resources.clj`). An action must belong to no model
- (superuser).
-
-Responses are field-filtered by role: superusers get full metadata, everyone else gets `name` and
-`display_name` only. The bundle blob is never serialized into JSON, and metadata reads go through
-the model's default fields, which leave the bundle out, so listing apps doesn't drag the bundles out of the DB.
-
-`csp.clj` exposes an app's `allowed_hosts` to the core security middleware through a `defenterprise`
-hook, which drives the `connect-src` of the iframe document's CSP. It's a separate namespace so the
-middleware's lookup doesn't pull in route code.
-
-## Permissions
-
-Each app owns a resource collection, created with the app in the `data-apps` namespace. The collection
-is never swapped for another; `resources.clj` keeps its name and permissions in step and restores it from the trash.
-Administrators assign existing internal permission groups through `/api/apps/:slug/groups`. Assignments live in
-`data_app_group_assignment` and stay local to the instance. Repository sync preserves them while the app row exists.
-
-Admins reuse existing permission groups through `/api/apps/:slug/groups`. Assignments are stored in
-`data_app_group_assignment`: an app can have multiple groups, and a group can be assigned to multiple apps.
-
-Membership in any assigned group grants app access. Administrators can access every app. The list API hides
-unassigned apps from other users, and metadata, bundle, and HTML entry-point requests check the same assignment.
-Collection access alone does not grant app access.
-
-Every app owns a collection from insertion. If its collection is deleted separately, resource reconciliation recreates it.
-
-Nobody can write a collection of the `data-apps` namespace, or what it holds, administrators included, with or
-without remote sync: the collection's `can-write?` refuses it, the write permissions of everything that takes them
-from its collection are a path no user holds, and a pull is what changes them.
-
-Assignments grant read-only access to the resource collection. Sync restores these grants and removes collection
-access from unassigned groups. Assignment changes never change data permissions. Deleting an app deletes its
-collection and assignments, but preserves the assigned groups.
-
-**A viewer sees an app's data only through access they already hold.** Assignment changes never change
-View Data permissions; the viewer's other groups and sandboxes continue to determine data access.
-
-**Managing is superuser-only** — assignments, enabling, disabling, deleting, and repo status.
-Exporting an app's resources also needs a superuser.
-
-The Data Apps feature is required for all app API endpoints, including group listing and removal.
-
-## Namespace map
-
-| Namespace | Responsibility |
-| -------------------------------------- | ----------------------------------------------------------------------------------------------- |
-| `apps.clj` | Creating apps; the connected repository's URL. |
-| `core.clj` | Public access checks and resource file problems. |
-| `config.clj` | The serialized layout and data app contract version constants. |
-| `schema.clj` | Column schemas, with the normalization and validation every write goes through. |
-| `api.clj` | The `/api/apps` endpoints, bundle serving, ETag handling. |
-| `resources.clj` | Lifecycle of the app-owned collection and derived collection permissions. |
-| `models/data_app.clj` | The `:model/DataApp` Toucan model: hooks, permissions, default fields, serialization. |
-| `generate.clj` | The facade over what Metabase generates for an app's author: its files, its schema, its collection's files. |
-| `generate/app.clj` | An app's `data_app.yaml` and collection file, each at its path with its YAML. |
-| `generate/resources.clj` | The files of an app's collection: built queries, action copies, metric copies, each with its YAML. |
-| `generate/schemas.clj` | The TypeScript schema of the root libraries' tables and metrics and the model-less query actions. |
-| `generate/schemas/` | The schema's stages: data access (`source`), entities (`table`, `metric`, `action`, `common`), rendering (`render`, `javascript`). |
-| `query_definition.clj` | The closed schema of a `defineQuery` definition the serialization accepts. |
-| `resource_validation.clj` | What the files of an app's collection may hold, checked on the whole snapshot before an import. |
-| `access.clj` | App access through assigned groups. |
-| `group_access.clj` | Assignment management, collection grant reconciliation, and the permission warnings, which walk the dependency graph (`:dependencies` feature) from the app's cards and actions to the tables they read. |
-| `models/data_app_group_assignment.clj` | App-to-group assignments. |
-| `db.clj` | The module's application-database queries. |
-| `csp.clj` | `allowed_hosts` lookup for the core CSP middleware. |
-| `init.clj` | Loads the above so endpoints, models, and hooks register. |
-
-`group_access.clj` manages assignments. `models/data_app_group_assignment.clj` defines the local association model.
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
index b1ba40b5435..2a9f9d03bf0 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
@@ -1,19 +1,15 @@
(ns metabase-enterprise.data-apps.api
- "Data-app endpoints, mounted at `/api/apps`: create, read, update, and delete data apps, and serve their
- bundles. See `README.md` in this directory for how the pieces fit together.
-
- Note: we can't use `/app/...` for the frontend or `/api/app/...` for the API
- because Metabase's server reserves `/app/*` for serving static assets (see
- `metabase.server.routes/static-files-handler`)."
+ "Data-app endpoints, mounted at `/api/ee/apps`: manage data apps and their groups, serve their bundles, and generate
+ their typed schema."
(:require
[clojure.string :as str]
[metabase-enterprise.data-apps.apps :as data-apps.apps]
[metabase-enterprise.data-apps.config :as data-app.config]
[metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate :as data-apps.generate]
[metabase-enterprise.data-apps.group-access :as data-app.group-access]
[metabase-enterprise.data-apps.models.data-app :as data-app]
[metabase-enterprise.data-apps.schema :as data-apps.schema]
+ [metabase-enterprise.data-apps.typed-schema :as data-apps.typed-schema]
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
@@ -31,8 +27,8 @@
;;; ------------------------------------------------ Constants ------------------------------------------------
-;; Slug must not collide with the literal `repo-status`/`sandbox-host`/`generate` sub-routes.
-(def ^:private slug-regex #"(?!repo-status$|sandbox-host$|generate$)[^/]+")
+;; Slug must not collide with the literal `repo-status`/`sandbox-host` sub-routes.
+(def ^:private slug-regex #"(?!repo-status$|sandbox-host$)[^/]+")
(def ^:private bundle-response-headers
;; `no-cache` so the browser may cache but must revalidate via
@@ -79,12 +75,19 @@
(def ^:private CreateDataAppRequest
[:map {:closed true}
[:name ::data-apps.schema/slug]
- [:display_name ::data-apps.schema/display-name]
+ [:display_name {:optional true} ::data-apps.schema/display-name]
[:description {:optional true} ::data-apps.schema/description]
[:version {:optional true} ::data-apps.schema/version]
- [:bundle_path ::data-apps.schema/bundle-path]
+ [:bundle_path {:optional true} ::data-apps.schema/bundle-path]
[:allowed_hosts {:optional true} ::data-apps.schema/allowed-hosts]
- [:bundle ::data-apps.schema/bundle-text]])
+ [:enabled {:optional true} :boolean]])
+
+(def ^:private create-defaults
+ "What a new app starts with: an empty bundle, and unless its request says otherwise, the bundle path the app
+ template builds to and disabled until it is ready to be served."
+ {:bundle_path "dist/index.js"
+ :bundle ""
+ :enabled false})
(def ^:private UpdateDataAppRequest
[:map {:closed true}
@@ -93,8 +96,7 @@
[:description {:optional true} ::data-apps.schema/description]
[:version {:optional true} ::data-apps.schema/version]
[:bundle_path {:optional true} ::data-apps.schema/bundle-path]
- [:allowed_hosts {:optional true} ::data-apps.schema/allowed-hosts]
- [:bundle {:optional true} ::data-apps.schema/bundle-text]])
+ [:allowed_hosts {:optional true} ::data-apps.schema/allowed-hosts]])
(def ^:private PublicDataAppResponse
[:map {:closed true}
@@ -187,8 +189,8 @@
app)
(api.macros/defendpoint :get "/" :- [:sequential [:or DataAppResponse PublicDataAppResponse]]
- "List the data apps. Pass `available=true` to return only enabled apps. An outdated app is never
- available, and otherwise listed only to superusers, who see it badged."
+ "List the data apps. Pass `available=true` to return only enabled apps. Disabled and outdated apps are listed only
+ to superusers, who see them badged, and never as available."
[_route-params
{:keys [available]} :- [:map {:closed true} [:available {:optional true} [:maybe :boolean]]]]
(->> (data-apps.db/data-apps available)
@@ -197,9 +199,6 @@
(data-app.config/outdated? %)))
(mapv data-app-response)))
-;; NOTE on the `slug-regex` constraint: the default path-param matcher allows
-;; slashes inside a segment, so `/:slug` would otherwise swallow `/x/bundle`.
-;; The regex also excludes the literal `repo-status` sub-route above.
(defn- with-bundle
"The DataApp `changes` with their `bundle` text as the bytes the app is served."
[changes]
@@ -217,24 +216,21 @@
(api/check-403 (remote-sync/model-editable? :model/DataApp app)))
(api.macros/defendpoint :post "/" :- DataAppResponse
- "Create a data app from its manifest fields and bundle."
+ "Create a data app with its resource collection and an empty bundle. It is disabled unless the request says
+ otherwise."
[_route-params
_query-params
body :- CreateDataAppRequest]
(api/create-check :model/DataApp body)
(check-editable! body)
- (let [app (data-apps.db/data-app (data-apps.apps/create-app! (with-bundle body)))]
+ (let [row (merge create-defaults {:display_name (:name body)} body)
+ app (data-apps.db/data-app (data-apps.apps/create-app! (with-bundle row)))]
(events/publish-event! :event/data-app-create {:object app :user-id api/*current-user-id*})
(data-app-response app)))
-;; NOTE on the `slug-regex` constraint: the default path-param matcher allows
-;; slashes inside a segment, so `/:slug` would otherwise swallow `/x/bundle`.
-;; The regex also excludes the literal `repo-status` sub-route above.
-(api.macros/defendpoint :put ["/:slug" :slug slug-regex] :- DataAppResponse
- "Update a data app's manifest fields or bundle, or enable or disable it. Disabled apps are not served."
- [{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]
- _query-params
- changes :- UpdateDataAppRequest]
+(defn- update-app!
+ "Apply `changes` to the data app named `slug` and return the updated app."
+ [slug changes]
(let [app (write-check-data-app slug)
synced-changes (dissoc changes :enabled)]
(when (seq synced-changes)
@@ -246,6 +242,22 @@
(events/publish-event! :event/data-app-update {:object app :user-id api/*current-user-id*}))
(data-app-response app))))
+;; NOTE on the `slug-regex` constraint: the default path-param matcher allows slashes inside a segment, so `/:slug`
+;; would otherwise swallow `/x/bundle`. The regex also excludes the literal `repo-status` and `sandbox-host` routes.
+(api.macros/defendpoint :put ["/:slug" :slug slug-regex] :- DataAppResponse
+ "Update a data app's manifest fields, or enable or disable it. A disabled app is served to superusers only."
+ [{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]
+ _query-params
+ changes :- UpdateDataAppRequest]
+ (update-app! slug changes))
+
+(api.macros/defendpoint :put ["/:slug/bundle" :slug slug-regex] :- DataAppResponse
+ "Replace a data app's bundle."
+ [{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]
+ _query-params
+ {:keys [bundle]} :- [:map {:closed true} [:bundle ::data-apps.schema/bundle-text]]]
+ (update-app! slug {:bundle bundle}))
+
(api.macros/defendpoint :delete ["/:slug" :slug slug-regex] :- :nil
"Delete a data app, its bundle, its collection, and its group assignments."
[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]]
@@ -257,17 +269,6 @@
;; above (returning `generic-204-no-content` would fail that validation).
nil)
-(api.macros/defendpoint :post "/generate/app" :- ::data-apps.schema/app-files
- "A data app's `data_app.yaml` and its collection's file, each at its path from the repository root and with the
- YAML a remote-sync export writes. An existing app passes its `entity_id` and `collection_id` to keep them; a new app
- gets new entity IDs. For superusers: the files are written into the app's repository, which only an admin works
- with."
- [_route-params
- _query-params
- body :- ::data-apps.schema/app-request]
- (api/check-superuser)
- (data-apps.generate/app body))
-
(def ^:private typescript-response-headers
{"Content-Type" "text/typescript; charset=utf-8"
"X-Content-Type-Options" "nosniff"
@@ -275,32 +276,13 @@
"Referrer-Policy" "no-referrer"
"Cache-Control" "no-store"})
-(api.macros/defendpoint :get "/generate/schemas" :- :any
- "The TypeScript module a data app's definitions are written against: the root libraries' tables and metrics, and
- the query actions that belong to no model. For superusers: a data app's author builds from it, and only an admin
- works with an app's repository."
- []
- (api/check-superuser)
+(api.macros/defendpoint :get ["/:slug/typed-schema" :slug slug-regex] :- :any
+ "Return the TypeScript module of the data app's queries and actions: a `defineQuery` call for each question and metric
+ and a `defineAction` call for each action in the app's collection, addressed by entity ID. Superusers only."
+ [{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]]
{:status 200
:headers typescript-response-headers
- :body (data-apps.generate/schemas)})
-
-(api.macros/defendpoint :post "/generate/resources" :- [:map {:closed true}
- [:queries [:sequential ::data-apps.schema/file]]
- [:actions [:sequential ::data-apps.schema/file]]
- [:metrics [:sequential ::data-apps.schema/file]]]
- "The files of a data app's collection, each a file name in the collection's folder and the YAML a remote-sync
- export writes: the saved question of each `defineQuery` definition in `queries`, the copy of each action without a
- model in `actions`, and the copies of the metrics the queries aggregate. Each item answers on its own, with its file
- or the error that stops it. For superusers: the files are written into the app's repository, which only an admin
- works with."
- [_route-params
- _query-params
- body :- [:map {:closed true}
- [:queries {:optional true} [:maybe [:sequential ::data-apps.schema/query]]]
- [:actions {:optional true} [:maybe [:sequential ::data-apps.schema/action]]]]]
- (api/check-superuser)
- (data-apps.generate/resources body))
+ :body (data-apps.typed-schema/generate (write-check-data-app slug))})
(def ^:private GroupPermissionWarning
[:map
@@ -352,13 +334,13 @@
;; what iframe to render, before any data-app realm exists, so the request never carries the
;; marker. The bundle is fetched from inside that iframe, where it does.
(api.macros/defendpoint :get ["/:slug" :slug slug-regex] :- [:or DataAppResponse PublicDataAppResponse]
- "Fetch metadata for a single enabled data app by its slug."
+ "Fetch metadata for a single data app by its slug, disabled ones to superusers only."
[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]]
- (let [app (read-check-data-app (data-apps.db/enabled-data-app-by-slug slug))]
+ (let [app (read-check-data-app (data-apps.db/data-app-by-slug slug))]
(data-app-response (cond-> app (not api/*is-superuser?*) check-not-outdated))))
(api.macros/defendpoint :get ["/:slug/bundle" :slug slug-regex] :- :any
- "Serve the cached JS bundle for a single enabled data app by slug. Honors
+ "Serve the cached JS bundle for a single data app by slug, disabled ones to superusers only. Honors
`If-None-Match` against the content-hash ETag with a 304."
{:scope api-scope/data-app}
[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]
@@ -368,7 +350,7 @@
respond
raise]
(try
- (let [row (check-not-outdated (read-check-data-app (data-apps.db/enabled-data-app-by-slug slug)))
+ (let [row (check-not-outdated (read-check-data-app (data-apps.db/data-app-by-slug slug)))
hash (:bundle_hash row)
etag (some->> hash (format "\"%s\""))]
(cond
@@ -388,10 +370,10 @@
:body (ByteArrayInputStream. bundle)})
(respond {:status 404
:headers {"Content-Type" "application/json"}
- :body "{\"error\":\"Bundle not synced yet\"}"})))))
+ :body "{\"error\":\"This app has no bundle yet\"}"})))))
(catch Throwable e
(raise e))))
(def routes
- "`/api/apps` routes."
+ "`/api/ee/apps` routes."
(api.macros/ns-handler *ns* +auth))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/config.clj b/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
index 38fa86b1889..2ab6f134b28 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
@@ -1,6 +1,6 @@
(ns metabase-enterprise.data-apps.config
"Constants of a data app's manifest, which a serialized app keeps in the `data_app.yaml` in its own directory
- under `data_apps/` (see `README.md` in this directory for the layout); the columns' schemas are in
+ under `data_apps/`; the columns' schemas are in
[[metabase-enterprise.data-apps.schema]]:
slug: sales # the slug the app is served at (column `name`)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/core.clj b/enterprise/backend/src/metabase_enterprise/data_apps/core.clj
index 87fab58d44a..0a62d4e106d 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/core.clj
@@ -14,8 +14,8 @@
warnings])
(defenterprise check-data-app-access!
- "Checks that the user belongs to one of the assigned groups"
+ "Checks that the user can read the app: a member of an assigned group while it is enabled, or a superuser."
:feature :data-apps
[request]
- (let [app (api/check-404 (data-apps.db/enabled-data-app-by-slug (get-in request [:route-params :name])))]
+ (let [app (api/check-404 (data-apps.db/data-app-by-slug (get-in request [:route-params :name])))]
(api/check-403 (mi/can-read? app))))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/csp.clj b/enterprise/backend/src/metabase_enterprise/data_apps/csp.clj
index ec8f7de99c1..6840e7a29c3 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/csp.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/csp.clj
@@ -7,10 +7,9 @@
[metabase.premium-features.core :refer [defenterprise]]))
(defenterprise data-app-connect-src-hosts
- "Origins the enabled data app with `slug` may reach (its `allowed_hosts`), or
- `[]` when there is no such enabled app. Drives the data-app iframe document's
- CSP `connect-src` so the sandboxed bundle can fetch/XHR those origins."
+ "Origins the data app with `slug` may reach (its `allowed_hosts`), or `[]` when there is no such app. Drives the
+ data-app iframe document's CSP `connect-src` so the sandboxed bundle can fetch/XHR those origins."
:feature :data-apps
[slug]
- (or (:allowed_hosts (data-apps.db/enabled-data-app-by-slug slug))
+ (or (:allowed_hosts (data-apps.db/data-app-by-slug slug))
[]))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
index 5c96e66a020..be9bb3986b4 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
@@ -4,7 +4,6 @@
(:require
[metabase-enterprise.data-apps.schema :as data-apps.schema]
[metabase.lib.schema.id :as lib.schema.id]
- [metabase.queries.core :as queries]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
@@ -28,11 +27,6 @@
[slug :- :string]
(t2/select-one non-blob-model :name slug))
-(mu/defn enabled-data-app-by-slug
- "The enabled DataApp named `slug` without its bundle, or nil."
- [slug :- :string]
- (t2/select-one non-blob-model :name slug :enabled true))
-
(mu/defn data-apps
"Every DataApp without its bundle, ordered by display name; only the enabled ones when `available?`."
[available? :- [:maybe :boolean]]
@@ -71,11 +65,6 @@
[collection-id :- pos-int?]
(t2/exists? :model/DataApp :resource_collection_id collection-id))
-(mu/defn insert-data-app!
- "Insert the DataApp `row`."
- [row :- ::data-apps.schema/data-app.update]
- (t2/insert! :model/DataApp row))
-
(mu/defn insert-data-app-returning-pk! :- ms/PositiveInt
"Insert the DataApp `row`, returning its ID."
[row :- ::data-apps.schema/data-app.update]
@@ -177,25 +166,6 @@
:object [:in paths]
:group_id [:not= group-id]))
-(defn table
- "The active Table with `table-id`, or nil. The typed schema lists only active tables."
- [table-id]
- (t2/select-one :model/Table :id table-id :active true {:from [(warehouse-schema-overlay/table-query)]}))
-
-(defn destination-database-ids
- "The IDs among `database-ids` of the databases that are routing destinations."
- [database-ids]
- (if (seq database-ids)
- (t2/select-fn-set :id :model/Database :id [:in database-ids] :router_database_id [:not= nil])
- #{}))
-
-(defn cards-by-ids
- "The Cards with `card-ids`."
- [card-ids]
- (if (seq card-ids)
- (t2/select :model/Card :id [:in card-ids])
- []))
-
(defn app-assignments
"Assignments for the requested apps."
[app-ids]
@@ -227,59 +197,6 @@
{:where [:or [:in :table_id table-ids] [:= :table_id nil]]})
[]))
-(mu/defn metric-cards-in-collections
- "The non-archived metric Cards in `collection-ids`, in name then id order."
- [collection-ids :- [:set ::lib.schema.id/collection]]
- (if (seq collection-ids)
- (t2/select :model/Card {:where [:and
- [:= :type "metric"]
- [:= :archived false]
- [:in :collection_id collection-ids]]
- :order-by [[:name :asc] [:id :asc]]})
- []))
-
-(mu/defn field-ids-and-table-ids
- "The id and Table id of the Fields with `field-ids`."
- [field-ids :- [:set ::lib.schema.id/field]]
- (t2/select [:model/Field :id :table_id] :id [:in field-ids] {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
-
-(mu/defn card-dimensions
- "The query-relevant columns (`:dimensions` and `:dimension_mappings` among them) of the Card with `card-id`,
- or nil."
- [card-id :- ::lib.schema.id/card]
- (queries/card-query-info card-id))
-
-(mu/defn table-names
- "The id, database id, name, and display name of the Tables with `table-ids`."
- [table-ids :- [:sequential ::lib.schema.id/table]]
- (t2/select [:model/Table :id :db_id :name :display_name] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))
-
-(mu/defn model-less-query-action-ids
- "The ids of the unarchived query Actions without a model outside `excluded-collection-ids`, in name then id
- order."
- [excluded-collection-ids :- [:set ::lib.schema.id/collection]]
- (t2/select-pks-vec :model/Action
- {:where [:and
- [:= :model_id nil]
- [:= :type "query"]
- [:= :archived false]
- [:or
- [:= :collection_id nil]
- [:exists ^:allow-subquery {:select [1]
- :from [[(t2/table-name :model/Collection) :c]]
- :where [:and
- [:= :c.id :action.collection_id]
- [:= :c.archived false]
- [:= :c.namespace "data-actions"]]}]]
- (when (seq excluded-collection-ids)
- [:or [:= :collection_id nil] [:not-in :collection_id excluded-collection-ids]])]
- :order-by [[:name :asc] [:id :asc]]}))
-
-(mu/defn field-table-id
- "The Table id of the Field with `field-id`, or nil."
- [field-id :- ::lib.schema.id/field]
- (t2/select-one-fn :table_id :model/Field :id field-id {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))
-
(mu/defn active-tables
"The active Tables with `table-ids`, in name then id order."
[table-ids :- [:set ::lib.schema.id/table]]
@@ -290,34 +207,14 @@
:order-by [[:name :asc] [:id :asc]]})
[]))
-(mu/defn published-tables-in-collections
- "The active, published Tables in `collection-ids`, in name then id order."
- [collection-ids :- [:set ::lib.schema.id/collection]]
- (if (seq collection-ids)
- (t2/select :model/Table
- {:from [(warehouse-schema-overlay/table-query)]
- :where [:and
- [:= :active true]
- [:= :is_published true]
- [:in :collection_id collection-ids]]
- :order-by [[:name :asc] [:id :asc]]})
- []))
-
-(mu/defn measure-definition
- "The definition of the Measure with `measure-id`, or nil."
- [measure-id :- ::lib.schema.id/measure]
- (t2/select-one-fn :definition :model/Measure :id measure-id))
-
-(mu/defn measure-definitions
- "The id and definition of the Measures with `measure-ids`."
- [measure-ids :- [:sequential ::lib.schema.id/measure]]
- (t2/select [:model/Measure :id :definition] :id [:in measure-ids]))
-
-(mu/defn unarchived-collections
- "The unarchived Collections among `collection-ids`, with their Library types."
- [collection-ids :- [:set ::lib.schema.id/collection]]
- (if (seq collection-ids)
- (t2/select [:model/Collection :id :type]
- :id [:in collection-ids]
- :archived false)
- []))
+(mu/defn resource-cards
+ "The unarchived Cards in the collection with `collection-id`, in name then id order."
+ [collection-id :- ::lib.schema.id/collection]
+ (t2/select :model/Card {:where [:and [:= :collection_id collection-id] [:= :archived false]]
+ :order-by [[:name :asc] [:id :asc]]}))
+
+(mu/defn resource-action-ids
+ "The IDs of the unarchived Actions in the collection with `collection-id`, in name then id order."
+ [collection-id :- ::lib.schema.id/collection]
+ (t2/select-pks-vec :model/Action {:where [:and [:= :collection_id collection-id] [:= :archived false]]
+ :order-by [[:name :asc] [:id :asc]]}))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate.clj
deleted file mode 100644
index aad3e31ba84..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate.clj
+++ /dev/null
@@ -1,24 +0,0 @@
-(ns metabase-enterprise.data-apps.generate
- "What a data app's author writes into the app's repository, generated by Metabase: a new app's files, the schema its
- definitions are written against, and the files of its collection those definitions build."
- (:require
- [metabase-enterprise.data-apps.generate.app :as generate.app]
- [metabase-enterprise.data-apps.generate.resources :as generate.resources]
- [metabase-enterprise.data-apps.generate.schemas :as generate.schemas]))
-
-(set! *warn-on-reflection* true)
-
-(defn app
- "A new data app's `data_app.yaml` and collection file, from its `request`."
- [request]
- (generate.app/generate request))
-
-(defn schemas
- "The data app schema as a TypeScript module."
- []
- (generate.schemas/generate))
-
-(defn resources
- "The files of a data app's collection that its `queries` and `actions` definitions build."
- [definitions]
- (generate.resources/generate definitions))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj
deleted file mode 100644
index 65b91f2f53d..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/app.clj
+++ /dev/null
@@ -1,60 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.app
- "A new data app's files: its `data_app.yaml` and its collection's YAML, each at the path a remote-sync export
- writes it."
- (:require
- [clojure.string :as str]
- [medley.core :as m]
- [metabase-enterprise.data-apps.config :as data-app.config]
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
- [metabase-enterprise.serialization.core :as serialization]
- [metabase.collections.core :as collections]
- [metabase.util :as u]
- [metabase.util.i18n :refer [tru]]
- [metabase.util.malli :as mu]
- [metabase.util.malli.registry :as mr]))
-
-(set! *warn-on-reflection* true)
-
-(def ^:private bundle-path
- "Where the app template builds its bundle, relative to the app's directory."
- "dist/index.js")
-
-(defn- name->slug
- "The slug `app-name` gives an app: its letters and numbers, lowercased, separated by single dashes."
- [app-name]
- (-> (u/lower-case-en app-name)
- (str/replace #"[^a-z0-9]+" "-")
- (str/replace #"^-+|-+$" "")))
-
-(defn- file
- "The `entity`'s YAML file at the path a remote-sync export writes it, resolving collection paths with `collections`."
- [entity collections]
- {:path (serialization/entity-file-path {:collections collections :unique-name-fns (atom {})} entity)
- :yaml (serialization/entity-yaml entity)})
-
-(mu/defn generate :- ::data-apps.schema/app-files
- "The `data_app.yaml` of an app named `name`, served at `slug` (from the name by default), and its collection's file,
- with the entity IDs `entity_id` and `collection_id` of an existing app, or new ones, and the YAML an export writes
- for them once a pull saves them."
- [{app-name :name :keys [slug description entity_id collection_id]} :- ::data-apps.schema/app-request]
- (let [slug (or slug (name->slug app-name))
- _ (when-not (mr/validate ::data-apps.schema/slug slug)
- (throw (ex-info (tru "Pass a slug: \"{0}\" is not a valid one." slug) {:status-code 400})))
- app-id (or entity_id (u/generate-nano-id))
- collection-id (or collection_id (u/generate-nano-id))
- collection {:name (str "Data App: " slug)
- :namespace "data-apps"
- :entity_id collection-id
- :slug (collections/slugify (str "Data App: " slug))
- :serdes/meta [{:model "Collection" :id collection-id
- :label (serialization/slugify-name (str "Data App: " slug))}]}
- app (m/assoc-some {:version data-app.config/supported-app-version
- :name app-name
- :slug slug
- :path bundle-path
- :collection collection-id
- :entity_id app-id
- :serdes/meta [{:model "DataApp" :id app-id :label slug}]}
- :description description)]
- {:files [(file app nil)
- (file collection {collection-id [{:label (:name collection) :key collection-id}]})]}))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/resources.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/resources.clj
deleted file mode 100644
index 05f32334a62..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/resources.clj
+++ /dev/null
@@ -1,261 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.resources
- "The files of a data app's collection: the saved question each `defineQuery` definition builds, and the copies of the
- actions the app runs and of the metrics its queries aggregate, each named and written as a remote-sync export writes
- it in the collection's folder.
-
- The endpoint is for superusers, who write an app's repository, so nothing here is checked against the caller.
- Opening it to anyone else needs that put back: every source, and every table a query reads at any depth. What a
- routing destination backs is left out, as the typed schema leaves it out."
- (:require
- [clojure.string :as str]
- [clojure.walk :as walk]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.query-definition :as query-definition]
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
- [metabase-enterprise.serialization.core :as serialization]
- [metabase.actions.core :as actions]
- [metabase.api.common :as api]
- [metabase.lib-be.core :as lib-be]
- [metabase.lib.core :as lib]
- [metabase.lib.schema :as lib.schema]
- [metabase.models.serialization :as serdes]
- [metabase.queries.core :as queries]
- [metabase.util.i18n :refer [tru]]
- [metabase.util.log :as log]
- [metabase.util.malli :as mu]
- [metabase.util.malli.registry :as mr]
- [metabase.util.malli.schema :as ms])
- (:import
- (java.nio.charset StandardCharsets)
- (java.security MessageDigest)
- (java.util Base64)))
-
-(set! *warn-on-reflection* true)
-
-(defn- fail
- "Throws the refusal of an item: a reason the author has to act on, which is not a failure of the serialization."
- [message]
- (throw (ex-info message {::refusal true})))
-
-(defn- with-item-error
- "Calls `thunk`, or answers the `:error` that stopped it, so one item that can't be serialized doesn't fail the
- rest. Anything other than a refusal is logged, so a bug in the serialization leaves a trace on the server."
- [item thunk]
- (try
- (thunk)
- (catch Exception e
- (when-not (::refusal (ex-data e))
- (log/warn e "Could not serialize a data app resource" item))
- {:error (or (ex-message e) (tru "Could not serialize it."))})))
-
-(defn- extract-by-entity-id
- "The entities of `model-name` with `ids`, as serialization extracts them, keyed by entity ID: one extraction for
- them all. Serialization leaves out what it can't extract, and goes on past one it fails on, so an entity missing
- from the result couldn't be extracted; [[extraction-error]] says why."
- [model-name ids]
- (if (seq ids)
- (into {}
- (comp (filter map?)
- (map (juxt (comp :id last :serdes/meta) identity)))
- (serdes/extract-all model-name {:filter-column :id :filter-ids (vec ids) :continue-on-error true}))
- {}))
-
-(defn- extraction-error
- "Why serialization can't extract the `model-name` entity with `id`, or nil when it leaves the entity out without an
- error: the one entity is extracted on its own, with the error let through. The reason is the innermost cause."
- [model-name id]
- (try
- (run! identity (serdes/extract-all model-name {:filter-column :id :filter-ids [id]}))
- nil
- (catch Exception e
- (ex-message (last (take-while some? (iterate ex-cause e)))))))
-
-(defn- cards-read
- "The IDs, as a comma-separated string, of the cards the `model-name` entity `source` references everywhere
- serialization looks, or nil when it references none. A data app's resources can't hold them, so a copy
- referencing one would fail to load."
- [model-name {:keys [parameters] :as source}]
- (some->> (concat (serdes/serialization-dependencies model-name source)
- (serdes/parameters-deps true parameters))
- (keep (fn [path]
- (let [{:keys [model id]} (last path)]
- (when (= "Card" model)
- id))))
- distinct
- not-empty
- sort
- (str/join ", ")))
-
-(defn- check-copyable
- "Throws when the `model-name` entity `source`, labelled `label` and extracted as `serialized`, can't be copied into a
- data app's resources: it's archived, a routing destination backs it, it references a card, or serialization
- couldn't extract it."
- [label model-name {:keys [archived database_id] :as source} serialized]
- (when archived
- (fail (tru "{0} is archived." label)))
- (when (seq (data-apps.db/destination-database-ids #{database_id}))
- (fail (tru "{0} is backed by a routing destination database." label)))
- (when-let [card-ids (cards-read model-name source)]
- (fail (tru "{0} reads card {1}, which a data app''s resources can''t hold." label card-ids)))
- (when-not serialized
- (fail (if-let [cause (extraction-error model-name (:id source))]
- (tru "Could not serialize {0}: {1}" label cause)
- (tru "Could not serialize {0}." label)))))
-
-(defn- check-unique-column-names
- "Throws naming the breakouts and aggregations of `query` whose result columns share a name."
- [query]
- (let [columns (concat (for [breakout (lib/breakouts query)
- :let [column (lib/breakout-column query breakout)]]
- {:name (:name column)
- :clause column})
- (for [aggregation (lib/aggregations query)]
- {:name (:name (lib/aggregation-column query aggregation))
- :clause aggregation}))
- conflicts (for [[column-name same-name] (group-by :name columns)
- :when (> (count same-name) 1)]
- (tru "{0} share the column name \"{1}\""
- (str/join ", " (map #(lib/display-name query (:clause %)) same-name))
- column-name))]
- (when (seq conflicts)
- (fail (tru "Breakouts and aggregations need unique column names: {0}. Name them apart with the `name` option of `breakout` or of an aggregation helper, or with `aggregations.measure` or `aggregations.metric` for a measure or metric."
- (str/join "; " conflicts))))))
-
-(mu/defn- build-query :- ::lib.schema/query
- "The query Lib builds from `query-definition`, once its source table and every table it reads at any depth exist."
- [{[{{table-id :id} :source}] :stages, :as query-definition} :- ::query-definition/query-definition]
- (let [table (data-apps.db/table table-id)]
- (when-not table
- (fail (tru "Table {0} does not exist." (str table-id))))
- (let [mp (lib-be/application-database-metadata-provider (:db_id table))
- _ (check-unique-column-names (lib/test-query mp (update-in query-definition [:stages 0] dissoc :order-bys)))
- query (lib/test-query mp query-definition)]
- (when-not (mr/validate ::lib.schema/query query)
- (fail (tru "The definition does not build a valid query.")))
- (doseq [read-id (sort (into (:table (lib/all-referenced-entity-ids-recursive query))
- (keep :table-id)
- (lib/returned-columns query -1 query {:include-remaps? true})))]
- (when-not (data-apps.db/table read-id)
- (fail (tru "Table {0} does not exist." (str read-id)))))
- query)))
-
-(mu/defn- build-card :- :map
- "The question named `card-name` with entity ID `entity-id`, holding `query`, as a pull saves it."
- [card-name :- ms/NonBlankString
- entity-id :- ms/NanoIdString
- query :- ::lib.schema/query]
- {:entity_id entity-id
- :name card-name
- :card_schema queries/current-card-schema-version
- :type :question
- :display :table
- :creator_id api/*current-user-id*
- :database_id (:database query)
- :dataset_query (lib/prepare-for-serialization query)
- :visualization_settings {}
- :parameters []
- :parameter_mappings []})
-
-(def ^:private public-keys
- "What makes a source public or embedded. A copy never is, and the pull refuses a file that says it is."
- [:public_uuid :made_public_by_id :enable_embedding :embedding_params :embedding_type])
-
-(defn- as-copy
- "The extracted `entity` as its copy with `entity-id` in the collection with `collection-id`."
- [entity entity-id collection-id]
- (-> (apply dissoc entity public-keys)
- (assoc :entity_id entity-id :collection_id collection-id)
- (update :serdes/meta #(assoc-in % [(dec (count %)) :id] entity-id))))
-
-(defn- copy-entity-id
- "The entity ID of the copy, in the collection with `collection-id`, of the entity with `source-entity-id`: the same
- every time, so the copy's file is the same every time."
- [collection-id source-entity-id]
- (let [digest (.digest (MessageDigest/getInstance "SHA-256")
- (.getBytes (str collection-id "/" source-entity-id) StandardCharsets/UTF_8))]
- (subs (.encodeToString (.withoutPadding (Base64/getUrlEncoder)) digest) 0 21)))
-
-(defn- metric-copies
- "The metric copies the built `queries` aggregate, as `{[collection-id card-id] {:file .. :entity-id ..}}`."
- [queries]
- (let [uses (into (sorted-set)
- (comp (remove :error)
- (mapcat (fn [{:keys [collection_id built]}]
- (map #(vector collection_id %) (lib/all-source-card-ids built)))))
- queries)
- cards (into {} (map (juxt :id identity)) (data-apps.db/cards-by-ids (into #{} (map second) uses)))
- serialized (extract-by-entity-id "Card" (keys cards))]
- (into (sorted-map)
- (for [[collection-id card-id :as use] uses
- :let [card (cards card-id)]]
- [use {:card card
- :entity-id (some->> card :entity_id (copy-entity-id collection-id))
- :copy (with-item-error
- {:metric card-id}
- (fn []
- (let [label (tru "Metric {0}" (str card-id))]
- (when-not (and card (= :metric (keyword (:type card))))
- (fail (tru "{0} does not exist." label)))
- (check-copyable label "Card" card (serialized (:entity_id card)))
- (as-copy (serialized (:entity_id card))
- (copy-entity-id collection-id (:entity_id card))
- collection-id))))}]))))
-
-(defn- read-metric-copies
- "The extracted question `entity` reading the copies of the metrics it aggregates, in its collection."
- [entity collection-id copies]
- (let [copy-ids (into {}
- (keep (fn [[[copy-collection-id _] {:keys [card entity-id copy]}]]
- (when (and (= copy-collection-id collection-id) (not (:error copy)))
- [(:entity_id card) entity-id])))
- copies)]
- (walk/postwalk #(get copy-ids % %) entity)))
-
-(mu/defn generate :- [:map {:closed true}
- [:queries [:sequential ::data-apps.schema/file]]
- [:actions [:sequential ::data-apps.schema/file]]
- [:metrics [:sequential ::data-apps.schema/file]]]
- "The files of a data app's collection: the saved question of each of `queries`, the copy of each of `actions`, and
- the copies of the metrics the queries aggregate, each named as serialization names it in the collection's folder.
- Each item comes back on its own, with its file or the error that stops it."
- [{:keys [queries actions]} :- [:map {:closed true}
- [:queries {:optional true} [:maybe [:sequential ::data-apps.schema/query]]]
- [:actions {:optional true} [:maybe [:sequential ::data-apps.schema/action]]]]]
- (serdes/with-cache
- (let [unique-name (lib/non-truncating-unique-name-generator)
- ->file (fn [entity]
- {:file (str (unique-name (:entity_id entity) (serialization/slugify-name (:name entity))) ".yaml")
- :yaml (serialization/entity-yaml entity)})
- built (mapv (fn [{:keys [query] :as item}]
- (merge item (with-item-error {:query (:entity_id item)} #(hash-map :built (build-query query)))))
- queries)
- copies (metric-copies built)
- actions-by-id (into {} (map (juxt :id identity))
- (actions/select-actions-for-ids nil (mapv :action_id actions)))
- serialized (extract-by-entity-id "Action" (keys actions-by-id))]
- {:queries (mapv (fn [{:keys [name entity_id collection_id built error]}]
- (if error
- {:error error}
- (with-item-error {:query entity_id}
- (fn []
- (-> (serdes/extract-one "Card" {} (build-card name entity_id built))
- (assoc :collection_id collection_id)
- (read-metric-copies collection_id copies)
- ->file)))))
- built)
- :actions (mapv (fn [{:keys [action_id entity_id collection_id] :as item}]
- (with-item-error item
- (fn []
- (let [label (tru "Action {0}" (str action_id))
- action (actions-by-id action_id)]
- (when-not action
- (fail (tru "{0} does not exist." label)))
- (when (:model_id action)
- (fail (tru "{0} belongs to a model. A data app runs query actions that belong to no model."
- label)))
- (check-copyable label "Action" action (serialized (:entity_id action)))
- (->file (as-copy (serialized (:entity_id action)) entity_id collection_id))))))
- actions)
- :metrics (mapv (fn [{:keys [copy]}]
- (if (:error copy) copy (->file copy)))
- (vals copies))})))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas.clj
deleted file mode 100644
index fc602a9ac4c..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas.clj
+++ /dev/null
@@ -1,69 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas
- "A data app's schema: the TypeScript module describing the root libraries' tables and metrics and the query actions
- that belong to no model, for the `Lib.createTestQuery` DSL; each entry's `/* metadata: {...} */` block carries
- context for humans and agents.
-
- (-> (fetch-items) create-schema render-typescript)
-
- [[fetch-items]] is the only impure stage, and reads through
- [[metabase-enterprise.data-apps.generate.schemas.source/SchemaSource]]. Which keys render as runtime data vs
- metadata is policy in `metabase-enterprise.data-apps.generate.schemas.render`; TypeScript syntax lives only in the
- `metabase-enterprise.data-apps.generate.schemas.javascript` printer. The table and metric details lookups
- read-check the current user, so callers outside a request must bind a current-user context first."
- (:require
- [clojure.set :as set]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [metabase-enterprise.data-apps.generate.schemas.javascript :as schemas.javascript]
- [metabase-enterprise.data-apps.generate.schemas.render :as schemas.render]
- [metabase-enterprise.data-apps.generate.schemas.source :as schemas.source]
- [metabase.system.core :as system])
- (:import
- (java.time Instant)))
-
-(set! *warn-on-reflection* true)
-
-(def Items
- "Fetched schema entities, ready for pure assembly by [[create-schema]]."
- [:map {:closed true}
- [:actions [:sequential :map]]
- [:tables [:sequential :map]]
- [:metrics [:sequential :map]]])
-
-(defn fetch-items
- "Fetches Library metrics, published tables and metric backing tables, and eligible query actions without a model."
- ([]
- (fetch-items schemas.source/app-db-source))
- ([source]
- (let [{:keys [data-collection-ids metric-collection-ids]} (schemas.source/library-scope source)
- metrics (schemas.source/metrics source metric-collection-ids)
- table-ids (set/union (into #{} (map :id) (schemas.source/library-tables source data-collection-ids))
- (into #{} (mapcat :mappedTableIds) metrics))]
- {:actions (vec (schemas.source/actions source))
- :tables (vec (schemas.source/tables source table-ids))
- :metrics (vec metrics)})))
-
-(defn create-schema
- "Assembles fetched [[Items]] into the schema value; `info` optionally pins `:generated-at` and `:instance-url`,
- which default to the current time and the configured site URL."
- ([items]
- (create-schema items nil))
- ([{:keys [actions tables metrics]} {:keys [generated-at instance-url]}]
- (array-map
- :schemaVersion 2
- :generatedAt (str (or generated-at (Instant/now)))
- :metabase {:instanceUrl (or instance-url (system/site-url))}
- :actions (schemas.common/keyed-map actions)
- :tables (schemas.common/keyed-map tables)
- :metrics (schemas.common/keyed-map metrics))))
-
-(defn render-typescript
- "Renders a schema value as an ES module of `as const` TypeScript constants."
- [schema]
- (-> schema
- schemas.render/schema->ast
- schemas.javascript/render-js))
-
-(defn generate
- "The data app's schema as a TypeScript module."
- []
- (-> (fetch-items) create-schema render-typescript))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/action.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/action.clj
deleted file mode 100644
index 6c61ff17ecf..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/action.clj
+++ /dev/null
@@ -1,91 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.action
- "The schema of the query actions that belong to no model."
- (:require
- [medley.core :as m]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [metabase.actions.core :as actions]
- [metabase.lib.core :as lib]
- [metabase.lib.schema.common :as lib.schema.common]
- [metabase.permissions.core :as perms]
- [metabase.util :as u]))
-
-(set! *warn-on-reflection* true)
-
-(defn- param-type->js-type
- "Returns the JS type for a Metabase action parameter type."
- [param-type]
- (when ((some-fn keyword? string?) param-type)
- (let [param-type-keyword (lib.schema.common/normalize-keyword param-type)
- prefix (or (namespace param-type-keyword) (name param-type-keyword))]
- (case prefix
- ("number" "numeric") "number"
- ("text" "string") "string"
- "date" "Date"
- "boolean" "boolean"
- nil))))
-
-(defn- template-tag-value-type
- "Returns the type a template tag's values carry: a field filter's `:type` is always `:dimension`, so its
- `:widget-type` (e.g. `:date/single`) is what tells us the value type."
- [{:keys [type widget-type]}]
- (if (= type :dimension)
- widget-type
- type))
-
-(defn- template-tag-types
- "Returns template-tag value types keyed by tag name, or nil when the action has no usable query."
- [{:keys [dataset_query]}]
- ;; a stored query that failed to deserialize comes back as {}, which Lib rejects
- (some-> dataset_query not-empty lib/all-template-tags-map (update-vals template-tag-value-type)))
-
-(defn- parameter-schema
- "Returns the schema for an action execute parameter."
- [tag-types {:keys [id slug name display-name type target required]}]
- (let [resolved-slug (or slug
- (some-> id clojure.core/name)
- (some-> name u/slugify))
- resolved-type (or (param-type->js-type type)
- (param-type->js-type
- (get tag-types (some-> target lib/parameter-target-template-tag-name)))
- "unknown")]
- (m/assoc-some
- {:slug resolved-slug
- :displayName (or display-name name resolved-slug)
- :jsType resolved-type}
- :required (when required true))))
-
-(defn- hidden-parameter-ids
- "The ids of the parameters the action's form hides; execute refuses a value for them."
- [{:keys [visualization_settings]}]
- (into #{} (keep #(when (:hidden %) (:id %))) (vals (:fields visualization_settings))))
-
-(defn action-schema
- "Returns the typed schema entry for a query action, leaving out the parameters its form hides."
- [{:keys [id name description parameters entity_id] :as action}]
- (let [tag-types (template-tag-types action)
- hidden? (hidden-parameter-ids action)]
- (m/assoc-some
- {:kind "action"
- :key (schemas.common/generated-key name id)
- :id id
- :name name
- :type "query"
- :parameters (into [] (comp (remove (comp hidden? :id))
- (map #(parameter-schema tag-types %)))
- parameters)}
- :description description
- :entityId entity_id)))
-
-(defn action-schemas
- "Returns schemas for standalone actions, excluding copies owned by data app and
- those a destination backs."
- []
- (let [ids (data-apps.db/model-less-query-action-ids (set (perms/data-app-collection-ids)))
- details-by-id (when (seq ids) (u/index-by :id (actions/select-actions-for-ids nil ids)))
- details (keep details-by-id ids)
- destination-ids (data-apps.db/destination-database-ids (into #{} (keep :database_id) details))]
- (into []
- (comp (remove #(contains? destination-ids (:database_id %)))
- (map action-schema))
- details)))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/common.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/common.clj
deleted file mode 100644
index fbef3e09c27..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/common.clj
+++ /dev/null
@@ -1,159 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.common
- "Shared helpers for generating a data app's schema."
- (:require
- [clojure.string :as str]
- [medley.core :as m]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase.audit-app.core :as audit]
- [metabase.lib-be.core :as lib-be]
- [metabase.lib.core :as lib]
- [metabase.metabot.core :as metabot]
- [metabase.premium-features.core :as premium-features]
- [metabase.types.core]
- [metabase.util :as u]))
-
-(set! *warn-on-reflection* true)
-
-(comment metabase.types.core/keep-me)
-
-;; Action parameters in generated schemas use these primitive types.
-(def ^:private primitive-type->js-type
- {:number "number"
- :boolean "boolean"
- :string "string"
- :date "Date"
- :datetime "Date"
- :time "Date"})
-
-(defn- type-keyword
- [schema-type]
- (cond
- (keyword? schema-type) schema-type
- (string? schema-type) (let [schema-type (str/replace schema-type #"^:" "")]
- (if (str/includes? schema-type "/")
- (keyword schema-type)
- (keyword "type" schema-type)))))
-
-;; Result columns and fields use Metabase base/effective types. For example,
-;; `:type/Integer` maps to "number", and `:type/UUID` maps to "string".
-(defn- schema-type->js-type
- [schema-type]
- (let [schema-type (type-keyword schema-type)]
- (cond
- (nil? schema-type) "unknown"
- (isa? schema-type :type/Boolean) "boolean"
- (isa? schema-type :type/Number) "number"
- (isa? schema-type :type/Temporal) "Date"
- (isa? schema-type :type/Text) "string"
- (isa? schema-type :type/TextLike) "string"
- :else "unknown")))
-
-(defn- js-type
- [{:keys [type base_type effective_type] :as _column}]
- (or (get primitive-type->js-type type)
- (schema-type->js-type (or effective_type base_type))))
-
-(defn column-schema
- "Returns the typed-schema representation for a result column or field-like map."
- [{:keys [name display_name base_type effective_type semantic_type description unit] :as column}]
- (let [effective-type (or effective_type base_type)]
- (m/assoc-some
- {:type "column"
- :name name
- :displayName (or display_name name)
- :jsType (js-type column)}
- :baseType base_type
- :effectiveType (when (not= effective-type base_type) effective-type)
- :semanticType semantic_type
- :description description
- :unit unit)))
-
-(defn generated-key
- "Returns a stable JavaScript object key for an entity name and id."
- [entity-name id]
- (let [generated-key (some-> entity-name u/->camelCaseEn)]
- (if (str/blank? generated-key)
- (str "entity" id)
- generated-key)))
-
-(defn pascal-case
- "Capitalizes the first character of string without changing the rest."
- [string]
- (when-not (str/blank? string)
- (str (u/upper-case-en (subs string 0 1))
- (subs string 1))))
-
-(defn- duplicate-key?
- [key->count key]
- (> (get key->count key 0) 1))
-
-(defn- keyed-map-candidate-key
- [key->count {:keys [key id]
- key-disambiguator :keyDisambiguator
- table-id :tableId}]
- (cond-> key
- (duplicate-key? key->count key) (str (or key-disambiguator table-id id))))
-
-(defn- unique-keys
- "Returns `ks` with every key that is still shared by several `entities` suffixed with its entity id until all are
- unique, so no entity overwrites another."
- [entities ks]
- (let [key->count (frequencies ks)]
- (if (every? #(= 1 %) (vals key->count))
- ks
- (recur entities (mapv (fn [entity k]
- (cond-> k (duplicate-key? key->count k) (str "_" (:id entity))))
- entities ks)))))
-
-(defn keyed-map
- "Returns a sorted map keyed by each entity key, disambiguating duplicate keys."
- [entities]
- (let [entities (vec entities)
- base-key->count (frequencies (map :key entities))
- candidate-keys (mapv (partial keyed-map-candidate-key base-key->count) entities)
- candidate->count (frequencies candidate-keys)
- ks (unique-keys entities
- (mapv (fn [entity candidate-key]
- (cond-> candidate-key
- (duplicate-key? candidate->count candidate-key) (str (:id entity))))
- entities candidate-keys))]
- (into (sorted-map)
- (map (fn [entity key]
- [key (-> entity
- (dissoc :keyDisambiguator)
- (assoc :key key))])
- entities ks))))
-
-(defn without-unavailable-cards
- "`cards` without any backed by a routing destination database, and, while the audit feature is off, without the
- audit database's and the audit collection's: the card details lookup refuses one in the audit collection, and one
- on the audit database would be listed without its table."
- [cards]
- (let [destination-ids (data-apps.db/destination-database-ids (into #{} (keep :database_id) cards))
- audit-off? (not (premium-features/enable-audit-app?))]
- (cond->> cards
- (seq destination-ids) (remove #(contains? destination-ids (:database_id %)))
- audit-off? (remove #(or (= audit/audit-db-id (:database_id %))
- (some-> (:collection_id %) audit/is-collection-id-audit?))))))
-
-(defn aggregation-result-column-with-metadata-provider
- "Returns an aggregation result column using an existing metadata provider."
- [metadata-provider query-definition]
- (try
- (let [query (lib/query metadata-provider query-definition)
- aggregation-column (m/find-first #(= (:lib/source %) :source/aggregations)
- (lib/returned-columns query))]
- (when aggregation-column
- (metabot/->result-column query aggregation-column)))
- (catch Exception _
- nil)))
-
-(defn aggregation-result-column
- "Returns the first aggregation result column for a saved query definition, for metrics and measures."
- [database-id query-definition]
- (try
- (aggregation-result-column-with-metadata-provider
- (lib-be/application-database-metadata-provider database-id)
- query-definition)
- (catch Exception _
- nil)))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/javascript.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/javascript.clj
deleted file mode 100644
index 66ba8a3e37f..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/javascript.clj
+++ /dev/null
@@ -1,258 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.javascript
- "A small TypeScript AST encoded as tagged vectors, and a printer for it.
-
- The AST covers exactly the subset of TypeScript that typed-schema modules
- emit. Expressions:
-
- [:lit 42] ; any JSON-encodable literal
- [:ref \"tables\" \"orders\" :fields] ; property path => tables.orders.fields
- [:call \"pickFields\" expr ...] ; call expression, args printed inline
- [:arr expr-or-item ...] ; array literal
- [:obj entry ...] ; object literal
-
- Object entries are `[key expr]` or `[key {:metadata {...}} obj]`; array
- items are bare expressions or `[:item {:metadata {...}} obj]`, where `obj`
- is an `[:obj ...]`. Metadata is a JSON-encodable map, printed as a
- `/* metadata: {...} */` block first inside the entry's object, before its
- properties, so a reader meets it before the data it describes and it never
- reaches runtime code. Statements:
-
- [:const \"tables\" expr] ; const tables = <expr> as const;
- [:raw \"function helper() {...}\"] ; verbatim TypeScript
- [:export-default expr] ; export default <expr>;
- [:module statement ...] ; one printed file
-
- For example:
-
- [:module
- [:const \"tables\"
- [:obj [\"orders\" {:metadata {\"entityId\" \"abc123\"}}
- [:obj [\"type\" [:lit \"table\"]]
- [\"ids\" [:arr [:lit 1] [:lit 2]]]]]]]
- [:export-default [:ref \"tables\"]]]
-
- prints as:
-
- const tables = {
- orders: {
- /* metadata: { \"entityId\": \"abc123\" } */
- type: \"table\",
- ids: [ 1, 2 ]
- }
- } as const;
-
- export default tables;
-
- Rendering rules: arrays whose items are all literals print on one line;
- `:call` arguments always print inline; object keys print bare when they are
- valid JavaScript identifiers and quoted otherwise. A metadata block whose
- values are all scalars prints on one line, so a field costs a reader one line
- of the file; one with a nested array or object prints as
- `JSON.stringify(x, null, 2)` would, one array item per line. [[render-js]]
- prints a `:module`; [[Module]] is the Malli schema for the whole grammar.
-
- The printer is deliberately option-free and policy-free: decisions about
- *what* to emit (runtime keys, metadata, compaction) belong in
- `metabase-enterprise.data-apps.generate.schemas.render`, which builds this AST. If the output needs
- new syntax, add a node type and its printer here rather than concatenating
- TypeScript strings elsewhere."
- (:require
- [clojure.string :as str]
- [metabase.util :as u]
- [metabase.util.json :as json]))
-
-(set! *warn-on-reflection* true)
-
-(def ^:private expression-tags
- #{:lit :ref :call :arr :obj})
-
-(def ^:private key-schema
- [:or :string :keyword])
-
-(def Module
- "Malli schema for the TypeScript AST accepted by [[render-js]]."
- [:schema {:registry
- {::entry-options [:map {:closed true}
- [:metadata {:optional true} [:map-of key-schema :any]]]
- ;; Metadata prints as a block inside the entry's object, so only an object can carry it.
- ::entry [:or
- [:tuple key-schema [:ref ::expr]]
- [:tuple key-schema [:ref ::entry-options] [:ref ::obj]]]
- ::item [:tuple [:= :item] [:ref ::entry-options] [:ref ::obj]]
- ::obj [:cat [:= :obj] [:* [:schema [:ref ::entry]]]]
- ::expr [:multi {:dispatch first}
- [:lit [:tuple [:= :lit] :any]]
- [:ref [:cat [:= :ref] [:+ key-schema]]]
- [:call [:cat [:= :call] :string [:* [:schema [:ref ::expr]]]]]
- [:arr [:cat [:= :arr] [:* [:or
- [:schema [:ref ::item]]
- [:schema [:ref ::expr]]]]]]
- [:obj [:ref ::obj]]]
- ::statement [:multi {:dispatch first}
- [:const [:tuple [:= :const] key-schema [:ref ::expr]]]
- [:raw [:tuple [:= :raw] :string]]
- [:export-default [:tuple [:= :export-default] [:ref ::expr]]]]}}
- [:cat [:= :module] [:* [:schema [:ref ::statement]]]]])
-
-(defn expression?
- "Returns true when `value` is an AST expression node."
- [value]
- (and (vector? value)
- (contains? expression-tags (first value))))
-
-(defn call?
- "Returns true when `value` is a `[:call ...]` expression node."
- [value]
- (and (vector? value)
- (= :call (first value))))
-
-(def ^:private js-identifier-pattern
- #"[A-Za-z_$][A-Za-z0-9_$]*")
-
-(defn- spaces
- [indent]
- (apply str (repeat indent " ")))
-
-(defn- javascript-key
- "Renders a value as a JavaScript object key.
-
- Valid identifiers render bare e.g. orders; everything else renders as a quoted
- property name e.g. \"orders-by-month\"."
- [entry-key]
- (let [key-name (u/qualified-name entry-key)]
- (if (re-matches js-identifier-pattern key-name)
- key-name
- (json/encode key-name))))
-
-(defn- reference-path
- "Renders `[:ref ...]` segments as a JavaScript property path."
- [path-segments]
- (str (u/qualified-name (first path-segments))
- (apply str (for [path-segment (rest path-segments)]
- (let [segment-name (u/qualified-name path-segment)]
- (if (re-matches js-identifier-pattern segment-name)
- (str "." segment-name)
- (str "[" (json/encode segment-name) "]")))))))
-
-(defn- entry-parts
- "Returns `[key entry-options expr]` for either entry form."
- [entry]
- (if (= 3 (count entry))
- entry
- [(first entry) nil (second entry)]))
-
-(defn- item-parts
- "Returns `[entry-options expr]` for either array item form."
- [item]
- (if (and (vector? item) (= :item (first item)))
- [(second item) (nth item 2)]
- [nil item]))
-
-(defn- metadata-json
- "Renders `metadata` as JSON: on one line when every value is a scalar, otherwise as `JSON.stringify(x, null, 2)`
- would, with each line after the first indented by `indent`."
- [metadata indent]
- (if (not-any? coll? (vals metadata))
- (str "{ "
- (str/join ", " (for [[k v] metadata]
- (str (json/encode (u/qualified-name k)) ": " (json/encode v))))
- " }")
- (str/replace (json/encode metadata {:pretty {:indent-arrays? true :object-field-value-separator ": "}})
- "\n" (str "\n" (spaces indent)))))
-
-(defn- metadata-block
- "Returns a `/* metadata: {...} */` block. A `*/` can only occur inside a JSON string, where it is written as the
- equivalent `*\\/`, so the block always ends where it should."
- [indent metadata]
- (str (spaces indent) "/* metadata: "
- (str/replace (metadata-json metadata indent) "*/" "*\\/")
- " */"))
-
-(declare render-expression)
-
-(defn- render-inline
- "Renders an expression on a single line, for `:call` arguments."
- [node]
- (case (first node)
- :lit (json/encode (second node))
- :ref (reference-path (rest node))
- :call (let [[_ function-name & args] node]
- (str function-name "(" (str/join ", " (map render-inline args)) ")"))
- :arr (str "[ " (str/join ", " (map render-inline (rest node))) " ]")
- :obj (str "{ "
- (str/join ", " (for [entry (rest node)
- :let [[entry-key options expr] (entry-parts entry)]]
- (do
- ;; An inline object has no room for a block, so refuse rather than drop it.
- (when (seq (:metadata options))
- (throw (ex-info "Metadata can only be attached to an object on its own lines."
- {:node node})))
- (str (javascript-key entry-key) ": " (render-inline expr)))))
- " }")))
-
-(defn- literal-node?
- [node]
- (and (vector? node) (= :lit (first node))))
-
-(defn- render-object
- [entries indent metadata]
- (let [inner-indent (+ indent 2)
- lines (for [entry entries
- :let [[entry-key options expr] (entry-parts entry)]]
- (str (spaces inner-indent)
- (javascript-key entry-key)
- ": "
- (render-expression expr inner-indent (:metadata options))))
- ;; the block comes first, so a reader meets it before the properties it describes
- body (str/join "\n" (remove str/blank? [(when (seq metadata) (metadata-block inner-indent metadata))
- (str/join ",\n" lines)]))]
- (if (str/blank? body)
- "{ }"
- (str "{\n" body "\n" (spaces indent) "}"))))
-
-(defn- render-array
- [items indent]
- (cond
- (empty? items)
- "[ ]"
-
- (every? literal-node? items)
- (str "[ " (str/join ", " (map #(json/encode (second %)) items)) " ]")
-
- :else
- (str "[\n"
- (str/join ",\n"
- (for [item items
- :let [[options expr] (item-parts item)
- item-indent (+ indent 2)]]
- (str (spaces item-indent)
- (render-expression expr item-indent (:metadata options)))))
- "\n" (spaces indent) "]")))
-
-(defn- render-expression
- "Renders `node` at `indent`. `metadata`, when present, prints inside `node`, which must then be an object."
- ([node indent]
- (render-expression node indent nil))
- ([node indent metadata]
- (when (and (seq metadata) (not= :obj (first node)))
- (throw (ex-info "Metadata can only be attached to an object." {:node node})))
- (case (first node)
- :lit (json/encode (second node))
- :ref (reference-path (rest node))
- :call (render-inline node)
- :obj (render-object (rest node) indent metadata)
- :arr (render-array (rest node) indent))))
-
-(defn- render-statement
- [statement]
- (case (first statement)
- :raw (second statement)
- :const (let [[_ const-name expr] statement]
- (str "const " (u/qualified-name const-name) " = "
- (render-expression expr 0) " as const;"))
- :export-default (str "export default " (render-expression (second statement) 0) ";")))
-
-(defn render-js
- "Renders a `[:module ...]` AST as TypeScript source."
- [[_ & statements]]
- (str (str/join "\n\n" (map render-statement statements)) "\n"))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/metric.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/metric.clj
deleted file mode 100644
index 85b6737a3a7..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/metric.clj
+++ /dev/null
@@ -1,310 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.metric
- "The schema of metrics, with their dimensions."
- (:require
- [medley.core :as m]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [metabase-enterprise.data-apps.generate.schemas.table :as schemas.table]
- [metabase.lib-be.core :as lib-be]
- [metabase.lib.core :as lib]
- [metabase.metabot.core :as metabot]
- [metabase.metrics.core :as metrics]
- [metabase.util :as u]
- [metabase.util.log :as log]))
-
-(set! *warn-on-reflection* true)
-
-(defn- metric-result-column
- "Returns the metric aggregation result column inferred by Lib."
- [card]
- (schemas.common/aggregation-result-column (:database_id card) (:dataset_query card)))
-
-(defn- metric-filters
- "Returns the display names of the filters `card`'s query applies. The schema exposes a metric only as an
- aggregation, so they are the only hint that it counts a subset of the rows."
- [card]
- (when-let [query-definition (not-empty (:dataset_query card))]
- (try
- (let [query (lib/query (lib-be/application-database-metadata-provider (:database_id card))
- query-definition)]
- (not-empty (vec (for [stage-number (range (lib/stage-count query))
- filter-clause (lib/filters query stage-number)]
- (lib/display-name query stage-number filter-clause)))))
- (catch Exception e
- (log/warnf e "Could not describe the filters of metric %s" (:id card))
- nil))))
-
-(defn- metric-details-error-message
- [card error-message]
- (format "Failed to build schema details for metric \"%s\" (card %s): %s"
- (or (:name card) "Untitled")
- (:id card)
- (or error-message "unknown error")))
-
-(defn- metric-details-error-data
- [card error-data]
- (m/assoc-some
- {:card-id (:id card)
- :card-name (:name card)
- :card-type (:type card)}
- :status-code (:status-code error-data)))
-
-(defn- metric-details
- "Returns metric details with queryable dimensions for schema generation."
- [card]
- (let [response (metabot/get-metric-details {:metric-id (:id card)
- ;; The typed schema does not use the default temporal dimension.
- :with-default-temporal-breakout? false
- :with-field-values? false
- :with-queryable-dimensions? true
- :with-segments? false})]
- (or (:structured-output response)
- (let [error-message (:output response)]
- (throw (ex-info (metric-details-error-message card error-message)
- (assoc (metric-details-error-data card response)
- :error-message error-message)))))))
-
-(defn- persisted-dimension->column
- "Converts persisted metric dimensions to column-shaped maps.
-
- Persisted metric dimensions use kebab-case field/type keys, while [[schemas.common/column-schema]] expects
- result-column-style snake_case keys."
- [dimension]
- (let [field-id (some-> dimension :sources first :field-id)]
- {:name (:name dimension)
- :display_name (:display-name dimension)
- :base_type (some-> (:effective-type dimension) u/qualified-name)
- :effective_type (some-> (:effective-type dimension) u/qualified-name)
- :semantic_type (some-> (:semantic-type dimension) u/qualified-name)
- :field_id field-id}))
-
-(defn- dimension-field-id
- "Returns the field id backing a metric dimension, including persisted dimension sources."
- [{:keys [field_id] :as dimension}]
- (or field_id (some-> dimension :sources first :field-id)))
-
-(defn- dimension-table-id
- "Returns the table id that backs a metric dimension, when known."
- ([dimension]
- (dimension-table-id dimension nil))
- ([dimension field-table-ids]
- (or (:table_id dimension)
- (:table-id dimension)
- (if field-table-ids
- (get field-table-ids (dimension-field-id dimension))
- (schemas.table/table-by-field-id (dimension-field-id dimension))))))
-
-(defn- field-table-ids
- "Returns backing table ids for dimensions that need field-based resolution."
- [dimensions]
- (let [field-ids (into #{}
- (comp (remove #(or (:table_id %) (:table-id %)))
- (keep dimension-field-id))
- dimensions)]
- (when (seq field-ids)
- (into {}
- (map (juxt :id :table_id))
- (data-apps.db/field-ids-and-table-ids field-ids)))))
-
-(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))))))
-
-(defn- mapping-source-field-id
- "Returns the mapped source field id for a metric dimension mapping."
- [{:keys [target]}]
- (when (and (vector? target)
- (= :field (first target))
- (map? (second target)))
- (let [source-field-id (:source-field (second target))]
- (when (integer? source-field-id)
- source-field-id))))
-
-(defn- enrich-dimensions-with-mappings
- "Adds mapped source field ids to persisted metric dimensions."
- [dimensions dimension-mappings]
- (let [mapping-by-dimension-id (into {} (map (juxt :dimension-id identity)) dimension-mappings)]
- (mapv (fn [{:keys [id] :as dimension}]
- (m/assoc-some dimension
- :source-field-id (some-> (get mapping-by-dimension-id id)
- mapping-source-field-id)))
- dimensions)))
-
-(defn- sync-and-fetch-metric-dimensions!
- "Syncs and returns persisted metric dimensions with mapping metadata."
- [{:keys [id]}]
- (metrics/sync-dimensions! :metadata/metric id)
- (let [{:keys [dimensions dimension_mappings]} (data-apps.db/card-dimensions id)]
- (enrich-dimensions-with-mappings dimensions dimension_mappings)))
-
-(defn- table-source-rows
- "Returns table rows for table-backed metric dimensions."
- [table-ids]
- (when (seq table-ids)
- (data-apps.db/table-names table-ids)))
-
-(defn- table-key-disambiguators
- "Returns table display keys used to disambiguate compacted metric dimensions."
- [table-rows]
- (when (seq table-rows)
- (->> table-rows
- (map (fn [{:keys [id name display_name]}]
- [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]]
- (when (and database-name table-name)
- {:databaseName database-name
- :schemaName schema-name
- :tableName table-name}))
-
-(defn- source-table-reference
- "Returns the metric query source-table reference from either query shape."
- [card]
- (or (get-in card [:dataset_query :query :source-table])
- (get-in card [:dataset_query :stages 0 :source-table])))
-
-(defn- source-table-id
- "Returns the source table id for table-backed metrics."
- [card]
- (let [source-table (source-table-reference card)]
- (when (integer? source-table)
- source-table)))
-
-(defn- source-card-id
- "Return the source card ID from the first stage of a metric's definition.
-
- This identifies the card the definition reads from. The source needed to use the metric is returned by
- [[metabase.metabot.tools.util/metric-required-source]]; a multi-stage definition can require its base table."
- [card]
- (or (when-let [source-card (get-in card [:dataset_query :stages 0 :source-card])]
- (when (integer? source-card)
- source-card))
- (let [source-table (source-table-reference card)]
- (when (string? source-table)
- (some->> (re-matches #"card__(\d+)" source-table)
- second
- parse-long)))))
-
-(defn- fallback-metric-column
- "Returns a stable fallback column when metric result-column inference fails."
- [{:keys [name]}]
- {:type "column"
- :name name
- :displayName name
- :jsType "unknown"})
-
-(defn- metric-dimensions-with-table-ids
- "Returns metric dimensions paired with their backing table ids, filtering mapped dimensions for card metrics."
- [details source-card-id-value]
- (let [dimensions (or (seq (sync-and-fetch-metric-dimensions! details))
- (:queryable-dimensions details))
- field-table-ids (field-table-ids dimensions)]
- (cond->> (mapv (fn [dimension]
- [dimension (dimension-table-id dimension field-table-ids)])
- dimensions)
- source-card-id-value (remove (fn [[_dimension table-id]]
- (integer? table-id))))))
-
-(defn- metric-dimension-schemas
- "Returns dimension schemas and mapped table ids for a metric."
- [metric-id 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-table-id-pairs)]
- {:dimension-schemas dimension-schemas
- :mapped-table-ids (->> dimension-schemas (keep :tableId) distinct sort vec)}))
-
-(defn- metric-schema
- "Returns the schema for a metric and its queryable dimensions."
- [{:keys [id name description verified portable_entity_id base_table_portable_fk] :as details}
- card]
- (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)]
- (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_id
- :description description
- :filters (metric-filters card)
- :verified (when verified true)
- :sourceTable (source-table-schema base_table_portable_fk)
- :mappedTableIds (not-empty mapped-table-ids)
- :dimensions (not-empty (schemas.common/keyed-map dimension-schemas)))))
-
-(defn- references-saved-card?
- "Whether `metric`'s query depends on a saved card anywhere, not only as its stage-0 source.
-
- `all-source-card-ids` throws on anything that is not an MBQL 5 query, and a `dataset_query` the app
- DB fails to deserialize arrives as `{}`; report no references for those and leave them to
- `metric-details`, which is what decides whether a metric it cannot read is emitted."
- [metric]
- (let [query (:dataset_query metric)]
- (and (= (:lib/type query) :mbql/query)
- (boolean (seq (lib/all-source-card-ids query))))))
-
-(defn metric-schemas
- "Returns the schemas of the metrics in the collections with `collection-ids`."
- [collection-ids]
- (for [metric (remove source-card-id
- (schemas.common/without-unavailable-cards (data-apps.db/metric-cards-in-collections collection-ids)))
- ;; A data app can copy only metrics that resolve entirely from tables: a copy reading a
- ;; saved question references a card outside the app's resources, which the pull refuses.
- ;; `source-card-id` sees only stage 0, so a table-sourced metric joining a saved question
- ;; would get through; check the whole query for saved-card dependencies instead.
- :when (not (references-saved-card? metric))
- :let [details (metric-details metric)]
- :when details]
- (metric-schema details metric)))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/render.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/render.clj
deleted file mode 100644
index 588caa5af75..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/render.clj
+++ /dev/null
@@ -1,364 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.render
- "Converts semantic schema values into the TypeScript AST.
-
- This is the pure policy stage of the typed-schema pipeline: it decides which
- schema keys become runtime data, which become `/* metadata */` blocks, and how
- metric dimensions compact into `pickFields(...)` calls. The output is the
- tagged-vector AST described in [[metabase-enterprise.data-apps.generate.schemas.javascript]];
- [[schema->ast]] produces it, and the public
- [[metabase-enterprise.data-apps.generate.schemas/render-typescript]] composes it with the
- printer.
-
- Nothing here touches the database or builds strings: new rendering behavior
- should be expressed as schema -> AST transformation (extend
- [[schema-render-policy]] or emit different nodes), keeping it assertable
- structurally in tests."
- (:require
- [clojure.string :as str]
- [metabase-enterprise.data-apps.generate.schemas.javascript :as schemas.javascript]
- [metabase.util :as u]))
-
-(set! *warn-on-reflection* true)
-
-;; The rendered schema serves two audiences:
-;; - `:runtime` keys are executable data consumed by the Lib.createTestQuery DSL.
-;; - `:metadata` keys are emitted as a `/* metadata: {...} */` block inside the
-;; entity for coding agents and humans, preserving useful context without
-;; making the runtime object larger or more ambiguous than the DSL needs.
-(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]}
- :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
- :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]}
- :column {:runtime [:type :name :jsType]
- :metadata [:displayName :description :baseType :effectiveType :semanticType :unit]}})
-
-;; Nested entity kinds by parent kind and entry key. Map values hold keyed
-;; entity maps; sequential values hold entity vectors. Metric `:dimensions` are
-;; special-cased in [[entity->node]] because compaction mixes call expressions
-;; with nested dimension maps.
-(def ^:private entity-children
- {:table {:fields :field, :segments :segment, :measures :measure}
- :measure {:columns :column}
- :metric {:columns :column}})
-
-(defn- policy-runtime-keys
- "Returns the keys that should be emitted as runtime data for a schema node."
- [kind value]
- (filter #(contains? value %)
- (get-in schema-render-policy [kind :runtime])))
-
-(defn- redundant-table-name?
- "Returns true when a table's name would duplicate its object key."
- [entry-key table-name]
- (and (some? entry-key)
- (some? table-name)
- (= (name entry-key) (str table-name))))
-
-(defn- metadata-value
- "Returns `value` as written in a metadata block, or nil when there is nothing to write: a nil or blank value says
- nothing, so a map loses such keys at any depth (a table without a schema has no `schemaName`)."
- [value]
- (cond
- (string? value) (when-not (str/blank? value) value)
- (map? value) (not-empty (into {} (keep (fn [[k v]] (when-some [v (metadata-value v)] [k v]))) value))
- (coll? value) (not-empty (into [] (keep metadata-value) value))
- :else value))
-
-(defn- entity-metadata
- "Returns the metadata-only keys of an entity, in policy order, or nil when it has none."
- [kind value entry-key]
- (not-empty
- (apply array-map
- (mapcat (fn [metadata-key]
- (when-some [metadata (metadata-value (get value metadata-key))]
- (when-not (and (= kind :table)
- (= metadata-key :tableName)
- (redundant-table-name? entry-key metadata))
- [(name metadata-key) metadata])))
- (get-in schema-render-policy [kind :metadata])))))
-
-(defn- generic->node
- "Converts a schema value without render policy into an AST expression.
-
- Values outside the policy table (action namespaces, parameter maps, instance
- metadata) emit every key and no metadata block."
- [value]
- (cond
- (schemas.javascript/expression? value) value
- (map? value) (into [:obj] (for [[entry-key entry-value] value]
- [entry-key (generic->node entry-value)]))
- (sequential? value) (into [:arr] (map generic->node) value)
- :else [:lit value]))
-
-(declare entity->node)
-
-(defn- keyed-entities->obj
- "Converts a map of entity key -> entity of `kind` into an object expression."
- [entities kind]
- (into [:obj]
- (for [[entry-key entity] entities]
- (if-let [metadata (entity-metadata kind entity entry-key)]
- [entry-key {:metadata metadata} (entity->node entity kind)]
- [entry-key (entity->node entity kind)]))))
-
-(defn- entities->arr
- "Converts a sequence of entities of `kind` into an array expression."
- [entities kind]
- (into [:arr]
- (for [entity entities]
- (if-let [metadata (entity-metadata kind entity nil)]
- [:item {:metadata metadata} (entity->node entity kind)]
- (entity->node entity kind)))))
-
-(defn- dimensions->obj
- "Converts compacted metric dimensions into an object expression.
-
- Compaction leaves a mix of `pickFields(...)` call expressions and grouped raw
- dimension maps under `:dimensions`."
- [dimensions]
- (into [:obj]
- (for [[entry-key group] dimensions]
- [entry-key (if (schemas.javascript/expression? group)
- group
- (keyed-entities->obj group :metric-dimension))])))
-
-(defn- entity->node
- "Converts one schema entity of `kind` into an object expression, emitting the
- runtime keys chosen by [[schema-render-policy]]."
- [entity kind]
- (into [:obj]
- (for [entry-key (policy-runtime-keys kind entity)
- :let [value (get entity entry-key)
- child-kind (get-in entity-children [kind entry-key])]]
- [entry-key
- (cond
- (and (= kind :metric)
- (= entry-key :dimensions)) (dimensions->obj value)
- (and child-kind (map? value)) (keyed-entities->obj value child-kind)
- (and child-kind
- (sequential? value)) (entities->arr value child-kind)
- :else (generic->node value))])))
-
-(defn- table-fields-reference-lookup
- "Returns table-id -> field-map reference expression for tables in `schema`."
- [schema]
- (into {}
- (for [[table-key {:keys [id]}] (:tables schema)
- :when (integer? id)]
- [id {:key table-key
- :reference [:ref "tables" table-key "fields"]}])))
-
-(defn- field-lookup-keys
- "Returns the table-scoped lookup keys that can identify a field."
- [table-id {:keys [fieldId name]}]
- (cond-> []
- (integer? fieldId) (conj [table-id fieldId])
- (string? name) (conj [table-id name])))
-
-(defn- table-field-key-entries
- "Returns lookup entries that map table/field identity to rendered field keys."
- [table-id table-key fields]
- (for [[field-key field] fields
- lookup-key (field-lookup-keys table-id field)]
- [lookup-key {:table-key table-key
- :field-key field-key}]))
-
-(defn- table-field-key-lookup
- "Returns [table-id field-id-or-name] -> rendered table/field keys for `schema`."
- [schema]
- (into {}
- (mapcat (fn [[table-key {:keys [id fields]}]]
- (when (integer? id)
- (table-field-key-entries id table-key fields))))
- (:tables schema)))
-
-(defn- pick-fields-call
- "Returns a `pickFields(...)` call expression for compacted metric dimensions."
- ([fields-reference field-keys]
- (pick-fields-call fields-reference field-keys nil))
- ([fields-reference field-keys source-field-id]
- (cond-> [:call "pickFields"
- fields-reference
- (into [:arr] (map (fn [field-key] [:lit field-key])) field-keys)]
- (integer? source-field-id)
- (conj [:obj ["sourceFieldId" [:lit source-field-id]]]))))
-
-(defn- dimension-group-output-key
- "Returns the output key for a compacted dimension group, suffixed by its foreign key's field key when several joins
- reach the same table."
- [table-key source-field-id table-key-count field-key-by-field-id]
- (if (= 1 (get table-key-count table-key))
- table-key
- (str table-key "Via" (if-let [field-key (get field-key-by-field-id source-field-id)]
- (u/capitalize-first-char field-key)
- source-field-id))))
-
-(defn- metric-dimension-table-field
- "Returns table field metadata when a metric dimension can be represented by `pickFields(...)`."
- [fields-reference-by-table field-key-by-table-and-field {:keys [tableId fieldId name sourceFieldId]}]
- (when-let [{table-key :key, :keys [reference]} (get fields-reference-by-table tableId)]
- (when-let [{:keys [field-key]} (or (get field-key-by-table-and-field [tableId fieldId])
- (get field-key-by-table-and-field [tableId name]))]
- {:field-key field-key
- :reference reference
- :source-field-id sourceFieldId
- :table-key table-key})))
-
-(defn- add-compact-metric-dimension-field
- "Adds one matched metric dimension to its table/source-field compaction group."
- [groups {:keys [field-key reference source-field-id table-key]}]
- (update groups
- [table-key source-field-id]
- (fnil (fn [group]
- (update group :field-keys conj field-key))
- {:field-keys []
- :reference reference
- :source-field-id source-field-id
- :table-key table-key})))
-
-(defn- compact-metric-dimension-fields
- "Compacts table-backed metric dimensions into grouped call expressions.
-
- Returns both the compacted `:fields` map and the original dimension keys that
- were compacted, so raw dimensions can be preserved without another lookup pass."
- [fields-reference-by-table field-key-by-table-and-field dimensions]
- (let [{:keys [fields-by-group dimension-keys]}
- (reduce (fn [acc [dimension-key dimension]]
- (if-let [field (metric-dimension-table-field fields-reference-by-table
- field-key-by-table-and-field
- dimension)]
- (-> acc
- (update :fields-by-group add-compact-metric-dimension-field field)
- (update :dimension-keys conj dimension-key))
- acc))
- {:dimension-keys #{}
- :fields-by-group (array-map)}
- dimensions)
- table-key-count (frequencies (map (comp :table-key val) fields-by-group))
- field-key-by-field-id (into {}
- (keep (fn [[[_table-id field-id-or-name] {:keys [field-key]}]]
- (when (integer? field-id-or-name)
- [field-id-or-name field-key])))
- field-key-by-table-and-field)]
- {:dimension-keys dimension-keys
- :fields (not-empty
- (reduce-kv (fn [acc _ {:keys [table-key reference field-keys source-field-id]}]
- (assoc acc
- (dimension-group-output-key table-key source-field-id table-key-count
- field-key-by-field-id)
- (pick-fields-call reference
- (distinct field-keys)
- source-field-id)))
- (array-map)
- fields-by-group))}))
-
-(defn- compact-metric-dimension-keys
- "Returns original metric dimension keys represented by compacted fields."
- [compact-fields]
- (:dimension-keys compact-fields))
-
-(defn- raw-metric-dimension-fields
- "Returns non-compacted metric dimensions as raw runtime fields.
-
- Raw dimensions are kept under `\"fields\"`, or `\"metricFields\"` if compacted
- fields 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)))]
- (let [group-key (if (contains? compact-fields "fields")
- "metricFields"
- "fields")]
- {group-key raw-fields})))
-
-(defn- compact-metric-dimensions
- "Rewrites metric dimensions into compact field references where possible."
- [schema]
- (let [fields-reference-by-table (table-fields-reference-lookup schema)
- field-key-by-table-and-field (table-field-key-lookup schema)]
- (update schema :metrics
- (fn [metrics]
- (update-vals metrics
- (fn [metric]
- (let [dimensions (:dimensions metric)
- compact-fields (compact-metric-dimension-fields fields-reference-by-table
- field-key-by-table-and-field
- dimensions)
- fields (:fields compact-fields)
- compact-keys (compact-metric-dimension-keys compact-fields)
- raw-fields (raw-metric-dimension-fields dimensions compact-keys fields)
- compact-dimensions (not-empty (merge raw-fields fields))]
- (cond-> (dissoc metric :dimensions)
- compact-dimensions (assoc :dimensions compact-dimensions)))))))))
-
-(def ^:private pick-fields-helper
- "The TypeScript helper used by compacted metric dimensions."
- [:raw (str/join "\n"
- ["function pickFields<TFields extends object, TKey extends keyof TFields>("
- " fields: TFields,"
- " keys: readonly TKey[],"
- " options?: { sourceFieldId?: number },"
- "): Pick<TFields, TKey> {"
- " return Object.fromEntries(keys.map((key) => {"
- " const field = fields[key] as { tableId?: number };"
- " if (options?.sourceFieldId == null) {"
- " return [key, field];"
- " }"
- " const { tableId, ...joinedField } = field;"
- ""
- " return [key, { ...joinedField, sourceFieldId: options.sourceFieldId }];"
- " })) as Pick<TFields, TKey>;"
- "}"])])
-
-(defn- uses-pick-fields-helper?
- "Returns true when compacted metrics contain `pickFields(...)` expressions."
- [schema]
- (boolean
- (some (fn [metric]
- (some schemas.javascript/call? (vals (:dimensions metric))))
- (vals (:metrics schema)))))
-
-(def ^:private top-level-keys
- [:actions :tables :metrics])
-
-(defn- section->node
- "Converts one top-level schema section into an object expression."
- [section-key section]
- (case section-key
- :actions (generic->node section)
- :tables (keyed-entities->obj section :table)
- :metrics (keyed-entities->obj section :metric)))
-
-(defn schema->ast
- "Converts a semantic schema value into a TypeScript module AST.
-
- Each present top-level section becomes its own `const`; the `schema` const
- ties them together with the remaining schema metadata and is exported as the
- module default."
- [schema]
- (let [schema (compact-metric-dimensions schema)
- present-keys (filter #(contains? schema %) top-level-keys)
- schema-metadata (apply dissoc schema top-level-keys)
- schema-entries (concat (for [[entry-key value] schema-metadata]
- [entry-key (generic->node value)])
- (for [section-key present-keys]
- [section-key [:ref (name section-key)]]))]
- (into [:module]
- (concat
- (when (uses-pick-fields-helper? schema)
- [pick-fields-helper])
- (for [section-key present-keys]
- [:const (name section-key) (section->node section-key (get schema section-key))])
- [[:const "schema" (into [:obj] schema-entries)]
- [:export-default [:ref "schema"]]]))))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/source.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/source.clj
deleted file mode 100644
index cb3f13b054f..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/source.clj
+++ /dev/null
@@ -1,70 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.source
- "Data access for a data app's schema, reified as a protocol.
-
- [[SchemaSource]] names every read that [[metabase-enterprise.data-apps.generate.schemas/fetch-items]] performs, so
- the data-access surface is one definition. [[app-db-source]] is the production implementation; tests reify the
- protocol with literal values. All methods return the shaped schema entities of the
- `metabase-enterprise.data-apps.generate.schemas.*` builders, except [[library-tables]], which returns raw table rows
- (only `:id` is consumed)."
- (:require
- [clojure.set :as set]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.action :as schemas.action]
- [metabase-enterprise.data-apps.generate.schemas.metric :as schemas.metric]
- [metabase-enterprise.data-apps.generate.schemas.table :as schemas.table]
- [metabase.collections.core :as collections]
- [metabase.collections.models.collection :as collection]))
-
-(set! *warn-on-reflection* true)
-
-(def LibraryScope
- "The library collection tree, classified by collection type: `:data-collection-ids` hold the published tables,
- `:metric-collection-ids` the metrics."
- [:map {:closed true}
- [:data-collection-ids [:set :int]]
- [:metric-collection-ids [:set :int]]])
-
-(defn- app-db-library-scope
- "The [[LibraryScope]] of the root libraries and their descendants, empty where the instance has no library."
- []
- (let [library (collections/library-collection)
- descendants (when (and library (not (:archived library)))
- (collection/descendants-flat-for [library]))
- collections (data-apps.db/unarchived-collections (into #{} (map :id) descendants))
- ids-of-type (fn [collection-type]
- (into #{} (comp (filter #(= (:type %) collection-type)) (map :id)) collections))]
- {:data-collection-ids (ids-of-type collection/library-data-collection-type)
- :metric-collection-ids (ids-of-type collection/library-metrics-collection-type)}))
-
-(defprotocol SchemaSource
- "The data-access patterns behind schema generation."
- (library-scope [source]
- "The [[LibraryScope]] of the root libraries.")
- (actions [source]
- "Schema entities for query actions that belong to no model.")
- (metrics [source collection-ids]
- "Schema entities for the metrics in `collection-ids`.")
- (tables [source table-ids]
- "Schema entities for the tables with `table-ids`.")
- (library-tables [source collection-ids]
- "Published table rows in `collection-ids`."))
-
-(defn- curated-library-ids
- [scope-key requested-ids]
- (set/intersection (scope-key (app-db-library-scope)) requested-ids))
-
-(def app-db-source
- "The production [[SchemaSource]], backed by the application database."
- (reify SchemaSource
- (library-scope [_]
- (app-db-library-scope))
- (actions [_]
- (schemas.action/action-schemas))
- (metrics [_ collection-ids]
- (vec (schemas.metric/metric-schemas
- (curated-library-ids :metric-collection-ids collection-ids))))
- (tables [_ table-ids]
- (vec (schemas.table/table-schemas (schemas.table/select-tables table-ids))))
- (library-tables [_ collection-ids]
- (vec (schemas.table/select-library-tables
- (curated-library-ids :data-collection-ids collection-ids))))))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/table.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/table.clj
deleted file mode 100644
index c44967b6626..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schemas/table.clj
+++ /dev/null
@@ -1,187 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.table
- "The schema of tables, with their fields, segments and measures."
- (:require
- [medley.core :as m]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [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]))
-
-(set! *warn-on-reflection* true)
-
-(defn table-by-field-id
- "Returns the table for a given field id."
- [field-id]
- (when (integer? field-id)
- (data-apps.db/field-table-id field-id)))
-
-(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]
- (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
- :fieldId (when (integer? field-id) field-id)
- :tableId (when (integer? table-id) table-id)
- :defaultTemporalBucket (:unit field)))))
-
-(defn- without-unavailable-tables
- "`tables` without any backed by a routing destination database, and without the audit database's while the audit
- feature is off, since the table details lookup refuses those."
- [tables]
- (let [destination-ids (data-apps.db/destination-database-ids (into #{} (keep :db_id) tables))
- audit-off? (not (premium-features/enable-audit-app?))]
- (cond->> tables
- (seq destination-ids) (remove #(contains? destination-ids (:db_id %)))
- audit-off? (remove #(= audit/audit-db-id (:db_id %))))))
-
-(defn select-tables
- "Returns the active tables with `table-ids`."
- [table-ids]
- (without-unavailable-tables (data-apps.db/active-tables table-ids)))
-
-(defn select-library-tables
- "Returns the published tables in the library collections with `collection-ids`."
- [collection-ids]
- (->> (data-apps.db/published-tables-in-collections collection-ids)
- (without-unavailable-tables)))
-
-(defn segment-schema
- "Returns the schema for a segment."
- [table-id {:keys [id name description display-name portable-entity-id portable_entity_id]}]
- (m/assoc-some
- {:type "segment"
- :key (schemas.common/generated-key (or display-name name) id)
- :id id
- :tableId table-id
- :name name}
- :entityId (or portable_entity_id portable-entity-id)
- :description description))
-
-(defn- fallback-measure-column
- [{:keys [name]}]
- {:type "column"
- :name name
- :displayName name
- :jsType "unknown"})
-
-(defn measure-result-column
- "Returns the result column for a measure definition, or nil when it cannot be inferred.
-
- Measures render as aggregate fields. When Lib can infer the
- aggregation column, the schema exposes the real result type. Otherwise, the
- measure remains usable with a fallback column."
- [database-id measure-id]
- (try
- (when-let [definition (data-apps.db/measure-definition measure-id)]
- (schemas.common/aggregation-result-column database-id definition))
- (catch Exception _
- nil)))
-
-(defn- measure-result-columns
- "Returns result columns for table measures with one definition query and metadata provider."
- [database-id measures]
- (let [measure-ids (into [] (keep :id) measures)]
- (when (seq measure-ids)
- (let [definition-by-measure-id (into {}
- (map (juxt :id :definition))
- (data-apps.db/measure-definitions measure-ids))]
- (try
- (let [metadata-provider (lib-be/application-database-metadata-provider database-id)]
- (into {}
- (keep (fn [{:keys [id]}]
- (when-let [definition (get definition-by-measure-id id)]
- [id (schemas.common/aggregation-result-column-with-metadata-provider metadata-provider definition)])))
- measures))
- ;; Result-column inference is best effort; callers fall back to an unknown column.
- (catch Exception _
- {}))))))
-
-(defn measure-schema
- "Returns the schema for a measure."
- ([table-id database-id measure]
- (measure-schema table-id database-id (measure-result-column database-id (:id measure)) measure))
- ([table-id _database-id result-column {:keys [id name description display-name portable-entity-id portable_entity_id]}]
- (m/assoc-some
- {:type "measure"
- :key (schemas.common/generated-key (or display-name name) id)
- :id id
- :tableId table-id
- :name name
- :columns [(or (some-> result-column schemas.common/column-schema)
- (fallback-measure-column {:name (or display-name name)}))]}
- :entityId (or portable_entity_id portable-entity-id)
- :description description)))
-
-(defn table-schema
- "Returns the schema for a table."
- [{:keys [id name description display_name database_id database_name database_schema portable_entity_id fields segments measures]}]
- (let [segments-map (schemas.common/keyed-map (map #(segment-schema id %) segments))
- measure-result-column-by-id (measure-result-columns database_id measures)
- measures-map (schemas.common/keyed-map (map #(measure-schema id database_id
- (get measure-result-column-by-id (:id %))
- %)
- measures))]
- (m/assoc-some
- {:type "table"
- :key (schemas.common/generated-key (or display_name name) id)
- :id id
- :name (or display_name name)
- :databaseName database_name
- :tableName name
- :fields (schemas.common/keyed-map (map #(field-schema % name id) fields))}
- :entityId portable_entity_id
- :description description
- :schemaName database_schema
- :segments (not-empty segments-map)
- :measures (not-empty measures-map))))
-
-(defn- table-details-error-message
- [table error-message]
- (format "Failed to build schema details for table \"%s\" (table %s): %s"
- (or (:name table) "Untitled")
- (:id table)
- (or error-message "unknown error")))
-
-(defn- table-details-error-data
- [table error-data]
- (m/assoc-some
- {:table-id (:id table)
- :table-name (:name table)}
- :status-code (:status-code error-data)))
-
-(defn- table-details
- [table]
- (let [response (metabot/get-table-details
- {:entity-type :table
- :entity-id (:id table)
- :with-fields? true
- :with-field-values? false
- :with-related-tables? false
- :with-metrics? false
- :with-measures? true
- :with-segments? true})]
- (or (:structured-output response)
- (let [error-message (:output response)]
- (throw (ex-info (table-details-error-message table error-message)
- (assoc (table-details-error-data table response)
- :error-message error-message)))))))
-
-(defn table-schemas
- "Returns table schemas for selected table rows."
- [tables]
- (for [table tables
- :let [details (table-details table)]
- :when details]
- (table-schema details)))
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.clj
index 4cb36942a0f..a9baf56fc90 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
@@ -102,14 +102,18 @@
(contains? app :allowed_hosts) (update :allowed_hosts #(or % []))))
(defmethod mi/can-read? :model/DataApp
- ([app] (mi/can-read? :model/DataApp (:id app)))
+ ([app]
+ (or api/*is-superuser?*
+ (boolean
+ (and (:enabled app)
+ api/*current-user-id*
+ (nil? (:tenant_id @api/*current-user*))
+ (let [group-ids (user/group-ids api/*current-user-id*)]
+ (some (comp group-ids :permission_group_id)
+ (data-apps.db/app-assignments [(:id app)])))))))
([_model pk]
(or api/*is-superuser?*
- (and api/*current-user-id*
- (nil? (:tenant_id @api/*current-user*))
- (let [group-ids (user/group-ids api/*current-user-id*)]
- (boolean (some (comp group-ids :permission_group_id)
- (data-apps.db/app-assignments [pk]))))))))
+ (boolean (some-> (data-apps.db/data-app pk) mi/can-read?)))))
(defmethod mi/can-write? :model/DataApp
([_instance] api/*is-superuser?*)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/query_definition.clj b/enterprise/backend/src/metabase_enterprise/data_apps/query_definition.clj
deleted file mode 100644
index a08d819d7e7..00000000000
--- a/enterprise/backend/src/metabase_enterprise/data_apps/query_definition.clj
+++ /dev/null
@@ -1,179 +0,0 @@
-(ns metabase-enterprise.data-apps.query-definition
- "Closed schemas for the data apps subset of `metabase.lib.schema.test-spec`.
-
- Data apps queries support a single table-sourced stage without explicit joins or expressions."
- (:require
- [malli.core :as mc]
- [malli.error :as me]
- [malli.transform :as mtx]
- [metabase.lib.schema.common :as lib.schema.common]
- [metabase.lib.schema.id :as lib.schema.id]
- [metabase.lib.schema.literal :as literal]
- [metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]
- [metabase.util :as u]
- [metabase.util.malli.registry :as mr]))
-
-(defn- query-map-decoder [schema _options]
- (let [metadata-keys (::sdk-metadata (mc/properties schema))]
- {:enter (fn [value]
- (when (map? value)
- ;; drop data apps specific metadata fields such as `js-type`
- (apply dissoc (update-keys value (comp keyword u/->kebab-case-en)) metadata-keys)))}))
-
-(mr/def ::table-source
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:name :columns :fields :segments :measures]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :table]]
- [:id [:ref ::lib.schema.id/table]]])
-
-(mr/def ::column
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:description :display-name :js-type :table-id :source-name :base-type :effective-type
- :default-temporal-bucket :id :metric-id]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :column]]
- [:name string?]
- [:field-id {:optional true} [:maybe ::lib.schema.id/field]]
- [:source-field-id {:optional true} [:maybe ::lib.schema.id/field]]])
-
-(mr/def ::temporal-bucket
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:unit {:optional true} [:maybe ::lib.schema.temporal-bucketing/unit]]])
-
-(mr/def ::binning
- [:multi {:decode/normalize {:compile query-map-decoder}
- :dispatch (comp keyword :strategy)}
- [:default [:map {:closed true}
- [:strategy [:= {:decode/normalize lib.schema.common/normalize-keyword} :default]]]]
- [:num-bins [:map {:closed true}
- [:strategy [:= {:decode/normalize lib.schema.common/normalize-keyword} :num-bins]]
- [:num-bins pos-int?]]]
- [:bin-width [:map {:closed true}
- [:strategy [:= {:decode/normalize lib.schema.common/normalize-keyword} :bin-width]]
- [:bin-width ::lib.schema.common/positive-number]]]])
-
-(mr/def ::column-with-binning
- [:merge
- ::column
- ::temporal-bucket
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:binning {:optional true} [:maybe ::binning]]]])
-
-(mr/def ::named-breakout
- "A breakout whose result column is named, as `breakout(field, { name })` names it."
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :breakout]]
- [:name string?]
- [:column [:ref ::column-with-binning]]])
-
-(mr/def ::breakout
- [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
- :dispatch (comp keyword :type)}
- [:column [:ref ::column-with-binning]]
- [:breakout [:ref ::named-breakout]]])
-
-(mr/def ::order-by
- [:merge
- ::column-with-binning
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:direction {:optional true} [:maybe [:enum {:decode/normalize lib.schema.common/normalize-keyword} :asc :desc]]]]])
-
-(mr/def ::literal-expression
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :literal]]
- [:value [:ref ::literal/literal]]])
-
-(mr/def ::operator
- keyword?)
-
-(mr/def ::operator-expression
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:columns]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :operator]]
- [:operator ::operator]
- [:args {:default []} [:sequential [:ref ::expression]]]])
-
-(mr/def ::aggregation-operator-expression
- "An aggregation can be named; a filter or a nested argument can't, so `name` is refused there rather than dropped."
- [:merge
- ::operator-expression
- [:map {:closed true :decode/normalize {:compile query-map-decoder}}
- [:name {:optional true} string?]]])
-
-(mr/def ::expression
- [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
- :dispatch (comp keyword :type)}
- [:column [:ref ::column]]
- [:literal [:ref ::literal-expression]]
- [:operator [:ref ::operator-expression]]])
-
-(mr/def ::segment
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:name :table-id]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :segment]]
- [:id [:ref ::lib.schema.id/segment]]])
-
-(mr/def ::measure
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:name :table-id :columns]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :measure]]
- [:id [:ref ::lib.schema.id/measure]]])
-
-(mr/def ::metric
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:name :database-id :source-table-id :source-card-id :mapped-table-ids :columns :dimensions]}
- [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :metric]]
- [:id [:ref ::lib.schema.id/metric]]])
-
-(mr/def ::aggregation-expression
- [:multi {:decode/normalize lib.schema.common/normalize-map-no-kebab-case
- :dispatch (comp keyword :type)}
- [:column [:ref ::column]]
- [:literal [:ref ::literal-expression]]
- [:operator [:ref ::aggregation-operator-expression]]])
-
-(mr/def ::named-aggregation
- "A measure or a metric whose result column is named, as `aggregations.measure` and `aggregations.metric` name it."
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:columns]}
- [:name string?]
- [:value [:or [:ref ::measure] [:ref ::metric]]]])
-
-(mr/def ::aggregation
- [:or
- [:ref ::aggregation-expression]
- [:ref ::named-aggregation]
- [:ref ::measure]
- [:ref ::metric]])
-
-(mr/def ::stage
- [:map {:closed true :decode/normalize {:compile query-map-decoder}
- ::sdk-metadata [:enabled]}
- [:source ::table-source]
- [:fields {:optional true} [:maybe [:sequential ::column]]]
- [:filters {:optional true} [:maybe [:sequential [:or ::expression ::segment]]]]
- [:aggregations {:optional true} [:maybe [:sequential ::aggregation]]]
- [:breakouts {:optional true} [:maybe [:sequential ::breakout]]]
- [:order-bys {:optional true} [:maybe [:sequential ::order-by]]]
- [:limit {:optional true} [:maybe number?]]])
-
-(defn- strict-query-decoder [schema _options]
- (let [decode (mc/decoder schema (mtx/transformer
- (mtx/string-transformer)
- (mtx/json-transformer)
- (mtx/default-value-transformer)
- {:name :normalize}))
- validate (mr/validator schema)]
- (fn [value]
- ;; validate before the middleware silently drops unsupported fields
- (let [query (decode value)]
- (when-not (validate query)
- (throw (ex-info "Invalid data app query"
- {:status-code 400
- :errors (me/humanize (mr/explain schema query))})))
- query))))
-
-(mr/def ::query-definition
- [:map {:closed true
- :decode/api {:compile strict-query-decoder}
- :decode/normalize {:compile query-map-decoder}}
- [:stages [:sequential {:min 1 :max 1} ::stage]]])
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
index e0a0d5b2643..b81b6477d24 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
@@ -2,7 +2,6 @@
"Malli schemas for the data-apps module."
(:require
[clojure.string :as str]
- [metabase-enterprise.data-apps.query-definition :as query-definition]
[metabase.util :as u]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]))
@@ -12,8 +11,8 @@
(* 10 1024 1024))
(def ^:private reserved-slugs
- "Slugs that collide with literal `/api/apps/*` sub-routes, so an app with one would be unreachable."
- #{"generate" "repo-status" "sandbox-host"})
+ "Slugs that collide with literal `/api/ee/apps/*` sub-routes, so an app with one would be unreachable."
+ #{"repo-status" "sandbox-host"})
(defn- decoders
"Schema properties decoding a value with `f` both in [[metabase.lib.core/normalize]] and at the API boundary."
@@ -123,44 +122,3 @@
[:name ::slug]
[:display_name ::display-name]
[:bundle_path ::bundle-path]]])
-
-(mr/def ::query
- "A data app `defineQuery` definition, with the name, entity ID and collection of the saved question that holds it."
- [:map {:closed true}
- [:name ms/NonBlankString]
- [:query ::query-definition/query-definition]
- [:entity_id ms/NanoIdString]
- [:collection_id ms/NanoIdString]])
-
-(mr/def ::action
- "An action without a model a data app runs, with the entity ID and collection of its copy."
- [:map {:closed true}
- [:action_id ms/PositiveInt]
- [:entity_id ms/NanoIdString]
- [:collection_id ms/NanoIdString]])
-
-(mr/def ::file
- "A serialization file in the folder of the app's collection, or the error that stops it."
- [:or
- [:map {:closed true}
- [:file :string]
- [:yaml :string]]
- [:map {:closed true}
- [:error :string]]])
-
-(mr/def ::app-request
- "What a data app's files are generated from. An existing app passes the entity IDs of its `data_app.yaml` and of its
- collection, so its files keep them."
- [:map {:closed true}
- [:name ::display-name]
- [:slug {:optional true} [:maybe ::slug]]
- [:description {:optional true} ::description]
- [:entity_id {:optional true} [:maybe ms/NanoIdString]]
- [:collection_id {:optional true} [:maybe ms/NanoIdString]]])
-
-(mr/def ::app-files
- "A data app's files, each at its path from the repository root."
- [:map {:closed true}
- [:files [:sequential [:map {:closed true}
- [:path :string]
- [:yaml :string]]]]])
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/typed_schema.clj b/enterprise/backend/src/metabase_enterprise/data_apps/typed_schema.clj
new file mode 100644
index 00000000000..02caf069259
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/typed_schema.clj
@@ -0,0 +1,178 @@
+(ns metabase-enterprise.data-apps.typed-schema
+ "The TypeScript module of a data app's queries and actions: a `defineQuery` call for each question in the app's
+ resource collection, typed by its result columns, and a `defineAction` call for each action, typed by its
+ parameters, each addressed by its entity ID."
+ (:require
+ [clojure.string :as str]
+ [medley.core :as m]
+ [metabase-enterprise.data-apps.db :as data-apps.db]
+ [metabase.actions.core :as actions]
+ [metabase.lib.core :as lib]
+ [metabase.lib.schema.common :as lib.schema.common]
+ [metabase.types.core]
+ [metabase.util :as u]
+ [metabase.util.json :as json]))
+
+(set! *warn-on-reflection* true)
+
+(comment metabase.types.core/keep-me)
+
+(defn- column-js-type
+ "The JavaScript type of a value of a column with `base-type`."
+ [base-type]
+ (let [base-type (some-> base-type keyword)]
+ (cond
+ (nil? base-type) "unknown"
+ (isa? base-type :type/Boolean) "boolean"
+ (isa? base-type :type/Number) "number"
+ (isa? base-type :type/Temporal) "Date"
+ (isa? base-type :type/TextLike) "string"
+ (isa? base-type :type/Text) "string"
+ :else "unknown")))
+
+(defn- column-schema
+ "The schema of a card's result `column`."
+ [{:keys [name display_name base_type effective_type semantic_type]}]
+ (let [effective-type (or effective_type base_type)]
+ (m/assoc-some
+ {:type "column"
+ :name name
+ :displayName (or display_name name)
+ :jsType (column-js-type effective-type)}
+ :baseType base_type
+ :effectiveType effective-type
+ :semanticType semantic_type)))
+
+(defn- query-definition
+ "The `defineQuery` argument of `card`: a card source with its result columns."
+ [{:keys [name entity_id result_metadata]}]
+ {:source (m/assoc-some {:type "card"
+ :entityId entity_id
+ :name name}
+ :columns (some->> result_metadata (mapv column-schema)))})
+
+(defn- param-type->js-type
+ "The JavaScript type of a value of an action parameter of `param-type`."
+ [param-type]
+ (when ((some-fn keyword? string?) param-type)
+ (let [param-type (lib.schema.common/normalize-keyword param-type)]
+ (case (or (namespace param-type) (name param-type))
+ ("number" "numeric") "number"
+ ("text" "string") "string"
+ "date" "Date"
+ "boolean" "boolean"
+ nil))))
+
+(defn- template-tag-types
+ "The value types of the template tags of `action`'s query, keyed by tag name. A field filter's `:type` is always
+ `:dimension`, so its `:widget-type` is what tells the value type."
+ [{:keys [dataset_query]}]
+ (some-> dataset_query
+ not-empty
+ lib/all-template-tags-map
+ (update-vals (fn [{:keys [type widget-type]}]
+ (if (= type :dimension) widget-type type)))))
+
+(defn- parameter-schema
+ "The schema of an action's execute parameter."
+ [tag-types {:keys [id slug name display-name type target required]}]
+ (let [slug (or slug (some-> id clojure.core/name) (some-> name u/slugify))]
+ (m/assoc-some
+ {:slug slug
+ :displayName (or display-name name slug)
+ :jsType (or (param-type->js-type type)
+ (param-type->js-type (get tag-types (some-> target lib/parameter-target-template-tag-name)))
+ "unknown")}
+ :required (when required true))))
+
+(defn- hidden-parameter-ids
+ "The IDs of the parameters `action`'s form hides, which execution refuses a value for."
+ [{:keys [visualization_settings]}]
+ (into #{} (keep #(when (:hidden %) (:id %))) (vals (:fields visualization_settings))))
+
+(defn- action-definition
+ "The `defineAction` argument of the query `action`: its entity ID and parameters."
+ [{:keys [entity_id name description type parameters] :as action}]
+ (let [tag-types (template-tag-types action)
+ hidden? (hidden-parameter-ids action)]
+ {:action (m/assoc-some
+ {:kind "action"
+ :id entity_id
+ :name name
+ :type type
+ :parameters (into [] (comp (remove (comp hidden? :id))
+ (map #(parameter-schema tag-types %)))
+ parameters)}
+ :description description)}))
+
+(defn- identifier-part
+ "`s` with the characters a JavaScript identifier can't hold removed."
+ [s]
+ (str/replace s #"[^\p{L}\p{N}_$]" ""))
+
+(defn- export-key
+ "A JavaScript identifier for an entity named `entity-name` with `entity-id`, the same on every instance."
+ [entity-name entity-id]
+ (let [k (some-> entity-name u/->camelCaseEn identifier-part)]
+ (cond
+ (str/blank? k) (str "entity_" (identifier-part entity-id))
+ (re-find #"^\p{N}" k) (str "_" k)
+ :else k)))
+
+(defn- keyed
+ "`entities` keyed by a unique JavaScript identifier made from each one's name, in their order."
+ [entities]
+ (let [ks (map #(export-key (:name %) (:entity_id %)) entities)
+ n (frequencies ks)]
+ (map (fn [entity k]
+ [(cond-> k (> (n k) 1) (str "_" (identifier-part (:entity_id entity)))) entity])
+ entities ks)))
+
+(defn- identifier? [k]
+ (re-matches #"[A-Za-z_$][A-Za-z0-9_$]*" k))
+
+(defn- render
+ "`value` as a TypeScript literal indented by `indent`."
+ [value indent]
+ (let [inner (str indent " ")]
+ (cond
+ (map? value) (if (empty? value)
+ "{}"
+ (str "{\n"
+ (str/join (for [[k v] value
+ :let [k (u/qualified-name k)]]
+ (str inner (if (identifier? k) k (json/encode k)) ": "
+ (render v inner) ",\n")))
+ indent "}"))
+ (sequential? value) (if (empty? value)
+ "[]"
+ (str "[\n"
+ (str/join (for [v value] (str inner (render v inner) ",\n")))
+ indent "]"))
+ :else (json/encode value))))
+
+(defn- render-entries
+ "The `export const` of `export-name`: an object of `entities` keyed by name, each its `define-fn` call on its
+ `definition`."
+ [export-name define-fn definition entities]
+ (str "export const " export-name " = {\n"
+ (str/join (for [[k entity] (keyed entities)]
+ (str " " k ": " define-fn "(" (render (definition entity) " ") "),\n")))
+ "};\n"))
+
+(defn generate
+ "The TypeScript module of the queries and actions of the data app `app`."
+ [{slug :name, collection-id :resource_collection_id}]
+ (let [cards (data-apps.db/resource-cards collection-id)
+ action-ids (data-apps.db/resource-action-ids collection-id)
+ actions (when (seq action-ids)
+ (let [by-id (u/index-by :id (actions/select-actions-for-ids nil action-ids))]
+ (keep by-id action-ids)))]
+ (str "// The queries and actions of the data app \"" slug "\", generated by Metabase from the questions and\n"
+ "// actions in the app's collection. Do not edit: after changing them, fetch it again with\n"
+ "// mb data-app typed-schema " slug " --out src/metabase.ts\n"
+ "import { defineAction, defineQuery } from \"@metabase/embedding-sdk-react/data-app\";\n"
+ "\n"
+ (render-entries "queries" "defineQuery" query-definition cards)
+ "\n"
+ (render-entries "actions" "defineAction" action-definition actions))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
index c555a5fec57..565faf77ece 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
@@ -27,16 +27,18 @@
->ingestable])
(defenterprise collection-editable?
- "Determines if a remote-synced collection should be editable.
-
- Takes a collection to check for editability.
-
- Returns true if the collection is editable, false otherwise. Returns true when remote-sync-type is :read-write
- or when the collection is not a remote-synced collection. Always returns true on OSS."
+ "Whether `collection` and what it holds can be edited: always on a read-write instance or on OSS; otherwise not for a
+ remote-synced collection, nor for a data app's collection while remote sync is on."
:feature :none
[collection]
(or (= (settings/remote-sync-type) :read-write)
- (not (collections/remote-synced-collection? collection))))
+ (not (or (collections/remote-synced-collection? collection)
+ (and (settings/remote-sync-enabled)
+ (spec/data-apps-namespace-collection?
+ {:namespace (if (and (map? collection) (contains? collection :namespace))
+ (:namespace collection)
+ ;; the root collection has no ID, and nothing in it is synced
+ (some-> (u/id collection) remote-sync.db/collection-namespace))}))))))
(defenterprise collection-syncable?
"Whether `collection` can be remote-synced: a Collection of a namespace remote sync syncs, the Library and every Collection inside it, and any other once custom collections are on."
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
index 2f1458210bc..718d66a493f 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
@@ -5,11 +5,10 @@
[metabase-enterprise.data-apps.apps :as data-apps.apps]
[metabase-enterprise.data-apps.config :as data-app.config]
[metabase-enterprise.data-apps.group-access :as group-access]
- [metabase-enterprise.data-apps.query-definition :as query-definition]
[metabase-enterprise.data-apps.resources :as data-app.resources]
[metabase.actions.core :as actions]
- [metabase.api.macros.defendpoint.closed-schemas :as closed-schemas]
[metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
[metabase.permissions.core :as perms]
[metabase.test :as mt]
[metabase.util.json :as json]
@@ -31,7 +30,7 @@
:display_name "Demo app"
:bundle_path "dist/index.js"
:allowed_hosts ["https://api.example.com"]
- :bundle "DEMOBUNDLE"})
+ :enabled true})
;;; ---------------------------------------------- Permissions ----------------------------------------------
@@ -48,18 +47,18 @@
permission_group_id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))]
(testing "collection access alone does not assign an app"
(perms/grant-collection-read-permissions! (perms/all-users-group) resource_collection_id)
- (mt/user-http-request :rasta :get 403 "apps/demo"))
+ (mt/user-http-request :rasta :get 403 "ee/apps/demo"))
(testing "a member can open a data app"
(group-access/add-groups! app [permission_group_id])
(perms/add-user-to-group! (mt/user->id :rasta) permission_group_id)
(is (str/includes?
- (str (mt/user-real-request :rasta :get 200 "apps/demo/bundle"))
+ (str (mt/user-real-request :rasta :get 200 "ee/apps/demo/bundle"))
"BUNDLE"))))
(testing "a non-superuser is still forbidden from managing data apps"
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :get 403 "apps/repo-status")))
+ (mt/user-http-request :rasta :get 403 "ee/apps/repo-status")))
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :put 403 "apps/demo" {:enabled false}))))))))
+ (mt/user-http-request :rasta :put 403 "ee/apps/demo" {:enabled false}))))))))
(deftest superuser-can-manage-and-view-test
(mt/test-helpers-set-global-values!
@@ -72,12 +71,12 @@
(mt/user-http-request :rasta :get 200 "collection")))))
(testing "a superuser can list, read metadata, and serve the bundle"
(is (=? [{:name "demo" :display_name "Demo"}]
- (mt/user-http-request :crowberto :get 200 "apps")))
+ (mt/user-http-request :crowberto :get 200 "ee/apps")))
(is (=? {:name "demo"
:resource_collection_id pos-int?}
- (mt/user-http-request :crowberto :get 200 "apps/demo")))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/demo")))
(is (str/includes?
- (str (mt/user-real-request :crowberto :get 200 "apps/demo/bundle"))
+ (str (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle"))
"BUNDLE")))))))
(deftest read-only-remote-sync-blocks-data-app-changes-test
@@ -87,38 +86,35 @@
(mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
remote-sync-type :read-only]
(testing "a read-only instance refuses to create, change, or delete an app"
- (mt/user-http-request :crowberto :post 403 "apps" (assoc app-request :name "other"))
+ (mt/user-http-request :crowberto :post 403 "ee/apps" (assoc app-request :name "other"))
(is (not (t2/exists? :model/DataApp :name "other")))
- (mt/user-http-request :crowberto :put 403 "apps/demo" {:display_name "Renamed"})
- (mt/user-http-request :crowberto :put 403 "apps/demo" {:enabled false :display_name "Renamed"})
- (mt/user-http-request :crowberto :delete 403 "apps/demo")
+ (mt/user-http-request :crowberto :put 403 "ee/apps/demo" {:display_name "Renamed"})
+ (mt/user-http-request :crowberto :put 403 "ee/apps/demo" {:enabled false :display_name "Renamed"})
+ (mt/user-http-request :crowberto :delete 403 "ee/apps/demo")
(is (=? {:display_name "Demo" :enabled true} (t2/select-one :model/DataApp :name "demo"))))
(testing "enabling and disabling stays allowed"
(is (=? {:enabled false}
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:enabled false})))))
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false})))))
(mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
remote-sync-type :read-write]
(testing "a read-write instance allows changes"
(is (=? {:display_name "Renamed"}
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:display_name "Renamed"}))))))))
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:display_name "Renamed"}))))))))
(deftest enabling-a-data-app-is-not-a-synced-change-test
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/RemoteSyncObject]
(create-app!)
(let [app-id (t2/select-one-pk :model/DataApp :name "demo")]
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:enabled false})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false})
(is (not (t2/exists? :model/RemoteSyncObject :model_type "DataApp" :model_id app-id)))
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:display_name "Renamed"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:display_name "Renamed"})
(is (=? {:model_name "demo" :status "update"}
(t2/select-one :model/RemoteSyncObject :model_type "DataApp" :model_id app-id)))))))
-(deftest ^:parallel query-definition-request-schema-is-closed-test
- (is (empty? (closed-schemas/findings ::query-definition/query-definition))))
-
(deftest data-app-write-endpoints-require-feature-token-test
(mt/with-premium-features #{}
- (mt/user-http-request :crowberto :post 402 "apps/generate/resources" {:queries [] :actions []})))
+ (mt/user-http-request :crowberto :post 402 "ee/apps" {:name "demo"})))
(deftest assigned-group-reaches-only-copied-actions-test
(testing "an action is reachable exactly when it lives in the data app collection"
@@ -159,7 +155,7 @@
(t2/insert! :model/DataApp :name "disabled" :display_name "Disabled" :bundle_path "data_apps/disabled/index.js"
:enabled false)
(is (=? [{:name "ready" :display_name "Ready"}]
- (mt/user-http-request :crowberto :get 200 "apps?available=true"))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps?available=true"))))))
(deftest outdated-apps-are-hidden-from-users-and-badged-for-admins-test
(mt/test-helpers-set-global-values!
@@ -177,35 +173,35 @@
(group-access/add-groups! app [group-id])))
(with-redefs [data-app.config/supported-app-version 2]
(testing "a regular user is never told about the outdated app in a list"
- (doseq [url ["apps" "apps?available=true"]]
+ (doseq [url ["ee/apps" "ee/apps?available=true"]]
(is (= [{:name "current" :display_name "Current"}]
(mt/user-http-request :rasta :get 200 url)))))
(testing "for a regular user, opening an outdated app is a 409 that says what to do"
- (doseq [url ["apps/old" "apps/old/bundle"]]
+ (doseq [url ["ee/apps/old" "ee/apps/old/bundle"]]
(is (=? {:error-code "data-app-outdated"
:message #"This app was built for version 1 of data apps.*"}
(mt/user-http-request :rasta :get 409 url)))))
(testing "the current app still opens"
(is (= {:name "current" :display_name "Current"}
- (mt/user-http-request :rasta :get 200 "apps/current")))
+ (mt/user-http-request :rasta :get 200 "ee/apps/current")))
(is (str/includes?
- (str (mt/user-real-request :crowberto :get 200 "apps/current/bundle"))
+ (str (mt/user-real-request :crowberto :get 200 "ee/apps/current/bundle"))
"BUNDLE")))
(testing "an admin sees the outdated app flagged, and can still read it to manage its groups"
(is (=? [{:name "current" :version 2 :outdated false}
{:name "old" :version 1 :outdated true}]
- (mt/user-http-request :crowberto :get 200 "apps")))
+ (mt/user-http-request :crowberto :get 200 "ee/apps")))
(is (=? [{:name "current"}]
- (mt/user-http-request :crowberto :get 200 "apps?available=true"))
+ (mt/user-http-request :crowberto :get 200 "ee/apps?available=true"))
"but the navbar's available list leaves it out for admins too")
(is (=? {:name "old" :version 1 :outdated true}
- (mt/user-http-request :crowberto :get 200 "apps/old"))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/old"))))
(testing "nobody gets an outdated bundle"
(is (=? {:error-code "data-app-outdated"}
- (mt/user-http-request :crowberto :get 409 "apps/old/bundle"))))
+ (mt/user-http-request :crowberto :get 409 "ee/apps/old/bundle"))))
(testing "a management response carries the flag too"
(is (=? {:name "old" :outdated true}
- (mt/user-http-request :crowberto :put 200 "apps/old" {:enabled false})))))))))
+ (mt/user-http-request :crowberto :put 200 "ee/apps/old" {:enabled false})))))))))
(deftest bundle-includes-allowed-hosts-header-test
(mt/with-premium-features #{:data-apps}
@@ -220,7 +216,7 @@
;; publish the app so its bundle is served (crowberto, as a superuser, can read the collection)
(data-app.resources/ensure-resources! (t2/select-one :model/DataApp :name "demo"))
(testing "the bundle response includes only the app's configured allowed hosts"
- (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 "ee/apps/demo/bundle")]
(is (= (json/encode ["https://api.example.com"])
(get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"]))))))))
@@ -236,7 +232,7 @@
:name "nohosts" :display_name "No"
:bundle_path "data_apps/nohosts/index.js")
(testing "the list endpoint returns allowed_hosts, always a list (NULL → [])"
- (let [by-name (->> (mt/user-http-request :crowberto :get 200 "apps")
+ (let [by-name (->> (mt/user-http-request :crowberto :get 200 "ee/apps")
(into {} (map (juxt :name identity))))]
(is (= ["https://api.example.com"]
(get-in by-name ["withhosts" :allowed_hosts])))
@@ -250,56 +246,55 @@
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
(testing "a non-superuser cannot create an app"
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "apps" app-request))))
- (testing "a superuser creates an app with its manifest fields and bundle"
+ (mt/user-http-request :rasta :post 403 "ee/apps" app-request))))
+ (testing "a superuser creates an app with its manifest fields"
(is (=? {:name "demo"
:display_name "Demo app"
:bundle_path "dist/index.js"
:allowed_hosts ["https://api.example.com"]
:version 1
:enabled true
- :bundle_hash string?
:resource_collection_id pos-int?}
- (mt/user-http-request :crowberto :post 200 "apps"
- (assoc app-request :bundle_path "./dist/index.js"))))
- (is (str/includes? (str (mt/user-real-request :crowberto :get 200 "apps/demo/bundle"))
- "DEMOBUNDLE")))
+ (mt/user-http-request :crowberto :post 200 "ee/apps"
+ (assoc app-request :bundle_path "./dist/index.js" :bundle "BUNDLE"))))
+ (testing "with an empty bundle, whatever the request carries"
+ (is (= 0 (alength ^bytes (t2/select-one-fn :bundle :model/DataApp :name "demo"))))))
(testing "a taken slug is refused"
(is (= "A data app with this slug already exists."
- (mt/user-http-request :crowberto :post 409 "apps" app-request))))
+ (mt/user-http-request :crowberto :post 409 "ee/apps" app-request))))
(testing "invalid manifest fields are refused"
(doseq [bad [{:name "Not A Slug"}
{:name "repo-status"}
{:bundle_path "../escape.js"}
{:allowed_hosts ["ftp://example.com"]}
{:version 0}]]
- (mt/user-http-request :crowberto :post 400 "apps" (merge app-request {:name "other"} bad))
+ (mt/user-http-request :crowberto :post 400 "ee/apps" (merge app-request {:name "other"} bad))
(is (not (t2/exists? :model/DataApp :name "other")) (str "should refuse: " (pr-str bad)))))))))
(deftest update-endpoint-test
(mt/test-helpers-set-global-values!
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
- (let [{:keys [bundle_hash]} (mt/user-http-request :crowberto :post 200 "apps" app-request)]
+ (mt/user-http-request :crowberto :post 200 "ee/apps" app-request)
+ (let [bundle-hash #(t2/select-one-fn :bundle_hash :model/DataApp :name "demo")]
(testing "a non-superuser cannot update an app"
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :put 403 "apps/demo" {:display_name "Mine"}))))
- (testing "a superuser updates manifest fields and the bundle"
- (let [updated (mt/user-http-request :crowberto :put 200 "apps/demo"
- {:display_name "Renamed"
- :description " What it does "
- :allowed_hosts []
- :bundle "NEWBUNDLE"})]
- (is (=? {:display_name "Renamed" :description "What it does" :allowed_hosts []}
- updated))
- (is (not= bundle_hash (:bundle_hash updated)))
- (is (str/includes? (str (mt/user-real-request :crowberto :get 200 "apps/demo/bundle"))
- "NEWBUNDLE"))))
+ (mt/user-http-request :rasta :put 403 "ee/apps/demo" {:display_name "Mine"}))))
+ (testing "a superuser updates manifest fields"
+ (is (=? {:display_name "Renamed" :description "What it does" :allowed_hosts []}
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo"
+ {:display_name "Renamed"
+ :description " What it does "
+ :allowed_hosts []}))))
(testing "invalid manifest fields are refused"
- (mt/user-http-request :crowberto :put 400 "apps/demo" {:bundle_path "/abs.js"})
+ (mt/user-http-request :crowberto :put 400 "ee/apps/demo" {:bundle_path "/abs.js"})
(is (= "dist/index.js" (t2/select-one-fn :bundle_path :model/DataApp :name "demo"))))
+ (testing "the bundle is ignored, since only PUT /:slug/bundle replaces it"
+ (let [before (bundle-hash)]
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:bundle "NEWBUNDLE"})
+ (is (= before (bundle-hash)))))
(testing "updating a missing app 404s"
- (mt/user-http-request :crowberto :put 404 "apps/missing" {:enabled false})))))))
+ (mt/user-http-request :crowberto :put 404 "ee/apps/missing" {:enabled false})))))))
(deftest delete-endpoint-requires-a-superuser-test
(mt/with-premium-features #{:data-apps}
@@ -311,15 +306,15 @@
(data-app.resources/ensure-resources! app)
(group-access/add-groups! app [group-id]))
(testing "an assigned user can open the app"
- (mt/user-http-request :rasta :get 200 "apps/demo"))
+ (mt/user-http-request :rasta :get 200 "ee/apps/demo"))
(testing "an assigned user cannot delete the app"
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :delete 403 "apps/demo")))
+ (mt/user-http-request :rasta :delete 403 "ee/apps/demo")))
(is (t2/exists? :model/DataApp :name "demo")))))))
(deftest delete-endpoint-404s-for-a-missing-app-test
(mt/with-premium-features #{:data-apps}
- (mt/user-http-request :crowberto :delete 404 "apps/missing")))
+ (mt/user-http-request :crowberto :delete 404 "ee/apps/missing")))
(deftest delete-endpoint-preserves-assigned-groups-test
(mt/with-premium-features #{:data-apps}
@@ -330,7 +325,7 @@
{collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)]
(mt/with-temp [:model/Card {card-id :id} {:collection_id collection-id}]
(group-access/add-groups! app [(:id group)])
- (mt/user-http-request :crowberto :delete 204 "apps/demo")
+ (mt/user-http-request :crowberto :delete 204 "ee/apps/demo")
(is (not (t2/exists? :model/DataApp :id (:id app))))
(is (not (t2/exists? :model/Collection :id collection-id)))
(is (not (t2/exists? :model/Card :id card-id)))
@@ -343,14 +338,15 @@
(mt/test-helpers-set-global-values!
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
- (mt/user-http-request :crowberto :post 200 "apps" app-request)
+ (mt/user-http-request :crowberto :post 200 "ee/apps" app-request)
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "DEMOBUNDLE"})
(testing "GET / lists the apps"
(is (=? [{:name "demo" :display_name "Demo app"
:bundle_path "dist/index.js" :enabled true}]
- (mt/user-http-request :crowberto :get 200 "apps"))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps"))))
(testing "GET /:slug/bundle serves the cached bytes"
(is (str/includes?
- (str (mt/user-real-request :crowberto :get 200 "apps/demo/bundle"))
+ (str (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle"))
"DEMOBUNDLE")))))))
(deftest repo-status-endpoint-test
@@ -358,31 +354,37 @@
(testing "reports no repository when none is connected"
(mt/with-dynamic-fn-redefs [data-apps.apps/repo-url (constantly nil)]
(is (=? {:configured false :url nil}
- (mt/user-http-request :crowberto :get 200 "apps/repo-status")))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/repo-status")))))
(testing "reports the connected repository URL"
(mt/with-dynamic-fn-redefs [data-apps.apps/repo-url (constantly "https://github.com/metabase/stats-remote-sync")]
(is (=? {:configured true :url "https://github.com/metabase/stats-remote-sync"}
- (mt/user-http-request :crowberto :get 200 "apps/repo-status")))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/repo-status")))))))
(deftest enable-disable-endpoint-test
(mt/test-helpers-set-global-values!
(mt/with-premium-features #{:data-apps}
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
- (mt/user-http-request :crowberto :post 200 "apps" app-request)
+ (mt/user-http-request :crowberto :post 200 "ee/apps" app-request)
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "DEMOBUNDLE"})
(testing "PUT /:slug can disable an app"
(is (=? {:name "demo" :enabled false}
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:enabled false}))))
- (testing "a disabled app is not served"
- (is (= "Not found." (mt/user-http-request :crowberto :get 404 "apps/demo")))
- (mt/user-real-request :crowberto :get 404 "apps/demo/bundle"))
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false}))))
+ (testing "a disabled app is not served to an assigned user"
+ (group-access/add-groups! (t2/select-one :model/DataApp :name "demo") [(:id (perms/all-users-group))])
+ (mt/user-http-request :rasta :get 403 "ee/apps/demo")
+ (mt/user-real-request :rasta :get 403 "ee/apps/demo/bundle"))
+ (testing "a superuser still reads a disabled app's metadata and bundle"
+ (is (=? {:name "demo" :enabled false}
+ (mt/user-http-request :crowberto :get 200 "ee/apps/demo")))
+ (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle"))
(testing "re-enabling restores serving"
(is (=? {:enabled true}
- (mt/user-http-request :crowberto :put 200 "apps/demo" {:enabled true})))
- (is (=? {:name "demo"} (mt/user-http-request :crowberto :get 200 "apps/demo"))))))))
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled true})))
+ (is (=? {:name "demo"} (mt/user-http-request :crowberto :get 200 "ee/apps/demo"))))))))
(deftest sandbox-host-endpoint-test
(mt/with-premium-features #{:data-apps}
- (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/sandbox-host")
+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 "ee/apps/sandbox-host")
headers (:headers resp)]
(testing "serves a minimal HTML document"
(is (str/includes? (:body resp) "<!doctype html>"))
@@ -412,4 +414,149 @@
(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
(create-app!)
(is (= 200 (:status (mt/user-http-request-full-response
- :crowberto :get 200 "apps/sandbox-host"))))))))
+ :crowberto :get 200 "ee/apps/sandbox-host"))))))))
+
+(deftest create-defaults-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
+ (testing "an app created from its slug alone is a disabled app with an empty bundle and its collection"
+ (is (=? {:name "demo"
+ :display_name "demo"
+ :bundle_path "dist/index.js"
+ :enabled false
+ :resource_collection_id pos-int?}
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})))
+ (is (= 0 (alength ^bytes (t2/select-one-fn :bundle :model/DataApp :name "demo"))))
+ (is (=? {:name "demo" :enabled false} (mt/user-http-request :crowberto :get 200 "ee/apps/demo")))))))
+
+(deftest replace-bundle-endpoint-test
+ (mt/test-helpers-set-global-values!
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo" :enabled true})
+ (testing "a non-superuser cannot replace the bundle"
+ (mt/user-http-request :rasta :put 403 "ee/apps/demo/bundle" {:bundle "MINE"}))
+ (testing "the new bundle is what the app serves"
+ (let [{:keys [bundle_hash]} (mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "V1"})]
+ (is (str/includes? (str (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle")) "V1"))
+ (is (not= bundle_hash
+ (:bundle_hash (mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "V2"}))))
+ (is (str/includes? (str (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle")) "V2"))))
+ (testing "a missing app 404s"
+ (mt/user-http-request :crowberto :put 404 "ee/apps/missing/bundle" {:bundle "V1"}))))))
+
+(deftest admins-write-the-resource-collection-through-the-api-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-actions-enabled
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card :model/Action]
+ (let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})
+ query (lib/query (mt/metadata-provider) (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))]
+ (testing "an admin creates a question in the app's collection"
+ (is (=? {:collection_id collection-id}
+ (mt/user-http-request :crowberto :post 200 "card" {:name "Venues"
+ :collection_id collection-id
+ :display "table"
+ :visualization_settings {}
+ :dataset_query query}))))
+ (testing "an admin creates an action in the app's collection"
+ (is (=? {:collection_id collection-id}
+ (mt/user-http-request :crowberto :post 200 "action"
+ {:name "Rename venues"
+ :type "query"
+ :database_id (mt/id)
+ :collection_id collection-id
+ :dataset_query (lib/native-query (mt/metadata-provider)
+ "UPDATE venues SET name = {{name}}")
+ :parameters [{:id "name" :slug "name" :type "string/="
+ :target [:variable [:template-tag "name"]]}]}))))
+ (testing "nobody else writes it"
+ (mt/user-http-request :rasta :post 403 "card" {:name "Mine"
+ :collection_id collection-id
+ :display "table"
+ :visualization_settings {}
+ :dataset_query query})))))))
+
+(deftest typed-schema-endpoint-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-actions-enabled
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card :model/Action]
+ (let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})
+ mp (mt/metadata-provider)
+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ (lib/aggregate (lib/count))
+ (lib/breakout (lib.metadata/field mp (mt/id :venues :category_id))))
+ card (mt/user-http-request :crowberto :post 200 "card" {:name "Venues by category"
+ :collection_id collection-id
+ :display "table"
+ :visualization_settings {}
+ :dataset_query query})
+ action-id (:id (mt/user-http-request :crowberto :post 200 "action"
+ {:name "Rename venue"
+ :type "query"
+ :database_id (mt/id)
+ :collection_id collection-id
+ :dataset_query (lib/native-query mp "UPDATE venues SET name = {{name}} WHERE id = {{id}}")
+ :parameters [{:id "name" :slug "name" :type "string/="
+ :target [:variable [:template-tag "name"]]
+ :required true}
+ {:id "id" :slug "id" :type "number/="
+ :target [:variable [:template-tag "id"]]}]}))
+ action-entity-id (t2/select-one-fn :entity_id :model/Action :id action-id)]
+ (testing "a non-superuser cannot read it"
+ (mt/user-http-request :rasta :get 403 "ee/apps/demo/typed-schema"))
+ (let [{:keys [body headers]} (mt/user-http-request-full-response :crowberto :get 200 "ee/apps/demo/typed-schema")]
+ (is (str/starts-with? (get headers "Content-Type") "text/typescript"))
+ (testing "the module says how to fetch it again"
+ (is (str/includes? body "// mb data-app typed-schema demo --out src/metabase.ts\n")))
+ (testing "the module imports the define functions"
+ (is (str/includes? body "import { defineAction, defineQuery } from \"@metabase/embedding-sdk-react/data-app\";")))
+ (testing "a question is a card source addressed by its entity ID and typed by its result columns"
+ (is (str/includes? body "export const queries = {\n venuesByCategory: defineQuery({"))
+ (is (str/includes? body (str "entityId: \"" (:entity_id card) "\"")))
+ (is (str/includes? body "name: \"count\""))
+ (is (str/includes? body "jsType: \"number\"")))
+ (testing "an action is addressed by its entity ID and typed by its parameters"
+ (is (str/includes? body "export const actions = {\n renameVenue: defineAction({"))
+ (is (str/includes? body (str "id: \"" action-entity-id "\"")))
+ (is (str/includes? body "slug: \"name\",\n displayName: \"name\",\n jsType: \"string\",\n required: true,"))
+ (is (str/includes? body "slug: \"id\",\n displayName: \"id\",\n jsType: \"number\",")))))))))
+
+(defn- create-question!
+ "A table question named `card-name` in the collection with `collection-id`, reading `query`, created by an admin."
+ [collection-id card-name query]
+ (mt/user-http-request :crowberto :post 200 "card" {:name card-name
+ :collection_id collection-id
+ :display "table"
+ :visualization_settings {}
+ :dataset_query query}))
+
+(deftest disabled-apps-are-listed-only-to-superusers-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]
+ (let [{:keys [id]} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})]
+ (group-access/add-groups! (t2/select-one :model/DataApp :id id) [(:id (perms/all-users-group))])
+ (testing "an assigned user doesn't see the disabled app"
+ (is (not-any? #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))
+ (testing "a superuser does"
+ (is (some #(= "demo" (:name %)) (mt/user-http-request :crowberto :get 200 "ee/apps"))))
+ (testing "an assigned user sees it once it's enabled"
+ (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled true})
+ (is (some #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))))))
+
+(deftest typed-schema-keys-are-the-same-on-every-instance-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card]
+ (let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})
+ mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :venues)))
+ _ (create-question! collection-id "Café crème" query)
+ twins [(create-question! collection-id "Twin" query) (create-question! collection-id "Twin" query)]
+ symbols (create-question! collection-id "???" query)
+ body (mt/user-http-request :crowberto :get 200 "ee/apps/demo/typed-schema")
+ eid-part #(str/replace (:entity_id %) "-" "")]
+ (testing "a name keeps its letters, whatever their script"
+ (is (str/includes? body " caféCrème: defineQuery({")))
+ (testing "names that collide, or that have no letters, are told apart by entity ID"
+ (doseq [twin twins]
+ (is (str/includes? body (str " twin_" (eid-part twin) ": defineQuery({"))))
+ (is (str/includes? body (str " entity_" (eid-part symbols) ": defineQuery({"))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj
index ad4b9ccaadc..c3379b7bea9 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj
@@ -4,7 +4,8 @@
[clojure.test :refer :all]
[metabase.config.core :as config]
[metabase.server.middleware.security :as mw.security]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -55,15 +56,10 @@
:allowed_hosts ["https://api.example.com"]}]
(testing "with the :data-apps feature the EE impl returns the app's allowed_hosts"
(mt/with-premium-features #{:data-apps}
- (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts "boba")))))
+ (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts "boba")))
+ (testing "including a disabled app's, whose document only superusers reach"
+ (t2/update! :model/DataApp {:name "boba"} {:enabled false})
+ (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts "boba"))))))
(testing "without the feature it falls back to []"
(mt/with-premium-features #{}
- (is (= [] (mw.security/data-app-connect-src-hosts "boba")))))
- (testing "a disabled app contributes nothing"
- (mt/with-premium-features #{:data-apps}
- (mt/with-temp [:model/DataApp _ {:name "off"
- :display_name "Off"
- :bundle_path "x"
- :enabled false
- :allowed_hosts ["https://nope.example.com"]}]
- (is (= [] (mw.security/data-app-connect-src-hosts "off"))))))))
+ (is (= [] (mw.security/data-app-connect-src-hosts "boba")))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj
deleted file mode 100644
index b45bc1774d2..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/app_test.clj
+++ /dev/null
@@ -1,103 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.app-test
- (:require
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.config :as data-apps.config]
- [metabase-enterprise.data-apps.generate.app :as generate.app]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]
- [metabase.util.yaml :as yaml]))
-
-(use-fixtures :once (fixtures/initialize :db :web-server :test-users))
-
-(defn- file-yaml
- "The parsed YAML of the file at `path` among the `files` of a generated app."
- [{:keys [files]} path]
- (some-> (first (filter #(= path (:path %)) files)) :yaml yaml/parse-string))
-
-(deftest generates-the-manifest-and-the-collection-file-test
- (let [result (generate.app/generate {:name "Sales Ops"})
- app (file-yaml result "data_apps/sales-ops/data_app.yaml")
- collection-id (:entity_id (file-yaml result (data-apps.tu/collection-path "Data App: sales-ops")))]
- (testing "a manifest and a collection file, each at the path a remote-sync export writes it"
- (is (= ["data_apps/sales-ops/data_app.yaml" "collections/data_apps/data_app__sales_ops.yaml"]
- (map :path (:files result)))))
- (testing "the manifest names the app, its bundle, its version and the collection it owns"
- (is (=? {:name "Sales Ops"
- :slug "sales-ops"
- :path "dist/index.js"
- :version data-apps.config/supported-app-version
- :collection collection-id
- :entity_id string?
- :serdes/meta [{:model "DataApp"}]}
- app))
- (is (not (contains? app :description))))
- (testing "the collection is in the data-apps namespace, named after the app's slug as the instance names it"
- (is (=? {:name "Data App: sales-ops"
- :namespace "data-apps"
- :entity_id string?
- :serdes/meta [{:model "Collection"}]}
- (file-yaml result "collections/data_apps/data_app__sales_ops.yaml"))))
- (testing "the entity IDs are new on each call"
- (is (not= (:entity_id app)
- (:entity_id (file-yaml (generate.app/generate {:name "Sales Ops"})
- "data_apps/sales-ops/data_app.yaml")))))))
-
-(deftest keeps-the-description-and-an-explicit-slug-test
- (let [result (generate.app/generate {:name "Sales Ops" :slug "ops" :description "Pipeline health"})]
- (is (=? {:slug "ops" :description "Pipeline health"}
- (file-yaml result "data_apps/ops/data_app.yaml")))
- (testing "the collection is named after the slug, so apps sharing a display name don't share a collection file"
- (is (=? {:name "Data App: ops"} (file-yaml result "collections/data_apps/data_app__ops.yaml"))))))
-
-(deftest keeps-the-entity-ids-of-an-existing-app-test
- (let [app-id "existingAppIdxxxxxxxx"
- collection-id "existingCollIdxxxxxxx"
- result (generate.app/generate {:name "Sales Ops" :entity_id app-id :collection_id collection-id})]
- (is (=? {:entity_id app-id :collection collection-id}
- (file-yaml result "data_apps/sales-ops/data_app.yaml")))
- (is (=? {:entity_id collection-id}
- (file-yaml result "collections/data_apps/data_app__sales_ops.yaml")))))
-
-(deftest derives-the-slug-from-the-name-test
- (are [app-name slug] (some? (file-yaml (generate.app/generate {:name app-name})
- (str "data_apps/" slug "/data_app.yaml")))
- "Sales Ops" "sales-ops"
- " Q3 -- Revenue!" "q3-revenue"
- "inventory 2" "inventory-2"))
-
-(deftest refuses-a-name-without-a-valid-slug-test
- (doseq [app-name ["???" "Generate"]]
- (is (= 400 (:status-code (ex-data (is (thrown? clojure.lang.ExceptionInfo
- (generate.app/generate {:name app-name}))))))
- app-name))
- (testing "an explicit slug is checked too"
- (is (thrown? clojure.lang.ExceptionInfo (generate.app/generate {:name "Sales" :slug "Not A Slug"})))))
-
-(deftest api-generates-the-files-test
- (mt/with-premium-features #{:data-apps}
- (testing "a superuser gets the files"
- (is (=? {:files [{:path "data_apps/sales-ops/data_app.yaml" :yaml string?}
- {:path "collections/data_apps/data_app__sales_ops.yaml" :yaml string?}]}
- (mt/user-http-request :crowberto :post 200 "apps/generate/app" {:name "Sales Ops"}))))
- (testing "an explicit slug is kept"
- (is (=? {:files [{:path "data_apps/ops/data_app.yaml"} {}]}
- (mt/user-http-request :crowberto :post 200 "apps/generate/app" {:name "Sales Ops" :slug "ops"}))))
- (testing "an existing app's entity IDs are kept"
- (let [{[app collection] :files} (mt/user-http-request :crowberto :post 200 "apps/generate/app"
- {:name "Sales Ops"
- :entity_id "existingAppIdxxxxxxxx"
- :collection_id "existingCollIdxxxxxxx"})]
- (is (=? {:entity_id "existingAppIdxxxxxxxx" :collection "existingCollIdxxxxxxx"}
- (yaml/parse-string (:yaml app))))
- (is (=? {:entity_id "existingCollIdxxxxxxx"}
- (yaml/parse-string (:yaml collection))))))
- (testing "an entity ID that is not a NanoID is refused"
- (mt/user-http-request :crowberto :post 400 "apps/generate/app" {:name "Sales Ops" :entity_id "not-an-id"}))
- (testing "only a superuser writes an app's repository"
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "apps/generate/app" {:name "Sales Ops"}))))
- (testing "a name that gives no valid slug is refused"
- (mt/user-http-request :crowberto :post 400 "apps/generate/app" {:name "???"}))
- (testing "a reserved slug is refused"
- (mt/user-http-request :crowberto :post 400 "apps/generate/app" {:name "Sales Ops" :slug "generate"}))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/resources_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/resources_test.clj
deleted file mode 100644
index aa9b0dc77dc..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/resources_test.clj
+++ /dev/null
@@ -1,641 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.resources-test
- (:require
- [clojure.string :as str]
- [clojure.test :refer :all]
- [clojure.walk :as walk]
- [medley.core :as m]
- [metabase-enterprise.data-apps.generate.resources :as generate.resources]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.actions.core :as actions]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.lib.test-util :as lib.tu]
- [metabase.models.serialization :as serdes]
- [metabase.test :as mt]
- [metabase.util :as u]
- [metabase.util.malli.fn :as mu.fn]
- [metabase.util.yaml :as yaml]
- [toucan2.core :as t2]))
-
-(set! *warn-on-reflection* true)
-
-(def ^:private app-collection "appCollectionEntity01")
-
-(def ^:private other-collection "otherCollectionEnt001")
-
-(defn- do-with-collections!
- "Call `f` with the collections `app-collection` and `other-collection` in place."
- [f]
- (mt/with-temp [:model/Collection _ {:entity_id app-collection :name "App collection" :namespace "data-apps"}
- :model/Collection _ {:entity_id other-collection :name "Other collection" :namespace "data-apps"}]
- (f)))
-
-(defn- query-item
- "A query to serialize named `query-name`, in `app-collection` with a fresh entity ID unless `extra` says otherwise."
- [query-name definition & {:as extra}]
- (merge {:name query-name :query definition :entity_id (u/generate-nano-id) :collection_id app-collection} extra))
-
-(defn- action-item
- "An action to serialize, in `app-collection` with a fresh entity ID unless `action` is a map saying otherwise."
- [action]
- (merge {:entity_id (u/generate-nano-id) :collection_id app-collection}
- (if (map? action) action {:action_id action})))
-
-(defn- request-body [{:keys [queries actions]}]
- {:queries (vec queries) :actions (mapv action-item actions)})
-
-(defn- generate!
- "Generate the files of `body` as `user` over HTTP, with the collections in place."
- ([user status body]
- (generate! user status body #{:data-apps}))
- ([user status body features]
- (do-with-collections!
- (fn []
- (mt/with-premium-features features
- (mt/user-http-request user :post status "apps/generate/resources" (request-body body)))))))
-
-(defn- generate-directly!
- "Generate the files of `body` as a superuser by calling the function, for a test that redefines what runs on the request thread,
- with the types of its definitions as keywords, as the endpoint hands them over."
- [body]
- (do-with-collections!
- (fn []
- (mt/with-premium-features #{:data-apps}
- (mt/with-current-user (mt/user->id :crowberto)
- (generate.resources/generate
- (walk/postwalk #(cond-> % (and (map? %) (string? (:type %))) (update :type keyword))
- (request-body body))))))))
-
-(defn- file-entity
- "The entity a serialization `file` holds."
- [{:keys [yaml]}]
- (yaml/parse-string yaml))
-
-(defn- db-name []
- (t2/select-one-fn :name :model/Database (mt/id)))
-
-(defn- table-path [table]
- [(db-name) "PUBLIC" table])
-
-(defn- field-path [table field]
- [(db-name) "PUBLIC" table field])
-
-(defn- venues-column [field-name & {:as extra}]
- (merge {:type "column" :name field-name :tableId (mt/id :venues) :sourceName "VENUES"} extra))
-
-(defn- venues-definition [& {:as stage}]
- {:stages [(merge {:source {:type "table" :id (mt/id :venues)}} stage)]})
-
-(defn- metric-count-definition [metric-id]
- (venues-definition :aggregations [{:type "metric" :id metric-id}]))
-
-(deftest serialization-needs-each-items-entity-id-name-and-collection-test
- (testing "the saved question is written with the given entity ID, name and collection, so every query names them"
- (mt/with-premium-features #{:data-apps}
- (doseq [missing [:name :entity_id :collection_id]]
- (testing (pr-str missing)
- (is (=? {:errors {:queries some?}}
- (mt/user-http-request :crowberto :post 400 "apps/generate/resources"
- {:queries [(dissoc (query-item "Venues" (venues-definition)) missing)]
- :actions []}))))))))
-
-(deftest serialization-needs-each-actions-entity-id-and-collection-test
- (mt/with-premium-features #{:data-apps}
- (doseq [missing [:entity_id :collection_id]]
- (testing (pr-str missing)
- (is (=? {:errors {:actions some?}}
- (mt/user-http-request :crowberto :post 400 "apps/generate/resources"
- {:queries []
- :actions [(dissoc (action-item 1) missing)]})))))))
-
-(deftest serializes-into-a-collection-absent-from-the-instance-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [action-id]}]
- (let [response (generate! :crowberto 200
- {:queries [(query-item "Gone" (venues-definition) :collection_id "noSuchCollectionEnt01")]
- :actions [{:action_id action-id :collection_id "noSuchCollectionEnt01"}]})]
- (is (=? {:queries [{:file "gone.yaml"}] :actions [{:file string?}]} response))
- (doseq [file (concat (:queries response) (:actions response))]
- (is (= "noSuchCollectionEnt01" (:collection_id (file-entity file)))))))))
-
-(deftest names-files-after-their-entities-and-deduplicates-them-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id]}]
- (is (=? {:queries [{:file "orders.yaml"} {:file "orders_2.yaml"} {:file "venue_count.yaml"}]
- :metrics [{:file string?}]}
- (generate! :crowberto 200
- {:queries [(query-item "Orders" (venues-definition))
- (query-item "Orders" (venues-definition))
- (query-item "Venue count" (metric-count-definition metric-id))]}))))))
-
-(deftest serializes-a-query-with-every-reference-portable-test
- (let [response (generate! :crowberto 200
- {:queries [(query-item "PriceByCategory"
- (venues-definition
- :filters [{:type "operator" :operator ">"
- :args [(venues-column "PRICE")
- {:type "literal" :value 1}]}]
- :aggregations [{:type "operator" :operator "sum" :name "total"
- :args [(venues-column "PRICE")]}]
- :breakouts [{:type "column"
- :name "NAME"
- :sourceName "CATEGORIES"
- :sourceFieldId (mt/id :venues :category_id)}]
- :orderBys [{:type "column"
- :name "total"
- :direction "desc"}]
- :limit 5))]})
- entity (file-entity (first (:queries response)))]
- (testing "the query Metabase builds from the definition, referencing tables and fields by name"
- (let [price (field-path "VENUES" "PRICE")
- category-id (field-path "VENUES" "CATEGORY_ID")
- category (field-path "CATEGORIES" "NAME")]
- (is (=? {:dataset_query {:lib/type "mbql/query"
- :database (db-name)
- :stages [{:source-table (table-path "VENUES")
- :filters [[">" {} ["field" {} price] 1]]
- :aggregation [["sum" {:name "total"} ["field" {} price]]]
- :breakout [["field" {:source-field category-id} category]]
- :order-by [["desc" {} ["aggregation"
- {:lib/source-name "total"}
- string?]]]
- :limit 5}]}}
- entity))
- (is (empty? (:metrics response)))))
- (testing "only the uuid the order by points at is kept, and it is the aggregation's"
- (let [stage (-> entity :dataset_query :stages first)]
- (is (= (get-in stage [:aggregation 0 1 :lib/uuid])
- (get-in stage [:order-by 0 2 2])))
- (is (nil? (get-in stage [:filters 0 1 :lib/uuid])))))))
-
-(deftest only-a-superuser-generates-test
- (testing "an app's resources are written into its repository, which only an admin works with"
- (let [body {:queries [(query-item "Venues" (venues-definition))]}]
- (is (= "You don't have permissions to do that." (generate! :rasta 403 body)))
- (is (=? {:queries [{:file string? :yaml string?}]} (generate! :crowberto 200 body))))))
-
-(deftest lists-the-metrics-a-query-aggregates-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id]}]
- (let [metric-eid (t2/select-one-fn :entity_id :model/Card :id metric-id)
- response (generate! :crowberto 200
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]})
- metric (file-entity (first (:metrics response)))
- question (file-entity (first (:queries response)))]
- (is (= 1 (count (:metrics response))))
- (is (=? {:type "metric"
- :collection_id app-collection
- :dataset_query {:stages [{:source-table (table-path "VENUES")}]}}
- metric))
- (testing "the copy has its own entity ID, and the question reads the copy rather than the source metric"
- (is (not= metric-eid (:entity_id metric)))
- (is (= (:entity_id metric) (get-in question [:dataset_query :stages 0 :aggregation 0 2])))
- (is (not= metric-eid (get-in question [:dataset_query :stages 0 :aggregation 0 2]))))
- (testing "the copy's entity ID is the same every time"
- (is (= (:entity_id metric)
- (-> (generate! :crowberto 200 {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]})
- :metrics first file-entity :entity_id))))))))
-
-(deftest copies-a-metric-once-per-collection-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id]}]
- (let [definition (metric-count-definition metric-id)
- response (generate! :crowberto 200
- {:queries [(query-item "A" definition)
- (query-item "B" definition)
- (query-item "C" definition :collection_id other-collection)]})
- metrics (mapv file-entity (:metrics response))
- references (mapv #(get-in (file-entity %) [:dataset_query :stages 0 :aggregation 0 2]) (:queries response))]
- (is (= 2 (count metrics)))
- (is (= #{app-collection other-collection} (set (map :collection_id metrics))))
- (is (apply distinct? (map :entity_id metrics)))
- (is (= (first references) (second references)))
- (is (= (set references) (set (map :entity_id metrics))))))))
-
-(deftest answers-each-query-on-its-own-test
- (let [response (generate! :crowberto 200
- {:queries [(query-item "Venues" (venues-definition))
- (query-item "Broken" (venues-definition :fields [(venues-column "NOT_A_COLUMN")]))]})]
- (is (=? [{:file string? :yaml string?} {:error "No column found"}]
- (:queries response)))
- (is (=? {:dataset_query {:stages [{:source-table (table-path "VENUES")}]}}
- (file-entity (first (:queries response)))))))
-
-(deftest serializes-a-query-action-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [action-id]}]
- (let [response (generate! :crowberto 200 {:actions [{:action_id action-id :entity_id "actionCopyEntity00001"}]})
- entity (file-entity (first (:actions response)))]
- (is (=? {:entity_id "actionCopyEntity00001"
- :collection_id app-collection
- :type "query"
- :query [{:database_id (db-name)
- :dataset_query {:database (db-name)}}]}
- entity))
- (is (not (contains? entity :model_id))
- "it names no model")))))
-
-(deftest refuses-an-action-that-belongs-to-a-model-test
- (testing "a data app runs only actions that belong to no model, as the typed schema lists only those"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [model-action-id]}]
- (is (=? {:actions [{:error #"Action \d+ belongs to a model\..*"}]}
- (generate! :crowberto 200 {:actions [model-action-id]})))))))
-
-(deftest refuses-what-a-copy-cannot-hold-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id model-id]}]
- (let [mp (mt/metadata-provider)
- model-count (lib/aggregate (lib/query mp (lib.metadata/card mp model-id)) (lib/count))]
- (mt/with-temp [:model/Card {reading-metric-id :id} {:name "Metric reading a model"
- :type :metric
- :database_id (mt/id)
- :dataset_query model-count}]
- (let [card-sql-id (actions/insert! {:name "Read a card"
- :type :query
- :database_id (mt/id)
- :dataset_query (lib/native-query
- mp (str "SELECT * FROM {{#" metric-id "}}"))})
- response (generate! :crowberto 200
- {:queries [(query-item "ReadsMetric" (metric-count-definition reading-metric-id))]
- :actions [card-sql-id Integer/MAX_VALUE]})]
- (testing "a query action whose SQL reads a card"
- (is (=? {:error (re-pattern (str ".*reads card " metric-id ".*"))}
- (nth (:actions response) 0))))
- (testing "an action that does not exist"
- (is (=? {:error #".*does not exist.*"} (nth (:actions response) 1))))
- (testing "a metric that reads a card; the query that aggregates it is still built"
- (is (=? {:queries [{:file string?}]
- :metrics [{:error (re-pattern (str ".*reads card " model-id ".*"))}]}
- response)))))))))
-
-(deftest rejects-unsupported-definitions-test
- (testing "the request accepts only what data app definitions support"
- (doseq [path [[:queries 0 :query :stages 0 :joins]
- [:queries 0 :query :stages 0 :expressions]]]
- (testing (pr-str path)
- (do-with-collections!
- (fn []
- (mt/with-premium-features #{:data-apps}
- (mt/user-http-request :crowberto :post 400 "apps/generate/resources"
- (assoc-in {:queries [(query-item "Venues" (venues-definition))] :actions []}
- path [])))))))))
-
-(deftest refuses-a-table-that-does-not-exist-test
- (is (=? {:queries [{:error (str "Table " Integer/MAX_VALUE " does not exist.")}]}
- (generate! :crowberto 200
- {:queries [(query-item "Nothing" {:stages [{:source {:type "table" :id Integer/MAX_VALUE}}]})]})))
- (testing "a deactivated table is gone too, as it is from the typed schema"
- (mt/with-temp [:model/Table {table-id :id} {:db_id (mt/id) :active false}]
- (is (=? {:queries [{:error (str "Table " table-id " does not exist.")}]}
- (generate! :crowberto 200
- {:queries [(query-item "Gone" {:stages [{:source {:type "table" :id table-id}}]})]}))))))
-
-(deftest refuses-a-column-that-reaches-a-deactivated-table-test
- (testing "types generated before categories was deactivated still select its name through venues.category_id, and
- the query would fail on the implicit join when it runs"
- (mt/with-temp-vals-in-db :model/Table (mt/id :categories) {:active false}
- (is (=? {:queries [{:error (str "Table " (mt/id :categories) " does not exist.")}]}
- (generate! :crowberto 200
- {:queries [(query-item "VenueCategories"
- (venues-definition
- :fields [{:type "column" :name "NAME"
- :sourceFieldId (mt/id :venues :category_id)}]))]}))))))
-
-(deftest refuses-a-metric-that-reaches-a-deactivated-table-test
- (testing "a metric the query aggregates reads categories through venues.category_id in its own query, which the
- built query holds only the ID of, and the query would fail on the table when it runs"
- (let [mp (mt/metadata-provider)
- venues (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- categories (mt/id :categories)
- category (m/find-first (comp #{(mt/id :categories :name)} :id) (lib/filterable-columns venues))]
- (mt/with-temp [:model/Card {bars-id :id} {:name "Bars"
- :type :metric
- :database_id (mt/id)
- :dataset_query (-> venues
- (lib/filter (lib/= category "Bar"))
- (lib/aggregate (lib/count)))}
- :model/Card {count-id :id} {:name "Venue count"
- :type :metric
- :database_id (mt/id)
- :dataset_query (lib/aggregate venues (lib/count))}]
- (mt/with-temp-vals-in-db :model/Table categories {:active false}
- (is (=? {:queries [{:error (str "Table " categories " does not exist.")}
- {:file string?}]
- :metrics [{:file string?}]}
- (generate! :crowberto 200
- {:queries [(query-item "BarCount" (metric-count-definition bars-id))
- (query-item "VenueCount" (metric-count-definition count-id))]}))))))))
-
-(deftest refuses-a-column-remapped-to-a-deactivated-table-test
- (testing "venues.category_id displays categories.name, a join the query processor adds only when the query runs, so
- the built query never names categories"
- (let [categories (mt/id :categories)]
- (mt/with-column-remappings [venues.category_id categories.name]
- (mt/with-temp-vals-in-db :model/Table categories {:active false}
- (is (=? {:queries [{:error (str "Table " categories " does not exist.")}
- {:file string?}]}
- (generate! :crowberto 200
- {:queries [(query-item "VenueCategoryIds"
- (venues-definition :fields [{:type "column" :name "CATEGORY_ID"}]))
- (query-item "VenuePrices"
- (venues-definition :fields [{:type "column" :name "PRICE"}]))]}))))))))
-
-(deftest refuses-a-definition-that-builds-an-invalid-query-test
- (testing "the request schema accepts what a type lets through, and lib's own checks are off in production, so the
- built query is checked: an invalid one must not serialize and then fail when it runs"
- (binding [mu.fn/*enforce* false]
- (is (=? {:queries [{:error "The definition does not build a valid query."}
- {:file string?}]}
- (generate-directly!
- {:queries [(query-item "Half" {:stages [{:source {:type :table :id (mt/id :venues)} :limit 1.5}]})
- (query-item "Whole" {:stages [{:source {:type :table :id (mt/id :venues)} :limit 2}]})]}))))))
-
-(deftest a-public-source-serializes-as-the-private-copy-the-author-writes-test
- (testing "what makes a source public or embedded is left out, since the pull refuses a copy that says it is"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id action-id]}]
- (t2/update! :model/Card :id metric-id {:public_uuid (str (random-uuid))
- :made_public_by_id (mt/user->id :crowberto)
- :enable_embedding true
- :embedding_params {}})
- (t2/update! :model/Action :id action-id {:public_uuid (str (random-uuid))
- :made_public_by_id (mt/user->id :crowberto)})
- (let [{:keys [actions metrics] :as response}
- (generate! :crowberto 200
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]
- :actions [action-id]})]
- (is (=? {:actions [{:file string?}] :metrics [{:file string?}]} response))
- (doseq [file (concat actions metrics)]
- (is (not-any? (partial contains? (file-entity file))
- [:public_uuid :made_public_by_id :enable_embedding :embedding_params :embedding_type]))))))))
-
-(deftest an-item-serialization-cannot-extract-answers-with-the-cause-test
- (testing "one entity that fails inside its extraction comes back with the reason, which serialization wraps at each
- level, and the rest still serialize"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id action-id]}]
- (t2/update! :model/Card :id metric-id {:visualization_settings {:broken true}})
- (let [export-settings (mt/original-fn #'serdes/export-visualization-settings)]
- (mt/with-dynamic-fn-redefs [serdes/export-visualization-settings (fn [settings]
- (if (:broken settings)
- (throw (ex-info "the metric is broken" {}))
- (export-settings settings)))]
- (is (=? {:queries [{:file string?}]
- :actions [{:file string?}]
- :metrics [{:error (str "Could not serialize Metric " metric-id ": the metric is broken")}]}
- (generate-directly!
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]
- :actions [action-id]})))))))))
-
-(deftest an-item-serialization-leaves-out-answers-without-a-reason-test
- (testing "a card materialized by an exploration Summary is one serialization leaves out without an error"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id]}]
- (mt/with-temp [:model/Exploration {exploration-id :id} {:name "Explo" :creator_id (mt/user->id :crowberto)}
- :model/Document {summary-id :id} {:name "Summary"
- :creator_id (mt/user->id :crowberto)
- :exploration_id exploration-id}]
- (t2/update! :model/Card :id metric-id {:document_id summary-id})
- (is (=? {:metrics [{:error (str "Could not serialize Metric " metric-id ".")}]}
- (generate! :crowberto 200
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]}))))))))
-
-(deftest a-failure-of-the-serialization-is-logged-and-a-refusal-is-not-test
- (testing "a refusal is the author's to act on; anything else is a failure the server keeps a trace of"
- (mt/with-log-messages-for-level [messages [metabase-enterprise.data-apps.generate.resources :warn]]
- (is (=? {:queries [{:error "No column found"}
- {:error (str "Table " Integer/MAX_VALUE " does not exist.")}]}
- (generate-directly!
- {:queries [(query-item "Broken"
- {:stages [{:source {:type :table :id (mt/id :venues)}
- :fields [{:type :column :name "NOT_A_COLUMN"}]}]})
- (query-item "Nothing" {:stages [{:source {:type :table :id Integer/MAX_VALUE}}]})]})))
- (let [logged (filter #(str/includes? (:message %) "Could not serialize a data app resource") (messages))]
- (is (= 1 (count logged)))
- (testing "naming the query by its entity ID, without its definition"
- (is (str/includes? (:message (first logged)) ":query"))
- (is (not (str/includes? (:message (first logged)) "NOT_A_COLUMN")))
- (is (not (str/includes? (:message (first logged)) "Broken"))))))))
-
-(deftest refuses-archived-sources-test
- (testing "the pull refuses an archived resource, so the serialization refuses an archived source"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [action-id]}]
- (t2/update! :model/Action :id action-id {:archived true})
- (is (=? {:actions [{:error #".*is archived.*"}]}
- (generate! :crowberto 200 {:actions [action-id]})))))))
-
-(deftest refuses-an-action-whose-parameter-values-come-from-a-card-test
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id]}]
- (let [mp (mt/metadata-provider)
- action-id (actions/insert! {:name "Pick"
- :type :query
- :database_id (mt/id)
- :dataset_query (lib/native-query mp "UPDATE venues SET name = {{name}}")
- :parameters [{:id "name" :slug "name" :type :string/=
- :values_source_type :card
- :values_source_config {:card_id metric-id}}]})]
- (is (=? {:actions [{:error (re-pattern (str ".*reads card " metric-id ".*"))}]}
- (generate! :crowberto 200 {:actions [action-id]})))))))
-
-(deftest serializes-a-segment-reference-by-entity-id-test
- (let [mp (mt/metadata-provider)]
- (mt/with-temp [:model/Segment {segment-id :id, segment-eid :entity_id}
- {:name "Cheap"
- :table_id (mt/id :venues)
- :definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/filter (lib/< (lib.metadata/field mp (mt/id :venues :price)) 3)))}]
- (is (=? {:dataset_query {:stages [{:filters [["segment" {} segment-eid]]}]}}
- (-> (generate! :crowberto 200
- {:queries [(query-item "Cheap"
- (venues-definition :filters [{:type "segment" :id segment-id}]))]})
- :queries first file-entity))))))
-
-(deftest refuses-sources-on-a-routing-destination-test
- (testing "a routing destination is reachable only through its router, so the typed schema leaves out what it backs,
- and so does the serialization"
- (mt/with-temp [:model/Database {router-id :id} {}
- :model/DatabaseRouter _ {:database_id router-id :user_attribute "region"}
- :model/Database {destination-id :id} {:router_database_id router-id}]
- (let [action-id (actions/insert! {:name "Rename venue"
- :type :query
- :database_id destination-id
- :dataset_query {:lib/type :mbql/query
- :database destination-id
- :stages [{:lib/type :mbql.stage/native
- :native "UPDATE venues SET name = 'x'"}]}})]
- (is (=? {:actions [{:error #".*is backed by a routing destination.*"}]}
- (generate! :crowberto 200 {:actions [action-id]})))))))
-
-(deftest refuses-a-source-whose-settings-read-a-card-test
- (testing "a copy keeps every card its serialization references, so a click behaviour linking a saved question is refused
- like a query reading one"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id model-id]}]
- (t2/update! :model/Card :id metric-id
- {:visualization_settings {:click_behavior {:type "link" :linkType "question" :targetId model-id}}})
- (is (=? {:metrics [{:error (re-pattern (str ".*reads card " model-id ".*"))}]}
- (generate! :crowberto 200
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]})))))))
-
-(deftest extracts-the-cards-and-the-actions-once-test
- (testing "the cards and the actions are each extracted in one serialization query, however many the app uses"
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id action-id model-action-id]}]
- (let [calls (atom [])
- extract (mt/original-fn #'generate.resources/extract-by-entity-id)]
- (mt/with-dynamic-fn-redefs [generate.resources/extract-by-entity-id (fn [model-name ids]
- (swap! calls conj model-name)
- (extract model-name ids))]
- (is (=? {:actions [{:file string?} {:error string?}] :metrics [{:file string?}]}
- (generate-directly!
- {:queries [(query-item "VenueCount" (metric-count-definition metric-id))]
- :actions [action-id model-action-id]}))))
- (is (= {"Card" 1 "Action" 1} (frequencies @calls))))))))
-
-(deftest serializes-the-saved-question-an-author-writes-test
- (testing "a query comes back as the saved question that holds it: named as given, in the given collection, with the
- given entity ID, created by the caller, and nothing unset"
- (let [{:keys [file yaml] :as item} (-> (generate! :crowberto 200
- {:queries [(query-item "Venues list" (venues-definition :limit 5)
- :entity_id "savedQuestionEntity01")]})
- :queries first)
- entity (file-entity item)]
- (is (= "venues_list.yaml" file))
- (is (string? yaml))
- (is (=? {:serdes/meta [{:model "Card"}]
- :entity_id "savedQuestionEntity01"
- :collection_id app-collection
- :name "Venues list"
- :type "question"
- :display "table"
- :creator_id "[email protected]"
- :card_schema pos-int?
- :visualization_settings {}
- :parameters []
- :parameter_mappings []
- :dataset_query {:database (db-name)
- :stages [{:source-table (table-path "VENUES") :limit 5}]}}
- entity))
- (is (not-any? (partial contains? entity)
- [:description :collection_position :public_uuid :archived :enable_embedding])
- "what serialization leaves unset or at its default is left out, as the format omits it"))))
-
-(deftest keys-come-in-the-order-serialization-writes-them-test
- (testing "the printed entity comes in the order serialization writes a file, so an author keeps it"
- (let [{:keys [yaml] :as file} (-> (generate-directly! {:queries [(query-item "Venues" (venues-definition))]})
- :queries first)]
- (is (= [:database :stages :lib/type] (keys (:dataset_query (file-entity file)))))
- (is (< (str/index-of yaml "name:") (str/index-of yaml "\ndataset_query:"))))))
-
-(defn- venues-sum
- [column-name]
- {:type "operator" :operator "sum" :args [{:type "column" :name column-name}]})
-
-(deftest refuses-aggregations-sharing-column-name-test
- (testing "aggregations named alike would be read as one by a later stage or a result row, so the author names them"
- (is (=? {:queries [{:error "Breakouts and aggregations need unique column names: Sum of Price, Sum of Latitude share the column name \"sum\". Name them apart with the `name` option of `breakout` or of an aggregation helper, or with `aggregations.measure` or `aggregations.metric` for a measure or metric."}]}
- (generate! :crowberto 200
- {:queries [(query-item "Sums" {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [(venues-sum "PRICE") (venues-sum "LATITUDE")]}]})]})))))
-
-(deftest accepts-named-aggregations-test
- (let [{[file] :queries} (generate! :crowberto 200
- {:queries [(query-item "Sums" {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [(assoc (venues-sum "PRICE") :name "price")
- (venues-sum "LATITUDE")]}]})]})]
- (is (=? [["sum" {:name "price"} some?] ["sum" {} some?]]
- (get-in (file-entity file) [:dataset_query :stages 0 :aggregation])))))
-
-(deftest names-measure-test
- (testing "measure wrapped as `{name, value}` gets that column name, and its own `name` is left out"
- (let [mp (mt/metadata-provider)]
- (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :venues)
- :name "Revenue"
- :definition (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :venues :price)))))}]
- (let [measure {:type "measure" :id measure-id :name "Revenue" :tableId (mt/id :venues)}
- body (fn [measure]
- {:queries [(query-item "Revenue" {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [measure (venues-sum "LATITUDE")]}]})]})]
- (testing "unnamed, it shares the column name of the sum beside it"
- (is (=? {:queries [{:error #".*share the column name \"sum\".*"}]}
- (generate! :crowberto 200 (body measure)))))
- (let [{[file] :queries} (generate! :crowberto 200 (body {:name "revenue" :value measure :columns [{:name "revenue"}]}))]
- (is (=? [["measure" {:name "revenue"} some?] ["sum" {} some?]]
- (get-in (file-entity file) [:dataset_query :stages 0 :aggregation])))))))))
-
-(deftest refuses-order-by-on-shared-column-name-test
- (testing "order-by naming a column two aggregations share gets the same refusal, not Lib's ambiguity error"
- (is (=? {:queries [{:error #"Breakouts and aggregations need unique column names: Sum of Price, Sum of Latitude share the column name \"sum\"\..*"}]}
- (generate! :crowberto 200
- {:queries [(query-item "Sums" {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [(venues-sum "PRICE") (venues-sum "LATITUDE")]
- :order-bys [{:type "column" :name "sum"}]}]})]})))))
-
-(defn- check-unique-names
- "Calls the uniqueness check on `query`, returning the refusal's message or nil."
- [query]
- (try
- (#'generate.resources/check-unique-column-names query)
- nil
- (catch clojure.lang.ExceptionInfo e
- (ex-message e))))
-
-(deftest refuses-aggregation-sharing-breakout-field-name-test
- (testing "a `sum` aggregation next to a breakout on a field named `sum` collide"
- (let [mp (mt/metadata-provider)
- mp (lib.tu/mock-metadata-provider
- mp
- {:fields [(assoc (lib.metadata/field mp (mt/id :venues :name)) :name "sum")]})
- query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))
- (lib/breakout (lib.metadata/field mp (mt/id :venues :name)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :venues :price)))))]
- (is (=? #"Breakouts and aggregations need unique column names: .* share the column name \"sum\".*"
- (check-unique-names query))))))
-
-(deftest refuses-named-breakouts-sharing-name-test
- (let [mp (mt/metadata-provider)
- created-at (lib.metadata/field mp (mt/id :checkins :date))
- named (fn [column column-name] (lib/update-options (lib/ref column) assoc :name column-name))
- query (fn [first-name second-name]
- (-> (lib/query mp (lib.metadata/table mp (mt/id :checkins)))
- (lib/breakout (named (lib/with-temporal-bucket created-at :month) first-name))
- (lib/breakout (named (lib/with-temporal-bucket created-at :year) second-name))
- (lib/aggregate (lib/count))))]
- (testing "named breakouts sharing a name collide"
- (is (=? #".* share the column name \"period\".*" (check-unique-names (query "period" "period")))))
- (testing "named apart they don't"
- (is (nil? (check-unique-names (query "month" "year")))))
- (testing "the same field broken out twice without names collides too"
- (is (=? #".* share the column name \"DATE\".*"
- (check-unique-names (-> (lib/query mp (lib.metadata/table mp (mt/id :checkins)))
- (lib/breakout (lib/with-temporal-bucket created-at :month))
- (lib/breakout (lib/with-temporal-bucket created-at :year))
- (lib/aggregate (lib/count)))))))))
-
-(deftest names-breakout-test
- (testing "breakout sent as `{type: breakout, name, column}` keeps its name in the saved question"
- (let [{[file] :queries} (generate! :crowberto 200
- {:queries [(query-item "Venues by category"
- {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [{:type "operator" :operator "count"}]
- :breakouts [{:type "breakout"
- :name "category"
- :column {:type "column" :name "CATEGORY_ID"}}]}]})]})]
- (is (=? [["field" {:name "category"} some?]]
- (get-in (file-entity file) [:dataset_query :stages 0 :breakout]))))))
-
-(deftest unsupported-bin-count-lists-presets-test
- (is (=? {:queries [{:error "Unsupported bin count 20. Supported counts for this field are 10, 50, and 100."}
- {:file string?}]}
- (generate! :crowberto 200
- {:queries (for [num-bins [20 50]]
- (query-item (str "Venues by price " num-bins)
- (venues-definition
- :aggregations [{:type "operator" :operator "count"}]
- :breakouts [(venues-column "PRICE"
- :binning {:strategy "num-bins"
- :numBins num-bins})])))}))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/action_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/action_test.clj
deleted file mode 100644
index 879a50a0780..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/action_test.clj
+++ /dev/null
@@ -1,64 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.action-test
- (:require
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas.action :as schemas.action]
- [metabase.actions.core :as actions]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.test :as mt]))
-
-(defn- standalone-action-schema
- "The schema entry for a model-less query action created from `action` and `dataset-query`."
- [action dataset-query]
- (mt/with-temp [:model/Action {action-id :id} (merge {:type :query, :name "Update bird"} action)
- :model/QueryAction _ {:action_id action-id, :dataset_query dataset-query}]
- (schemas.action/action-schema (actions/select-action :id action-id))))
-
-(deftest action-schema-types-a-parameter-from-its-template-tag-test
- (testing ":category has no JS type of its own, so the persisted template tag's type decides it"
- (is (=? {:kind "action"
- :key "updateBird"
- :type "query"
- :parameters [{:slug "name", :displayName "Name", :jsType "string"}]}
- (standalone-action-schema
- {:parameters [{:id "name", :name "Name", :type :category, :target [:variable [:template-tag "name"]]}]}
- (lib/native-query (mt/metadata-provider) "UPDATE birds SET name = {{name}}"))))))
-
-(deftest action-schema-resolves-field-filter-widget-type-test
- (testing "a field filter's value type comes from its widget type, through a :dimension parameter target"
- (let [mp (mt/metadata-provider)]
- (is (=? {:parameters [{:slug "created_at", :displayName "Created At", :jsType "Date"}]}
- (standalone-action-schema
- {:parameters [{:id "created_at"
- :name "Created At"
- :type :category
- :target [:dimension [:template-tag "created_at"]]}]}
- (-> (lib/native-query mp "UPDATE birds SET name = 'x' WHERE {{created_at}}")
- (lib/with-template-tags
- {"created_at" {:name "created_at"
- :display-name "Created At"
- :type :dimension
- :widget-type :date/single
- :dimension (lib/ref (lib.metadata/field mp (mt/id :categories :name)))}}))))))))
-
-(deftest action-schema-tolerates-an-empty-query-test
- (testing "an action whose stored query degraded to {} still builds, typing parameters from their own types"
- (is (=? {:parameters [{:slug "name", :displayName "Name", :jsType "string"}]}
- (schemas.action/action-schema {:id 7, :name "Update bird", :dataset_query {}
- :parameters [{:id "name", :name "Name", :type :text}]})))))
-
-(deftest action-schema-leaves-out-hidden-parameters-test
- (testing "a parameter the action's form hides is left out, since execute refuses a value for it"
- (is (=? {:parameters [{:slug "name"}]}
- (schemas.action/action-schema
- {:id 7
- :name "Update bird"
- :dataset_query {}
- :parameters [{:id "name", :name "Name", :type :text}
- {:id "updated_by", :name "Updated by", :type :text, :required true}]
- :visualization_settings {:fields {"updated_by" {:id "updated_by", :hidden true}}}})))
- (is (= ["name"]
- (map :slug (:parameters (schemas.action/action-schema
- {:id 7, :name "Update bird", :dataset_query {}
- :parameters [{:id "name", :type :text} {:id "updated_by", :type :text}]
- :visualization_settings {:fields {"updated_by" {:id "updated_by", :hidden true}}}})))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/common_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/common_test.clj
deleted file mode 100644
index 44b9d7c4179..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/common_test.clj
+++ /dev/null
@@ -1,114 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.common-test
- (:require
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [metabase.audit-app.core :as audit]
- [metabase.audit-app.impl :as audit.impl]
- [metabase.config.core :as config]
- [metabase.test :as mt]
- [toucan2.core :as t2]))
-
-(deftest column-schema-includes-description-test
- (is (= {:type "column"
- :name "name"
- :displayName "Name"
- :baseType "type/Text"
- :jsType "string"
- :description "Name of the customer"}
- (schemas.common/column-schema {:name "name"
- :display_name "Name"
- :base_type "type/Text"
- :description "Name of the customer"}))))
-
-(deftest column-schema-maps-metabase-types-test
- (are [column js-type] (= js-type
- (:jsType (schemas.common/column-schema column)))
- {:name "bool", :base_type :type/Boolean} "boolean"
- {:name "int", :base_type :type/Integer} "number"
- {:name "date", :base_type :type/DateTime} "Date"
- {:name "uuid", :base_type :type/UUID} "string"
- {:name "string", :base_type "type/Text"} "string"
- {:name "decimal", :base_type "Decimal"} "number"
- {:name "effective", :effective_type :type/Text} "string"
- {:name "unknown", :base_type :type/Structured} "unknown"))
-
-(deftest keyed-map-disambiguates-duplicate-keys-with-readable-suffix-test
- (is (= {"channelOrderItems" {:key "channelOrderItems"
- :id "40f15584-bca0-4557-910d-e5e789757f23"
- :tableId 261}
- "channelOrders" {:key "channelOrders"
- :id "ca9bef16-d484-4add-8245-ddbc78287e8f"
- :tableId 167}}
- (schemas.common/keyed-map
- [{:key "channel"
- :id "ca9bef16-d484-4add-8245-ddbc78287e8f"
- :tableId 167
- :keyDisambiguator "Orders"}
- {:key "channel"
- :id "40f15584-bca0-4557-910d-e5e789757f23"
- :tableId 261
- :keyDisambiguator "OrderItems"}]))))
-
-(deftest keyed-map-falls-back-to-id-when-readable-suffix-does-not-disambiguate-test
- (is (= {"channelOrders1" {:key "channelOrders1"
- :id 1
- :tableId 167}
- "channelOrders2" {:key "channelOrders2"
- :id 2
- :tableId 168}}
- (schemas.common/keyed-map
- [{:key "channel"
- :id 1
- :tableId 167
- :keyDisambiguator "Orders"}
- {:key "channel"
- :id 2
- :tableId 168
- :keyDisambiguator "Orders"}]))))
-
-(deftest destination-database-ids-test
- (testing "returns only the ids that back a destination (routed) database"
- (mt/with-temp [:model/Database {router-id :id} {}
- :model/Database {destination-id :id} {:router_database_id router-id}
- :model/Database {open-id :id} {}]
- (is (= #{destination-id}
- (data-apps.db/destination-database-ids #{router-id destination-id open-id})))))
- (testing "returns an empty set for an empty input"
- (is (= #{} (data-apps.db/destination-database-ids #{})))
- (is (= #{} (data-apps.db/destination-database-ids nil)))))
-
-(deftest without-unavailable-cards-excludes-destination-database-cards-test
- (testing "cards backed by a destination (routed) database are left out, even for a superuser"
- (mt/with-temp [:model/Database {router-id :id} {}
- :model/Database {destination-id :id} {:router_database_id router-id}
- :model/Card {open-card-id :id} {:type :metric :database_id (mt/id)}
- :model/Card {destination-card-id :id} {:type :metric :database_id destination-id}]
- (mt/with-test-user :crowberto
- (let [cards (t2/select :model/Card :id [:in [open-card-id destination-card-id]])
- card-ids (into #{} (map :id) (schemas.common/without-unavailable-cards cards))]
- (is (contains? card-ids open-card-id))
- (is (not (contains? card-ids destination-card-id))))))))
-
-(deftest the-audit-apps-cards-are-left-out-without-the-audit-feature-test
- (let [cards [{:id 1 :database_id (mt/id)}
- {:id 2 :database_id audit/audit-db-id}
- {:id 3 :database_id (mt/id) :collection_id 7}]]
- ;; the var itself rather than its re-export through core, which only follows the root of this one
- (mt/with-dynamic-fn-redefs [audit.impl/is-collection-id-audit? #(= % 7)]
- (testing "a card in the audit collection 403s on its details lookup and one on the audit database has no table
- while the feature is off, so the schema leaves both out rather than fail an unscoped request"
- (mt/with-premium-features #{}
- (is (= [1] (map :id (schemas.common/without-unavailable-cards cards))))))
- ;; a premium feature is never on without the EE code, whatever the token says
- (when config/ee-available?
- (testing "with the feature on they are listed"
- (mt/with-premium-features #{:audit-app}
- (is (= [1 2 3] (map :id (schemas.common/without-unavailable-cards cards))))))))))
-
-(deftest ^:parallel keyed-map-never-drops-an-entity-test
- (testing "a disambiguated key that equals another entity's own key is disambiguated again rather than overwriting it"
- (let [entities [{:key "step", :id 1} {:key "step", :id 23} {:key "step1", :id 7} {:key "step11", :id 40}]
- result (schemas.common/keyed-map entities)]
- (is (= 4 (count result)))
- (is (= #{1 23 7 40} (into #{} (map :id) (vals result)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/javascript_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/javascript_test.clj
deleted file mode 100644
index 211fb20fdba..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/javascript_test.clj
+++ /dev/null
@@ -1,155 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.javascript-test
- (:require
- [clojure.string :as str]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas.javascript :as schemas.javascript]
- [metabase.util.malli.registry :as mr]))
-
-(defn- render-lines
- "Renders a one-statement module and returns its lines, without the trailing
- newline, so goldens read naturally as vectors of lines."
- [statement]
- (str/split-lines (schemas.javascript/render-js [:module statement])))
-
-(deftest literal-expressions-test
- (are [expr rendered] (= [(str "const x = " rendered " as const;")]
- (render-lines [:const "x" expr]))
- [:lit 42] "42"
- [:lit "hi"] "\"hi\""
- [:lit true] "true"
- [:lit nil] "null"
- [:ref "tables"] "tables"
- [:arr] "[ ]"
- [:obj] "{ }"))
-
-(deftest reference-paths-quote-non-identifier-segments-test
- (are [segments rendered] (= [(str "const x = " rendered " as const;")]
- (render-lines (into [:const "x"] [(into [:ref] segments)])))
- ["tables" "orders" "fields"] "tables.orders.fields"
- ["tables" "50thPercentile"] "tables[\"50thPercentile\"]"
- [:tables :orders] "tables.orders"))
-
-(deftest arrays-of-literals-render-inline-test
- (is (= ["const x = [ 1, 2, 3 ] as const;"]
- (render-lines [:const "x" [:arr [:lit 1] [:lit 2] [:lit 3]]]))))
-
-(deftest arrays-of-objects-render-multiline-with-metadata-test
- (is (= ["const x = ["
- " {"
- " /* metadata: { \"displayName\": \"Total\" } */"
- " name: \"total\""
- " },"
- " {"
- " name: \"tax\""
- " }"
- "] as const;"]
- (render-lines
- [:const "x" [:arr
- [:item {:metadata {"displayName" "Total"}}
- [:obj ["name" [:lit "total"]]]]
- [:obj ["name" [:lit "tax"]]]]]))))
-
-(deftest objects-render-metadata-first-and-quote-non-identifier-keys-test
- (is (= ["const x = {"
- " orders: {"
- " /* metadata: { \"entityId\": \"abc123\", \"description\": \"All orders\" } */"
- " type: \"table\","
- " \"has-totals\": true"
- " }"
- "} as const;"]
- (render-lines
- [:const "x" [:obj
- ["orders" {:metadata (array-map "entityId" "abc123"
- "description" "All orders")}
- [:obj ["type" [:lit "table"]]
- ["has-totals" [:lit true]]]]]]))))
-
-(deftest metadata-renders-in-an-otherwise-empty-object-test
- (is (= ["const x = {"
- " orders: {"
- " /* metadata: { \"id\": 1 } */"
- " }"
- "} as const;"]
- (render-lines
- [:const "x" [:obj ["orders" {:metadata {"id" 1}} [:obj]]]]))))
-
-(deftest metadata-with-nested-values-renders-one-item-per-line-test
- (is (= ["const x = {"
- " orders: {"
- " /* metadata: {"
- " \"filters\": ["
- " \"Status is paid\","
- " \"Total is greater than 10\""
- " ],"
- " \"sourceTable\": {"
- " \"databaseName\": \"Sample\","
- " \"tableName\": \"ORDERS\""
- " }"
- " } */"
- " id: 1"
- " }"
- "} as const;"]
- (render-lines
- [:const "x" [:obj ["orders" {:metadata (array-map "filters" ["Status is paid" "Total is greater than 10"]
- "sourceTable" (array-map "databaseName" "Sample"
- "tableName" "ORDERS"))}
- [:obj ["id" [:lit 1]]]]]]))))
-
-(deftest metadata-cannot-end-its-comment-early-test
- (testing "`*/` inside a value is written as `*\\/`, which JSON reads back as `*/`"
- (let [rendered (schemas.javascript/render-js
- [:module [:const "x" [:obj ["orders" {:metadata {"description" "Paid orders /* see note */"}}
- [:obj ["id" [:lit 1]]]]]]])]
- (is (str/includes? rendered "\"description\": \"Paid orders /* see note *\\/\""))
- (is (= 1 (count (re-seq #"\*/" rendered)))))))
-
-(deftest call-expressions-render-inline-test
- (is (= ["const x = {"
- " orders: pickFields(tables.orders.fields, [ \"total\" ], { sourceFieldId: 42 })"
- "} as const;"]
- (render-lines
- [:const "x" [:obj
- ["orders" [:call "pickFields"
- [:ref "tables" "orders" "fields"]
- [:arr [:lit "total"]]
- [:obj ["sourceFieldId" [:lit 42]]]]]]]))))
-
-(deftest modules-join-statements-with-blank-lines-test
- (is (= (str "function helper() {}\n"
- "\n"
- "const schema = { } as const;\n"
- "\n"
- "export default schema;\n")
- (schemas.javascript/render-js
- [:module
- [:raw "function helper() {}"]
- [:const "schema" [:obj]]
- [:export-default [:ref "schema"]]]))))
-
-(deftest module-schema-accepts-the-grammar-test
- (is (mr/validate schemas.javascript/Module
- [:module
- [:raw "function helper() {}"]
- [:const "tables"
- [:obj ["orders" {:metadata {"entityId" "abc"}}
- [:obj ["ids" [:arr [:lit 1] [:item {:metadata {"name" "c"}} [:obj]]]]
- ["fields" [:call "pickFields" [:ref "tables" "orders"]]]]]]]
- [:export-default [:ref "schema"]]])))
-
-(deftest module-schema-rejects-unknown-nodes-test
- (are [module] (not (mr/validate schemas.javascript/Module module))
- [:module [:const "x" [:string "not-a-node"]]]
- [:module [:const "x" [:obj ["key" {:metadataz {"typo" 1}} [:lit 1]]]]]
- ;; Comments were replaced by metadata blocks.
- [:module [:const "x" [:obj ["key" {:comments ["Entity ID: abc"]} [:obj]]]]]
- ;; A block prints inside an object, so metadata on anything else has nowhere to go.
- [:module [:const "x" [:obj ["key" {:metadata {"entityId" "abc"}} [:lit 1]]]]]
- [:module [:const "x" [:arr [:item {:metadata {"entityId" "abc"}} [:lit 1]]]]]
- [:const "x" [:lit 1]]))
-
-(deftest metadata-inside-a-call-argument-is-refused-test
- (testing "a `:call` argument prints on one line, where a block can't go, so its metadata is an error, not dropped"
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Metadata can only be attached to an object"
- (schemas.javascript/render-js
- [:module [:const "x" [:call "pickFields"
- [:obj ["orders" {:metadata {"entityId" "abc"}} [:obj]]]]]])))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/library_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/library_test.clj
deleted file mode 100644
index 638774e7efa..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/library_test.clj
+++ /dev/null
@@ -1,143 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.library-test
- (:require
- [clojure.set :as set]
- [clojure.string :as str]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas :as schemas]
- [metabase-enterprise.data-apps.generate.schemas.source :as source]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.collections.models.collection :as collection]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]
- [toucan2.core :as t2]))
-
-(use-fixtures :once (fixtures/initialize :db :web-server :test-users))
-
-(deftest library-content-and-metric-backing-tables-are-exported-test
- (mt/dataset test-data
- (data-apps.tu/do-with-library!
- (fn [{:keys [data-id metrics-id]}]
- (let [mp (mt/metadata-provider)
- orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
- query (lib/aggregate orders-query
- (lib/sum (lib.metadata/field mp (mt/id :orders :total))))]
- (mt/with-temp [:model/Collection nested-data {:type "library-data"
- :location (collection/children-location
- (t2/select-one :model/Collection :id data-id))}
- :model/Collection nested-metrics {:type "library-metrics"
- :location (collection/children-location
- (t2/select-one :model/Collection :id metrics-id))}
- :model/Collection outside {}
- :model/Collection lookalike {:type "library-data"}
- :model/Table published {:db_id (mt/id), :name "library_widgets"
- :is_published true, :collection_id data-id}
- :model/Field _ {:table_id (:id published), :name "price", :base_type :type/Float}
- :model/Table nested {:db_id (mt/id), :name "nested_widgets"
- :is_published true, :collection_id (:id nested-data)}
- :model/Table _unpublished {:db_id (mt/id), :name "unpublished_widgets"
- :is_published false, :collection_id data-id}
- :model/Table _elsewhere {:db_id (mt/id), :name "outside_widgets"
- :is_published true, :collection_id (:id outside)}
- :model/Table _lookalike {:db_id (mt/id), :name "lookalike_widgets"
- :is_published true, :collection_id (:id lookalike)}
- :model/Card metric {:name "Library revenue", :type :metric, :display :scalar
- :database_id (mt/id), :collection_id metrics-id, :dataset_query query}
- :model/Card nested-metric {:name "Nested revenue", :type :metric, :display :scalar
- :database_id (mt/id), :collection_id (:id nested-metrics)
- :dataset_query query}
- :model/Card _outside-metric {:name "Outside revenue", :type :metric, :display :scalar
- :database_id (mt/id), :collection_id (:id outside)
- :dataset_query query}
- :model/Card _archived-metric {:name "Archived revenue", :type :metric, :display :scalar
- :database_id (mt/id), :collection_id metrics-id
- :dataset_query query, :archived true}
- :model/Card _model {:name "Outside model", :type :model, :database_id (mt/id)
- :collection_id (:id outside), :dataset_query orders-query}]
- (mt/with-test-user :crowberto
- (testing "items published to libraries can be exported"
- (let [items (schemas/fetch-items)]
- (is (= #{(:id published) (:id nested) (mt/id :orders)} (into #{} (map :id) (:tables items))))
- (is (= #{(:id metric) (:id nested-metric)} (into #{} (map :id) (:metrics items))))
- (is (= #{(mt/id :orders)} (into #{} (mapcat :mappedTableIds) (:metrics items))))))
- (testing "only collection IDs from the library can be exported"
- (is (= [] (source/library-tables source/app-db-source #{(:id outside) (:id lookalike)})))
- (is (= [] (source/metrics source/app-db-source #{(:id outside)}))))
- ; we pick the metric dimensions from table fields, therefore tables
- ; that backed metrics are included in the export, even if they are not published to the library
- (testing "the response includes metric backing tables and excludes unrelated content"
- (mt/with-premium-features #{:data-apps}
- (let [body (:body (mt/user-http-request-full-response
- :crowberto :get 200 "apps/generate/schemas"))]
- (doseq [included ["libraryWidgets: {" "nestedWidgets: {" "libraryRevenue: {"
- "nestedRevenue: {" "orders: {"]]
- (is (str/includes? body included) included))
- (doseq [excluded ["unpublishedWidgets" "outsideWidgets" "lookalikeWidgets" "outsideRevenue"
- "archivedRevenue" "outsideModel" "models:"]]
- (is (not (str/includes? body excluded)) excluded)))))
- (testing "moving a metric out of the library immediately removes it"
- (mt/with-temp-vals-in-db :model/Card (:id metric) {:collection_id (:id outside)}
- (is (= [(:id nested-metric)]
- (map :id (source/metrics source/app-db-source #{metrics-id (:id nested-metrics)})))))))))))))
-
-(deftest table-publication-lifecycle-test
- (data-apps.tu/do-with-library!
- (fn [{:keys [data-id]}]
- (mt/with-temp [:model/Database db {}
- :model/Table table {:db_id (:id db), :name "widgets", :is_published true, :collection_id data-id}
- :model/Field _ {:table_id (:id table), :name "price", :base_type :type/Float}]
- (mt/with-test-user :crowberto
- (is (= [(:id table)] (map :id (source/library-tables source/app-db-source #{data-id}))))
- (doseq [changes [{:is_published false} {:collection_id nil} {:active false}]]
- (testing (str "table eligibility after " changes)
- (mt/with-temp-vals-in-db :model/Table (:id table) changes
- (is (= [] (source/library-tables source/app-db-source #{data-id}))))))
- (testing "an archived Library collection is excluded"
- (mt/with-temp-vals-in-db :model/Collection data-id {:archived true}
- (is (= [] (source/library-tables source/app-db-source #{data-id}))))))
- (testing "Library publication does not bypass read permissions"
- (mt/with-no-data-perms-for-all-users!
- (mt/with-test-user :rasta
- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"permissions"
- (source/tables source/app-db-source #{(:id table)}))))))))))
-
-(deftest only-published-data-actions-are-exported-test
- (mt/with-actions-enabled
- (let [mp (mt/metadata-provider)
- query (lib/native-query mp "UPDATE categories SET name = name")
- model-query (lib/query mp (lib.metadata/table mp (mt/id :categories)))]
- (mt/with-temp [:model/Collection data-actions {:namespace "data-actions"}
- :model/Collection archived-collection {:namespace "data-actions", :archived true}
- :model/Collection app-collection {:namespace "data-apps"}
- :model/Card model {:type :model, :dataset_query model-query}
- :model/Action root {:name "Root data action", :type :query}
- :model/QueryAction _ {:action_id (:id root), :dataset_query query}
- :model/Action nested {:name "Nested data action", :type :query, :collection_id (:id data-actions)}
- :model/QueryAction _ {:action_id (:id nested), :dataset_query query}
- :model/Action attached {:name "Model action", :type :query, :model_id (:id model)}
- :model/QueryAction _ {:action_id (:id attached), :dataset_query query}
- :model/Action copy {:name "App copy action", :type :query, :collection_id (:id app-collection)}
- :model/QueryAction _ {:action_id (:id copy), :dataset_query query}
- :model/Action archived {:name "Archived data action", :type :query, :archived true}
- :model/QueryAction _ {:action_id (:id archived), :dataset_query query}
- :model/Action in-archived {:name "Archived collection action", :type :query
- :collection_id (:id archived-collection)}
- :model/QueryAction _ {:action_id (:id in-archived), :dataset_query query}]
- (let [ours (into #{} (map :id) [root nested attached copy archived in-archived])]
- (mt/with-test-user :crowberto
- (testing "only standalone actions can be exported"
- (is (= #{(:id root) (:id nested)}
- (set/intersection ours (into #{} (map :id) (source/actions source/app-db-source))))))
- (testing "the response includes root and nested actions and excludes other actions"
- (mt/with-premium-features #{:data-apps}
- (let [body (:body (mt/user-http-request-full-response
- :crowberto :get 200 "apps/generate/schemas"))]
- (doseq [included ["rootDataAction: {" "nestedDataAction: {"]]
- (is (str/includes? body included) included))
- (doseq [excluded ["modelAction" "appCopyAction" "archivedDataAction" "archivedCollectionAction"]]
- (is (not (str/includes? body excluded)) excluded)))))
- (testing "moving a published action into an app collection immediately removes it"
- (mt/with-temp-vals-in-db :model/Action (:id nested) {:collection_id (:id app-collection)}
- (is (= #{(:id root)}
- (set/intersection ours (into #{} (map :id) (source/actions source/app-db-source)))))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/metric_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/metric_test.clj
deleted file mode 100644
index e87e0f125ea..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/metric_test.clj
+++ /dev/null
@@ -1,284 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.metric-test
- (:require
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.generate.schemas.metric :as schemas.metric]
- [metabase-enterprise.data-apps.generate.schemas.table :as schemas.table]
- [metabase.lib.core :as lib]
- [metabase.metabot.core :as metabot]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]))
-
-(use-fixtures :once (fixtures/initialize :db))
-
-(deftest ^:parallel metric-dimension-schema-uses-dimension-id-test
- (is (= {:type "column"
- :name "category"
- :displayName "Category"
- :baseType "type/Text"
- :jsType "string"
- :key "category"
- :id "550e8400-e29b-41d4-a716-446655440001"
- :fieldId 3815
- :tableId 12
- :metricId 247}
- (#'schemas.metric/dimension-schema
- {:id "550e8400-e29b-41d4-a716-446655440001"
- :name "category"
- :display-name "Category"
- :effective-type :type/Text
- :table-id 12
- :sources [{:type :field, :field-id 3815}]}
- 247))))
-
-(deftest ^:parallel metric-dimension-schema-preserves-source-field-id-test
- (is (= 102
- (:sourceFieldId
- (#'schemas.metric/dimension-schema
- {:id "category-dimension"
- :name "category"
- :source-field-id 102}
- 247)))))
-
-(deftest ^:parallel metric-source-id-test
- (testing "integer source-table emits sourceTableId but not sourceCardId"
- (let [card {:dataset_query {:query {:source-table 10}}}]
- (is (= 10 (#'schemas.metric/source-table-id card)))
- (is (nil? (#'schemas.metric/source-card-id card)))))
- (testing "card source-table emits sourceCardId but not sourceTableId"
- (let [card {:dataset_query {:query {:source-table "card__42"}}}]
- (is (nil? (#'schemas.metric/source-table-id card)))
- (is (= 42 (#'schemas.metric/source-card-id card)))))
- (testing "stage source-card emits sourceCardId"
- (is (= 42 (#'schemas.metric/source-card-id
- {:dataset_query {:stages [{:source-card 42}]}}))))
- (testing "The first stage identifies the source card even when a multi-stage metric requires its base table"
- (is (= 42 (#'schemas.metric/source-card-id
- {:dataset_query {:stages [{:source-card 42} {:aggregation [[:count]]}]}})))))
-
-(defn- orders-metric-query
- [filters]
- (lib/test-query
- (mt/metadata-provider)
- {:stages [{:source {:type :table :id (mt/id :orders)}
- :filters filters
- :aggregations [{:type :operator :operator :sum
- :args [{:type :column :name "TOTAL" :table-id (mt/id :orders)}]}]}]}))
-
-(deftest metric-filters-test
- (testing "a metric's filters are described the way the query builder names them"
- (mt/with-temp [:model/Card card {:type :metric
- :database_id (mt/id)
- :dataset_query (orders-metric-query
- [{:type :operator :operator :>=
- :args [{:type :column :name "CREATED_AT" :table-id (mt/id :orders)}
- {:type :literal :value "2025-01-01"}]}
- {:type :operator :operator :=
- :args [{:type :column :name "CATEGORY"
- :source-field-id (mt/id :orders :product_id)}
- {:type :literal :value "Widget"}]}])}]
- (is (= ["Created At is greater than or equal to \"2025-01-01\"" "Category is Widget"]
- (#'schemas.metric/metric-filters card)))))
- (testing "a metric without filters has none"
- (mt/with-temp [:model/Card card {:type :metric
- :database_id (mt/id)
- :dataset_query (orders-metric-query [])}]
- (is (nil? (#'schemas.metric/metric-filters card)))))
- (testing "a card without a query has none"
- (is (nil? (#'schemas.metric/metric-filters {:id 247 :dataset_query {}})))))
-
-(deftest metric-details-skips-default-temporal-breakout-test
- (let [requested (atom nil)]
- (mt/with-dynamic-fn-redefs [metabot/get-metric-details
- (fn [options]
- (reset! requested options)
- {:structured-output {:id 247}})]
- (is (= {:id 247}
- (#'schemas.metric/metric-details {:id 247})))
- (is (=? {:with-default-temporal-breakout? false}
- @requested)))))
-
-(deftest metric-details-surfaces-error-responses-test
- (mt/with-dynamic-fn-redefs [metabot/get-metric-details
- (constantly {:output "Not found."
- :status-code 404})]
- (let [exception (is (thrown? clojure.lang.ExceptionInfo
- (#'schemas.metric/metric-details {:id 247
- :name "Customer Lifetime Value"
- :type :metric})))]
- (is (=? {:card-id 247
- :card-name "Customer Lifetime Value"
- :card-type :metric
- :status-code 404
- :error-message "Not found."}
- (ex-data exception))))))
-
-(deftest metric-schemas-excludes-card-sourced-metrics-test
- (mt/with-dynamic-fn-redefs [data-apps.db/metric-cards-in-collections
- (constantly [{:id 247
- :dataset_query {:lib/type :mbql/query
- :database 1
- :stages [{:lib/type :mbql.stage/mbql
- :source-table 10}]}}
- {:id 258
- :dataset_query {:query {:source-table "card__42"}}}
- {:id 259
- :dataset_query {:stages [{:source-card 42}]}}])
- schemas.metric/metric-details identity
- schemas.metric/metric-schema (fn [details _card] (:id details))]
- (is (= [247]
- (vec (schemas.metric/metric-schemas #{1}))))))
-
-(deftest metric-schemas-excludes-metrics-that-reference-other-metrics-test
- (mt/with-dynamic-fn-redefs [data-apps.db/metric-cards-in-collections
- (constantly [{:id 247
- :dataset_query {:lib/type :mbql/query
- :database 1
- :stages [{:lib/type :mbql.stage/mbql
- :source-table 10}]}}
- {:id 258
- :dataset_query {:lib/type :mbql/query
- :database 1
- :stages [{:lib/type :mbql.stage/mbql
- :source-table 10
- :aggregation [[:metric
- {:lib/uuid
- "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}
- 247]]}]}}])
- schemas.metric/metric-details identity
- schemas.metric/metric-schema (fn [details _card] (:id details))]
- (is (= [247]
- (vec (schemas.metric/metric-schemas #{1}))))))
-
-(deftest metric-schemas-excludes-metrics-that-join-a-saved-question-test
- ;; Metric 258 is table-sourced and joins a saved question, so `source-card-id` — which only reads
- ;; stage 0's source — passes it. A data app's copy of it would reference a saved question outside
- ;; the app's resources, which the pull refuses, so codegen has to drop it here as well.
- (mt/with-dynamic-fn-redefs [data-apps.db/metric-cards-in-collections
- (constantly [{:id 247
- :dataset_query {:lib/type :mbql/query
- :database 1
- :stages [{:lib/type :mbql.stage/mbql
- :source-table 10}]}}
- {:id 258
- :dataset_query {:lib/type :mbql/query
- :database 1
- :stages [{:lib/type :mbql.stage/mbql
- :source-table 10
- :joins [{:lib/type :mbql/join
- :alias "Question"
- :stages [{:lib/type :mbql.stage/mbql
- :source-card 42}]
- :conditions
- [[:=
- {:lib/uuid "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}
- [:field {:lib/uuid "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"} 11]
- [:field {:lib/uuid "cccccccc-cccc-cccc-cccc-cccccccccccc"} 12]]]}]}]}}])
- schemas.metric/metric-details identity
- schemas.metric/metric-schema (fn [details _card] (:id details))]
- (is (= [247]
- (vec (schemas.metric/metric-schemas #{1}))))))
-
-(deftest metric-schema-keys-dimensions-test
- (mt/with-dynamic-fn-redefs [schemas.metric/metric-result-column (constantly nil)
- schemas.metric/table-source-rows
- (constantly [{:id 10 :name "orders" :display_name "Orders"}])
- schemas.metric/sync-and-fetch-metric-dimensions!
- (constantly [{:id "550e8400-e29b-41d4-a716-446655440001"
- :name "orders"
- :display-name "Orders"
- :effective-type :type/Integer
- :table-id 10
- :sources [{:type :field, :field-id 42}]}])]
- (is (= {:type "metric"
- :key "customerLifetimeValue"
- :id 247
- :name "Customer Lifetime Value"
- :columns [{:type "column"
- :name "Customer Lifetime Value"
- :displayName "Customer Lifetime Value"
- :jsType "unknown"}]
- :mappedTableIds [10]
- :dimensions {"orders" {:type "column"
- :name "orders"
- :sourceName "orders"
- :displayName "Orders"
- :baseType "type/Integer"
- :jsType "number"
- :key "orders"
- :id "550e8400-e29b-41d4-a716-446655440001"
- :fieldId 42
- :tableId 10
- :metricId 247}}}
- (#'schemas.metric/metric-schema
- {:id 247
- :name "Customer Lifetime Value"}
- {:id 247})))))
-
-(deftest metric-schema-reuses-table-source-rows-test
- (let [table-select-count (atom 0)]
- (mt/with-dynamic-fn-redefs [schemas.metric/metric-result-column (constantly nil)
- schemas.metric/sync-and-fetch-metric-dimensions!
- (constantly [{:id "orders-dimension"
- :name "orders"
- :table-id 10}])
- data-apps.db/table-names
- (fn [_table-ids]
- (swap! table-select-count inc)
- [{:id 10 :name "orders" :display_name "Orders"}])]
- (#'schemas.metric/metric-schema
- {:id 247
- :name "Customer Lifetime Value"}
- {:id 247})
- (is (= 1 @table-select-count)))))
-
-(deftest metric-dimensions-bulk-load-table-ids-test
- (let [field-select-count (atom 0)
- field-lookup-attempts (atom [])
- dimensions [{:id "orders-dimension"
- :sources [{:type :field, :field-id 42}]}
- {:id "people-dimension"
- :sources [{:type :field, :field-id 84}]}]]
- (mt/with-dynamic-fn-redefs [schemas.metric/sync-and-fetch-metric-dimensions! (constantly dimensions)
- schemas.table/table-by-field-id (fn [field-id]
- (swap! field-lookup-attempts conj field-id)
- ({42 10, 84 20} field-id))
- data-apps.db/field-ids-and-table-ids
- (fn [_field-ids]
- (swap! field-select-count inc)
- [{:id 42 :table_id 10}
- {:id 84 :table_id 20}])]
- (is (= (mapv vector dimensions [10 20])
- (#'schemas.metric/metric-dimensions-with-table-ids {:id 247} nil)))
- (is (= 1 @field-select-count))
- (is (empty? @field-lookup-attempts)))))
-
-(deftest source-card-metric-schema-omits-mapped-table-dimensions-test
- (mt/with-dynamic-fn-redefs [schemas.metric/metric-result-column (constantly nil)
- schemas.metric/sync-and-fetch-metric-dimensions!
- (constantly [{:id "count-dimension-uuid"
- :name "count"}
- {:id "store-name-dimension-uuid"
- :name "store_name"
- :table-id 10}])]
- (is (= {:type "metric"
- :key "storesWithOver5Employees"
- :id 259
- :name "Stores with Over 5 Employees"
- :columns [{:type "column"
- :name "Stores with Over 5 Employees"
- :displayName "Stores with Over 5 Employees"
- :jsType "unknown"}]
- :sourceCardId 258
- :dimensions {"count" {:type "column"
- :name "count"
- :displayName "count"
- :jsType "unknown"
- :key "count"
- :id "count-dimension-uuid"
- :metricId 259}}}
- (#'schemas.metric/metric-schema
- {:id 259
- :name "Stores with Over 5 Employees"}
- {:id 259
- :dataset_query {:stages [{:source-card 258}]}})))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/render_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/render_test.clj
deleted file mode 100644
index b013edb3c70..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/render_test.clj
+++ /dev/null
@@ -1,204 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.render-test
- (:require
- [clojure.string :as str]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas :as schemas]
- [metabase-enterprise.data-apps.generate.schemas.javascript :as schemas.javascript]
- [metabase-enterprise.data-apps.generate.schemas.render :as schemas.render]
- [metabase.util.malli.registry :as mr]))
-
-(def ^:private orders-table
- {:type "table"
- :id 10
- :fields {"paymentMethod" {:type "column"
- :name "payment_method"
- :displayName "Payment Method"
- :semanticType "type/Category"
- :jsType "string"
- :fieldId 3970
- :tableId 10}}})
-
-(def ^:private franchises-table
- {:type "table"
- :id 20
- :fields {"name" {:type "column"
- :name "name"
- :displayName "Name"
- :jsType "string"
- :fieldId 500
- :tableId 20}}})
-
-(def ^:private payment-method-dimension
- {:name "payment_method"
- :displayName "Payment Method"
- :semanticType "type/Category"
- :fieldId 3970
- :tableId 10
- :metricId 5})
-
-(def ^:private franchise-name-dimension
- {:name "name"
- :fieldId 500
- :tableId 20
- :sourceFieldId 42
- :metricId 5})
-
-(def ^:private revenue-metric
- {:type "metric"
- :databaseId 1
- :sourceTableId 10
- :description "Total order revenue"
- :filters ["Status is paid" "Created At is in the previous 30 days"]
- :mappedTableIds [10 20]
- :dimensions {"paymentMethod" payment-method-dimension
- "franchiseName" franchise-name-dimension}})
-
-(def ^:private compacting-schema
- {:schemaVersion 2
- :tables {"orders" orders-table
- "franchises" franchises-table}
- :metrics {"revenue" revenue-metric}})
-
-(def ^:private raw-dimensions-schema
- {:schemaVersion 2
- :metrics {"modelRevenue" {:type "metric"
- :id 6
- :name "Model Revenue"
- :dimensions {"createdAt" {:type "column"
- :name "created_at"
- :baseType "type/DateTime"
- :jsType "Date"}}}}})
-
-(deftest typescript-renderer-emits-metadata-blocks-and-runtime-data-test
- (let [body (schemas/render-typescript compacting-schema)]
- (testing "context for agents is a metadata block, never a line comment"
- (is (not (re-find #"(?m)^\s*//" body)))
- (testing "an entry opens with its block, so a reader meets the context before the data"
- (is (re-find #"(?s)revenue: \{\n\s*/\* metadata: \{.*?\} \*/\n\s*type: \"metric\"" body))
- (is (str/includes? body "\"description\": \"Total order revenue\""))
- (is (str/includes? body (str "/* metadata: { \"displayName\": \"Payment Method\", "
- "\"semanticType\": \"type/Category\" } */\n type: \"column\""))))
- (testing "a block with a nested value prints one array item per line"
- (is (re-find #"(?s)\"filters\": \[\n\s*\"Status is paid\",\n\s*\"Created At is in the previous 30 days\"\n\s*\]" body))))
- (testing "data for the Lib.createTestQuery DSL stays runtime"
- (is (str/includes? body "revenue: {\n /* metadata:"))
- (is (str/includes? body "paymentMethod: {\n /* metadata:"))
- (is (str/includes? body "databaseId: 1"))
- (is (str/includes? body "sourceTableId: 10"))
- (is (str/includes? body "mappedTableIds: [ 10, 20 ]")))
- (testing "metadata-only keys never become runtime fields"
- (is (not (str/includes? body "displayName: \"Payment Method\"")))
- (is (not (str/includes? body "filters:"))))))
-
-(deftest typescript-renderer-omits-what-a-metadata-block-has-nothing-to-say-test
- (let [body (schemas/render-typescript
- {:schemaVersion 2
- :tables {"orders" {:type "table"
- :id 10
- :name "Orders"
- :description " "
- :fields {}}}
- :metrics {"revenue" {:type "metric"
- :id 31
- :sourceTable {:databaseName "Sample Database"
- :schemaName nil
- :tableName "ORDERS"}}
- "profit" {:type "metric"
- :id 32
- :sourceTable {:databaseName "Sample Database"
- :schemaName ""
- :tableName "PRODUCTS"}}}})]
- (testing "a table in a database without schemas has no schema key, whether the schema is nil or blank"
- (is (= 2 (count (re-seq #"\"sourceTable\": \{\n\s*\"databaseName\": \"Sample Database\",\n\s*\"tableName\": \"(ORDERS|PRODUCTS)\"\n\s*\}" body))))
- (is (not (str/includes? body "null")))
- (is (not (str/includes? body "schemaName"))))
- (testing "a blank description is not written"
- (is (not (str/includes? body "description"))))))
-
-(deftest typescript-renderer-names-joins-to-the-same-table-by-their-foreign-keys-test
- (let [orders (-> orders-table
- (assoc-in [:fields "createdBy"] {:type "column" :name "created_by" :jsType "number" :fieldId 41 :tableId 10})
- (assoc-in [:fields "approvedBy"] {:type "column" :name "approved_by" :jsType "number" :fieldId 42 :tableId 10}))
- body (schemas/render-typescript
- {:schemaVersion 2
- :tables {"orders" orders "franchises" franchises-table}
- :metrics {"revenue" (assoc revenue-metric :dimensions
- {"creatorName" (assoc franchise-name-dimension :sourceFieldId 41)
- "approverName" (assoc franchise-name-dimension :sourceFieldId 42)})}})]
- (is (str/includes? body "franchisesViaCreatedBy: pickFields(tables.franchises.fields, [ \"name\" ], { sourceFieldId: 41 })"))
- (is (str/includes? body "franchisesViaApprovedBy: pickFields(tables.franchises.fields, [ \"name\" ], { sourceFieldId: 42 })"))))
-
-(deftest typescript-renderer-compacts-metric-dimensions-test
- (let [body (schemas/render-typescript compacting-schema)]
- ;; Metric dimensions should compact into pickFields(...) references.
- (is (str/includes? body "function pickFields"))
- (is (str/includes? body "const field = fields[key] as { tableId?: number };"))
- (is (str/includes? body "const { tableId, ...joinedField } = field;"))
- (is (str/includes? body "orders: pickFields(tables.orders.fields, [ \"paymentMethod\" ])"))
- (is (str/includes? body "franchises: pickFields(tables.franchises.fields, [ \"name\" ], { sourceFieldId: 42 })"))
- ;; Source field id should be preserved.
- (is (= 1 (count (re-seq #"sourceFieldId: 42" body))))
- ;; `metricId` is only used to identify dimensions while compacting them.
- ;; The generated TypeScript module should not contain metric id.
- (is (not (str/includes? body "metricId: 5")))))
-
-(defn- module-const
- "Returns the expression bound to `const-name` in a module AST."
- [ast const-name]
- (some (fn [statement]
- (when (and (= :const (first statement))
- (= const-name (second statement)))
- (nth statement 2)))
- (rest ast)))
-
-(defn- obj-entry
- "Returns the expression stored under `entry-key` in an `[:obj ...]` node."
- [obj-node entry-key]
- (some (fn [entry]
- (when (= entry-key (first entry))
- (last entry)))
- (rest obj-node)))
-
-(deftest schema->ast-produces-valid-modules-test
- (are [schema] (mr/validate schemas.javascript/Module (schemas.render/schema->ast schema))
- compacting-schema
- raw-dimensions-schema))
-
-(deftest schema->ast-compacts-metric-dimensions-test
- (let [ast (schemas.render/schema->ast compacting-schema)
- dimensions (-> (module-const ast "metrics")
- (obj-entry "revenue")
- (obj-entry :dimensions))]
- (is (= [:call "pickFields"
- [:ref "tables" "orders" "fields"]
- [:arr [:lit "paymentMethod"]]]
- (obj-entry dimensions "orders")))
- (is (= [:call "pickFields"
- [:ref "tables" "franchises" "fields"]
- [:arr [:lit "name"]]
- [:obj ["sourceFieldId" [:lit 42]]]]
- (obj-entry dimensions "franchises")))))
-
-(deftest schema->ast-splits-runtime-keys-from-metadata-test
- (let [ast (schemas.render/schema->ast compacting-schema)
- fields (-> (module-const ast "tables")
- (obj-entry "orders")
- (obj-entry :fields))
- [entry-key options field-node] (-> fields rest first)]
- (testing "metadata-only policy keys become the entry's metadata"
- (is (= "paymentMethod" entry-key))
- (is (= {:metadata {"displayName" "Payment Method"
- "semanticType" "type/Category"}}
- options)))
- (testing "runtime policy keys become object entries"
- (is (= [:lit "payment_method"] (obj-entry field-node :name)))
- (is (= [:lit "string"] (obj-entry field-node :jsType)))
- (is (nil? (obj-entry field-node :displayName))))))
-
-(deftest typescript-renderer-omits-pick-fields-helper-for-raw-dimensions-test
- (let [body (schemas/render-typescript raw-dimensions-schema)]
- ;; Dimensions that cannot be resolved to table fields stay as raw fields, so
- ;; the rendered module should not include the pickFields helper.
- (is (not (str/includes? body "function pickFields")))
- (is (not (str/includes? body "pickFields(")))
- (is (str/includes? body "fields: {\n createdAt: {"))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/source_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/source_test.clj
deleted file mode 100644
index 13ed3f7e937..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/source_test.clj
+++ /dev/null
@@ -1,132 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.source-test
- "Tests [[schemas.source/app-db-source]] against real application-database rows.
-
- Everything downstream of the source works on plain data and is covered by
- cheap in-memory tests; these tests are the seam where the data app schema meets the
- database."
- (:require
- [clojure.set :as set]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas.source :as schemas.source]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.collections.models.collection :as collection]
- [metabase.lib-be.core :as lib-be]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]
- [metabase.util.malli.registry :as mr]
- [toucan2.core :as t2]))
-
-(use-fixtures :once (fixtures/initialize :db :test-users))
-
-(deftest library-scope-and-library-tables-test
- (data-apps.tu/do-with-library!
- (fn [{:keys [data-id metrics-id]}]
- (mt/with-temp [:model/Collection data-child {:name "Data child"
- :type "library-data"
- :location (collection/children-location (t2/select-one :model/Collection :id data-id))}
- :model/Collection plain-collection {:name "Not A Library"}
- :model/Database db {}
- :model/Table published {:db_id (:id db), :name "published_table", :active true
- :is_published true, :collection_id data-id}
- :model/Table nested {:db_id (:id db), :name "nested_table", :active true
- :is_published true, :collection_id (:id data-child)}
- :model/Table _unpublished {:db_id (:id db), :name "unpublished_table", :active true
- :is_published false, :collection_id data-id}
- :model/Table _elsewhere {:db_id (:id db), :name "elsewhere_table", :active true
- :is_published true, :collection_id (:id plain-collection)}]
- (mt/with-current-user (mt/user->id :crowberto)
- (let [scope (schemas.source/library-scope schemas.source/app-db-source)]
- (testing "library scope classifies the root libraries' collections by type"
- (is (mr/validate schemas.source/LibraryScope scope))
- (is (= {:data-collection-ids #{data-id (:id data-child)}
- :metric-collection-ids #{metrics-id}}
- scope)))
- (testing "library tables are the published tables in the data collections and their descendants"
- (is (= #{(:id published) (:id nested)}
- (into #{}
- (map :id)
- (schemas.source/library-tables schemas.source/app-db-source (:data-collection-ids scope)))))
- (is (= [] (schemas.source/library-tables schemas.source/app-db-source #{}))))))))))
-
-(deftest metrics-honour-collection-ids-test
- (data-apps.tu/do-with-library!
- (fn [{:keys [metrics-id]}]
- (mt/with-temp [:model/Collection child {:name "Child"
- :type "library-metrics"
- :location (collection/children-location (t2/select-one :model/Collection :id metrics-id))}
- :model/Card _metric {:name "Library revenue"
- :type :metric
- :collection_id (:id child)}]
- (mt/with-test-user :crowberto
- (let [{:keys [metric-collection-ids]} (schemas.source/library-scope schemas.source/app-db-source)]
- (is (some #{"libraryRevenue"}
- (map :key (schemas.source/metrics schemas.source/app-db-source metric-collection-ids))))
- (is (= [] (schemas.source/metrics schemas.source/app-db-source #{})))))))))
-
-(deftest tables-test
- (mt/with-temp [:model/Database db {}
- :model/Table table {:db_id (:id db), :name "widgets", :display_name "Widgets", :active true}
- :model/Field _ {:table_id (:id table), :name "price", :base_type :type/Float}]
- (mt/with-current-user (mt/user->id :crowberto)
- (testing "explicit table ids include unpublished metric backing tables"
- (is (= [(:id table)] (map :id (schemas.source/tables schemas.source/app-db-source #{(:id table)})))))
- (testing "an empty table-id set matches nothing"
- (is (= [] (schemas.source/tables schemas.source/app-db-source #{})))))))
-
-(deftest empty-table-request-does-not-query-test
- (t2/with-call-count [call-count]
- (is (= [] (schemas.source/tables schemas.source/app-db-source #{})))
- (is (zero? (call-count)))))
-
-(defn- touch-category-query []
- (let [query (lib/native-query (mt/metadata-provider) "UPDATE categories SET name = name WHERE id = {{id}}")]
- (lib/with-template-tags query (mapv #(assoc % :type :number) (lib/template-tags query)))))
-
-(def ^:private id-parameter
- {:id "id", :slug "id", :type :number/=, :target [:variable [:template-tag "id"]], :required true})
-
-(deftest actions-test
- (mt/with-temp [:model/Collection {collection-id :id} {:name "Actions" :namespace "data-actions"}
- :model/Collection {app-collection-id :id} {:name "Data App: orders" :namespace "data-apps"}
- :model/Card {model-id :id} {:type :model
- :dataset_query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider)
- (mt/id :categories)))}
- :model/Action {standalone :id} {:type :query, :name "Touch category", :parameters [id-parameter]}
- :model/QueryAction _ {:action_id standalone, :dataset_query (touch-category-query)}
- :model/Action {archived :id} {:type :query, :name "Archived", :archived true}
- :model/QueryAction _ {:action_id archived, :dataset_query (touch-category-query)}
- :model/Action {in-collection :id} {:type :query, :name "In a collection", :collection_id collection-id}
- :model/QueryAction _ {:action_id in-collection, :dataset_query (touch-category-query)}
- :model/Action {on-model :id} {:type :query, :name "On a model", :model_id model-id}
- :model/QueryAction _ {:action_id on-model, :dataset_query (touch-category-query)}
- :model/Action {app-copy :id} {:type :query, :name "App copy", :collection_id app-collection-id}
- :model/QueryAction _ {:action_id app-copy, :dataset_query (touch-category-query)}]
- (let [action-ids (fn [] (into #{} (map :id) (schemas.source/actions schemas.source/app-db-source)))
- ours #{standalone archived in-collection on-model app-copy}]
- (mt/with-current-user (mt/user->id :crowberto)
- (testing "returns unarchived actions without a model and excludes data app copies"
- (is (= #{standalone in-collection} (set/intersection ours (action-ids)))))
- (testing "an action renders with its parameters typed from the template tag"
- (is (=? [{:kind "action"
- :key "touchCategory"
- :id standalone
- :type "query"
- :parameters [{:slug "id", :jsType "number", :required true}]}]
- (filter #(= standalone (:id %)) (schemas.source/actions schemas.source/app-db-source)))))))))
-
-(deftest actions-leave-out-those-backed-by-a-routing-destination-test
- (mt/with-temp [:model/Database {router-id :id} {}
- :model/Database {destination-id :id} {:router_database_id router-id}
- :model/Action {open :id} {:type :query, :name "Open action"}
- :model/QueryAction _ {:action_id open, :dataset_query (touch-category-query)}
- :model/Action {routed :id} {:type :query, :name "Routed action"}
- :model/QueryAction _ {:action_id routed
- :dataset_query (lib/native-query (lib-be/application-database-metadata-provider destination-id)
- "UPDATE categories SET name = name")}]
- (mt/with-current-user (mt/user->id :crowberto)
- (let [ids (into #{} (map :id) (schemas.source/actions schemas.source/app-db-source))]
- (is (contains? ids open))
- (is (not (contains? ids routed)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/table_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/table_test.clj
deleted file mode 100644
index 120ed9443ed..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas/table_test.clj
+++ /dev/null
@@ -1,191 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas.table-test
- (:require
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas.common :as schemas.common]
- [metabase-enterprise.data-apps.generate.schemas.table :as schemas.table]
- [metabase.audit-app.core :as audit]
- [metabase.config.core :as config]
- [metabase.lib-be.core :as lib-be]
- [metabase.metabot.core :as metabot]
- [metabase.test :as mt]
- [toucan2.core :as t2]))
-
-(def ^:private created-at-field
- {:id 42
- :table_id 10
- :name "created_at"
- :display_name "Created At"
- :base_type "type/DateTime"})
-
-(def ^:private created-at-field-schema
- {:type "column"
- :name "created_at"
- :displayName "Created At"
- :baseType "type/DateTime"
- :jsType "Date"
- :key "createdAt"
- :id 42
- :fieldId 42
- :tableId 10})
-
-(def ^:private orders-table
- {:id 10
- :name "orders"
- :display_name "Orders"
- :database_id 1
- :database_name "Boba"
- :fields [created-at-field]})
-
-(def ^:private total-revenue-measure
- {:id 1
- :name "Total Revenue"})
-
-(def ^:private total-revenue-schema
- {:type "measure"
- :key "totalRevenue"
- :id 1
- :tableId 10
- :name "Total Revenue"})
-
-(deftest field-schema-uses-field-id-test
- (is (= created-at-field-schema
- (schemas.table/field-schema created-at-field))))
-
-(deftest table-schema-keys-fields-test
- (is (= {:type "table"
- :key "orders"
- :id 10
- :name "Orders"
- :databaseName "Boba"
- :tableName "orders"
- :fields {"createdAt" (assoc created-at-field-schema :sourceName "orders")}}
- (schemas.table/table-schema orders-table))))
-
-; Avoid N+1 on table fields by re-using the known table id
-(deftest table-schema-uses-known-table-id-for-detail-fields-test
- (let [field-table-lookups (atom 0)
- detail-fields [{:field_id 42
- :name "created_at"
- :display_name "Created At"
- :base_type "type/DateTime"}
- {:field_id 43
- :name "total"
- :display_name "Total"
- :base_type "type/Decimal"}]]
- (with-redefs [t2/select-one-fn (fn [& _]
- (swap! field-table-lookups inc)
- 999)]
- (let [schema (schemas.table/table-schema (assoc orders-table :fields detail-fields))]
- (is (= #{10} (->> (:fields schema) vals (map :tableId) set)))
- (is (zero? @field-table-lookups))))))
-
-(deftest segment-schema-uses-type-discriminator-test
- (is (= {:type "segment"
- :key "completedOrders"
- :id 12
- :tableId 10
- :name "Completed Orders"}
- (schemas.table/segment-schema
- 10
- {:id 12
- :name "Completed Orders"
- :display-name "Completed Orders"}))))
-
-(deftest measure-schema-uses-result-column-test
- (testing "measure result columns come from the measure definition when available"
- (mt/with-dynamic-fn-redefs [schemas.table/measure-result-column
- (constantly {:name "sum"
- :display_name "Sum of Total"
- :base_type "type/Decimal"})]
- (is (= (assoc total-revenue-schema
- :columns [{:type "column"
- :name "sum"
- :displayName "Sum of Total"
- :baseType "type/Decimal"
- :jsType "number"}])
- (schemas.table/measure-schema 10 2 total-revenue-measure)))))
- (testing "measure result columns fall back to the measure name"
- (mt/with-dynamic-fn-redefs [schemas.table/measure-result-column (constantly nil)]
- (is (= (assoc total-revenue-schema
- :columns [{:type "column", :name "Total Revenue", :displayName "Total Revenue", :jsType "unknown"}])
- (schemas.table/measure-schema 10 2 total-revenue-measure))))))
-
-(deftest table-schemas-surface-detail-error-responses-test
- (mt/with-dynamic-fn-redefs [metabot/get-table-details
- (constantly {:output "Not found."
- :status-code 404})]
- (let [exception (is (thrown? clojure.lang.ExceptionInfo
- (doall (schemas.table/table-schemas [{:id 10 :name "Orders"}]))))]
- (is (=? {:table-id 10
- :table-name "Orders"
- :status-code 404
- :error-message "Not found."}
- (ex-data exception))))))
-
-(deftest select-tables-excludes-destination-database-tables-test
- (testing "select-tables excludes tables backed by a destination (routed) database, even for a superuser"
- (mt/with-temp [:model/Database {router-id :id} {}
- :model/Database {destination-id :id} {:router_database_id router-id}
- :model/Table {open-table-id :id} {:db_id (mt/id)}]
- ;; A table can't exist on a destination in production (destinations aren't synced), so a normal
- ;; `with-temp :model/Table` trips a different guard. Insert it directly, like the analogous
- ;; metabot resource-guard tests do (metabase.metabot.agent.user-context-test,
- ;; metabase.metabot.tools.metadata-test).
- (let [destination-table-id (t2/insert-returning-pk!
- (t2/table-name :model/Table)
- {:db_id destination-id
- :name "destination-table"
- :active true
- :created_at :%now
- :updated_at :%now})]
- (mt/with-test-user :crowberto
- (let [tables (schemas.table/select-tables #{open-table-id destination-table-id})]
- (is (= [open-table-id] (map :id tables)))))))))
-
-(deftest the-audit-databases-tables-are-left-out-without-the-audit-feature-test
- (let [tables [{:id 1 :db_id (mt/id)} {:id 2 :db_id audit/audit-db-id}]]
- (testing "the table details lookup refuses the audit database's tables while the feature is off, so the schema
- leaves them out rather than fail an unscoped request"
- (mt/with-premium-features #{}
- (is (= [1] (map :id (#'schemas.table/without-unavailable-tables tables))))))
- ;; a premium feature is never on without the EE code, whatever the token says
- (when config/ee-available?
- (testing "with the feature on they are listed"
- (mt/with-premium-features #{:audit-app}
- (is (= [1 2] (map :id (#'schemas.table/without-unavailable-tables tables)))))))))
-
-;; Batch measure definitions to avoid N+1 queries.
-(deftest table-schema-bulk-loads-measure-definitions-test
- (let [measure-select-count (atom 0)
- metadata-provider-count (atom 0)
- measures [{:id 1 :name "Total Revenue"}
- {:id 2 :name "Average Revenue"}]]
- (with-redefs [lib-be/application-database-metadata-provider (fn [_database-id]
- (swap! metadata-provider-count inc)
- :metadata-provider)
- schemas.common/aggregation-result-column-with-metadata-provider (constantly nil)
- t2/select (fn [columns & _args]
- (when (= columns [:model/Measure :id :definition])
- (swap! measure-select-count inc)
- [{:id 1 :definition [:aggregation 1]}
- {:id 2 :definition [:aggregation 2]}]))]
- (schemas.table/table-schema (assoc orders-table :measures measures))
- (is (= 1 @measure-select-count))
- (is (= 1 @metadata-provider-count)))))
-
-(deftest table-schemas-with-stored-fingerprint-statistics-test
- (mt/with-temp [:model/Table table {:db_id (mt/id) :name "customers"}
- :model/Field _ {:table_id (:id table)
- :name "customer_id"
- :base_type :type/Text
- :fingerprint {:type {:type/Text {:min-length 8.0 :max-length 9.0}}}}
- :model/Field _ {:table_id (:id table)
- :name "last_ordered_at"
- :base_type :type/DateTime
- :fingerprint {:type {:type/DateTime
- {:skewness -1.2937
- :weekday-distribution [0.1 0.1 0.1 0.1 0.2 0.2 0.2]
- :hour-distribution (vec (repeat 24 (/ 1.0 24)))}}}}]
- (mt/with-test-user :crowberto
- (let [schema (first (schemas.table/table-schemas [table]))]
- (is (= #{"customerId" "lastOrderedAt"} (set (keys (:fields schema)))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_api_test.clj
deleted file mode 100644
index cbc26c482d7..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_api_test.clj
+++ /dev/null
@@ -1,81 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas-api-test
- (:require
- [clojure.string :as str]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]
- [toucan2.core :as t2]))
-
-(use-fixtures :once (fixtures/initialize :db :web-server :test-users))
-
-(defn- get-schemas
- "The response of `GET /api/apps/generate/schemas` as `user`, expecting `status`, with the data apps feature on."
- [user status]
- (mt/with-premium-features #{:data-apps}
- (mt/user-http-request-full-response user :get status "apps/generate/schemas")))
-
-(deftest only-a-superuser-generates-the-schemas-test
- (testing "the schema is what an app's author builds from, and only an admin works with an app's repository"
- (mt/with-premium-features #{:data-apps}
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :get 403 "apps/generate/schemas")))
- (is (string? (mt/user-http-request :crowberto :get 200 "apps/generate/schemas"))))))
-
-(deftest typescript-endpoint-test
- (let [response (get-schemas :crowberto 200)]
- (is (= "text/typescript; charset=utf-8" (get-in response [:headers "Content-Type"])))
- (is (str/includes? (:body response) "\nconst schema = {"))
- (is (str/includes? (:body response) "\n schemaVersion: 2"))
- (is (not (str/includes? (:body response) "\n questions:")))
- (is (str/includes? (:body response) "\n tables: tables"))
- (is (str/includes? (:body response) "\n metrics: metrics"))
- (is (str/ends-with? (:body response) "export default schema;\n"))
- (is (not (str/includes? (:body response) "\"schemaVersion\"")))
- (is (not (str/includes? (:body response) "operators: [ ]")))
- (is (not (str/includes? (:body response) "parameters: [ ]")))
- (is (not (str/includes? (:body response) "verified: false")))))
-
-(deftest schemas-cover-the-libraries-and-the-model-less-actions-test
- (mt/dataset test-data
- (mt/with-temp-copy-of-db
- (mt/with-actions-enabled
- (data-apps.tu/do-with-library!
- (fn [{:keys [data-id metrics-id]}]
- (let [mp (mt/metadata-provider)
- orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
- revenue-query (lib/aggregate orders-query
- (lib/sum (lib.metadata/field mp (mt/id :orders :total))))]
- (t2/update! :model/Table (mt/id :orders) {:is_published true, :collection_id data-id})
- (mt/with-temp [:model/Card _metric {:name "Order revenue", :database_id (mt/id), :table_id (mt/id :orders)
- :type :metric, :display :scalar, :collection_id metrics-id
- :dataset_query revenue-query}
- :model/Card model {:name "Order model", :database_id (mt/id), :table_id (mt/id :orders)
- :type :model
- :dataset_query orders-query
- :result_metadata [{:name "total", :display_name "Total"
- :base_type :type/Float
- :field_ref [:field (mt/id :orders :total) nil]
- :id (mt/id :orders :total)}]}
- :model/Action action {:name "Update order", :model_id (:id model), :type :implicit}
- :model/ImplicitAction _ {:action_id (:id action), :kind "row/update"}
- :model/Action standalone {:name "Discount order", :type :query}
- :model/QueryAction _ {:action_id (:id standalone)
- :dataset_query (lib/native-query mp "UPDATE orders SET discount = 0")}
- :model/Collection copies {:name "Data App: orders", :namespace "data-apps"}
- :model/Action copy {:name "Copied order", :type :query, :collection_id (:id copies)}
- :model/QueryAction _ {:action_id (:id copy)
- :dataset_query (lib/native-query mp "UPDATE orders SET discount = 0")}]
- (let [body (:body (get-schemas :crowberto 200))]
- (testing "a table in the data library is listed"
- (is (str/includes? body " orders: {"))
- (is (str/includes? body "name: \"Orders\"")))
- (testing "a metric in the metrics library is listed"
- (is (str/includes? body "name: \"Order revenue\"")))
- (testing "an action without a model is listed, and one with a model never is"
- (is (str/includes? body "discountOrder"))
- (is (not (str/includes? body "updateOrder"))))
- (testing "a copy a data app owns stays out"
- (is (not (str/includes? body "copiedOrder")))))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_test.clj
deleted file mode 100644
index 62b3cc244a0..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schemas_test.clj
+++ /dev/null
@@ -1,144 +0,0 @@
-(ns metabase-enterprise.data-apps.generate.schemas-test
- "Data app schema tests form a pyramid; put new tests at the lowest level that
- can express them:
-
- - In-memory (no db): printer goldens in `javascript-test` (the output format
- spec), structural =? AST tests in `render-test`, pure shaping in
- `common-test` and the `schemas.*` tests, assembly from literal [[Items]]
- against a reified literal source here.
- - Db seam (with-temp): `source-test` for `app-db-source`.
- - One end-to-end test here on `mt/dataset test-data`, through the whole
- pipeline to rendered TypeScript."
- (:require
- [clojure.string :as str]
- [clojure.test :refer :all]
- [metabase-enterprise.data-apps.generate.schemas :as schemas]
- [metabase-enterprise.data-apps.generate.schemas.source :as schemas.source]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
- [metabase.lib.core :as lib]
- [metabase.lib.metadata :as lib.metadata]
- [metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]
- [toucan2.core :as t2]))
-
-(use-fixtures :once (fixtures/initialize :db :test-users))
-
-(def ^:private test-info
- {:generated-at "2026-01-01T00:00:00Z"
- :instance-url "https://metabase.example.com"})
-
-(deftest create-schema-assembles-items-test
- (is (= {:schemaVersion 2
- :generatedAt "2026-01-01T00:00:00Z"
- :metabase {:instanceUrl "https://metabase.example.com"}
- :actions {"shipOrder" {:kind "action", :key "shipOrder", :id 11}}
- :tables {"orders" {:type "table", :key "orders", :id 3}}
- :metrics {"revenue" {:type "metric", :key "revenue", :id 2}}}
- (schemas/create-schema
- {:actions [{:kind "action", :key "shipOrder", :id 11}]
- :tables [{:type "table", :key "orders", :id 3}]
- :metrics [{:type "metric", :key "revenue", :id 2}]}
- test-info))))
-
-(deftest create-schema-disambiguates-duplicate-keys-test
- (let [schema (schemas/create-schema
- {:actions []
- :tables [{:type "table", :key "orders", :id 3}
- {:type "table", :key "orders", :id 4}]
- :metrics []}
- test-info)]
- (is (= ["orders3" "orders4"] (keys (:tables schema))))
- (is (= ["orders3" "orders4"] (map :key (vals (:tables schema)))))))
-
-(deftest create-schema-defaults-info-test
- (let [schema (schemas/create-schema {:actions [], :tables [], :metrics []})]
- (is (string? (:generatedAt schema)))
- (is (contains? (:metabase schema) :instanceUrl))))
-
-(defn- literal-source
- "A [[schemas.source/SchemaSource]] over literal values. `tables` are filtered by the
- requested table ids so tests can see which tables fetching asked for."
- [{:keys [library-scope library-tables actions metrics tables]}]
- (reify schemas.source/SchemaSource
- (library-scope [_] library-scope)
- (actions [_] (vec actions))
- (metrics [_ _] (vec metrics))
- (tables [_ table-ids] (filterv #(contains? table-ids (:id %)) tables))
- (library-tables [_ _] (vec library-tables))))
-
-(deftest fetch-items-includes-tables-mapped-by-library-metrics-test
- (let [source (literal-source
- {:library-scope {:metric-collection-ids #{20}
- :data-collection-ids #{10}}
- :library-tables [{:id 10}]
- :actions [{:kind "action", :key "shipOrder", :id 11}]
- :metrics [{:type "metric", :key "revenue", :id 1, :mappedTableIds [42]}]
- :tables [{:id 10, :type "table", :key "publishedTable"}
- {:id 42, :type "table", :key "mappedTable"}
- {:id 99, :type "table", :key "notInScope"}]})]
- (is (= {:actions [{:kind "action", :key "shipOrder", :id 11}]
- :tables [{:id 10, :type "table", :key "publishedTable"}
- {:id 42, :type "table", :key "mappedTable"}]
- :metrics [{:type "metric", :key "revenue", :id 1, :mappedTableIds [42]}]}
- (schemas/fetch-items source)))))
-
-(deftest fetch-items-hands-the-source-the-library-scope-test
- (let [metric-collections (atom nil)
- library-tables (atom nil)
- source (reify schemas.source/SchemaSource
- (library-scope [_] {:data-collection-ids #{10} :metric-collection-ids #{20}})
- (actions [_] [])
- (metrics [_ collection-ids] (reset! metric-collections collection-ids) [])
- (tables [_ _] [])
- (library-tables [_ collection-ids] (reset! library-tables collection-ids) []))]
- (schemas/fetch-items source)
- (is (= #{20} @metric-collections))
- (is (= #{10} @library-tables))))
-
-(deftest full-pipeline-end-to-end-test
- (mt/dataset test-data
- (mt/with-temp-copy-of-db
- (mt/with-actions-enabled
- (data-apps.tu/do-with-library!
- (fn [{:keys [data-id metrics-id]}]
- (let [mp (mt/metadata-provider)
- orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
- revenue-query (lib/aggregate orders-query
- (lib/sum (lib.metadata/field mp (mt/id :orders :total))))]
- (t2/update! :model/Table (mt/id :orders) {:is_published true, :collection_id data-id})
- (mt/with-temp [:model/Card _metric {:name "Order revenue", :database_id (mt/id), :table_id (mt/id :orders)
- :type :metric, :display :scalar, :collection_id metrics-id
- :dataset_query revenue-query}
- :model/Card model {:name "Order model", :database_id (mt/id), :table_id (mt/id :orders)
- :type :model
- :dataset_query orders-query
- :result_metadata [{:name "total", :display_name "Total"
- :base_type :type/Float
- :field_ref [:field (mt/id :orders :total) nil]
- :id (mt/id :orders :total)}]}
- :model/Action action {:name "Update order", :model_id (:id model), :type :implicit}
- :model/ImplicitAction _ {:action_id (:id action), :kind "row/update"}
- :model/Action standalone {:name "Discount order", :type :query}
- :model/QueryAction _ {:action_id (:id standalone)
- :dataset_query (lib/native-query mp "UPDATE orders SET discount = 0 WHERE id = {{id}}")}]
- (mt/with-current-user (mt/user->id :crowberto)
- (let [schema (schemas/create-schema (schemas/fetch-items) test-info)
- body (schemas/render-typescript schema)]
- (testing "the library's table and metric and the model-less action land in the schema with their real relationships"
- (is (=? {:generatedAt "2026-01-01T00:00:00Z"
- :metabase {:instanceUrl "https://metabase.example.com"}
- :tables {"orders" {:fields {"total" {:jsType "number"}}}}
- :metrics {"orderRevenue" {:mappedTableIds [(mt/id :orders)]
- :columns [{:displayName "Sum of Total"
- :jsType "number"}]}}
- :actions {"discountOrder" {:kind "action", :id (:id standalone), :type "query"}}}
- schema)))
- (testing "saved questions are absent from the schema"
- (is (not (contains? schema :questions))))
- (testing "an action that belongs to a model stays out"
- (is (not (str/includes? body "updateOrder"))))
- (testing "the rendered module carries the real entities"
- (is (str/includes? body "orders: {"))
- (is (str/includes? body "name: \"Order revenue\""))
- (is (str/includes? body "discountOrder: {"))
- (is (str/ends-with? body "export default schema;\n")))))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
index f63d038603f..f705f2b3cfd 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj
@@ -44,10 +44,9 @@
(let [list-apps (api.macros/find-route-fn 'metabase-enterprise.data-apps.api :get "/")]
(mt/with-current-user (mt/user->id :rasta)
(list-apps {} {})
- (doseq [[query expected] [[{} [{:name "birds" :display_name "Birds"}
- {:name "disabled" :display_name "Disabled"}]]
- [{:available true} [{:name "birds" :display_name "Birds"}]]]]
- (is (= expected (list-apps {} query))))))))
+ (testing "a disabled app isn't listed to a non-superuser, available or not"
+ (doseq [query [{} {:available true}]]
+ (is (= [{:name "birds" :display_name "Birds"}] (list-apps {} query)))))))))
(deftest read-access-agrees-across-entry-points-test
(mt/with-premium-features #{:data-apps :tenants}
@@ -71,9 +70,9 @@
[(:id tenant-user) false false]]]
(testing (str "read access for user " user-id)
(is (= (if allowed? ["birds"] [])
- (mapv :name (mt/user-http-request user-id :get 200 "apps"))))
- (mt/user-http-request user-id :get (if allowed? 200 403) "apps/birds")
- (mt/user-http-request user-id :get (if allowed? 200 403) "apps/birds/bundle")
+ (mapv :name (mt/user-http-request user-id :get 200 "ee/apps"))))
+ (mt/user-http-request user-id :get (if allowed? 200 403) "ee/apps/birds")
+ (mt/user-http-request user-id :get (if allowed? 200 403) "ee/apps/birds/bundle")
(mt/with-current-user user-id
(is (= allowed? (boolean (mi/can-read? app)))))
(mt/with-current-user user-id
@@ -89,27 +88,27 @@
:model/PermissionsGroup finches {:name "Finches"}
:model/PermissionsGroup owls {:name "Owls"}
:model/PermissionsGroup tenant {:is_tenant_group true}]
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/groups")))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))
(doseq [ids [[(:id finches) (:id (perms/admin-group))]
[(:id finches) (:id tenant)]
[]
(vec (range 1 102))]]
- (mt/user-http-request :crowberto :post 400 "apps/birds/groups" {:group_ids ids})
+ (mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids ids})
(is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app)))))
- (mt/user-http-request :crowberto :post 404 "apps/birds/groups" {:group_ids [(:id finches) Integer/MAX_VALUE]})
+ (mt/user-http-request :crowberto :post 404 "ee/apps/birds/groups" {:group_ids [(:id finches) Integer/MAX_VALUE]})
(is (=? [{:id (:id finches) :name "Finches" :member_count 0}
{:id (:id owls) :name "Owls" :member_count 0}]
- (mt/user-http-request :crowberto :post 200 "apps/birds/groups"
+ (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups"
{:group_ids [(:id finches) (:id finches) (:id owls)]})))
(is (= #{(:id finches) (:id owls)}
(t2/select-fn-set :permission_group_id :model/DataAppGroupAssignment :data_app_id (:id app))))
- (mt/user-http-request :crowberto :post 400 "apps/birds/groups" {:group_ids [(:id finches)]})
- (mt/user-http-request :crowberto :delete 204 (str "apps/birds/groups/" (:id finches)))
- (is (= [(:id owls)] (mapv :id (mt/user-http-request :crowberto :get 200 "apps/birds/groups"))))
+ (mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids [(:id finches)]})
+ (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id finches)))
+ (is (= [(:id owls)] (mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups"))))
(doseq [[method path body] [[:get "groups" nil]
[:post "groups" {:group_ids [(:id finches)]}]
[:delete (str "groups/" (:id owls)) nil]]]
- (apply mt/user-http-request :rasta method 403 (str "apps/birds/" path) (when body [body]))))))
+ (apply mt/user-http-request :rasta method 403 (str "ee/apps/birds/" path) (when body [body]))))))
(deftest group-api-ignores-unknown-fields-test
(mt/with-premium-features #{:data-apps}
@@ -117,7 +116,7 @@
:model/PermissionsGroup group {}
:model/PermissionsGroup ignored-group {}]
(is (=? [{:id (:id group)}]
- (mt/user-http-request :crowberto :post 200 "apps/birds/groups"
+ (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups"
{:group_ids [(:id group)]
:user_ids [(mt/user->id :rasta)]
:permission_group_ids [(:id ignored-group)]})))
@@ -131,7 +130,7 @@
(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}
:model/PermissionsGroup group {}]
(t2/delete! :model/Collection :id (:resource_collection_id app))
- (mt/user-http-request :crowberto :post 200 "apps/birds/groups" {:group_ids [(:id group)]})
+ (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups" {:group_ids [(:id group)]})
(is (t2/exists? :model/DataAppGroupAssignment :data_app_id (:id app) :permission_group_id (:id group)))
(is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app)))))))
@@ -142,7 +141,7 @@
(doseq [[tenants? expected-name] [[false "All Users"] [true "All internal users"]]]
(mt/with-temporary-setting-values [use-tenants tenants?]
(is (= [expected-name]
- (mapv :name (mt/user-http-request :crowberto :get 200 "apps/birds/groups")))))))))
+ (mapv :name (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))))
(deftest revoke-assignment-with-trashed-collection-test
(mt/with-premium-features #{:data-apps}
@@ -156,7 +155,7 @@
(mt/with-dynamic-fn-redefs [perms/data-app-collection? (constantly false)]
(collection/archive-or-unarchive-collection!
(t2/select-one :model/Collection :id collection-id) {:archived true})))
- (mt/user-http-request :crowberto :delete 204 (str "apps/birds/groups/" (:id group)))
+ (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id group)))
(is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))
(is (empty? (t2/select :model/Permissions :group_id (:id group)
:object [:in [(perms/collection-read-path collection-id)
@@ -213,6 +212,20 @@
(is (true? (data-apps/check-data-app-access!
{:route-params {:name "birds"} :metabase-user-id (mt/user->id :rasta)})))))))
+(deftest disabled-app-access-test
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js" :enabled false}]
+ (group-access/add-groups! app [(:id (perms/all-users-group))])
+ (testing "a superuser reaches a disabled app"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (is (true? (data-apps/check-data-app-access!
+ {:route-params {:name "birds"} :metabase-user-id (mt/user->id :crowberto) :is-superuser? true})))))
+ (testing "a user with group access does not reach it"
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."
+ (data-apps/check-data-app-access!
+ {:route-params {:name "birds"} :metabase-user-id (mt/user->id :rasta)}))))))))
+
(deftest group-endpoints-require-feature-test
(mt/with-premium-features #{:data-apps}
(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}
@@ -221,10 +234,10 @@
(group-access/add-groups! app [(:id group)])
(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))]
(mt/with-premium-features #{}
- (mt/user-http-request :crowberto :get 402 "apps")
- (mt/user-http-request :crowberto :get 402 "apps/birds/groups")
- (mt/user-http-request :crowberto :post 402 "apps/birds/groups" {:group_ids [(:id group)]})
- (mt/user-http-request :crowberto :delete 402 (str "apps/birds/groups/" (:id group)))
+ (mt/user-http-request :crowberto :get 402 "ee/apps")
+ (mt/user-http-request :crowberto :get 402 "ee/apps/birds/groups")
+ (mt/user-http-request :crowberto :post 402 "ee/apps/birds/groups" {:group_ids [(:id group)]})
+ (mt/user-http-request :crowberto :delete 402 (str "ee/apps/birds/groups/" (:id group)))
(is (t2/exists? :model/DataAppGroupAssignment :data_app_id (:id app) :permission_group_id (:id group)))
(is (t2/exists? :model/Permissions :group_id (:id group)
:object (perms/collection-read-path collection-id))))))))
@@ -247,10 +260,10 @@
(resources/ensure-resources! app)
(group-access/add-groups! app [(:id group)])
(t2/update! :model/PermissionsGroup (:id group) {:is_tenant_group true})
- (mt/user-http-request :rasta :delete 403 (str "apps/birds/groups/" (:id group)))
+ (mt/user-http-request :rasta :delete 403 (str "ee/apps/birds/groups/" (:id group)))
(is (= [(:id group)] (mapv :id (group-access/assigned-groups app))))
- (mt/user-http-request :crowberto :delete 204 (str "apps/birds/groups/" (:id group)))
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/groups")))
+ (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id group)))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))
(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))]
(is (not (t2/exists? :model/Permissions :group_id (:id group)
:object (perms/collection-read-path collection-id))))))))
@@ -264,8 +277,8 @@
;; Insert a legacy assignment without the model hooks that now reject it.
(t2/query {:insert-into (t2/table-name :model/DataAppGroupAssignment)
:values [{:data_app_id (:id app) :permission_group_id admin-id}]})
- (mt/user-http-request :crowberto :delete 204 (str "apps/birds/groups/" admin-id))
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/groups")))
+ (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" admin-id))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))
(is (= permissions (t2/select :model/Permissions :group_id admin-id)))))))
(deftest revoke-assignment-requires-matching-app-and-group-test
@@ -274,14 +287,14 @@
:model/DataApp _ {:name "wrens" :display_name "Wrens" :bundle_path "wrens.js"}
:model/PermissionsGroup group {}]
(group-access/add-groups! app [(:id group)])
- (doseq [[path status] [[(str "apps/wrens/groups/" (:id group)) 404]
- [(str "apps/missing/groups/" (:id group)) 404]
- ["apps/birds/groups/2147483647" 404]
- ["apps/birds/groups/0" 400]
- ["apps/birds/groups/not-an-id" 400]]]
+ (doseq [[path status] [[(str "ee/apps/wrens/groups/" (:id group)) 404]
+ [(str "ee/apps/missing/groups/" (:id group)) 404]
+ ["ee/apps/birds/groups/2147483647" 404]
+ ["ee/apps/birds/groups/0" 400]
+ ["ee/apps/birds/groups/not-an-id" 400]]]
(mt/user-http-request :crowberto :delete status path))
(is (= [(:id group)]
- (mapv :id (mt/user-http-request :crowberto :get 200 "apps/birds/groups")))))))
+ (mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))
(deftest collection-drift-denies-access-until-grants-are-reconciled-test
(mt/with-premium-features #{:data-apps}
@@ -291,10 +304,10 @@
(group-access/add-groups! app [group-id])
(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))]
(perms/revoke-collection-permissions! group-id collection-id)
- (mt/user-http-request :rasta :get 403 "apps/birds")
+ (mt/user-http-request :rasta :get 403 "ee/apps/birds")
(resources/ensure-resources! app)
(is (= {:name "birds" :display_name "Birds"}
- (mt/user-http-request :rasta :get 200 "apps/birds"))))))))
+ (mt/user-http-request :rasta :get 200 "ee/apps/birds"))))))))
(deftest tenant-users-cannot-inherit-app-access-test
(mt/with-premium-features #{:tenants}
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
index 94729b701b9..0d8fa728916 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj
@@ -128,7 +128,7 @@
(with-app-reading! [_app [(mt/id :venues) (mt/id :orders)]]
(mt/with-temp [:model/PermissionsGroup group {}]
(perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warnings-include-inherited-access-test
(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}
@@ -140,7 +140,7 @@
(perms/set-table-permission! (perms/all-users-group) (mt/id :venues) :perms/view-data :unrestricted)
(is (=? [{:group_id (:id group)
:missing_tables [{:id (mt/id :orders) :name "Orders" :database_id (mt/id)}]}]
- (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warnings-preserve-permissions-test
(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}
@@ -150,7 +150,7 @@
(perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)
(group-access/add-groups! app [(:id group)])
(let [before (t2/select :model/DataPermissions)]
- (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings")
+ (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")
(is (= before (t2/select :model/DataPermissions)))))))))
(deftest group-permission-warnings-include-direct-access-test
@@ -162,7 +162,7 @@
(perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)
(perms/set-table-permission! (:id group) (mt/id :venues) :perms/view-data :unrestricted)
(perms/set-table-permission! (:id group) (mt/id :orders) :perms/view-data :unrestricted)
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warnings-include-sandbox-access-test
(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}
@@ -176,13 +176,13 @@
{"PUBLIC" {(mt/id :venues) :unrestricted
(mt/id :orders) :sandboxed}})
(assoc :sandboxes [{:group_id (:id group) :table_id (mt/id :orders)}])))
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings")))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")))
(mt/user-http-request :crowberto :put 200 "permissions/graph"
(assoc-in (data-perms.graph/api-graph)
[:groups (:id group) (mt/id) :view-data "PUBLIC" (mt/id :orders)]
:blocked))
(is (=? [{:group_id (:id group) :missing_tables [{:id (mt/id :orders)}]}]
- (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warnings-require-admin-test
(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}
@@ -190,7 +190,7 @@
(with-app-reading! [app [(mt/id :venues) (mt/id :orders)]]
(mt/with-temp [:model/PermissionsGroup group {}]
(group-access/add-groups! app [(:id group)])
- (mt/user-http-request :rasta :get 403 "apps/birds/group-permission-warnings"))))))
+ (mt/user-http-request :rasta :get 403 "ee/apps/birds/group-permission-warnings"))))))
(deftest group-permission-warnings-without-dependencies-test
(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}
@@ -198,11 +198,11 @@
(with-app-reading! [app []]
(mt/with-temp [:model/PermissionsGroup group {}]
(group-access/add-groups! app [(:id group)])
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warnings-missing-app-test
(mt/with-premium-features #{:data-apps}
- (mt/user-http-request :crowberto :get 404 "apps/missing/group-permission-warnings")))
+ (mt/user-http-request :crowberto :get 404 "ee/apps/missing/group-permission-warnings")))
(deftest group-permission-warnings-use-group-access-not-memberships-test
(mt/with-premium-features #{:data-apps :advanced-permissions}
@@ -229,13 +229,13 @@
(assoc-in [:groups (:id group) (mt/id) :view-data] :impersonated)
(assoc :impersonations [{:group_id (:id group) :db_id (mt/id)
:attribute "role"}])))
- (is (= [] (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings")))
+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")))
(mt/user-http-request :crowberto :put 200 "permissions/graph"
(assoc-in (data-perms.graph/api-graph)
[:groups (:id group) (mt/id) :view-data]
:blocked))
(is (=? [{:group_id (:id group) :missing_tables [{:id (mt/id :venues)}]}]
- (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
(deftest group-permission-warning-lookups-are-batched-test
(mt/with-no-data-perms-for-all-users!
@@ -256,8 +256,8 @@
(mt/with-temp [:model/PermissionsGroup group {}]
(group-access/add-groups! app [(:id group)])
(mt/with-premium-features #{}
- (mt/user-http-request :crowberto :get 402 "apps/birds/group-permission-warnings")
- (mt/user-http-request :rasta :get 402 "apps/birds/group-permission-warnings")))))
+ (mt/user-http-request :crowberto :get 402 "ee/apps/birds/group-permission-warnings")
+ (mt/user-http-request :rasta :get 402 "ee/apps/birds/group-permission-warnings")))))
(deftest group-permission-warning-lookups-exclude-unrelated-permissions-test
(mt/with-temp [:model/Database other-db {}
@@ -307,4 +307,4 @@
:database_id (:id aviary) :database_name "Aviary"}
{:id (:id albatrosses) :name "Albatrosses" :schema nil
:database_id (:id zoo) :database_name "Zoo"}]}]
- (mt/user-http-request :crowberto :get 200 "apps/birds/group-permission-warnings"))))))))
+ (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/query_definition_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/query_definition_test.clj
deleted file mode 100644
index a2a0238af01..00000000000
--- a/enterprise/backend/test/metabase_enterprise/data_apps/query_definition_test.clj
+++ /dev/null
@@ -1,85 +0,0 @@
-(ns metabase-enterprise.data-apps.query-definition-test
- (:require
- [clojure.test :refer [deftest is testing]]
- [metabase-enterprise.data-apps.query-definition :as query-definition]
- [metabase.lib.schema.test-spec :as test-spec]
- [metabase.util.malli.registry :as mr]))
-
-(def ^:private query-definition
- {:stages [{:source {:type :table :id 1}
- :fields [{:type :column :name "ID"}
- {:type :column :name "NAME" :source-field-id 2}]
- :filters [{:type :operator :operator :>
- :args [{:type :column :name "PRICE"}
- {:type :literal :value 1}]}
- {:type :segment :id 1}]
- :aggregations [{:type :operator :operator :count :args []}
- {:type :measure :id 1}
- {:type :metric :id 1}]
- :breakouts [{:type :column :name "DATE" :unit :month}
- {:type :column :name "PRICE" :binning {:strategy :num-bins :num-bins 10}}]
- :order-bys [{:type :column :name "DATE" :unit :month :direction :asc}]
- :limit 10}]})
-
-(deftest ^:parallel supported-query-fields-test
- (is (nil? (mr/explain ::query-definition/query-definition query-definition))))
-
-(deftest ^:parallel named-aggregation-test
- (testing "an aggregation named in its definition, which the SDK sends as `name`"
- (is (nil? (mr/explain ::query-definition/query-definition
- (assoc-in query-definition [:stages 0 :aggregations]
- [{:type :operator :operator :sum :name "revenue"
- :args [{:type :column :name "PRICE"}]}]))))))
-
-(deftest ^:parallel only-an-aggregation-is-named-test
- (testing "a name on a filter or a nested argument is refused rather than dropped"
- (doseq [path [[:stages 0 :filters 0]
- [:stages 0 :aggregations 0 :args 0]]]
- (testing (pr-str path)
- (is (some? (mr/explain ::query-definition/query-definition
- (assoc-in query-definition path
- {:type :operator :operator :count :name "n" :args []}))))))))
-
-(deftest ^:parallel unknown-query-fields-test
- (doseq [path [[] [:stages 0] [:stages 0 :source]
- [:stages 0 :fields 0]
- [:stages 0 :filters 0 :args 1]
- [:stages 0 :aggregations 1] [:stages 0 :breakouts 0]
- [:stages 0 :order-bys 0]]]
- (testing (str "Unknown fields are not part of the request contract at " path)
- (is (not (mr/validate ::query-definition/query-definition
- (assoc-in query-definition (conj path :unexpected) true)))))))
-
-(deftest ^:parallel required-query-source-test
- (is (not (mr/validate ::query-definition/query-definition {:stages []})))
- (is (not (mr/validate ::query-definition/query-definition {:stages [{}]}))))
-
-(deftest ^:parallel column-unit-only-allowed-with-bucketing-test
- (let [column {:type :column :name "DATE" :unit :month}]
- (is (not (mr/validate ::test-spec/test-column-spec column)))
- (is (mr/validate ::test-spec/test-breakout-spec column))
- (is (not (mr/validate ::query-definition/column column)))
- (is (mr/validate ::query-definition/breakout column))))
-
-(deftest ^:parallel unsupported-query-features-test
- (doseq [query [{:stages (repeat 2 (first (:stages query-definition)))}
- (assoc-in query-definition [:stages 0 :source] {:type :card :id 1})
- (assoc-in query-definition [:stages 0 :joins] [])
- (assoc-in query-definition [:stages 0 :expressions] [])
- (assoc-in query-definition [:stages 0 :aggregations]
- [{:name "count" :value {:type :operator :operator :count :args []}}])]]
- (is (not (mr/validate ::query-definition/query-definition query)))))
-
-(deftest ^:parallel named-saved-aggregation-test
- (testing "measure or metric named with `aggregations.measure` / `aggregations.metric`"
- (doseq [saved [{:type :measure :id 1} {:type :metric :id 1}]]
- (is (nil? (mr/explain ::query-definition/query-definition
- (assoc-in query-definition [:stages 0 :aggregations]
- [{:name "revenue" :value saved}])))))))
-
-(deftest ^:parallel named-breakout-test
- (testing "breakout named with `breakout(field, { name })`"
- (is (nil? (mr/explain ::query-definition/query-definition
- (assoc-in query-definition [:stages 0 :breakouts]
- [{:type :breakout :name "created_month"
- :column {:type :column :name "DATE" :unit :month}}]))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj
index d655a00ade7..8524ba5f1ae 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj
@@ -60,7 +60,7 @@
(testing "a slug is used verbatim"
(is (= "inventory-2" (normalize ::data-apps.schema/slug "inventory-2"))))
(testing "a slug that can't appear in a URL as-is, or collides with an API sub-route, is rejected"
- (doseq [bad [nil "" "Sales" "my_app" "sales app" "-sales" "sales-" "sales\n" "repo-status" "sandbox-host" "generate"
+ (doseq [bad [nil "" "Sales" "my_app" "sales app" "-sales" "sales-" "sales\n" "repo-status" "sandbox-host"
(apply str (repeat 101 "a"))]]
(is (not (valid? ::data-apps.schema/slug bad)) (str "should reject: " (pr-str bad))))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
index 72b3650e3a9..ca169561cec 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
@@ -10,9 +10,7 @@
[metabase-enterprise.dependencies.test-util :as deps.test]
[metabase-enterprise.remote-sync.impl :as impl]
[metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task]
- [metabase-enterprise.remote-sync.source :as source]
[metabase-enterprise.remote-sync.source.protocol :as source.p]
- [metabase-enterprise.remote-sync.spec :as spec]
[metabase-enterprise.remote-sync.test-helpers :as test-helpers]
[metabase.actions.core :as actions]
[metabase.collections.models.collection :as collection]
@@ -24,7 +22,6 @@
[metabase.search.test-util :as search.tu]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
- [metabase.util :as u]
[metabase.util.yaml :as yaml]
[toucan2.core :as t2]))
@@ -111,10 +108,10 @@
(with-data-apps-sync
(let [mock (test-helpers/create-mock-source :initial-files {"main" {}})
repo #(get @(:files-atom mock) "main")]
- (mt/user-http-request :crowberto :post 200 "apps" {:name "sales"
- :display_name "Sales"
- :bundle_path "dist/index.js"
- :bundle "BUNDLE"})
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "sales"
+ :display_name "Sales"
+ :bundle_path "dist/index.js"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/sales/bundle" {:bundle "BUNDLE"})
(testing "a created app is exported as its manifest, its bundle file, and its collection's file"
(is (= :success (:status (export! mock))))
(is (=? {:slug "sales" :name "Sales" :path "dist/index.js" :collection string?}
@@ -123,15 +120,15 @@
(is (=? {:name "Data App: sales" :namespace "data-apps"}
(yaml/parse-string (get (repo) "collections/data_apps/data_app__sales.yaml")))))
(testing "an updated bundle is exported"
- (mt/user-http-request :crowberto :put 200 "apps/sales" {:bundle "BUNDLE-V2"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/sales/bundle" {:bundle "BUNDLE-V2"})
(is (= :success (:status (export! mock))))
(is (= "BUNDLE-V2" (get (repo) "data_apps/sales/dist/index.js"))))
(testing "a deleted app's files are removed from the repo, and the other apps' files stay"
- (mt/user-http-request :crowberto :post 200 "apps" {:name "ops"
- :display_name "Ops"
- :bundle_path "app.js"
- :bundle "OPS"})
- (mt/user-http-request :crowberto :delete 204 "apps/sales")
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "ops"
+ :display_name "Ops"
+ :bundle_path "app.js"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/ops/bundle" {:bundle "OPS"})
+ (mt/user-http-request :crowberto :delete 204 "ee/apps/sales")
(is (= :success (:status (export! mock))))
(is (= #{"data_apps/ops/data_app.yaml" "data_apps/ops/app.js" "collections/data_apps/data_app__ops.yaml"}
(app-files-in (repo))))))))
@@ -147,16 +144,16 @@
:initial-files {"main" (merge (app-tree sales-eid "sales" "BUNDLE-V1") source)})
repo #(get @(:files-atom mock) "main")]
(is (= :success (:status (import-at! mock "main" :force? true))))
- (mt/user-http-request :crowberto :put 200 "apps/sales" {:bundle "BUNDLE-V2"})
- (mt/user-http-request :crowberto :post 200 "apps" {:name "ops"
- :display_name "Ops"
- :bundle_path "app.js"
- :bundle "OPS"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/sales/bundle" {:bundle "BUNDLE-V2"})
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "ops"
+ :display_name "Ops"
+ :bundle_path "app.js"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/ops/bundle" {:bundle "OPS"})
(is (= :success (:status (export! mock))))
(is (= "BUNDLE-V2" (get (repo) "data_apps/sales/dist/index.js")))
(is (= source (select-keys (repo) (keys source))))
(testing "deleting the app removes only the files serialization owns"
- (mt/user-http-request :crowberto :delete 204 "apps/sales")
+ (mt/user-http-request :crowberto :delete 204 "ee/apps/sales")
(is (= :success (:status (export! mock))))
(is (= (into #{"data_apps/ops/data_app.yaml" "data_apps/ops/app.js" "collections/data_apps/data_app__ops.yaml"}
(keys source))
@@ -168,10 +165,10 @@
(let [app (app-tree sales-eid "sales" "BUNDLE")
src (test-helpers/versioned-source :trees {"v0" app "v1" (assoc app "README.md" "x")} :current "v0")]
(is (= :success (:status (import-at! src "v0" :force? true))))
- (mt/user-http-request :crowberto :post 200 "apps" {:name "ops"
- :display_name "Ops"
- :bundle_path "app.js"
- :bundle "OPS"})
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "ops"
+ :display_name "Ops"
+ :bundle_path "app.js"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/ops/bundle" {:bundle "OPS"})
(is (=? {:status :conflict} (import-at! src "v1")))
(is (t2/exists? :model/DataApp :name "ops"))))))
@@ -290,10 +287,10 @@
(with-data-apps-sync
(let [mock (test-helpers/create-mock-source :initial-files {"main" {}})
repo #(get @(:files-atom mock) "main")]
- (mt/user-http-request :crowberto :post 200 "apps" {:name "sales"
- :display_name "Sales"
- :bundle_path "dist/index.js"
- :bundle "BUNDLE"})
+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "sales"
+ :display_name "Sales"
+ :bundle_path "dist/index.js"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/sales/bundle" {:bundle "BUNDLE"})
(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :name "sales")
mp (mt/metadata-provider)]
(mt/with-temp [:model/Card _ {:name "Venues list"
@@ -365,8 +362,8 @@
(is (=? [{:group_id (:id group) :missing_tables [{:id (mt/id :checkins)} {:id (mt/id :venues)}]}]
(group-access/permission-warnings app))))))))))))
-(deftest an-apps-collection-is-read-only-test
- (testing "an admin can't write an app's collection, its cards or its actions, whatever the sync mode"
+(deftest an-apps-collection-is-written-by-admins-test
+ (testing "an admin can write an app's collection, its cards and its actions, unless the instance is read-only"
(with-data-apps-sync
(data-apps.tu/do-with-sources!
(fn [{:keys [action-id]}]
@@ -374,46 +371,29 @@
shop-collection-name shop-collection-eid
[{:entity_id question-eid :name "VenuesList" :query (venues-query)}]
[action-id])
- src (test-helpers/versioned-source :trees {"v0" (shop-tree resources)} :current "v0")
- mp (mt/metadata-provider)
- venues-q (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
+ src (test-helpers/versioned-source :trees {"v0" (shop-tree resources)} :current "v0")]
(is (= :success (:status (import-at! src "v0" :force? true))))
(let [collection-id (shop-collection-id)
card-id (t2/select-one-pk :model/Card :entity_id question-eid)
copy-id (t2/select-one-pk :model/Action :collection_id collection-id)
- read-only! (fn []
- (is (=? {:can_write false}
+ writes! (fn [status can-write?]
+ (is (=? {:can_write can-write?}
(mt/user-http-request :crowberto :get 200 (str "collection/" collection-id))))
- (is (=? {:can_write false}
+ (is (=? {:can_write can-write?}
(mt/user-http-request :crowberto :get 200 (str "card/" card-id))))
- (mt/user-http-request :crowberto :put 403 (str "collection/" collection-id) {:name "Renamed"})
- (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:description "edited here"})
- (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:collection_id nil})
- (mt/user-http-request :crowberto :post 403 "card/collections" {:card_ids [card-id] :collection_id nil})
- (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "Elsewhere"}
- :model/Card {mover-id :id} {:name "Mover" :dataset_query venues-q}]
- (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:dashboard_id dashboard-id})
- (mt/user-http-request :crowberto :post 403 "card/collections" {:card_ids [mover-id] :collection_id collection-id}))
- (mt/user-http-request :crowberto :delete 403 (str "card/" card-id))
- (mt/user-http-request :crowberto :post 403 "card"
- {:name "New"
- :display "table"
- :visualization_settings {}
- :collection_id collection-id
- :dataset_query venues-q})
+ (mt/user-http-request :crowberto :put status (str "card/" card-id) {:description "edited here"})
(mt/with-actions-enabled
- (mt/user-http-request :crowberto :put 403 (str "action/" copy-id) {:name "Renamed"})))]
+ (mt/user-http-request :crowberto :put status (str "action/" copy-id) {:name "Renamed"}))
+ (mt/user-http-request :rasta :put 403 (str "card/" card-id) {:description "mine"}))]
(testing "on a read-write instance"
(mt/with-temporary-setting-values [remote-sync-type :read-write remote-sync-url "https://example.com/repo.git"]
- (read-only!)))
- (testing "on a read-only instance"
- (mt/with-temporary-setting-values [remote-sync-type :read-only remote-sync-url "https://example.com/repo.git"]
- (read-only!)))
+ (writes! 200 true)))
(testing "without remote sync"
(mt/with-temporary-setting-values [remote-sync-url nil]
- (read-only!)))
- (is (=? {:collection_id collection-id :name "VenuesList" :description nil}
- (t2/select-one :model/Card :id card-id))))))))))
+ (writes! 200 true)))
+ (testing "on a read-only instance"
+ (mt/with-temporary-setting-values [remote-sync-type :read-only remote-sync-url "https://example.com/repo.git"]
+ (writes! 403 false))))))))))
(deftest a-copy-of-an-apps-card-elsewhere-is-a-card-like-any-other-test
(testing "copying a card out of an app's collection needs only read access to it"
@@ -572,7 +552,7 @@
:database_id (mt/id)
:collection_id collection-id
:dataset_query (lib/query mp (lib.metadata/card mp card-id))})]
- (mt/user-http-request :crowberto :delete 204 "apps/shop")
+ (mt/user-http-request :crowberto :delete 204 "ee/apps/shop")
(is (not (t2/exists? :model/Collection :id collection-id)))
(is (not (t2/exists? :model/Card :id [:in [card-id reader-id]]))))))))
@@ -771,7 +751,7 @@
(is (= before (resource-files mock)))
(testing "deleting the app still deletes its collection"
(let [collection-id (shop-collection-id)]
- (mt/user-http-request :crowberto :delete 204 "apps/shop")
+ (mt/user-http-request :crowberto :delete 204 "ee/apps/shop")
(is (not (t2/exists? :model/Collection :id collection-id))))))))))
(deftest an-apps-own-yaml-is-left-alone-test
@@ -782,7 +762,7 @@
:initial-files {"main" (assoc (shop-tree (question-resources)) own "greeting: hello\n")})]
(let [result (import-at! mock "main" :force? true)]
(is (= :success (:status result)) (:message result)))
- (mt/user-http-request :crowberto :put 200 "apps/shop" {:bundle "B2"})
+ (mt/user-http-request :crowberto :put 200 "ee/apps/shop/bundle" {:bundle "B2"})
(is (= :success (:status (export! mock))))
(is (= "greeting: hello\n" (get-in @(:files-atom mock) ["main" own])))))))
@@ -992,41 +972,3 @@
(is (= :error (:status result)))
(is (str/includes? (:message result) "must be a question or metric"))
(is (str/includes? (:message result) card-file))))))
-
-(defn- generated-app-tree
- "The repo files of the `shop` app as `/api/apps/generate` writes them: its manifest and collection, a question
- aggregating `metric-id`, the copy of that metric, the copy of `action-id`, and its bundle."
- [metric-id action-id]
- (let [{:keys [files]} (mt/user-http-request :crowberto :post 200 "apps/generate/app" {:name "Shop" :slug "shop"})
- collection (first (filter #(str/starts-with? (:path %) "collections/") files))
- collection-eid (:entity_id (yaml/parse-string (:yaml collection)))
- folder (str (str/replace (:path collection) #"\.yaml$" "") "/")
- resources (mt/user-http-request :crowberto :post 200 "apps/generate/resources"
- {:queries [{:name "VenueCount"
- :entity_id (u/generate-nano-id)
- :collection_id collection-eid
- :query {:stages [{:source {:type "table" :id (mt/id :venues)}
- :aggregations [{:type "metric" :id metric-id}]}]}}]
- :actions [{:action_id action-id
- :entity_id (u/generate-nano-id)
- :collection_id collection-eid}]})]
- (merge (into {} (map (juxt :path :yaml)) files)
- (into {} (map (fn [{:keys [file yaml]}] [(str folder file) yaml])) (mapcat resources [:queries :actions :metrics]))
- {"data_apps/shop/dist/index.js" "BUNDLE"})))
-
-(deftest generated-files-are-what-an-export-writes-test
- (testing "the files the generate endpoints write for an app are the ones an export writes once a pull saves them"
- (with-data-apps-sync
- (data-apps.tu/do-with-sources!
- (fn [{:keys [metric-id action-id]}]
- (t2/update! :model/Action action-id {:creator_id (mt/user->id :rasta)})
- (let [tree (generated-app-tree metric-id action-id)
- src (test-helpers/versioned-source :trees {"v0" tree} :current "v0")]
- (is (= 6 (count (app-files-in tree))))
- (let [result (import-at! src "v0" :force? true)]
- (is (= :success (:status result)) (:message result)))
- (let [exported (into {}
- (map (juxt :path :content))
- (source/serialize-specs (spec/extract-entities-for-export (spec/exportable-entities)) nil))]
- (is (= (select-keys tree (app-files-in tree))
- (select-keys exported (app-files-in exported)))))))))))
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/cli.ts b/enterprise/frontend/src/embedding-sdk-package/cli/cli.ts
index 4131d7c2ef6..59c257a1c0b 100644
--- a/enterprise/frontend/src/embedding-sdk-package/cli/cli.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/cli/cli.ts
@@ -1,7 +1,6 @@
import { Command } from "commander";
import { start } from "./actions/start";
-import { addDataAppsCommands } from "./commands/data-apps";
import { printError } from "./utils/print";
const program = new Command();
@@ -15,8 +14,6 @@ program
.description("downloads and starts a local Metabase instance")
.action(start);
-addDataAppsCommands(program);
-
program.parseAsync().catch((error: unknown) => {
printError(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.ts b/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.ts
deleted file mode 100644
index f8c208514df..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import type { Command } from "commander";
-
-import { checkResources } from "../../data-app-resources/check";
-import { writeResources } from "../../data-app-resources/serialize";
-
-type AppRootOptions = { appRoot: string };
-
-/** Adds `name` as a data app command that runs from the app's directory unless `--app-root` says otherwise. */
-const addAppCommand = (parent: Command, name: string, description: string) =>
- parent
- .command(name)
- .description(description)
- .option("--app-root <path>", "data app directory", process.cwd());
-
-export function addDataAppsCommands(program: Command) {
- const dataAppsCommand = program
- .command("data-apps")
- .description("manage Metabase data apps");
-
- addAppCommand(
- dataAppsCommand,
- "write-resources",
- "regenerate the files of the data app's collection, serialized by Metabase, next to the collection's file",
- ).action(async ({ appRoot }: AppRootOptions) => {
- process.stdout.write(`${await writeResources(appRoot)}\n`);
- });
-
- addAppCommand(
- dataAppsCommand,
- "check-resources",
- "check that the files of the data app's collection, under collections/data_apps/, back its query and action definitions",
- ).action(async ({ appRoot }: AppRootOptions) => {
- await checkResources(appRoot);
- process.stdout.write("The app's collection files back every definition.\n");
- });
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.unit.spec.ts
deleted file mode 100644
index f761a73290f..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/cli/commands/data-apps.unit.spec.ts
+++ /dev/null
@@ -1,116 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { Command } from "commander";
-
-import {
- makeApp,
- setupResourceTests,
- writeAction,
- writeQuery,
- writeResource,
-} from "../../data-app-resources/tests/setup";
-
-import { addDataAppsCommands } from "./data-apps";
-
-const QUESTION = "questionEntityId00010";
-const ACTION_COPY = "actionCopyEntityId001";
-const QUESTION_PATH =
- "collections/data_apps/data_app/orders_questionEntityId00010.yaml";
-
-async function run(...args: string[]) {
- const program = new Command();
- addDataAppsCommands(program);
- await program.parseAsync(["node", "cli", "data-apps", ...args]);
-}
-
-function appWithQuery() {
- const appRoot = makeApp();
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION}", source: { type: "table", id: 1 } });`,
- );
- return appRoot;
-}
-
-describe("data app commands", () => {
- setupResourceTests();
-
- let stdout: jest.SpyInstance;
-
- beforeEach(() => {
- stdout = jest.spyOn(process.stdout, "write").mockImplementation(() => true);
- });
-
- const printed = () =>
- stdout.mock.calls.map(([chunk]) => String(chunk)).join("");
-
- it("regenerates the collection's files for the definitions in the app root", async () => {
- const appRoot = appWithQuery();
- writeAction(
- appRoot,
- `export const Create = defineAction({ copiedActionEntityId: "${ACTION_COPY}", action: { id: 51, parameters: [] } });`,
- );
- fs.writeFileSync(
- path.join(appRoot, ".env.local"),
- "DATA_APP_MB_URL=http://metabase.test\nDATA_APP_MB_API_KEY=mb_test_key\n",
- );
- const fetchSpy = jest.spyOn(global, "fetch").mockResolvedValue(
- new Response(
- JSON.stringify({
- queries: [
- {
- file: "orders_questionEntityId00010.yaml",
- yaml: "name: Orders\n",
- },
- ],
- actions: [{ file: "create.yaml", yaml: "name: Create\n" }],
- metrics: [],
- }),
- ),
- );
-
- await run("write-resources", "--app-root", appRoot);
-
- expect(JSON.parse(String(fetchSpy.mock.calls[0][1]?.body)).actions).toEqual(
- [
- {
- action_id: 51,
- entity_id: ACTION_COPY,
- collection_id: "appCollectionEntity01",
- },
- ],
- );
- expect(fs.readFileSync(path.join(appRoot, QUESTION_PATH), "utf8")).toBe(
- "name: Orders\n",
- );
- expect(printed()).toBe(
- `Wrote ${QUESTION_PATH}\nWrote collections/data_apps/data_app/create.yaml\n`,
- );
- });
-
- it("confirms resources that back every definition", async () => {
- const appRoot = appWithQuery();
- writeResource(appRoot, "data_app/orders.yaml", {
- name: "Orders",
- type: "question",
- entity_id: QUESTION,
- "serdes/meta": [{ model: "Card", id: QUESTION }],
- });
-
- await run("check-resources", "--app-root", appRoot);
-
- expect(printed()).toBe(
- "The app's collection files back every definition.\n",
- );
- });
-
- it("fails when a definition's resource is missing", async () => {
- const appRoot = appWithQuery();
-
- await expect(run("check-resources", "--app-root", appRoot)).rejects.toThrow(
- `queries/orders.query.ts:Orders names saved question ${QUESTION}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- expect(printed()).toBe("");
- });
-});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/DevToolbar.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/DevToolbar.unit.spec.tsx
index e5b649b4d9b..39ad46b4c42 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/DevToolbar.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/DevToolbar.unit.spec.tsx
@@ -138,6 +138,7 @@ describe("DevToolbar open", () => {
],
{
manifest: {
+ exists: true,
name: null,
bundlePath: "dist/index.js",
bundlePathExists: true,
@@ -256,6 +257,7 @@ describe("DevToolbar open", () => {
it("renders the manifest status the feed carries", async () => {
serve([], {
manifest: {
+ exists: true,
name: "Demo",
bundlePath: "dist/index.js",
bundlePathExists: false,
@@ -274,6 +276,27 @@ describe("DevToolbar open", () => {
expect(screen.getByText(/file not found/)).toBeInTheDocument();
});
+ it("says when the app has no manifest", async () => {
+ serve([], {
+ manifest: {
+ exists: false,
+ name: null,
+ bundlePath: null,
+ bundlePathExists: false,
+ allowedHosts: [],
+ errors: [],
+ warnings: [],
+ restartRequired: false,
+ },
+ });
+ await setup();
+ await open();
+ await userEvent.click(screen.getByRole("tab", { name: /^Manifest/ }));
+
+ expect(screen.getByText(/No data_app\.yaml/)).toBeInTheDocument();
+ expect(screen.queryByText(/is valid/)).not.toBeInTheDocument();
+ });
+
it("renders the connection status the feed carries", async () => {
serve([], {
connection: {
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/ManifestTab/ManifestTab.tsx b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/ManifestTab/ManifestTab.tsx
index edf29c89c1d..f0bc6ea3795 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/ManifestTab/ManifestTab.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/components/DevToolbar/ManifestTab/ManifestTab.tsx
@@ -12,6 +12,15 @@ export const ManifestTab = ({ manifest }: Props) => {
return <div className={S.Empty}>Manifest has not been validated yet.</div>;
}
+ if (manifest.exists === false) {
+ return (
+ <div className={S.Empty}>
+ No data_app.yaml. It is optional: add one with allowed_hosts to let the
+ preview reach other hosts.
+ </div>
+ );
+ }
+
return (
<div className={S.StatusBody}>
{manifest.restartRequired && (
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.ts
index 9c4919d4091..ffc73de4316 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.ts
@@ -44,7 +44,10 @@ export function validateDataAppManifest(
appRoot: string,
initialAllowedHosts: string[],
): DataAppManifestStatus {
+ const manifestPath = path.join(appRoot, DATA_APP_MANIFEST_FILE_NAME);
+
const status: DataAppManifestStatus = {
+ exists: fs.existsSync(manifestPath),
name: null,
bundlePath: null,
bundlePathExists: false,
@@ -54,13 +57,7 @@ export function validateDataAppManifest(
restartRequired: false,
};
- const manifestPath = path.join(appRoot, DATA_APP_MANIFEST_FILE_NAME);
-
- if (!fs.existsSync(manifestPath)) {
- status.errors.push(
- `No ${DATA_APP_MANIFEST_FILE_NAME} found — this app will not sync.`,
- );
-
+ if (!status.exists) {
return status;
}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.unit.spec.ts
index bfa350dc6ef..955fd6a47e4 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/config/validate-manifest.unit.spec.ts
@@ -31,14 +31,14 @@ const setup = (files: Record<string, string | true>) => {
afterEach(() => jest.resetAllMocks());
describe("validateDataAppManifest", () => {
- it("errors when the manifest is missing (the app would silently not sync)", () => {
+ it("reports nothing when the app has no manifest", () => {
setup({});
const status = validateDataAppManifest(APP_ROOT, []);
- expect(status.errors).toEqual([
- "No data_app.yaml found — this app will not sync.",
- ]);
+ expect(status.exists).toBe(false);
+ expect(status.errors).toEqual([]);
+ expect(status.warnings).toEqual([]);
});
it("accepts a valid manifest whose bundle exists", () => {
@@ -50,6 +50,7 @@ describe("validateDataAppManifest", () => {
const status = validateDataAppManifest(APP_ROOT, []);
expect(status).toMatchObject({
+ exists: true,
name: "Sales dashboard",
bundlePath: "dist/index.js",
bundlePathExists: true,
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
index 822c2460bd4..958b9072404 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.ts
@@ -2,10 +2,6 @@ import path from "node:path";
import type { Plugin } from "vite";
-import {
- ACTION_DEFINITIONS,
- QUERY_DEFINITIONS,
-} from "../../data-app-resources/discover";
import { validateDataAppManifest } from "../config/validate-manifest";
import { DATA_APP_REBUILT_EVENT } from "../constants/bundle";
import { DATA_APP_DIAGNOSTICS_CHANGED_EVENT } from "../constants/diagnostics-channel";
@@ -21,18 +17,12 @@ import {
resolveDataAppVirtualId,
} from "./virtual-modules";
-// `src/` imports the definitions from the root-level `queries/` and `actions/`,
-// so the bundle is built from all three.
-const BUNDLED_DIRECTORIES: readonly string[] = [
- "src",
- QUERY_DEFINITIONS.directory,
- ACTION_DEFINITIONS.directory,
-];
+const BUNDLED_DIRECTORY = "src";
const isBundledFile = (root: string, file: string) => {
const [topDirectory] = path.relative(root, file).split(path.sep);
- return BUNDLED_DIRECTORIES.includes(topDirectory);
+ return topDirectory === BUNDLED_DIRECTORY;
};
export function dataAppSandboxDevPlugin(
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
index e94351417a8..fbf79a286e0 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/dev-plugin/plugin.unit.spec.ts
@@ -328,7 +328,7 @@ describe("dataAppSandboxDevPlugin", () => {
expect(mockedBuild).toHaveBeenCalledTimes(3);
});
- it("rebuilds when a query or action definition changes", async () => {
+ it("ignores changes outside the app's src/", async () => {
const { server } = await setup();
await server.watcher.emit(
@@ -336,22 +336,6 @@ describe("dataAppSandboxDevPlugin", () => {
"change",
path.join("/app", "queries", "orders.query.ts"),
);
- await server.watcher.emit(
- "all",
- "change",
- path.join("/app", "actions", "orders.action.ts"),
- );
-
- expect(mockedBuild).toHaveBeenCalledTimes(3);
- expect(server.ws.send).toHaveBeenCalledWith({
- type: "custom",
- event: DATA_APP_REBUILT_EVENT,
- });
- });
-
- it("ignores changes outside the app's src/, queries/, and actions/", async () => {
- const { server } = await setup();
-
await server.watcher.emit(
"all",
"change",
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.ts
index e8d8b2889f1..d5d0fb13e36 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.ts
@@ -8,8 +8,6 @@ import {
loadEnv,
} from "vite";
-import { checkResources } from "../data-app-resources/check";
-
import { dataAppBuildPlugins, dataAppLibBuild } from "./config/build-config";
import { getDataAppDefine } from "./config/define";
import { buildDevCsp } from "./config/dev-connect-src";
@@ -22,29 +20,13 @@ function dataAppVitePlugin(): PluginOption[] {
const appRoot = process.cwd();
const envDir = findEnvRoot(appRoot);
- const manifestResult = readManifest(appRoot);
-
- if (!manifestResult) {
- throw new Error(
- `No data_app.yaml found in ${appRoot}. Run vite from the data app's own ` +
- `directory (data_apps/<slug>/).`,
- );
- }
-
const appSlug = path.basename(appRoot);
- const allowedHosts = manifestResult.manifest.allowed_hosts ?? [];
+ const allowedHosts = readManifest(appRoot)?.manifest.allowed_hosts ?? [];
return [
react(),
...dataAppBuildPlugins(),
dataAppSandboxDevPlugin(appSlug, allowedHosts),
- {
- name: "metabase-resource-check",
- apply: "build",
- buildStart: async () => {
- await checkResources(appRoot);
- },
- },
{
name: "metabase-data-app",
// Merged over the user's config, so these win — the bundle Metabase loads
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.unit.spec.ts
index 156029e6848..22e5a53e700 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/index.unit.spec.ts
@@ -1,13 +1,3 @@
-import { isObject } from "metabase-types/guards";
-
-import {
- COLLECTION,
- makeApp,
- setupResourceTests,
- writeQuery,
- writeResource,
-} from "../data-app-resources/tests/setup";
-
import { readManifest } from "./config/read-manifest";
import { dataAppConfig } from "./index";
@@ -34,10 +24,10 @@ const mockedReadManifest = jest.mocked(readManifest);
afterEach(() => jest.resetAllMocks());
describe("dataAppConfig", () => {
- it("throws when the cwd has no data_app.yaml (run from the wrong directory)", () => {
+ it("builds the config without a data_app.yaml", () => {
mockedReadManifest.mockReturnValue(null);
- expect(() => dataAppConfig()).toThrow(/No data_app\.yaml found/);
+ expect(dataAppConfig().plugins).toBeDefined();
});
it("builds the config when a manifest is present", () => {
@@ -60,80 +50,4 @@ describe("dataAppConfig", () => {
expect(dataAppConfig({ port: 4000 }).server?.port).toBe(4000);
});
-
- describe("the resource check", () => {
- setupResourceTests();
-
- const QUESTION = "questionEntityId00010";
-
- type ResourceCheckPlugin = {
- apply: unknown;
- buildStart: () => Promise<void>;
- };
-
- const isResourceCheckPlugin = (
- candidate: unknown,
- ): candidate is ResourceCheckPlugin =>
- isObject(candidate) &&
- candidate.name === "metabase-resource-check" &&
- typeof candidate.buildStart === "function";
-
- // Vite accepts nested plugin arrays, and `dataAppConfig` returns one.
- const flatten = (option: unknown): unknown[] =>
- Array.isArray(option) ? option.flatMap(flatten) : [option];
-
- /** The plugin of an app in `appRoot`, which vite runs from its own directory. */
- const resourceCheckPlugin = (appRoot: string) => {
- jest.spyOn(process, "cwd").mockReturnValue(appRoot);
- mockedReadManifest.mockReturnValue({
- manifestPath: `${appRoot}/data_app.yaml`,
- manifest: { collection: COLLECTION },
- });
-
- const plugin = flatten(dataAppConfig().plugins).find(
- isResourceCheckPlugin,
- );
-
- if (!plugin) {
- throw new Error("The resource check plugin is missing.");
- }
-
- return plugin;
- };
-
- const appWithQuery = () => {
- const appRoot = makeApp();
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION}", source: { type: "table", id: 1 } });`,
- );
- return appRoot;
- };
-
- it("runs only for a production build", () => {
- expect(resourceCheckPlugin(appWithQuery()).apply).toBe("build");
- });
-
- it("lets the build start when the app's resources back its definitions", async () => {
- const appRoot = appWithQuery();
- writeResource(appRoot, "data_app/orders.yaml", {
- name: "Orders",
- type: "question",
- entity_id: QUESTION,
- "serdes/meta": [{ model: "Card", id: QUESTION }],
- });
-
- await expect(
- resourceCheckPlugin(appRoot).buildStart(),
- ).resolves.toBeUndefined();
- });
-
- it("fails the build when a definition's resource is missing", async () => {
- await expect(
- resourceCheckPlugin(appWithQuery()).buildStart(),
- ).rejects.toThrow(
- `queries/orders.query.ts:Orders names saved question ${QUESTION}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- });
- });
});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/types/manifest-status.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/types/manifest-status.ts
index 75348c601d1..abb10927fed 100644
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-dev/types/manifest-status.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/data-app-dev/types/manifest-status.ts
@@ -1,4 +1,5 @@
export interface DataAppManifestStatus {
+ exists: boolean;
name: string | null;
bundlePath: string | null;
bundlePathExists: boolean;
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/README.md b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/README.md
deleted file mode 100644
index 95872690302..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/README.md
+++ /dev/null
@@ -1,116 +0,0 @@
-# Data app resources
-
-A data app runs in a browser sandbox as one of its viewers, not as its author. Those viewers reach
-Metabase through a permission group that can read exactly one collection: the app's own. So a query
-the app runs, and an action it triggers, have to be reachable **through that collection** or they
-fail on permissions.
-
-The files of the app's collection make that true. Under the repository's `collections/data_apps/`, the
-folder of the `data-apps` collection namespace, they hold serdes YAML for the app's collection, a saved
-question per query, and copies of the actions the app runs and of the metrics its queries use. Metabase
-serializes everything but the collection's own file from the app's definitions, and the CLI writes it. Nothing changes in Metabase until the repository is pulled: the pull loads the files as
-serialized content, like everything else the repository holds, so a local experiment can't break the
-running app, and the saved questions never get ahead of the app code that is deployed.
-
-`check-resources` reads nothing from Metabase; `write-resources` asks it for the files.
-
-## What an app declares
-
-Two directories at the data-app root, beside `package.json`:
-
-```
-queries/orders.query.ts export const RevenueQuery = defineQuery({ savedQuestionEntityId: "…", source: schema.tables.orders })
-actions/orders.action.ts export const CreateOrder = defineAction({ copiedActionEntityId: "…", action: schema.actions.createOrder })
-```
-
-Nothing else is scanned: definitions under `src/` are invisible to both commands, which is a common
-authoring mistake.
-
-## What the app's collection holds
-
-```
-data_apps/<slug>/data_app.yaml collection: <entity ID of the app's collection>
-collections/data_apps/<collection>.yaml the app's collection, a root collection of the data-apps namespace
-collections/data_apps/<collection>/*.yaml a saved question per query, copies of the metrics they use, and a copy of each action
-```
-
-`<collection>` is the collection's name as serialization slugs it (`data_app__sales` for `Data App: Sales`).
-The commands find the app's files by content, as a pull does: the collection whose `entity_id` the
-manifest's `collection` names, and the cards and actions whose `collection_id` is it, wherever they sit
-under `collections/data_apps/`. The repository is the directory that holds the app's `data_apps/`; an
-app that isn't under `data_apps/` is its own root, so its files sit under its own `collections/`.
-
-A data app runs only query actions that belong to no model, which the typed schema lists under
-`schema.actions`. Such an action's permissions resolve through its own collection, so making it
-reachable means copying it into the app's collection. Metrics a query uses are copied for the same
-reason, and the question reads the copy. Every saved question and copy is written by
-`write-resources`, with the entity IDs the definitions carry and the metric references already
-rewritten to the copies.
-
-## `write-resources`
-
-`embedding-sdk-react data-apps write-resources` (`serialize.ts`) sends the evaluated definitions to
-`POST /api/apps/generate/resources`, with the instance and API key from `.env.local`, and regenerates the app's
-collection folder from the files it answers with: the saved question Metabase builds for each
-`defineQuery` definition, the copy of each `defineAction`'s action, and the copies of the metrics the
-queries aggregate. Each answer is a file name and the YAML text exactly as a remote-sync export writes it;
-the command writes it into the folder beside the collection's file (`<collection>/`). It first deletes every
-card and action file whose `collection_id` is the app's collection, wherever it sits under
-`collections/data_apps/`, so a copy no definition needs any more disappears, and a file name that would
-leave the folder is refused. It prints a `Wrote <path>` line per file. The collection's own file must exist
-already; the instance needs neither the collection nor a pull before the command runs. The endpoint
-answers only a superuser, so the API key must be one in the Administrators group. The saved question is
-named after the export, in the app's collection, with the definition's `savedQuestionEntityId`, and holds
-the query Metabase builds with the same `createTestQuery` code the dev preview runs. An action that belongs
-to a model, or an item that can't be built or copied, comes back with an `error`: then the command writes
-and deletes nothing and fails listing every error. It refuses to run before `data_app.yaml` names the
-app's collection, and before every definition carries its entity ID.
-
-## `check-resources`
-
-`embedding-sdk-react data-apps check-resources` (`check.ts`) checks only what a repository pull can't:
-the pull validates the files on their own, never against the app's code. It fails, listing every
-problem, for:
-
-- a manifest that names no collection, or one whose collection has no file under
- `collections/data_apps/`: the pull refuses the app before it loads anything;
-- a definition without its entity ID, or naming one no file in the app's collection holds, or a query
- naming a card that isn't a question: the pull would load, and the app would fail at runtime in
- production;
-- a question or action no definition names: the pull would load it into the app's collection, and a
- leftover action is one more write the app's viewers can run.
-
-It runs at `buildStart` for production builds, so `npm run build` fails the same way. The YAML's format
-is `validate-schema`'s job (the template's `npm run check-resources` runs it after this check), and the rest of the resource
-rules (the collection's namespace, duplicate or foreign entity IDs, what a card or action may hold) are the pull's.
-Nothing checks that a saved question's query matches its definition.
-
-## Discovery evaluates the definitions
-
-`discover.ts` bundles every file under `queries/` (or `actions/`) into one module with esbuild,
-evaluates it, and takes every exported object as a definition: `defineQuery` and `defineAction`
-return their argument as is, and the two directories hold nothing else. A definition a second file
-re-exports keeps its identity through the single bundle, so it counts once, for the first file that
-exports it.
-Anything that isn't a definition is rejected by the serialization endpoint's schema, not here.
-
-Discovery refuses what makes a definition unusable on its own or against the others: an action that
-doesn't reference a generated action, two definitions of one source action, two claiming one entity
-ID. Every command needs that, `write-resources` included. What a definition lacks against the app's
-collection files is `check-resources`' to report. A definition's own entity ID is the exception: its file is
-written with it, so `write-resources` refuses a `defineQuery` without `savedQuestionEntityId` or a
-`defineAction` without `copiedActionEntityId` and names it, and the author generates the ID with
-`npx representations generate-entity-id` first.
-
-## Dev preview vs production build
-
-The entity IDs only take effect in a production build. `isDataAppDev()` gates both swaps, so an app
-runs against the original table and the authored action before its resources exist:
-
-| | Dev preview | Production |
-| ------------ | ------------------- | -------------------------------------------- |
-| query source | the table | the saved question (`savedQuestionEntityId`) |
-| action | the authored action | the copy (`copiedActionEntityId`) |
-
-Both swaps live in the bundle (`toSourceInput` and `toExecutableActionId`), not in app code, so an
-author passes the definition and never branches on the environment.
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/check.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/check.ts
deleted file mode 100644
index 426de64ee61..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/check.ts
+++ /dev/null
@@ -1,163 +0,0 @@
-import path from "node:path";
-
-import { readManifest } from "../data-app-dev/config/read-manifest";
-
-import {
- discoverActions,
- discoverQueries,
- getRelativeDefinitionLocation,
-} from "./discover";
-import {
- type AppResources,
- COLLECTIONS_DIR,
- type ResourceFile,
- readResources,
-} from "./resources";
-import type { DiscoveredAction, DiscoveredQuery } from "./types";
-
-const isQuestion = (file: ResourceFile) =>
- file.model === "Card" && file.entity.type === "question";
-
-function definitionProblems(
- appRoot: string,
- queries: DiscoveredQuery[],
- actions: DiscoveredAction[],
- files: ResourceFile[],
-) {
- const fileOf = (model: ResourceFile["model"], entityId: string) =>
- files.find(
- (file) => file.model === model && file.entity.entity_id === entityId,
- );
-
- const queryProblems = queries.flatMap((query) => {
- const location = getRelativeDefinitionLocation(appRoot, query);
- const entityId = query.savedQuestionEntityId;
-
- if (!entityId) {
- return [`${location} has no savedQuestionEntityId.`];
- }
-
- const file = fileOf("Card", entityId);
-
- if (!file) {
- return [
- `${location} names saved question ${entityId}, which no file in ${COLLECTIONS_DIR}/ holds in the app's collection.`,
- ];
- }
-
- return isQuestion(file)
- ? []
- : [`${location} names ${file.path}, which is not a question.`];
- });
-
- const actionProblems = actions.flatMap((action) => {
- const location = getRelativeDefinitionLocation(appRoot, action);
- const entityId = action.copiedActionEntityId;
-
- if (!entityId) {
- return [`${location} has no copiedActionEntityId.`];
- }
-
- return fileOf("Action", entityId)
- ? []
- : [
- `${location} names action ${entityId}, which no file in ${COLLECTIONS_DIR}/ holds in the app's collection.`,
- ];
- });
-
- return [...queryProblems, ...actionProblems];
-}
-
-/**
- * A question or action no definition names is still loaded by a pull: the
- * question and the action into the app's collection, where the action is one
- * more write the app's viewers can run.
- */
-function leftoverProblems(
- queries: DiscoveredQuery[],
- actions: DiscoveredAction[],
- files: ResourceFile[],
-) {
- const namedEntityIds: Record<ResourceFile["model"], Set<unknown>> = {
- Card: new Set(queries.map((query) => query.savedQuestionEntityId)),
- Action: new Set(actions.map((action) => action.copiedActionEntityId)),
- };
- const isNamed = ({ model, entity }: ResourceFile) =>
- typeof entity.entity_id === "string" &&
- namedEntityIds[model].has(entity.entity_id);
-
- return files
- .filter(
- (file) => (isQuestion(file) || file.model === "Action") && !isNamed(file),
- )
- .map(
- (file) => `${file.path} is a resource that is not referenced anywhere.`,
- );
-}
-
-/**
- * The app's collection files, located through the collection its manifest
- * names, or the problem that keeps them from being located: a pull refuses a
- * manifest that names no collection, or one whose collection the repository
- * doesn't hold, before it loads anything.
- */
-function locateResources(
- appRoot: string,
-): { resources: AppResources } | { problems: string[] } {
- const manifest = readManifest(appRoot);
-
- if (!manifest) {
- throw new Error(`No data_app.yaml found in ${appRoot}.`);
- }
-
- const collection = manifest.manifest.collection;
-
- if (collection === undefined) {
- return {
- problems: [
- `data_app.yaml names no collection. Write the app's collection under ${COLLECTIONS_DIR}/ and name its entity ID as \`collection\`.`,
- ],
- };
- }
-
- const resources = readResources(appRoot, collection);
-
- return resources.collectionPath === undefined
- ? {
- problems: [
- `data_app.yaml names collection ${collection}, which no file in ${COLLECTIONS_DIR}/ holds.`,
- ],
- }
- : { resources };
-}
-
-/**
- * Fails when the app's definitions and the files of its collection disagree in
- * a way a repository pull can't see: the pull validates the files on their own,
- * never against the app's code. Reads nothing from Metabase.
- */
-export async function checkResources(appDirectory: string) {
- const appRoot = path.resolve(appDirectory);
- const [queries, actions] = await Promise.all([
- discoverQueries(appRoot),
- discoverActions(appRoot),
- ]);
- const located = locateResources(appRoot);
-
- const problems =
- "problems" in located
- ? located.problems
- : [
- ...definitionProblems(
- appRoot,
- queries,
- actions,
- located.resources.files,
- ),
- ...leftoverProblems(queries, actions, located.resources.files),
- ];
-
- if (problems.length > 0) {
- throw new Error(problems.join("\n"));
- }
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts
deleted file mode 100644
index 29ac922c755..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/discover.ts
+++ /dev/null
@@ -1,286 +0,0 @@
-import fs from "node:fs";
-import { createRequire } from "node:module";
-import path from "node:path";
-
-import { build } from "esbuild";
-
-import { isEntityId, isObject } from "./guards";
-import type { DiscoveredAction, DiscoveredQuery } from "./types";
-
-/** A source-controlled definition kind, and where the CLI looks for it. */
-interface DefinitionKind {
- directory: string;
- idKey: string;
-}
-
-export const QUERY_DEFINITIONS: DefinitionKind = {
- directory: "queries",
- idKey: "savedQuestionEntityId",
-};
-
-export const ACTION_DEFINITIONS: DefinitionKind = {
- directory: "actions",
- idKey: "copiedActionEntityId",
-};
-
-const DEFINITION_FILE_EXTENSIONS = [
- ".js",
- ".jsx",
- ".ts",
- ".tsx",
- ".cjs",
- ".cts",
- ".mjs",
- ".mts",
-];
-
-/** Where a definition is, as messages name it: `queries/orders.query.ts:Orders`. */
-export function getRelativeDefinitionLocation(
- appRoot: string,
- { filePath, exportName }: { filePath: string; exportName: string },
-) {
- return `${path.relative(appRoot, filePath)}:${exportName}`;
-}
-
-interface EvaluatedDefinition {
- exportName: string;
- filePath: string;
- value: Record<string, unknown>;
-}
-
-function listDefinitionFiles(directory: string): string[] {
- if (!fs.existsSync(directory)) {
- return [];
- }
-
- return fs
- .readdirSync(directory, { recursive: true, encoding: "utf8" })
- .filter((relativePath) =>
- DEFINITION_FILE_EXTENSIONS.some((extension) =>
- relativePath.endsWith(extension),
- ),
- )
- .map((relativePath) => path.join(directory, relativePath))
- .sort();
-}
-
-/**
- * Bundles every definition file into one module and evaluates it, so a file
- * two others import is instantiated once and its definitions keep their
- * identity across them.
- */
-async function evaluateFiles(directory: string, filePaths: string[]) {
- const result = await build({
- stdin: {
- contents: filePaths
- .map(
- (filePath, index) =>
- `export * as file${index} from ${JSON.stringify(filePath)};`,
- )
- .join("\n"),
- resolveDir: directory,
- loader: "ts",
- },
- absWorkingDir: directory,
- bundle: true,
- format: "cjs",
- packages: "external",
- platform: "node",
- target: "node22",
- write: false,
- logLevel: "silent",
- });
-
- const compiled = result.outputFiles[0]?.text;
-
- if (!compiled) {
- throw new Error(`Could not evaluate the definitions in ${directory}.`);
- }
-
- const runtimeModule: { exports: Record<string, unknown> } = { exports: {} };
-
- new Function("require", "module", "exports", compiled)(
- createRequire(path.join(directory, "definitions.ts")),
- runtimeModule,
- runtimeModule.exports,
- );
-
- return filePaths.map((filePath, index) => ({
- filePath,
- exports: runtimeModule.exports[`file${index}`],
- }));
-}
-
-/** Narrows discovery to one definition file. */
-interface DiscoveryOptions {
- filePath?: string;
-}
-
-/**
- * Every object a definition file exports is a definition: `defineQuery` and
- * `defineAction` return their argument as is, and the directories hold nothing
- * else. An object re-exported by a second file counts once, for the first file
- * that exports it; asking for one file reads that file's exports alone, so a
- * barrel that re-exports its definitions doesn't claim them.
- */
-async function evaluateDefinitions(
- appRoot: string,
- kind: DefinitionKind,
- { filePath: requestedFilePath }: DiscoveryOptions,
-): Promise<EvaluatedDefinition[]> {
- const directory = path.join(appRoot, kind.directory);
- const filePaths = listDefinitionFiles(directory);
-
- if (filePaths.length === 0) {
- return [];
- }
-
- const seen = new Set<object>();
- const evaluated: EvaluatedDefinition[] = [];
-
- for (const { filePath, exports } of await evaluateFiles(
- directory,
- filePaths,
- )) {
- if (requestedFilePath !== undefined && filePath !== requestedFilePath) {
- continue;
- }
-
- for (const [exportName, value] of Object.entries(
- isObject(exports) ? exports : {},
- )) {
- if (isObject(value) && !seen.has(value)) {
- seen.add(value);
- evaluated.push({ exportName, filePath, value });
- }
- }
- }
-
- return evaluated;
-}
-
-function definedEntityId(value: unknown, location: string, idKey: string) {
- if (value === undefined) {
- return undefined;
- }
-
- if (!isEntityId(value)) {
- throw new Error(`${location} has an invalid ${idKey}.`);
- }
-
- return value;
-}
-
-/** Rejects two definitions claiming the same ID, as a copied definition would. */
-function assertUniqueIds(
- entries: Array<{ id: number | string | undefined; location: string }>,
- subject: string,
- fix: string,
-) {
- const byId = new Map<number | string, string[]>();
-
- for (const { id, location } of entries) {
- if (id !== undefined) {
- byId.set(id, [...(byId.get(id) ?? []), location]);
- }
- }
-
- for (const [id, locations] of byId) {
- if (locations.length > 1) {
- throw new Error(
- `${subject} ${id} is referenced by ${locations.join(", ")}. ${fix}`,
- );
- }
- }
-}
-
-export async function discoverQueries(
- appRoot: string,
- options: DiscoveryOptions = {},
-): Promise<DiscoveredQuery[]> {
- const definitions = await evaluateDefinitions(
- appRoot,
- QUERY_DEFINITIONS,
- options,
- );
-
- const discovered = definitions.map(({ exportName, filePath, value }) => ({
- exportName,
- filePath,
- query: value,
- savedQuestionEntityId: definedEntityId(
- value[QUERY_DEFINITIONS.idKey],
- getRelativeDefinitionLocation(appRoot, { filePath, exportName }),
- QUERY_DEFINITIONS.idKey,
- ),
- }));
-
- assertUniqueIds(
- discovered.map((query) => ({
- id: query.savedQuestionEntityId,
- location: getRelativeDefinitionLocation(appRoot, query),
- })),
- "Saved question",
- "Give each definition its own saved question.",
- );
-
- return discovered;
-}
-
-const isGeneratedActionId = (id: unknown): id is number =>
- typeof id === "number" && Number.isInteger(id) && id > 0;
-
-export async function discoverActions(
- appRoot: string,
- options: DiscoveryOptions = {},
-): Promise<DiscoveredAction[]> {
- const definitions = await evaluateDefinitions(
- appRoot,
- ACTION_DEFINITIONS,
- options,
- );
-
- const discovered = definitions.map(({ exportName, filePath, value }) => {
- const location = getRelativeDefinitionLocation(appRoot, {
- filePath,
- exportName,
- });
- const action = value.action;
-
- if (!isObject(action) || !isGeneratedActionId(action.id)) {
- throw new Error(
- `${location} must reference a generated action, such as \`schema.actions.<action>\`.`,
- );
- }
-
- return {
- exportName,
- filePath,
- copiedActionEntityId: definedEntityId(
- value[ACTION_DEFINITIONS.idKey],
- location,
- ACTION_DEFINITIONS.idKey,
- ),
- sourceActionId: action.id,
- };
- });
-
- assertUniqueIds(
- discovered.map((action) => ({
- id: action.sourceActionId,
- location: getRelativeDefinitionLocation(appRoot, action),
- })),
- "Action",
- "Declare each action once.",
- );
- assertUniqueIds(
- discovered.map((action) => ({
- id: action.copiedActionEntityId,
- location: getRelativeDefinitionLocation(appRoot, action),
- })),
- "Copied action",
- "Give each definition its own copy.",
- );
-
- return discovered;
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/env.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/env.ts
deleted file mode 100644
index f93e27b40e9..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/env.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-import { parseEnv } from "node:util";
-
-import { findEnvRoot } from "../data-app-dev/config/find-env-root";
-import {
- DATA_APP_MB_API_KEY_ENV,
- DATA_APP_MB_URL_ENV,
-} from "../data-app-dev/constants/env";
-
-function readEnvFile(filePath: string) {
- return fs.existsSync(filePath)
- ? parseEnv(fs.readFileSync(filePath, "utf8"))
- : {};
-}
-
-/** The Metabase instance and API key the dev preview uses: the repo-root `.env.local`, overridden by the environment. */
-export function getMetabaseCredentials(appRoot: string) {
- const values = {
- ...readEnvFile(path.join(findEnvRoot(appRoot), ".env.local")),
- ...process.env,
- };
- const metabaseUrl = values[DATA_APP_MB_URL_ENV];
- const apiKey = values[DATA_APP_MB_API_KEY_ENV];
-
- if (!metabaseUrl || !apiKey) {
- throw new Error(
- `${DATA_APP_MB_URL_ENV} and ${DATA_APP_MB_API_KEY_ENV} must be set, in the repo-root .env.local or the environment.`,
- );
- }
-
- return { metabaseUrl: metabaseUrl.replace(/\/+$/, ""), apiKey };
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/guards.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/guards.ts
deleted file mode 100644
index 8be9e9dc7fa..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/guards.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-// `metabase-types/guards` and `metabase-types/api` are main-app code, which the
-// SDK package's non-CLI code can't import (`no-external-references-for-sdk-package-code`).
-
-export const isObject = (value: unknown): value is Record<string, unknown> =>
- typeof value === "object" && value !== null;
-
-/** A Metabase entity ID: a 21-character NanoID. */
-export const isEntityId = (value: unknown): value is string =>
- typeof value === "string" && /^[A-Za-z0-9_-]{21}$/.test(value);
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/resources.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/resources.ts
deleted file mode 100644
index 92dd08ad2c4..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/resources.ts
+++ /dev/null
@@ -1,122 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { load as parseYaml } from "js-yaml";
-
-import { isObject } from "./guards";
-import type { ResourceModel } from "./types";
-
-/**
- * Where the repository holds the collections of the `data-apps` namespace: an
- * app's collection, and the saved questions and copies in it, as serialization
- * writes them. A pull reads the files by their content, not their location, so
- * the check does too: the app's files are the ones that name its collection.
- */
-export const COLLECTIONS_DIR = "collections/data_apps";
-
-const RESOURCE_MODELS: readonly ResourceModel[] = ["Card", "Action"];
-
-export interface ResourceFile {
- /** Relative to the repository root. */
- path: string;
- model: ResourceModel;
- entity: Record<string, unknown>;
-}
-
-export interface AppResources {
- /** The repository the app lives in, which `COLLECTIONS_DIR` is under. */
- repoRoot: string;
- /** The file of the app's collection, relative to the repository root, when one holds it. */
- collectionPath?: string;
- /** The cards and actions in the app's collection. */
- files: ResourceFile[];
-}
-
-/**
- * The repository an app lives in: the directory that holds its `data_apps/`
- * directory. An app that isn't under `data_apps/` stands in for its own
- * repository, so its collection files sit under its own `collections/`.
- */
-export function repoRootOf(appRoot: string) {
- const parent = path.dirname(appRoot);
-
- return path.basename(parent) === "data_apps" ? path.dirname(parent) : appRoot;
-}
-
-function listYamlFiles(directory: string): string[] {
- if (!fs.existsSync(directory)) {
- return [];
- }
-
- return fs
- .readdirSync(directory, { withFileTypes: true })
- .sort((a, b) => a.name.localeCompare(b.name))
- .flatMap((entry) => {
- const entryPath = path.join(directory, entry.name);
-
- if (entry.isDirectory()) {
- return listYamlFiles(entryPath);
- }
-
- // A pull skips hidden files, so a check that counted them could pass an
- // app whose resources never load.
- return entry.isFile() &&
- !entry.name.startsWith(".") &&
- entry.name.endsWith(".yaml")
- ? [entryPath]
- : [];
- });
-}
-
-function readEntity(repoRoot: string, filePath: string) {
- const relativePath = path.relative(repoRoot, filePath);
-
- try {
- const entity: unknown = parseYaml(fs.readFileSync(filePath, "utf8"));
- return { path: relativePath, entity: isObject(entity) ? entity : {} };
- } catch (error) {
- throw new Error(`Could not parse ${relativePath}: ${String(error)}`);
- }
-}
-
-/** The model a serialized entity's `serdes/meta` names, as a pull reads it. */
-function modelOf(entity: Record<string, unknown>): unknown {
- const meta = entity["serdes/meta"];
- const leaf = Array.isArray(meta) ? meta[meta.length - 1] : undefined;
-
- return isObject(leaf) ? leaf.model : undefined;
-}
-
-const isResourceModel = (model: unknown): model is ResourceModel =>
- RESOURCE_MODELS.some((resourceModel) => resourceModel === model);
-
-/**
- * The files of the app's collection, the one with `collectionEntityId`: the
- * collection's own file, and the cards and actions whose `collection_id` is it,
- * wherever they sit under the repository's `collections/data_apps/`.
- */
-export function readResources(
- appRoot: string,
- collectionEntityId: string,
-): AppResources {
- const repoRoot = repoRootOf(appRoot);
- const entities = listYamlFiles(path.join(repoRoot, COLLECTIONS_DIR)).map(
- (filePath) => readEntity(repoRoot, filePath),
- );
-
- const collectionPath = entities.find(
- ({ entity }) =>
- modelOf(entity) === "Collection" &&
- entity.entity_id === collectionEntityId,
- )?.path;
-
- const files = entities.flatMap(({ path: filePath, entity }) => {
- const model = modelOf(entity);
-
- return isResourceModel(model) && entity.collection_id === collectionEntityId
- ? [{ path: filePath, model, entity }]
- : [];
- });
-
- return { repoRoot, collectionPath, files };
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/serialize.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/serialize.ts
deleted file mode 100644
index dd98cee82f2..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/serialize.ts
+++ /dev/null
@@ -1,212 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { readManifest } from "../data-app-dev/config/read-manifest";
-
-import {
- QUERY_DEFINITIONS,
- discoverActions,
- discoverQueries,
- getRelativeDefinitionLocation,
-} from "./discover";
-import { getMetabaseCredentials } from "./env";
-import { isObject } from "./guards";
-import { COLLECTIONS_DIR, readResources, repoRootOf } from "./resources";
-import type { DiscoveredAction, DiscoveredQuery } from "./types";
-
-type SerializedFile = { file: string; yaml: string } | { error: string };
-
-interface SerializedResources {
- queries: SerializedFile[];
- actions: SerializedFile[];
- metrics: SerializedFile[];
-}
-
-const isSerializedFile = (value: unknown): value is SerializedFile =>
- isObject(value) &&
- ((typeof value.file === "string" && typeof value.yaml === "string") ||
- typeof value.error === "string");
-
-const isSerializedFiles = (value: unknown): value is SerializedFile[] =>
- Array.isArray(value) && value.every(isSerializedFile);
-
-function isSerializedResources(value: unknown): value is SerializedResources {
- return (
- isObject(value) &&
- isSerializedFiles(value.queries) &&
- isSerializedFiles(value.actions) &&
- isSerializedFiles(value.metrics)
- );
-}
-
-export function exportNameToCardName(exportName: string) {
- const words = exportName
- .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
- .replace(/[\s_]+/g, " ")
- .toLowerCase();
-
- return words.charAt(0).toUpperCase() + words.slice(1);
-}
-
-async function requestSerialization(
- appRoot: string,
- body: { queries: unknown[]; actions: unknown[] },
-): Promise<SerializedResources> {
- const { metabaseUrl, apiKey } = getMetabaseCredentials(appRoot);
- const response = await fetch(`${metabaseUrl}/api/apps/generate/resources`, {
- method: "POST",
- headers: { "Content-Type": "application/json", "X-API-Key": apiKey },
- body: JSON.stringify(body),
- });
-
- if (!response.ok) {
- throw new Error(
- `The serialization request failed (${response.status}): ${await response.text()}`,
- );
- }
-
- const serialized: unknown = await response.json();
-
- if (!isSerializedResources(serialized)) {
- throw new Error("The serialization response has an unexpected body.");
- }
-
- return serialized;
-}
-
-function missingEntityIds(
- appRoot: string,
- queries: DiscoveredQuery[],
- actions: DiscoveredAction[],
-) {
- return [
- ...queries
- .filter((query) => query.savedQuestionEntityId === undefined)
- .map(
- (query) =>
- `${getRelativeDefinitionLocation(appRoot, query)} has no savedQuestionEntityId. Generate one with \`npx representations generate-entity-id\` and set it first: its saved question is written with it.`,
- ),
- ...actions
- .filter((action) => action.copiedActionEntityId === undefined)
- .map(
- (action) =>
- `${getRelativeDefinitionLocation(appRoot, action)} has no copiedActionEntityId. Generate one with \`npx representations generate-entity-id\` and set it first: its copy is written with it.`,
- ),
- ];
-}
-
-function collectionDirectory(appRoot: string, collection: string) {
- const { repoRoot, collectionPath, files } = readResources(
- appRoot,
- collection,
- );
-
- if (collectionPath === undefined) {
- throw new Error(
- `No file under ${COLLECTIONS_DIR}/ holds the collection ${collection} that data_app.yaml names. Write it first: the files are written next to it.`,
- );
- }
-
- return {
- directory: path.join(repoRoot, collectionPath.replace(/\.yaml$/, "")),
- existing: files.map((file) => path.join(repoRoot, file.path)),
- };
-}
-
-function targetFile(directory: string, file: { file: string; yaml: string }) {
- const target = path.join(directory, file.file);
-
- if (path.dirname(target) !== directory) {
- throw new Error(
- `The serialization holds a file outside the collection: ${file.file}`,
- );
- }
-
- return { target, yaml: file.yaml };
-}
-
-/**
- * Regenerates the files of the app's collection: the saved question Metabase
- * builds for each `defineQuery` definition, the copy of each `defineAction`'s
- * action, and the copies of the metrics the queries aggregate, written next to
- * the collection's file in place of the cards and actions it held. Writes
- * nothing when Metabase couldn't serialize an item, and throws listing why.
- * One request to the Metabase instance and API key in `.env.local`.
- */
-export async function writeResources(appDirectory: string) {
- const appRoot = path.resolve(appDirectory);
- const queries = await discoverQueries(appRoot);
- const actions = await discoverActions(appRoot);
- const collection = readManifest(appRoot)?.manifest.collection;
-
- if (collection === undefined) {
- throw new Error(
- `No data_app.yaml in ${appRoot} names the app's collection. Write the collection under ${COLLECTIONS_DIR}/ and name its entity ID as \`collection\` first: the files are written into it.`,
- );
- }
-
- const { directory, existing } = collectionDirectory(appRoot, collection);
- const missing = missingEntityIds(appRoot, queries, actions);
-
- if (missing.length > 0) {
- throw new Error(missing.join("\n"));
- }
-
- const serialized = await requestSerialization(appRoot, {
- queries: queries.map(({ exportName, query, savedQuestionEntityId }) => {
- const { [QUERY_DEFINITIONS.idKey]: _entityId, ...definition } = query;
- return {
- name: exportNameToCardName(exportName),
- query: { stages: [definition] },
- entity_id: savedQuestionEntityId,
- collection_id: collection,
- };
- }),
- actions: actions.map(({ sourceActionId, copiedActionEntityId }) => ({
- action_id: sourceActionId,
- entity_id: copiedActionEntityId,
- collection_id: collection,
- })),
- });
-
- if (
- serialized.queries.length !== queries.length ||
- serialized.actions.length !== actions.length
- ) {
- throw new Error(
- "The serialization response doesn't answer every definition.",
- );
- }
-
- const labelled = [
- ...serialized.queries.map((result, index) => ({
- label: getRelativeDefinitionLocation(appRoot, queries[index]),
- result,
- })),
- ...serialized.actions.map((result, index) => ({
- label: getRelativeDefinitionLocation(appRoot, actions[index]),
- result,
- })),
- ...serialized.metrics.map((result) => ({ label: "A metric", result })),
- ];
-
- const errors = labelled.flatMap(({ label, result }) =>
- "error" in result ? [`${label}: ${result.error}`] : [],
- );
-
- if (errors.length > 0) {
- throw new Error(errors.join("\n"));
- }
-
- const targets = labelled.flatMap(({ result }) =>
- "error" in result ? [] : [targetFile(directory, result)],
- );
-
- existing.forEach((filePath) => fs.rmSync(filePath, { force: true }));
- fs.mkdirSync(directory, { recursive: true });
- targets.forEach(({ target, yaml }) => fs.writeFileSync(target, yaml));
-
- return targets
- .map(({ target }) => `Wrote ${path.relative(repoRootOf(appRoot), target)}`)
- .join("\n");
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/check.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/check.unit.spec.ts
deleted file mode 100644
index 12320bcfc29..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/check.unit.spec.ts
+++ /dev/null
@@ -1,221 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { checkResources } from "../check";
-
-import {
- COLLECTION,
- makeApp,
- setupResourceTests,
- writeAction,
- writeQuery,
- writeResource,
-} from "./setup";
-
-const QUESTION = "questionEntityId00010";
-const METRIC = "metricCopyEntityId001";
-const ACTION = "actionCopyEntityId001";
-
-const cardFile = (entityId: string, type: string) => ({
- name: entityId,
- type,
- entity_id: entityId,
- "serdes/meta": [{ model: "Card", id: entityId }],
-});
-
-const actionFile = (entityId: string) => ({
- name: "Create",
- type: "query",
- entity_id: entityId,
- "serdes/meta": [{ model: "Action", id: entityId }],
-});
-
-/** An app whose collection files back its one query and one action. */
-function checkedApp(options?: Parameters<typeof makeApp>[0]) {
- const appRoot = makeApp(options);
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION}", source: { type: "table", id: 1 } });`,
- );
- writeAction(
- appRoot,
- `export const Create = defineAction({ copiedActionEntityId: "${ACTION}", action: { id: 51, parameters: [] } });`,
- );
- writeResource(
- appRoot,
- "data_app/orders.yaml",
- cardFile(QUESTION, "question"),
- );
- writeResource(appRoot, "data_app/revenue.yaml", cardFile(METRIC, "metric"));
- writeResource(appRoot, "data_app/create.yaml", actionFile(ACTION));
- return appRoot;
-}
-
-describe("resource check", () => {
- setupResourceTests();
-
- it("passes when every definition's file exists and nothing is left over", async () => {
- await expect(checkResources(checkedApp())).resolves.toBeUndefined();
- });
-
- it("fails for a query without a saved question ID", async () => {
- const appRoot = checkedApp();
- fs.appendFileSync(
- path.join(appRoot, "queries/orders.query.ts"),
- `export const Products = defineQuery({ source: { type: "table", id: 2 } });\n`,
- );
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "queries/orders.query.ts:Products has no savedQuestionEntityId.",
- );
- });
-
- it("fails for a query whose saved question has no file", async () => {
- const appRoot = checkedApp();
- fs.rmSync(path.join(appRoot, "collections/data_apps/data_app/orders.yaml"));
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- `queries/orders.query.ts:Orders names saved question ${QUESTION}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- });
-
- it("ignores hidden files, which a pull skips", async () => {
- const appRoot = checkedApp();
- fs.renameSync(
- path.join(appRoot, "collections/data_apps/data_app/orders.yaml"),
- path.join(appRoot, "collections/data_apps/data_app/.orders.yaml"),
- );
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- `names saved question ${QUESTION}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- });
-
- it("fails for a query that names a card that is not a question", async () => {
- const appRoot = checkedApp();
- writeResource(
- appRoot,
- "data_app/orders.yaml",
- cardFile(QUESTION, "metric"),
- );
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "queries/orders.query.ts:Orders names collections/data_apps/data_app/orders.yaml, which is not a question.",
- );
- });
-
- it("fails for an action without a copied action ID", async () => {
- const appRoot = checkedApp();
- fs.appendFileSync(
- path.join(appRoot, "actions/orders.action.ts"),
- `export const Update = defineAction({ action: { id: 52, parameters: [] } });\n`,
- );
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "actions/orders.action.ts:Update has no copiedActionEntityId.",
- );
- });
-
- it("fails for an action whose copy has no file", async () => {
- const appRoot = checkedApp();
- fs.rmSync(path.join(appRoot, "collections/data_apps/data_app/create.yaml"));
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- `actions/orders.action.ts:Create names action ${ACTION}, which no file in collections/data_apps/ holds in the app's collection.`,
- );
- });
-
- it.each([
- [
- "a question no query names",
- "data_app/stale.yaml",
- cardFile("staleQuestionEntity01", "question"),
- "collections/data_apps/data_app/stale.yaml is a resource that is not referenced anywhere.",
- ],
- [
- "an action no definition names",
- "data_app/stale.yaml",
- actionFile("staleActionEntityId01"),
- "collections/data_apps/data_app/stale.yaml is a resource that is not referenced anywhere.",
- ],
- ])("fails for %s", async (_, relativePath, entity, message) => {
- const appRoot = checkedApp();
- writeResource(appRoot, relativePath, entity);
-
- await expect(checkResources(appRoot)).rejects.toThrow(message);
- });
-
- it("reports an action no definition names even when a query names the same ID", async () => {
- const appRoot = checkedApp();
- writeResource(appRoot, "data_app/stale.yaml", actionFile(QUESTION));
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "collections/data_apps/data_app/stale.yaml is a resource that is not referenced anywhere.",
- );
- });
-
- it("reports a question file without an entity ID even when a definition lacks one too", async () => {
- const appRoot = checkedApp();
- fs.appendFileSync(
- path.join(appRoot, "queries/orders.query.ts"),
- `export const Products = defineQuery({ source: { type: "table", id: 2 } });\n`,
- );
- writeResource(appRoot, "data_app/unnamed.yaml", {
- name: "Unnamed",
- type: "question",
- collection_id: COLLECTION,
- "serdes/meta": [{ model: "Card" }],
- });
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "collections/data_apps/data_app/unnamed.yaml is a resource that is not referenced anywhere.",
- );
- });
-
- it("reads the files of an app under the repository's data_apps/", async () => {
- await expect(
- checkResources(checkedApp({ underDataApps: true })),
- ).resolves.toBeUndefined();
- });
-
- it("leaves a card in another collection alone", async () => {
- const appRoot = checkedApp();
- writeResource(appRoot, "other/stale.yaml", {
- ...cardFile("otherQuestionEntity01", "question"),
- collection_id: "otherCollectionEntit1",
- });
-
- await expect(checkResources(appRoot)).resolves.toBeUndefined();
- });
-
- it("fails for a manifest that names no collection", async () => {
- const appRoot = checkedApp();
- fs.writeFileSync(
- path.join(appRoot, "data_app.yaml"),
- "name: Shop\nslug: shop\npath: dist/index.js\n",
- );
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- "data_app.yaml names no collection.",
- );
- });
-
- it("fails for a collection whose file is missing", async () => {
- const appRoot = checkedApp();
- fs.rmSync(path.join(appRoot, "collections/data_apps/data_app.yaml"));
-
- await expect(checkResources(appRoot)).rejects.toThrow(
- `data_app.yaml names collection ${COLLECTION}, which no file in collections/data_apps/ holds.`,
- );
- });
-
- it("reports every problem at once", async () => {
- const appRoot = checkedApp();
- fs.rmSync(path.join(appRoot, "collections/data_apps/data_app/orders.yaml"));
- fs.rmSync(path.join(appRoot, "collections/data_apps/data_app/create.yaml"));
-
- const error = await checkResources(appRoot).catch((e: Error) => e);
- expect(String(error)).toContain(`saved question ${QUESTION}`);
- expect(String(error)).toContain(`action ${ACTION}`);
- });
-});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/discover.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/discover.unit.spec.ts
deleted file mode 100644
index 21a0aca7098..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/discover.unit.spec.ts
+++ /dev/null
@@ -1,169 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { discoverActions, discoverQueries } from "../discover";
-
-import { makeApp, setupResourceTests, writeAction, writeQuery } from "./setup";
-
-const QUESTION_10 = "questionEntityId00010";
-const ACTION_91 = "actionEntityId0000091";
-
-describe("query discovery", () => {
- setupResourceTests();
-
- it("discovers a direct named definition", async () => {
- const appRoot = makeApp();
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION_10}", source: { type: "table", id: 1 }, limit: 5 });`,
- );
-
- await expect(discoverQueries(appRoot)).resolves.toEqual([
- expect.objectContaining({
- exportName: "Orders",
- savedQuestionEntityId: QUESTION_10,
- }),
- ]);
- });
-
- it("rejects a saved question ID two definitions share", async () => {
- const appRoot = makeApp();
-
- writeQuery(
- appRoot,
- `export const First = defineQuery({ savedQuestionEntityId: "${QUESTION_10}", source: { type: "table", id: 1 } });
- export const Second = defineQuery({ savedQuestionEntityId: "${QUESTION_10}", source: { type: "table", id: 1 } });`,
- );
-
- await expect(discoverQueries(appRoot)).rejects.toThrow(
- `Saved question ${QUESTION_10} is referenced by`,
- );
- });
-
- it("counts a definition another file re-exports once, and skips exports that aren't objects", async () => {
- const appRoot = makeApp();
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION_10}", source: { type: "table", id: 1 } });
- export const LIMIT = 5;`,
- );
- fs.writeFileSync(
- path.join(appRoot, "queries/index.ts"),
- 'export { Orders } from "./orders.query";\n',
- );
-
- await expect(discoverQueries(appRoot)).resolves.toEqual([
- expect.objectContaining({ exportName: "Orders" }),
- ]);
- });
-
- it("finds the definitions of one file even when a barrel sorted before it re-exports them", async () => {
- const appRoot = makeApp();
- const filePath = writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION_10}", source: { type: "table", id: 1 } });`,
- );
- fs.writeFileSync(
- path.join(appRoot, "queries/index.ts"),
- 'export { Orders } from "./orders.query";\n',
- );
-
- await expect(discoverQueries(appRoot, { filePath })).resolves.toEqual([
- expect.objectContaining({ exportName: "Orders", filePath }),
- ]);
- });
-
- it("rejects an invalid saved question ID", async () => {
- const appRoot = makeApp();
-
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: 10, source: { type: "table", id: 1 } });`,
- );
-
- await expect(discoverQueries(appRoot)).rejects.toThrow(
- "has an invalid savedQuestionEntityId",
- );
- });
-});
-
-describe("action discovery", () => {
- setupResourceTests();
-
- it("discovers named definitions and the action each references", async () => {
- const appRoot = makeApp();
-
- writeAction(
- appRoot,
- `export const Create = defineAction({ copiedActionEntityId: "${ACTION_91}", action: { id: 51, entityId: "sourceActionEntity051", parameters: [] } });
- export const Update = defineAction({ action: { id: 52, parameters: [] } });`,
- );
-
- await expect(discoverActions(appRoot)).resolves.toEqual([
- expect.objectContaining({
- exportName: "Create",
- sourceActionId: 51,
- copiedActionEntityId: ACTION_91,
- }),
- expect.objectContaining({
- exportName: "Update",
- sourceActionId: 52,
- copiedActionEntityId: undefined,
- }),
- ]);
- });
-
- it("rejects two definitions claiming the same source action", async () => {
- const appRoot = makeApp();
-
- writeAction(
- appRoot,
- `export const First = defineAction({ action: { id: 51, parameters: [] } });
- export const Second = defineAction({ action: { id: 51, parameters: [] } });`,
- );
-
- await expect(discoverActions(appRoot)).rejects.toThrow(
- /Action 51 is referenced by .*\. Declare each action once\./,
- );
- });
-
- it("rejects a copied definition that carries another's copied action ID", async () => {
- const appRoot = makeApp();
-
- writeAction(
- appRoot,
- `export const First = defineAction({ copiedActionEntityId: "${ACTION_91}", action: { id: 51, parameters: [] } });
- export const Second = defineAction({ copiedActionEntityId: "${ACTION_91}", action: { id: 52, parameters: [] } });`,
- );
-
- await expect(discoverActions(appRoot)).rejects.toThrow(
- `Copied action ${ACTION_91} is referenced by`,
- );
- });
-
- it("rejects a definition that does not reference a generated action", async () => {
- const appRoot = makeApp();
-
- writeAction(
- appRoot,
- `export const Create = defineAction({ action: { name: "not a schema entry" } });`,
- );
-
- await expect(discoverActions(appRoot)).rejects.toThrow(
- "must reference a generated action",
- );
- });
-
- it("rejects an invalid copied action ID", async () => {
- const appRoot = makeApp();
-
- writeAction(
- appRoot,
- `export const Create = defineAction({ copiedActionEntityId: 91, action: { id: 51, parameters: [] } });`,
- );
-
- await expect(discoverActions(appRoot)).rejects.toThrow(
- "has an invalid copiedActionEntityId",
- );
- });
-});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/serialize.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/serialize.unit.spec.ts
deleted file mode 100644
index 0e859140d74..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/serialize.unit.spec.ts
+++ /dev/null
@@ -1,306 +0,0 @@
-import fs from "node:fs";
-import path from "node:path";
-
-import { exportNameToCardName, writeResources } from "../serialize";
-
-import {
- COLLECTION,
- makeApp,
- setupResourceTests,
- writeAction,
- writeQuery,
- writeResource,
-} from "./setup";
-
-const QUESTION = "questionEntityId00010";
-const ACTION_COPY = "actionCopyEntityId001";
-
-const COLLECTION_DIR = "collections/data_apps/data_app";
-const QUESTION_PATH = `${COLLECTION_DIR}/orders_${QUESTION}.yaml`;
-const ACTION_PATH = `${COLLECTION_DIR}/create_${ACTION_COPY}.yaml`;
-const METRIC_PATH = `${COLLECTION_DIR}/revenue_metricCopyEntityId0001.yaml`;
-
-const SERIALIZED = {
- queries: [{ file: `orders_${QUESTION}.yaml`, yaml: "name: Orders\n" }],
- actions: [{ file: `create_${ACTION_COPY}.yaml`, yaml: "name: Create\n" }],
- metrics: [
- { file: "revenue_metricCopyEntityId0001.yaml", yaml: "name: Revenue\n" },
- ],
-};
-
-function appWithDefinitions() {
- const appRoot = makeApp();
- fs.writeFileSync(
- path.join(appRoot, ".env.local"),
- "DATA_APP_MB_URL=http://metabase.test/\nDATA_APP_MB_API_KEY=mb_test_key\n",
- );
- fs.writeFileSync(
- path.join(appRoot, "data_app.yaml"),
- `name: Orders\npath: ./dist/index.js\ncollection: ${COLLECTION}\n`,
- );
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION}", source: { type: "table", id: 1 }, limit: 5 });`,
- );
- writeAction(
- appRoot,
- `export const Create = defineAction({ copiedActionEntityId: "${ACTION_COPY}", action: { id: 51, parameters: [] } });`,
- );
- return appRoot;
-}
-
-function mockSerialization(response: Response) {
- return jest.spyOn(global, "fetch").mockResolvedValue(response);
-}
-
-describe("serializing what resources are written from", () => {
- setupResourceTests();
-
- it("refuses to write before the manifest names the app's collection", async () => {
- const appRoot = appWithDefinitions();
- fs.writeFileSync(
- path.join(appRoot, "data_app.yaml"),
- "name: Orders\npath: ./dist/index.js\n",
- );
- const fetchSpy = mockSerialization(
- new Response(JSON.stringify(SERIALIZED)),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "names the app's collection",
- );
- expect(fetchSpy).not.toHaveBeenCalled();
- });
-
- it("refuses to write a query that has no saved question ID yet", async () => {
- const appRoot = appWithDefinitions();
- fs.appendFileSync(
- path.join(appRoot, "queries/orders.query.ts"),
- `export const Products = defineQuery({ source: { type: "table", id: 2 } });\n`,
- );
- const fetchSpy = mockSerialization(
- new Response(JSON.stringify(SERIALIZED)),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "queries/orders.query.ts:Products has no savedQuestionEntityId.",
- );
- expect(fetchSpy).not.toHaveBeenCalled();
- });
-
- it("refuses to write an action that has no copied action ID yet", async () => {
- const appRoot = appWithDefinitions();
- writeAction(
- appRoot,
- `export const Create = defineAction({ action: { id: 51, parameters: [] } });`,
- );
- const fetchSpy = mockSerialization(
- new Response(JSON.stringify(SERIALIZED)),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "actions/orders.action.ts:Create has no copiedActionEntityId.",
- );
- expect(fetchSpy).not.toHaveBeenCalled();
- });
-
- it("sends the definitions with their IDs and the app's collection in one request, and writes every returned file at its path", async () => {
- const appRoot = appWithDefinitions();
- const fetchSpy = mockSerialization(
- new Response(JSON.stringify(SERIALIZED)),
- );
-
- const output = await writeResources(appRoot);
-
- expect(fetchSpy).toHaveBeenCalledTimes(1);
- const [url, init] = fetchSpy.mock.calls[0];
- expect(url).toBe("http://metabase.test/api/apps/generate/resources");
- expect(init?.headers).toEqual({
- "Content-Type": "application/json",
- "X-API-Key": "mb_test_key",
- });
- expect(JSON.parse(String(init?.body))).toEqual({
- queries: [
- {
- name: "Orders",
- query: { stages: [{ source: { type: "table", id: 1 }, limit: 5 }] },
- entity_id: QUESTION,
- collection_id: COLLECTION,
- },
- ],
- actions: [
- { action_id: 51, entity_id: ACTION_COPY, collection_id: COLLECTION },
- ],
- });
- expect(output).toBe(
- [QUESTION_PATH, ACTION_PATH, METRIC_PATH]
- .map((written) => `Wrote ${written}`)
- .join("\n"),
- );
- expect(fs.readFileSync(path.join(appRoot, QUESTION_PATH), "utf8")).toBe(
- "name: Orders\n",
- );
- expect(fs.readFileSync(path.join(appRoot, ACTION_PATH), "utf8")).toBe(
- "name: Create\n",
- );
- expect(fs.readFileSync(path.join(appRoot, METRIC_PATH), "utf8")).toBe(
- "name: Revenue\n",
- );
- });
-
- it("writes into the repository above an app under data_apps/", async () => {
- const appRoot = makeApp({ underDataApps: true });
- fs.writeFileSync(
- path.join(appRoot, ".env.local"),
- "DATA_APP_MB_URL=http://metabase.test/\nDATA_APP_MB_API_KEY=mb_test_key\n",
- );
- writeQuery(
- appRoot,
- `export const Orders = defineQuery({ savedQuestionEntityId: "${QUESTION}", source: { type: "table", id: 1 } });`,
- );
- mockSerialization(
- new Response(JSON.stringify({ ...SERIALIZED, actions: [], metrics: [] })),
- );
-
- await writeResources(appRoot);
-
- expect(fs.existsSync(path.join(appRoot, "..", "..", QUESTION_PATH))).toBe(
- true,
- );
- });
-
- it("replaces the cards and actions the collection held", async () => {
- const appRoot = appWithDefinitions();
- const stale = writeResource(appRoot, "other_place/stale.yaml", {
- name: "Stale",
- type: "question",
- entity_id: "staleEntityId0000001",
- "serdes/meta": [{ model: "Card", id: "staleEntityId0000001" }],
- });
- mockSerialization(new Response(JSON.stringify(SERIALIZED)));
-
- await writeResources(appRoot);
-
- expect(fs.existsSync(stale)).toBe(false);
- expect(
- fs.existsSync(path.join(appRoot, "collections/data_apps/data_app.yaml")),
- ).toBe(true);
- });
-
- it("writes nothing and throws every per-item error", async () => {
- const appRoot = appWithDefinitions();
- const stale = writeResource(appRoot, "stale.yaml", {
- name: "Stale",
- type: "question",
- entity_id: "staleEntityId0000001",
- "serdes/meta": [{ model: "Card", id: "staleEntityId0000001" }],
- });
- mockSerialization(
- new Response(
- JSON.stringify({
- queries: [{ error: "Unknown database" }],
- actions: [{ file: `create_${ACTION_COPY}.yaml`, yaml: "x: 1\n" }],
- metrics: [{ error: "Unknown metric" }],
- }),
- ),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- [
- "queries/orders.query.ts:Orders: Unknown database",
- "A metric: Unknown metric",
- ].join("\n"),
- );
- expect(fs.existsSync(stale)).toBe(true);
- expect(fs.existsSync(path.join(appRoot, ACTION_PATH))).toBe(false);
- });
-
- it("refuses a returned file that would leave the collection's folder", async () => {
- const appRoot = appWithDefinitions();
- const stale = writeResource(appRoot, "stale.yaml", {
- name: "Stale",
- type: "question",
- entity_id: "staleEntityId0000001",
- "serdes/meta": [{ model: "Card", id: "staleEntityId0000001" }],
- });
- mockSerialization(
- new Response(
- JSON.stringify({
- ...SERIALIZED,
- queries: [{ file: "../escaped.yaml", yaml: "name: Orders\n" }],
- }),
- ),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "The serialization holds a file outside the collection: ../escaped.yaml",
- );
- expect(
- fs.existsSync(path.join(appRoot, "collections/data_apps/escaped.yaml")),
- ).toBe(false);
- expect(fs.existsSync(stale)).toBe(true);
- });
-
- it("refuses to write before the collection has a file", async () => {
- const appRoot = appWithDefinitions();
- fs.rmSync(path.join(appRoot, "collections"), { recursive: true });
- const fetchSpy = mockSerialization(
- new Response(JSON.stringify(SERIALIZED)),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- `No file under collections/data_apps/ holds the collection ${COLLECTION}`,
- );
- expect(fetchSpy).not.toHaveBeenCalled();
- });
-
- it("fails without the Metabase instance and API key", async () => {
- const appRoot = appWithDefinitions();
- fs.rmSync(path.join(appRoot, ".env.local"));
- // The shell's own variables would stand in for the file.
- const shell = {
- url: process.env.DATA_APP_MB_URL,
- key: process.env.DATA_APP_MB_API_KEY,
- };
- delete process.env.DATA_APP_MB_URL;
- delete process.env.DATA_APP_MB_API_KEY;
-
- try {
- await expect(writeResources(appRoot)).rejects.toThrow(
- "DATA_APP_MB_URL and DATA_APP_MB_API_KEY must be set, in the repo-root .env.local or the environment.",
- );
- } finally {
- if (shell.url !== undefined) {
- process.env.DATA_APP_MB_URL = shell.url;
- }
- if (shell.key !== undefined) {
- process.env.DATA_APP_MB_API_KEY = shell.key;
- }
- }
- });
-
- it("fails when the response doesn't answer every definition", async () => {
- const appRoot = appWithDefinitions();
- mockSerialization(
- new Response(JSON.stringify({ ...SERIALIZED, queries: [] })),
- );
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "The serialization response doesn't answer every definition.",
- );
- });
-
- it("fails with the response when the serialization request fails", async () => {
- const appRoot = appWithDefinitions();
- mockSerialization(new Response("Unauthenticated", { status: 401 }));
-
- await expect(writeResources(appRoot)).rejects.toThrow(
- "The serialization request failed (401): Unauthenticated",
- );
- });
-
- it("turns an export name into a card name", () => {
- expect(exportNameToCardName("OrdersByMonth")).toBe("Orders by month");
- expect(exportNameToCardName("monthly_revenue")).toBe("Monthly revenue");
- });
-});
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/setup.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/setup.ts
deleted file mode 100644
index 57367408028..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/tests/setup.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-import fs from "node:fs";
-import os from "node:os";
-import path from "node:path";
-
-import { dump as dumpYaml } from "js-yaml";
-
-import { repoRootOf } from "../resources";
-
-const appRoots: string[] = [];
-
-/** Removes every app `makeApp` created and restores mocks after each test. */
-export function setupResourceTests(): void {
- afterEach(() => {
- jest.restoreAllMocks();
-
- appRoots
- .splice(0)
- .forEach((appRoot) =>
- fs.rmSync(appRoot, { recursive: true, force: true }),
- );
- });
-}
-
-/** The entity ID of the collection `makeApp`'s manifest names. */
-export const COLLECTION = "appCollectionEntity01";
-
-/**
- * Creates an app with `queries/`, `actions/`, a manifest naming `COLLECTION`,
- * the collection's file, and a stub of the SDK's data-app entry. The app is
- * its own repository root, or lives at `data_apps/shop/` under one with
- * `underDataApps`.
- */
-export function makeApp({ underDataApps = false } = {}) {
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "data-app-resources-"));
- appRoots.push(root);
-
- const appRoot = underDataApps ? path.join(root, "data_apps", "shop") : root;
-
- fs.mkdirSync(path.join(appRoot, "queries"), { recursive: true });
- fs.mkdirSync(path.join(appRoot, "actions"));
- fs.writeFileSync(
- path.join(appRoot, "data_app.yaml"),
- dumpYaml({
- name: "Shop",
- slug: "shop",
- path: "dist/index.js",
- collection: COLLECTION,
- entity_id: "appEntityId0000000001",
- "serdes/meta": [
- { model: "DataApp", id: "appEntityId0000000001", label: "shop" },
- ],
- }),
- );
- writeResource(appRoot, "data_app.yaml", {
- name: "Data App: Shop",
- namespace: "data-apps",
- entity_id: COLLECTION,
- "serdes/meta": [{ model: "Collection", id: COLLECTION }],
- });
-
- const packageRoot = path.join(
- appRoot,
- "node_modules/@metabase/embedding-sdk-react",
- );
-
- fs.mkdirSync(packageRoot, { recursive: true });
- fs.writeFileSync(
- path.join(packageRoot, "package.json"),
- JSON.stringify({
- name: "@metabase/embedding-sdk-react",
- exports: { "./data-app": "./data-app.js" },
- }),
- );
-
- fs.writeFileSync(
- path.join(packageRoot, "data-app.js"),
- [
- "exports.defineQuery = (query) => query;",
- "exports.defineAction = (definition) => definition;",
- ].join("\n"),
- );
-
- return appRoot;
-}
-
-export function writeAction(appRoot: string, body: string) {
- const filePath = path.join(appRoot, "actions/orders.action.ts");
- fs.writeFileSync(
- filePath,
- `import { defineAction } from "@metabase/embedding-sdk-react/data-app";\n${body}\n`,
- );
- return filePath;
-}
-
-export function writeQuery(appRoot: string, body: string) {
- const filePath = path.join(appRoot, "queries/orders.query.ts");
- fs.writeFileSync(
- filePath,
- `import { defineQuery } from "@metabase/embedding-sdk-react/data-app";\n${body}\n`,
- );
- return filePath;
-}
-
-/**
- * Writes `entity` as YAML to `collections/data_apps/<relativePath>` of the
- * app's repository, in the app's collection unless the entity says otherwise.
- */
-export function writeResource(
- appRoot: string,
- relativePath: string,
- entity: Record<string, unknown>,
-) {
- const filePath = path.join(
- repoRootOf(appRoot),
- "collections/data_apps",
- relativePath,
- );
- const inCollection =
- entity["serdes/meta"] === undefined ||
- modelOf(entity) === "Collection" ||
- "collection_id" in entity
- ? entity
- : { ...entity, collection_id: COLLECTION };
-
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
- fs.writeFileSync(filePath, dumpYaml(inCollection));
- return filePath;
-}
-
-function modelOf(entity: Record<string, unknown>) {
- const meta = entity["serdes/meta"];
- const leaf = Array.isArray(meta) ? meta[meta.length - 1] : undefined;
- return typeof leaf === "object" && leaf !== null && "model" in leaf
- ? leaf.model
- : undefined;
-}
diff --git a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/types.ts b/enterprise/frontend/src/embedding-sdk-package/data-app-resources/types.ts
deleted file mode 100644
index 6be7aca1f9c..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/data-app-resources/types.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-export interface DiscoveredQuery {
- exportName: string;
- filePath: string;
- query: Record<string, unknown>;
- savedQuestionEntityId?: string;
-}
-
-export interface DiscoveredAction {
- exportName: string;
- filePath: string;
- copiedActionEntityId?: string;
- sourceActionId: number;
-}
-
-export type ResourceModel = "Card" | "Action";
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/define-action.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/define-action.ts
index 883de52431a..981d10488fc 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/define-action.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-action/define-action.ts
@@ -1,14 +1,13 @@
import type { DefinedAction } from "./types";
/**
- * Defines a source-controlled data app action. `action` names the generated
- * action the app runs. `copiedActionEntityId` is the entity ID of the action's
- * copy in the app's collection under `collections/data_apps/`, which a production build runs.
+ * Defines a data app action. `action.id` is the action's entity ID or its
+ * numeric id. The typed schema Metabase generates for an app holds one
+ * definition per action in the app's collection.
*/
export function defineAction<
const TDefinition extends {
- action: { id: number; parameters: readonly unknown[] };
- copiedActionEntityId?: string;
+ action: { id: number | string; parameters: readonly unknown[] };
},
>(definition: TDefinition): TDefinition & DefinedAction {
// `DefinedAction` has no runtime member, so the object is returned as is; the
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.ts
index c9f91d45214..871e9b1d48b 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
@@ -1,7 +1,6 @@
export { useAction, useDataAppAction } from "./use-action";
export type {
ActionExecuteError,
- ActionImplicitKind,
ActionKind,
ActionKindFromDataAppSchema,
ActionParameterSchema,
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.ts
index 84312d01acb..3854e0cfe19 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
@@ -5,7 +5,7 @@
* discriminated `ActionResultForKind` shape it drives describe just the
* kind / response surface. The `…FromDataAppSchema` helpers below layer on
* top, deriving the hook's `TParameters` / `TKind` generics from a
- * generated `metabase.data.ts` schema entry; the hook never sees the
+ * data app typed schema entry; the hook never sees the
* schema directly.
*/
@@ -135,28 +135,14 @@ export type ActionResultForKind<TKind extends ActionKind | undefined> =
// ============================================================================
// Data App schema-derived helpers
// ----------------------------------------------------------------------------
-// These read a single action entry in a generated `metabase.data.ts` schema
+// These read a single action entry in a data app typed schema
// and derive the hook's `TParameters` / `TKind`. `useAction` applies them to a
// `defineAction(...)` export, so a Data App writes neither generic.
// ============================================================================
-/**
- * Backend-namespaced `implicitKind` values for basic actions. Mapped onto
- * the flat `ActionKind` union by `ActionKindFromDataAppSchema`.
- *
- * @category useAction
- */
-export type ActionImplicitKind =
- | "row/create"
- | "row/update"
- | "row/delete"
- | "bulk/create"
- | "bulk/update"
- | "bulk/delete";
-
/**
* Shape of a single action parameter as it appears in a generated
- * `metabase.data.ts` schema entry. An action parameter is not a column, so it
+ * typed schema entry. An action parameter is not a column, so it
* carries no `name`.
*
* @category useAction
@@ -169,19 +155,18 @@ export type ActionParameterSchema = {
};
/**
- * Shape of a single action entry in a generated `metabase.data.ts` schema.
+ * Shape of a single action entry in a data app typed schema.
* The entry is what `defineAction` wraps; `parameters` drives
- * `ActionParametersFromDataAppSchema`, and `implicitKind` / `type` drive
+ * `ActionParametersFromDataAppSchema`, and `type` drives
* `ActionKindFromDataAppSchema`.
*
* @category useAction
*/
export type ActionSchema = {
kind?: "action";
- id: number;
+ id: number | string;
name?: string;
- type?: "query" | "implicit";
- implicitKind?: ActionImplicitKind;
+ type?: "query";
parameters: readonly ActionParameterSchema[];
description?: string;
entityId?: string;
@@ -225,35 +210,19 @@ export type ActionParametersFromDataAppSchema<TAction> = TAction extends {
: Record<string, unknown>;
/**
- * Maps a schema entry's `implicitKind` (or `type === "query"`) to the flat
- * `ActionKind` union. Feeds the hook's `TKind` generic so `result` is
- * typed via `ActionResultForKind<TKind>` with no cast — schema-derived,
- * but the value the hook sees is just a string literal.
- *
- * row/create → "create"
- * row/update → "update"
- * row/delete → "delete"
- * bulk/* → "bulk"
- * type=query → "sql"
- *
+ * Maps a schema entry to the flat `ActionKind` union: a data app's actions
+ * are query actions, so `type: "query"` gives `"sql"`. Feeds the hook's
+ * `TKind` generic so `result` is typed via `ActionResultForKind<TKind>`.
* Falls back to the full `ActionKind` union if `TAction` is not a schema
* entry (e.g. a bare numeric id).
*
* @category useAction
*/
export type ActionKindFromDataAppSchema<TAction> = TAction extends {
- implicitKind: "row/create";
+ type: "query";
}
- ? "create"
- : TAction extends { implicitKind: "row/update" }
- ? "update"
- : TAction extends { implicitKind: "row/delete" }
- ? "delete"
- : TAction extends { implicitKind: `bulk/${string}` }
- ? "bulk"
- : TAction extends { type: "query" }
- ? "sql"
- : ActionKind;
+ ? "sql"
+ : ActionKind;
/**
* @interface
@@ -318,7 +287,7 @@ export interface UseAction {
}
/**
- * A data app's `useAction`: only a `defineAction(...)` export from `actions/`.
+ * A data app's `useAction`: only a `defineAction(...)` definition from its typed schema.
*/
export interface UseDataAppAction {
<TDefinition extends DefinedAction & { action: ActionSchema }>(
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.ts
index 8adad5e70dd..99adbead219 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.ts
@@ -7,14 +7,9 @@ import type {
RequireGroupedOrderByNames,
} from "./types";
-type SavedQuestionBinding = {
- savedQuestionEntityId?: string;
-};
-
/**
- * Defines a source-controlled data app query. `savedQuestionEntityId` is the
- * entity ID of its saved question in the app's collection under `collections/data_apps/`, which a
- * production build runs.
+ * Defines a data app query. A data app's typed schema, generated by Metabase,
+ * defines each question in the app's collection with it.
*/
export function defineQuery<
TEntity extends TableSchema | undefined = undefined,
@@ -22,7 +17,6 @@ export function defineQuery<
const TQuery = MetabaseQueryOptions<TEntity, TSchema>,
>(
query: TQuery &
- (TQuery extends SavedQuestionBinding ? unknown : SavedQuestionBinding) &
(TQuery extends MetabaseQueryOptions<TEntity, TSchema>
? TQuery extends { source: unknown }
? RequireAggregationsForBreakouts<TQuery> &
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.unit.spec.ts
index 3fee15703df..8afead8054a 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/define-query.unit.spec.ts
@@ -3,9 +3,8 @@ import { defineQuery } from "./define-query";
describe("defineQuery", () => {
it("returns the query definition unchanged", () => {
const query = {
- savedQuestionEntityId: "revenueQuestionEntity",
- source: { type: "table", id: 1 },
- limit: 10,
+ source: { type: "card", entityId: "revenueQuestionEntity" },
+ enabled: false,
} as const;
expect(defineQuery(query)).toBe(query);
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/dynamic-query.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/dynamic-query.unit.spec.tsx
index 0999444fe54..2f0ae54a852 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/dynamic-query.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/dynamic-query.unit.spec.tsx
@@ -9,22 +9,20 @@ import {
} from "./setup";
import { resolveDatasetQuery as resolveDatasetQueryInBundle } from "embedding-sdk-bundle/lib/create-metabase-query";
-import { EMBEDDING_SDK_CONFIG } from "metabase/embedding-sdk/config";
import { count, filter, orderBy, sum } from "..";
import { TEST_SCHEMA } from "./fixtures";
beforeEach(resetTestState);
-afterEach(() => {
- EMBEDDING_SDK_CONFIG.isDataApp = false;
- EMBEDDING_SDK_CONFIG.isDataAppDev = false;
-});
const STATIC_QUERY = {
- source: TEST_SCHEMA.tables.orders,
- savedQuestionEntityId: PUBLISHED_QUESTION_ENTITY_ID,
-};
+ source: {
+ type: "card",
+ entityId: PUBLISHED_QUESTION_ENTITY_ID,
+ columns: TEST_SCHEMA.questions.ordersQuestion.columns,
+ },
+} as const;
const statusFilter = filter(
TEST_SCHEMA.tables.orders.fields.status,
@@ -32,60 +30,40 @@ const statusFilter = filter(
"paid",
);
-describe("a table source without a saved question", () => {
- const QUERY_WITHOUT_SAVED_QUESTION = { source: TEST_SCHEMA.tables.orders };
-
- it("is refused in a deployed data app", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
-
- await expect(
- resolveDatasetQueryInBundle(createMockStore())(
- QUERY_WITHOUT_SAVED_QUESTION,
- ),
- ).rejects.toThrow(
- "This query has no saved question. Set its `savedQuestionEntityId` in `defineQuery(...)` in `queries/`",
- );
- });
-
- it("still runs in the dev preview, before the app's resources exist", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
- QUERY_WITHOUT_SAVED_QUESTION,
- );
-
- expect(stagesOf(datasetQuery)).toMatchObject([{ "source-table": 1 }]);
- });
-
- it("still runs outside a data app, where the SDK addresses tables directly", async () => {
- const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
- QUERY_WITHOUT_SAVED_QUESTION,
- );
+describe("a table source", () => {
+ it("runs the table directly", async () => {
+ const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())({
+ source: TEST_SCHEMA.tables.orders,
+ });
expect(stagesOf(datasetQuery)).toMatchObject([{ "source-table": 1 }]);
});
});
-describe("a table source backed by a saved question in a deployed data app", () => {
- beforeEach(() => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
- });
-
- it("looks the published card up by its entity ID", async () => {
+describe("a card source addressed by its entity ID", () => {
+ it("looks the card up by its entity ID and fetches it only once", async () => {
const datasetQuery =
await resolveDatasetQueryInBundle(createMockStore())(STATIC_QUERY);
- expect(stagesOf(datasetQuery)[0]).toMatchObject({ "source-card": 41 });
- expect(mockRunRtkEndpoint).toHaveBeenCalledWith(
- { id: PUBLISHED_QUESTION_ENTITY_ID },
- expect.anything(),
- expect.objectContaining({ name: "getCard" }),
- { forceRefetch: false },
- );
+ expect(datasetQuery).toMatchObject({ database: 1 });
+ expect(stagesOf(datasetQuery)).toMatchObject([{ "source-card": 41 }]);
+ expect(mockRunRtkEndpoint.mock.calls).toEqual([
+ [
+ { id: PUBLISHED_QUESTION_ENTITY_ID },
+ expect.anything(),
+ expect.objectContaining({ name: "getCard" }),
+ { forceRefetch: false },
+ ],
+ [
+ 41,
+ expect.anything(),
+ expect.objectContaining({ name: "getCardQueryMetadata" }),
+ { forceRefetch: false },
+ ],
+ ]);
});
- it("explains a published card the instance hasn't imported yet", async () => {
+ it("explains a card the instance doesn't have", async () => {
mockRunRtkEndpoint.mockRejectedValueOnce({
status: 404,
data: "Not found.",
@@ -93,17 +71,14 @@ describe("a table source backed by a saved question in a deployed data app", ()
await expect(
resolveDatasetQueryInBundle(createMockStore())(STATIC_QUERY),
- ).rejects.toThrow("This app's saved questions have not been imported.");
+ ).rejects.toThrow(
+ `Saved question ${PUBLISHED_QUESTION_ENTITY_ID} does not exist.`,
+ );
});
});
describe("dynamic query clauses", () => {
- // The swap these cover is a data-app rule; elsewhere the table is what runs.
- beforeEach(() => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
- });
-
- it("runs the published card in production and layers the dynamic stage on top", async () => {
+ it("layers the dynamic stage on top of the card", async () => {
const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
STATIC_QUERY,
{ filters: [statusFilter] },
@@ -124,85 +99,6 @@ describe("dynamic query clauses", () => {
]);
});
- it("keeps the table source in the dev preview, with the same dynamic stage", async () => {
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
- STATIC_QUERY,
- { filters: [statusFilter] },
- );
-
- expect(stagesOf(datasetQuery)).toMatchObject([
- { "source-table": 1 },
- {
- filters: [
- [
- "=",
- expect.anything(),
- ["field", expect.anything(), "STATUS"],
- "paid",
- ],
- ],
- },
- ]);
- });
-
- // The static clauses are already inside the published card, so the swapped
- // source must drop them rather than apply them a second time.
- it("drops the static clauses when it swaps in the published card", async () => {
- const aggregatingQuery = {
- source: TEST_SCHEMA.tables.orders,
- aggregations: [count()],
- breakouts: [TEST_SCHEMA.tables.orders.fields.status],
- savedQuestionEntityId: PUBLISHED_QUESTION_ENTITY_ID,
- };
-
- const production = await resolveDatasetQueryInBundle(createMockStore())(
- aggregatingQuery,
- { filters: [filter({ type: "column", name: "STATUS" }, "=", "paid")] },
- );
-
- expect(stagesOf(production)[0]).toEqual({
- "lib/type": "mbql.stage/mbql",
- "source-card": 41,
- });
-
- // The dev preview keeps them, since nothing has been published into a card.
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- const preview = await resolveDatasetQueryInBundle(createMockStore())(
- aggregatingQuery,
- { filters: [filter({ type: "column", name: "count" }, ">", 1)] },
- );
-
- expect(stagesOf(preview)[0]).toMatchObject({
- "source-table": 1,
- aggregation: [["count", expect.anything()]],
- breakout: [["field", expect.anything(), 101]],
- });
- });
-
- it("stays a single stage when there is no dynamic part", async () => {
- const datasetQuery =
- await resolveDatasetQueryInBundle(createMockStore())(STATIC_QUERY);
-
- expect(stagesOf(datasetQuery)).toMatchObject([{ "source-card": 41 }]);
- });
-
- it("layers the dynamic stage on a preview query that has no card", async () => {
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
- { source: TEST_SCHEMA.tables.orders },
- { filters: [statusFilter] },
- );
-
- expect(stagesOf(datasetQuery)).toMatchObject([
- { "source-table": 1 },
- { filters: [expect.anything()] },
- ]);
- });
-
it("groups and orders in the dynamic stage", async () => {
const countAgg = count();
@@ -227,15 +123,13 @@ describe("dynamic query clauses", () => {
// Products' ID is reachable through PRODUCT_ID under the same name.
it.each([
- ["the published card", false],
- ["the dev preview table", true],
+ ["the card", STATIC_QUERY],
+ ["a table", { source: TEST_SCHEMA.tables.orders }],
])(
"tells a result column from a same-named implicitly joinable column on %s",
- async (_source, isDataAppDev) => {
- EMBEDDING_SDK_CONFIG.isDataAppDev = isDataAppDev;
-
+ async (_source, staticQuery) => {
const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
- STATIC_QUERY,
+ staticQuery,
{
orderBys: [orderBy(TEST_SCHEMA.tables.orders.fields.id, "desc")],
},
@@ -252,8 +146,6 @@ describe("dynamic query clauses", () => {
// Unnamed, both sums return a column named `sum`, and the later stage cannot
// tell them apart.
it("orders the dynamic stage by one of two sums, by its name", async () => {
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
const totalAmount = sum(TEST_SCHEMA.tables.orders.fields.amount, {
name: "total amount",
});
@@ -263,7 +155,7 @@ describe("dynamic query clauses", () => {
const datasetQuery = await resolveDatasetQueryInBundle(createMockStore())(
{
- ...STATIC_QUERY,
+ source: TEST_SCHEMA.tables.orders,
aggregations: [totalAmount, totalIds],
breakouts: [TEST_SCHEMA.tables.orders.fields.status],
},
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.ts
index 0186687aae0..5ee2961ce5b 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
@@ -18,13 +18,24 @@ import { TEST_SCHEMA } from "./fixtures";
type OrdersTable = (typeof TEST_SCHEMA)["tables"]["orders"];
-const queryWithInvalidSavedQuestionEntityId = {
- savedQuestionEntityId: 54,
- source: TEST_SCHEMA.tables.orders,
+const queryWithInvalidQuestionEntityId = {
+ source: { type: "card", entityId: 54 },
} as const;
// @ts-expect-error saved-question entity IDs are strings
-defineQuery(queryWithInvalidSavedQuestionEntityId);
+defineQuery(queryWithInvalidQuestionEntityId);
+
+const questionQueryWithStaticLimit = {
+ source: {
+ type: "card",
+ entityId: "ordersQuestionEntity1",
+ columns: TEST_SCHEMA.questions.ordersQuestion.columns,
+ },
+ limit: 10,
+} as const;
+
+// @ts-expect-error a card query's static clauses are the question's own
+defineQuery(questionQueryWithStaticLimit);
// @ts-expect-error query definitions with breakouts require aggregations
defineQuery({
@@ -32,13 +43,12 @@ defineQuery({
breakouts: [breakout(TEST_SCHEMA.tables.orders.fields.createdAt)],
});
-const actionWithInvalidCopiedActionEntityId = {
- copiedActionEntityId: 91,
- action: TEST_SCHEMA.actions.createOrder,
+const actionWithInvalidId = {
+ action: { ...TEST_SCHEMA.actions.createOrder, id: true },
} as const;
-// @ts-expect-error copied action entity IDs are strings
-defineAction(actionWithInvalidCopiedActionEntityId);
+// @ts-expect-error action ids are numbers or entity IDs
+defineAction(actionWithInvalidId);
// @ts-expect-error action definitions must reference a generated action
defineAction({ action: TEST_SCHEMA.tables.orders });
@@ -190,13 +200,24 @@ const aggregatedOrdersQuery = defineQuery({
function InvalidTypeFixtures() {
const staticQuery = defineQuery({
source: TEST_SCHEMA.tables.orders,
- savedQuestionEntityId: "ordersQuestionEntity1",
});
- // @ts-expect-error a query is a `defineQuery` export from `queries/`, never an inline object
+ const questionQuery = defineQuery({
+ source: {
+ type: "card",
+ entityId: "ordersQuestionEntity1",
+ columns: TEST_SCHEMA.questions.ordersQuestion.columns,
+ },
+ });
+
+ const questionResult = useMetabaseQuery(questionQuery);
+ // @ts-expect-error a card query's rows hold only the card's result columns
+ const _missing = questionResult.data?.rows[0]?.MISSING;
+
+ // @ts-expect-error a query is a `defineQuery` definition, never an inline object
useMetabaseQuery({ source: TEST_SCHEMA.tables.orders });
- // @ts-expect-error a query is a `defineQuery` export from `queries/`, never an inline object
+ // @ts-expect-error a query is a `defineQuery` definition, never an inline object
useMetabaseQueryObject({ source: TEST_SCHEMA.tables.orders });
const plainQuery = {
@@ -209,16 +230,13 @@ function InvalidTypeFixtures() {
// @ts-expect-error a typed query object is still not a `defineQuery` export
useMetabaseQueryObject(plainQuery);
- // @ts-expect-error saved-question metadata cannot be passed to querying hooks
- defineQuery({ source: TEST_SCHEMA.questions.ordersQuestion });
-
// @ts-expect-error a copy of a definition is not the definition
useMetabaseQuery({ ...staticQuery, limit: 10 });
// @ts-expect-error a copy of a definition is not the definition
useMetabaseQueryObject({ ...staticQuery, limit: 10 });
- // @ts-expect-error a data app's action is a `defineAction` export from `actions/`, never an inline object
+ // @ts-expect-error a data app's action is a `defineAction` definition, never an inline object
useDataAppAction({ action: TEST_SCHEMA.actions.createOrder });
// @ts-expect-error a copy of a definition is not the definition
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.ts
index 01959906a91..c0919782db1 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
@@ -20,29 +20,47 @@ import { TEST_SCHEMA } from "./fixtures";
type OrdersTable = (typeof TEST_SCHEMA)["tables"]["orders"];
const revenueQuery = defineQuery({
- savedQuestionEntityId: "revenueQuestionEntity",
source: TEST_SCHEMA.tables.orders,
limit: 10,
});
-const _savedQuestionEntityId: "revenueQuestionEntity" =
- revenueQuery.savedQuestionEntityId;
const _queryLimit: 10 = revenueQuery.limit;
+const ordersQuestionQuery = defineQuery({
+ source: {
+ type: "card",
+ entityId: "ordersQuestionEntity1",
+ columns: TEST_SCHEMA.questions.ordersQuestion.columns,
+ },
+});
+
+const _questionEntityId: "ordersQuestionEntity1" =
+ ordersQuestionQuery.source.entityId;
+
const CreateOrder = defineAction({
- copiedActionEntityId: "createOrderCopyEntity",
- action: TEST_SCHEMA.actions.createOrder,
+ action: { ...TEST_SCHEMA.actions.createOrder, id: "createOrderEntity" },
});
-const _copiedActionEntityId: "createOrderCopyEntity" =
- CreateOrder.copiedActionEntityId;
-const _sourceActionId: 51 = CreateOrder.action.id;
+const _actionEntityId: "createOrderEntity" = CreateOrder.action.id;
-// A definition is valid before its copy is written.
const UpdateOrder = defineAction({
action: TEST_SCHEMA.actions.updateOrder,
});
+function CardQueryFixtures() {
+ const { data } = useMetabaseQuery(ordersQuestionQuery);
+ const _status: string | null | undefined = data?.rows[0]?.STATUS;
+ const _amount: number | null | undefined = data?.rows[0]?.AMOUNT;
+
+ const grouped = useMetabaseQuery(ordersQuestionQuery, {
+ aggregations: [count()],
+ breakouts: [{ type: "column", name: "STATUS" }],
+ });
+ const _groupedStatus: string | null | undefined =
+ grouped.data?.rows[0]?.STATUS;
+ const _groupedCount: number | null | undefined = grouped.data?.rows[0]?.count;
+}
+
// --------
// Compile-time contracts that must pass type-checking.
//
@@ -306,10 +324,9 @@ function ValidTypeFixtures() {
}),
);
- // A static query published as a card, with dynamic clauses layered on top.
+ // A table query with dynamic clauses layered on top.
const staticQuery = defineQuery({
source: TEST_SCHEMA.tables.orders,
- savedQuestionEntityId: "ordersQuestionEntity1",
});
const dynamicResult = useMetabaseQuery(staticQuery, {
@@ -436,3 +453,4 @@ for (const numBins of [10, 50, 100] as const) {
orderBys: [orderBy(binnedAmount, "asc")],
});
}
+void CardQueryFixtures;
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/use-metabase-query.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/use-metabase-query.unit.spec.tsx
index f5f7a58cd82..b9f34ec49df 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/use-metabase-query.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/tests/use-metabase-query.unit.spec.tsx
@@ -473,8 +473,7 @@ describe("dynamic query clauses", () => {
const { rerender } = renderHook(() =>
useMetabaseQuery(
defineQuery({
- source: TEST_SCHEMA.tables.orders,
- savedQuestionEntityId: PUBLISHED_QUESTION_ENTITY_ID,
+ source: { type: "card", entityId: PUBLISHED_QUESTION_ENTITY_ID },
}),
{
filters: [filter(TEST_SCHEMA.tables.orders.fields.status, "=", "x")],
diff --git a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/types.ts b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/types.ts
index 28db7df4db9..21a97bc7e95 100644
--- a/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/types.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/hooks/public/use-metabase-query/types.ts
@@ -16,6 +16,7 @@ import type {
InferSchema,
MetricSchema,
QueryData,
+ QuestionSchema,
SchemaColumn,
SchemaJavaScriptType,
SchemaValue,
@@ -103,8 +104,7 @@ export type MetricReference<TTable = unknown> = Omit<
/**
* A dimension on the query's own source table — what `filter`, `breakout`, and
- * `orderBy` take. Apps with a generated `metabase.data.ts` schema get these from
- * it; an app building one by hand (from a field id and table id) names this type.
+ * `orderBy` take. A table query builds one from a field id and table id.
* `FieldReference` is deliberately not the type to reach for: it also admits
* FK-joined dimensions, which a local source table can't accept.
*/
@@ -447,14 +447,6 @@ type StageClauses<TDimension, TAggregation, TFilter> = {
type TableQueryBase<TTable> = {
source: TTable extends TableSchema ? SourceQuerySpec<TTable> : TableSchema;
fields?: readonly FieldReference<TTable>[];
- /**
- * The entity ID of this query's saved question in the app's collection,
- * under the repository's `collections/data_apps/`. A production build runs
- * that card instead of the table: it sits in the app's collection, which the
- * app's viewers can read, and a table source isn't. The dev preview ignores
- * it and runs the table source.
- */
- savedQuestionEntityId?: string;
} & StageClauses<
FieldReference<TTable>,
AnyAggregation<TTable>,
@@ -539,10 +531,25 @@ type RequireDynamicOrderByNames<TEntity, TQuery, TDynamic> =
ColumnName<QueryResultColumns<TEntity, TQuery>>
>;
-export type MetabaseQueryOptions<
- TEntity = unknown,
- _TSchema = unknown,
-> = TableQuery<TEntity>;
+/**
+ * A saved question run as is, typed by its result columns. A data app's typed
+ * schema defines each of its questions this way. Its static clauses are the
+ * question's own.
+ */
+type QuestionQuery = {
+ source: QuestionSchema;
+ enabled?: boolean;
+ fields?: never;
+ filters?: never;
+ aggregations?: never;
+ breakouts?: never;
+ orderBys?: never;
+ limit?: never;
+};
+
+export type MetabaseQueryOptions<TEntity = unknown, _TSchema = unknown> =
+ | TableQuery<TEntity>
+ | QuestionQuery;
type EmptyRow = Record<never, never>;
diff --git a/enterprise/frontend/src/embedding-sdk-package/package.template.json b/enterprise/frontend/src/embedding-sdk-package/package.template.json
index 37910d0f6ef..8b790a30419 100644
--- a/enterprise/frontend/src/embedding-sdk-package/package.template.json
+++ b/enterprise/frontend/src/embedding-sdk-package/package.template.json
@@ -40,17 +40,12 @@
},
"typesVersions": {
"*": {
- "next": [
- "./dist/next.d.ts"
- ]
+ "next": ["./dist/next.d.ts"]
}
},
"types": "./dist/index.d.ts",
"peerDependencies": {
"react": ">=18 <=19",
"react-dom": ">=18 <=19"
- },
- "dependencies": {
- "esbuild": "^0.28.1"
}
}
diff --git a/enterprise/frontend/src/embedding-sdk-package/test/generate-sdk-package.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/test/generate-sdk-package.unit.spec.ts
deleted file mode 100644
index 7e89f6183bb..00000000000
--- a/enterprise/frontend/src/embedding-sdk-package/test/generate-sdk-package.unit.spec.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import fs from "fs";
-import path from "path";
-
-jest.mock("fs");
-
-describe("SDK package generation", () => {
- it("publishes only esbuild and typescript as CLI runtime dependencies", async () => {
- const repositoryManifest = {
- dependencies: { typescript: "6.0.3", react: "19.0.0" },
- devDependencies: { esbuild: "0.28.1", jest: "30.0.0" },
- };
-
- jest.mocked(fs.readFileSync).mockImplementation((filename) => {
- if (filename === path.resolve("package.json")) {
- return JSON.stringify(repositoryManifest);
- }
-
- return JSON.stringify({ version: "1.0.0" });
- });
-
- await jest.isolateModulesAsync(async () => {
- await import("../../../../../bin/embedding-sdk/generate-sdk-package-files.js");
- });
-
- expect(fs.writeFileSync).toHaveBeenCalledWith(
- path.resolve("resources/embedding-sdk/package.json"),
- expect.any(String),
- "utf-8",
- );
-
- const packageJsonPath = path.resolve(
- "resources/embedding-sdk/package.json",
- );
-
- const [, manifest] = jest
- .mocked(fs.writeFileSync)
- .mock.calls.find(([filename]) => filename === packageJsonPath)!;
-
- expect(JSON.parse(String(manifest)).dependencies).toStrictEqual({
- esbuild: "0.28.1",
- typescript: "6.0.3",
- });
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/api/data-app.ts b/enterprise/frontend/src/metabase-enterprise/api/data-app.ts
index 9df7ed2fdb6..29764af0c5a 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/data-app.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/data-app.ts
@@ -22,7 +22,7 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
listDataApps: builder.query<DataApp[], ListDataAppsOptions | void>({
query: (options) => ({
method: "GET",
- url: "/api/apps",
+ url: "/api/ee/apps",
params: options,
}),
providesTags: (apps = []) => [
@@ -33,14 +33,14 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
getDataApp: builder.query<DataApp, string>({
query: (name) => ({
method: "GET",
- url: `/api/apps/${encodeURIComponent(name)}`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}`,
}),
providesTags: (_, __, name) => [idTag("data-app", name)],
}),
getDataAppRepoStatus: builder.query<DataAppRepoStatus, void>({
query: () => ({
method: "GET",
- url: "/api/apps/repo-status",
+ url: "/api/ee/apps/repo-status",
}),
providesTags: () => [REPO_STATUS_TAG],
}),
@@ -50,14 +50,14 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
>({
query: (name) => ({
method: "GET",
- url: `/api/apps/${encodeURIComponent(name)}/group-permission-warnings`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}/group-permission-warnings`,
}),
providesTags: (_, __, name) => [idTag("data-app", name)],
}),
getDataAppGroups: builder.query<DataAppGroup[], string>({
query: (name) => ({
method: "GET",
- url: `/api/apps/${encodeURIComponent(name)}/groups`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}/groups`,
}),
providesTags: (_, __, name) => [idTag("data-app", name)],
}),
@@ -65,7 +65,7 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
{
query: ({ name, group_ids }) => ({
method: "POST",
- url: `/api/apps/${encodeURIComponent(name)}/groups`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}/groups`,
body: { group_ids },
}),
async onQueryStarted({ name }, { dispatch, queryFulfilled }) {
@@ -92,7 +92,7 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
removeDataAppGroup: builder.mutation<void, RemoveDataAppGroupRequest>({
query: ({ name, group_id }) => ({
method: "DELETE",
- url: `/api/apps/${encodeURIComponent(name)}/groups/${group_id}`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}/groups/${group_id}`,
}),
async onQueryStarted({ name, group_id }, { dispatch, queryFulfilled }) {
@@ -118,7 +118,7 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
setDataAppEnabled: builder.mutation<DataApp, SetDataAppEnabledRequest>({
query: ({ name, enabled }) => ({
method: "PUT",
- url: `/api/apps/${encodeURIComponent(name)}`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}`,
body: { enabled },
}),
invalidatesTags: (_, error, { name }) =>
@@ -127,7 +127,7 @@ export const dataAppApi = EnterpriseApi.injectEndpoints({
deleteDataApp: builder.mutation<void, string>({
query: (name) => ({
method: "DELETE",
- url: `/api/apps/${encodeURIComponent(name)}`,
+ url: `/api/ee/apps/${encodeURIComponent(name)}`,
}),
invalidatesTags: (_, error, name) =>
invalidateTags(error, [listTag("data-app"), idTag("data-app", name)]),
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppGroupsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppGroupsPage.unit.spec.tsx
index f5a35dea56a..bd660eb2c8f 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppGroupsPage.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppGroupsPage.unit.spec.tsx
@@ -73,7 +73,7 @@ describe("ManageDataAppGroupsPage", () => {
await waitFor(() => {
const requests = fetchMock.callHistory.calls(
- "path:/api/apps/sales/groups",
+ "path:/api/ee/apps/sales/groups",
{ method: "POST" },
);
@@ -115,7 +115,7 @@ describe("ManageDataAppGroupsPage", () => {
).toBeInTheDocument();
const requests = fetchMock.callHistory.calls(
- "path:/api/apps/sales/groups",
+ "path:/api/ee/apps/sales/groups",
{ method: "POST" },
);
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppsPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppsPage.unit.spec.tsx
index ec7e6902bcc..205e8de57cd 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppsPage.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/ManageDataAppsPage.unit.spec.tsx
@@ -224,7 +224,7 @@ describe("ManageDataAppsPage", () => {
await screen.findByRole("menuitem", { name: "Disable" }),
);
- const request = fetchMock.callHistory.lastCall("path:/api/apps/sales");
+ const request = fetchMock.callHistory.lastCall("path:/api/ee/apps/sales");
expect(await request?.request?.json()).toEqual({ enabled: false });
});
@@ -239,7 +239,7 @@ describe("ManageDataAppsPage", () => {
await screen.findByRole("menuitem", { name: "Re-enable" }),
);
- const request = fetchMock.callHistory.lastCall("path:/api/apps/sales");
+ const request = fetchMock.callHistory.lastCall("path:/api/ee/apps/sales");
expect(await request?.request?.json()).toEqual({ enabled: true });
});
@@ -277,7 +277,7 @@ describe("ManageDataAppsPage", () => {
);
expect(
- fetchMock.callHistory.called("path:/api/apps/sales", {
+ fetchMock.callHistory.called("path:/api/ee/apps/sales", {
method: "DELETE",
}),
).toBe(true);
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/components/DataAppActionsMenu/DataAppActionsMenu.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/components/DataAppActionsMenu/DataAppActionsMenu.unit.spec.tsx
index 3aa7bb048a5..9bb1b2cbc5e 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/components/DataAppActionsMenu/DataAppActionsMenu.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/components/DataAppActionsMenu/DataAppActionsMenu.unit.spec.tsx
@@ -66,7 +66,7 @@ describe("DataAppActionsMenu", () => {
});
it("should show a toast when toggling enabled fails", async () => {
- fetchMock.put("path:/api/apps/sales", 500);
+ fetchMock.put("path:/api/ee/apps/sales", 500);
setup({ enabled: true });
await openMenu();
@@ -80,7 +80,7 @@ describe("DataAppActionsMenu", () => {
});
it("should show a toast when removal fails", async () => {
- fetchMock.delete("path:/api/apps/sales", 500);
+ fetchMock.delete("path:/api/ee/apps/sales", 500);
setup({ canRemove: true });
await openMenu();
@@ -97,7 +97,7 @@ describe("DataAppActionsMenu", () => {
it("should show a loading state on the trigger while a delete is in flight", async () => {
let finishDelete = () => {};
fetchMock.delete(
- "path:/api/apps/sales",
+ "path:/api/ee/apps/sales",
() =>
new Promise(
(resolve) => (finishDelete = () => resolve({ status: 204 })),
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/tests/ManageDataAppGroupsPage.setup.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/tests/ManageDataAppGroupsPage.setup.tsx
index 68f2d7a4eaf..b20ab37af31 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/admin/tests/ManageDataAppGroupsPage.setup.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/admin/tests/ManageDataAppGroupsPage.setup.tsx
@@ -60,17 +60,17 @@ export const setup = ({
} = {}) => {
let assigned = [...groups];
- fetchMock.get("path:/api/apps", [app]);
+ fetchMock.get("path:/api/ee/apps", [app]);
fetchMock.get("path:/api/permissions/group", candidates);
- fetchMock.get("path:/api/apps/sales/groups", () => assigned);
+ fetchMock.get("path:/api/ee/apps/sales/groups", () => assigned);
fetchMock.get(
- "path:/api/apps/sales",
+ "path:/api/ee/apps/sales",
!app.enabled ? 404 : !app.resource_collection_id ? 409 : app,
);
fetchMock.get(
- "path:/api/apps/sales/group-permission-warnings",
+ "path:/api/ee/apps/sales/group-permission-warnings",
() =>
warningsResponse ??
(warningsError
@@ -81,7 +81,7 @@ export const setup = ({
{ name: "data-app-group-permission-warnings" },
);
- fetchMock.post("path:/api/apps/sales/groups", ({ options: { body } }) => {
+ fetchMock.post("path:/api/ee/apps/sales/groups", ({ options: { body } }) => {
if (failFirstAdd) {
failFirstAdd = false;
return 500;
@@ -99,8 +99,8 @@ export const setup = ({
return assigned;
});
- fetchMock.delete("express:/api/apps/sales/groups/:id", ({ url }) => {
- // `/api/apps/sales/groups/5` -> `5`
+ fetchMock.delete("express:/api/ee/apps/sales/groups/:id", ({ url }) => {
+ // `/api/ee/apps/sales/groups/5` -> `5`
const groupId = Number(url.split("/").pop());
assigned = assigned.filter((group) => group.id !== groupId);
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.tsx
index ea5d69f5fc9..d5913d82ed5 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.tsx
@@ -299,7 +299,7 @@ export function DataAppView() {
return (
<NotFound
title={t`This data app isn’t ready yet`}
- message={t`Its bundle hasn’t finished syncing from the connected repository. Try again in a moment.`}
+ message={t`This app has no bundle yet. Build it and push it, or wait for the connected repository to sync, then try again.`}
/>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.unit.spec.tsx
index 15f62f665f1..4fa11f132d1 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/components/DataAppView/DataAppView.unit.spec.tsx
@@ -147,7 +147,7 @@ describe("DataAppView", () => {
return screen.getByTitle<HTMLIFrameElement>("Sales");
}
- it("shows the not-ready screen when the iframe reports its bundle hasn't synced", () => {
+ it("shows the not-ready screen when the iframe reports it has no bundle yet", () => {
const iframe = setupIframe();
postMessage(iframe.contentWindow, {
@@ -158,6 +158,9 @@ describe("DataAppView", () => {
expect(
screen.getByText("This data app isn’t ready yet"),
).toBeInTheDocument();
+ expect(
+ screen.getByText(/This app has no bundle yet\. Build it and push it/),
+ ).toBeInTheDocument();
});
it("shows the themed error screen with the reported message when the iframe's bundle crashes", () => {
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
index 90602b790af..0a75a582c13 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/navbar/DataAppsNavbarSection.unit.spec.tsx
@@ -8,7 +8,7 @@ import { DataAppsNavbarSection } from "./DataAppsNavbarSection";
describe("DataAppsNavbarSection", () => {
it("does not show apps section when there are no data apps", async () => {
- fetchMock.get("path:/api/apps?available=true", []);
+ fetchMock.get("path:/api/ee/apps?available=true", []);
renderWithProviders(<DataAppsNavbarSection onItemSelect={jest.fn()} />);
@@ -20,7 +20,7 @@ describe("DataAppsNavbarSection", () => {
});
it("shows every data app as a link", async () => {
- fetchMock.get("path:/api/apps?available=true", [
+ fetchMock.get("path:/api/ee/apps?available=true", [
{ name: "gizmo", display_name: "Gizmo" },
{ name: "gadget", display_name: "Gadget" },
]);
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.ts b/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.ts
index c89ac7f1061..650beb7acea 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.ts
@@ -77,7 +77,7 @@ export const fetchDataAppBundleCode = async (
name: string,
): Promise<FetchedDataAppBundle> => {
const url = getSubpathSafeUrl(
- `/api/apps/${encodeURIComponent(name)}/bundle?t=${Date.now()}`,
+ `/api/ee/apps/${encodeURIComponent(name)}/bundle?t=${Date.now()}`,
);
let res: Response;
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.unit.spec.ts
index 825de2786ad..b1c7d6b9e89 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.unit.spec.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/runtime/loader.unit.spec.ts
@@ -43,7 +43,7 @@ describe("fetchDataAppBundleCode", () => {
const [url, init] = fetchMock.mock.calls[0];
- expect(url).toContain("/api/apps/my%20app/bundle");
+ expect(url).toContain("/api/ee/apps/my%20app/bundle");
expect(init).toEqual({ cache: "no-store" });
});
@@ -122,7 +122,7 @@ describe("instantiateDataAppBundle", () => {
await instantiate();
expect(mockedCreateSandbox).toHaveBeenCalledWith(
- expect.objectContaining({ realmHostUrl: "/api/apps/sandbox-host" }),
+ expect.objectContaining({ realmHostUrl: "/api/ee/apps/sandbox-host" }),
);
});
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/sandbox.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/sandbox.unit.spec.ts
index 35e581a9841..12cb9ed7d26 100644
--- a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/sandbox.unit.spec.ts
+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/sandbox.unit.spec.ts
@@ -82,13 +82,13 @@ describe("createDataAppSandbox", () => {
// CSP, not the data-app document's — see `realmHostUrl`.
it("points the membrane's realm iframe at the realm host when given one", async () => {
const { options } = await setup({
- realmHostUrl: "/api/apps/sandbox-host",
+ realmHostUrl: "/api/ee/apps/sandbox-host",
});
// Untyped: `iframeSrc` comes from Metabase's near-membrane patch, so it is
// absent from the upstream option types.
expect((options as { iframeSrc?: string })?.iframeSrc).toBe(
- "/api/apps/sandbox-host",
+ "/api/ee/apps/sandbox-host",
);
});
diff --git a/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/index.ts b/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/index.ts
index 93672fff1ac..3a9ee2ae0aa 100644
--- a/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/index.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/index.ts
@@ -6,9 +6,9 @@ import {
isQuestionInput,
isTableInput,
} from "embedding-sdk-shared/lib/create-metabase-query/input-guards";
+import type { QuestionSchema } from "embedding-sdk-shared/lib/create-metabase-query/schema";
import { cardApi, selectCard, selectTableQueryMetadata } from "metabase/api";
import { runRtkEndpoint } from "metabase/api/utils/run-rtk-endpoint";
-import { isDataApp, isDataAppDev } from "metabase/embedding-sdk/config";
import { selectMetadataProviderUnfiltered } from "metabase/metadata-store";
import { fetchTableMetadataAndForeignKeys } from "metabase/redux/tables";
import * as Lib from "metabase-lib";
@@ -47,62 +47,52 @@ export const resolveDatasetQuery: ResolveDatasetQuery =
);
}
- const { sourceInput, publishedCard } = await toSourceInput(store, input);
+ const { input: sourceInput, card: sourceCard } = await resolveSourceCard(
+ store,
+ input,
+ );
validateQueryInput(sourceInput);
validateDynamicQuery(dynamicQuery);
- await loadSourceMetadata(store, sourceInput, publishedCard);
+ await loadSourceMetadata(store, sourceInput, sourceCard);
return resolveQueryFromLoadedMetadata(
sourceInput,
dynamicQuery,
store.getState(),
- publishedCard,
+ sourceCard,
);
};
type SdkState = ReturnType<SdkStore["getState"]>;
+type ResolvedSource = {
+ input: QueryInput;
+ card?: Card;
+};
+
/**
- * The query whose source actually runs. Outside the dev preview a published card
- * replaces the table source: the card is what grants an app's viewers permission
- * to run the query, through the collection it lives in. Its static clauses are
- * already baked into the card, so only the source and the dynamic stage remain.
+ * A card source given by its entity ID, with the card's numeric id: query
+ * metadata and the query itself address cards by numeric id.
*/
-async function toSourceInput(
+async function resolveSourceCard(
store: SdkStore,
input: QueryInput,
-): Promise<{ sourceInput: QueryInput; publishedCard?: Card }> {
- // Only a data app runs published cards. In the dev preview they do not exist
- // yet, and outside a data app they never do, so the table is what runs.
- if (!isTableInput(input) || isDataAppDev() || !isDataApp()) {
- return { sourceInput: input };
+): Promise<ResolvedSource> {
+ if (!isQuestionInput(input) || input.source.id !== undefined) {
+ return { input };
}
- const entityId = input.savedQuestionEntityId;
-
- if (entityId === undefined) {
- // An app's viewers can only read the published cards, so a table source 403s.
- throw new Error(
- "This query has no saved question. Set its `savedQuestionEntityId` in `defineQuery(...)` in `queries/`, run `npm run write-resources` and `npm run check-resources`, commit, and rebuild.",
- );
- }
-
- const publishedCard = await fetchPublishedCard(store, entityId);
+ const card = await fetchCardByEntityId(store, input.source.entityId);
return {
- sourceInput: { source: { type: "card", id: publishedCard.id } },
- publishedCard,
+ input: { ...input, source: { ...input.source, id: card.id } },
+ card,
};
}
-/**
- * The published card is addressed by the entity ID its resource file gives it,
- * which Metabase only knows once the repository is imported. Query metadata and
- * the query itself address cards by numeric ID.
- */
-async function fetchPublishedCard(
+async function fetchCardByEntityId(
store: SdkStore,
entityId: string,
): Promise<Card> {
@@ -116,7 +106,7 @@ async function fetchPublishedCard(
} catch (error) {
if (isObject(error) && error.status === 404) {
throw new Error(
- "This app's saved questions have not been imported. Pull the connected repository again, or check the app's sync status.",
+ `Saved question ${entityId} does not exist. Fetch the app's typed schema again to pick up its current questions.`,
);
}
@@ -124,11 +114,19 @@ async function fetchPublishedCard(
}
}
+function getQuestionId(source: QuestionSchema): number {
+ if (source.id === undefined) {
+ throw new Error(`Saved question ${source.entityId} was not resolved.`);
+ }
+
+ return source.id;
+}
+
function resolveQueryFromLoadedMetadata(
input: QueryInput,
dynamicQuery: DynamicQueryInput | undefined,
state: SdkState,
- publishedCard: Card | undefined,
+ sourceCard: Card | undefined,
) {
if (!isQueryInput(input)) {
throw new Error(
@@ -136,15 +134,12 @@ function resolveQueryFromLoadedMetadata(
);
}
- const databaseId =
- publishedCard?.dataset_query?.database ?? getSourceDatabaseId(input, state);
+ const databaseId = getSourceDatabaseId(input, state, sourceCard);
const provider = selectMetadataProviderUnfiltered(state, databaseId);
const sourceStage = toStageSpec(input);
- // The dynamic clauses run as their own stage rather than merging into the
- // source stage. Merged, they would apply before the static aggregation on
- // a table source but after it on the published card — the same app would
- // return different numbers in the dev preview and in production.
+ // Merged into the source stage, the dynamic clauses would run before its
+ // aggregation; as their own stage they act on its result columns.
const dynamicStage = dynamicQuery && toResultColumnStageSpec(dynamicQuery);
validateUniqueColumnNames(
@@ -181,7 +176,7 @@ function toStageSpec(input: QueryInput): TestStageWithSourceSpec {
}
return {
- source: { type: "card", id: input.source.id },
+ source: { type: "card", id: getQuestionId(input.source) },
...toResultColumnStageSpec(input),
};
}
@@ -261,15 +256,14 @@ function toResultColumnArgSpec(spec: TestOperatorArgSpec): TestOperatorArgSpec {
async function loadSourceMetadata(
store: SdkStore,
input: QueryInput,
- publishedCard: Card | undefined,
+ sourceCard: Card | undefined,
) {
- if (publishedCard) {
- await loadCardQueryMetadata(store, publishedCard.id);
- return;
- }
+ if (isQuestionInput(input)) {
+ const cardId = getQuestionId(input.source);
- if (input.source.type === "card") {
- await loadCardMetadata(store, input.source.id);
+ await (sourceCard
+ ? loadCardQueryMetadata(store, cardId)
+ : loadCardMetadata(store, cardId));
return;
}
@@ -299,13 +293,22 @@ function loadCardQueryMetadata(store: SdkStore, id: number) {
);
}
-function getSourceDatabaseId(input: QueryInput, state: SdkState) {
+function getSourceDatabaseId(
+ input: QueryInput,
+ state: SdkState,
+ sourceCard: Card | undefined,
+) {
if (isTableInput(input)) {
return getTableDatabaseId(input.source.id, state);
}
if (isQuestionInput(input)) {
- return getCardDatabaseId(input.source.id, state);
+ const cardId = getQuestionId(input.source);
+
+ return getCardDatabaseId(
+ cardId,
+ sourceCard ?? selectCard({ id: cardId })(state).data,
+ );
}
throw new Error("Unable to find database for query source.");
@@ -323,8 +326,7 @@ function getTableDatabaseId(tableId: number, state: SdkState) {
throw new Error(`Unable to find database for table ${tableId}.`);
}
-function getCardDatabaseId(cardId: number, state: SdkState) {
- const { data: card } = selectCard({ id: cardId })(state);
+function getCardDatabaseId(cardId: number, card: Card | undefined) {
const databaseId = card?.dataset_query?.database;
if (typeof databaseId === "number") {
diff --git a/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/validation.ts b/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/validation.ts
index d30e666ba9c..e20aa22867d 100644
--- a/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/validation.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/create-metabase-query/validation.ts
@@ -192,7 +192,7 @@ function validateQuestionResultColumn(
if (!source.columns.some(({ name }) => name === columnName)) {
throw new Error(
- `${context} must reference a result column of saved question ${source.id}, but received ${columnName}.`,
+ `${context} must reference a result column of saved question ${source.id ?? source.entityId}, but received ${columnName}.`,
);
}
}
diff --git a/frontend/src/embedding-sdk-bundle/lib/execute-action.ts b/frontend/src/embedding-sdk-bundle/lib/execute-action.ts
index 64251ae1ee3..876155cc0d7 100644
--- a/frontend/src/embedding-sdk-bundle/lib/execute-action.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/execute-action.ts
@@ -5,7 +5,7 @@ import type {
SdkActionInput,
} from "embedding-sdk-bundle/types/action";
import { executeAction as executeActionMutation } from "metabase/api/action";
-import { isDataApp, isDataAppDev } from "metabase/embedding-sdk/config";
+import { isDataApp } from "metabase/embedding-sdk/config";
import type {
BaseEntityId,
ParametersForActionExecution,
@@ -50,33 +50,18 @@ const isActionDefinition = (
input: SdkActionInput,
): input is SdkActionDefinition => isObject(input) && "action" in input;
-/**
- * The action that actually runs. Outside the dev preview the app's copy
- * replaces the authored action: the copy is what grants an app's viewers
- * permission to run it, through the app's collection.
- */
function toExecutableActionId(input: SdkActionInput): SdkActionId {
- if (!isActionDefinition(input)) {
- if (isDataApp()) {
- throw new Error(
- `Action ${input} was passed to \`useAction\` as a raw id. A data app must pass the \`defineAction(...)\` export, so the app's copy of the action runs.`,
- );
- }
-
- return input;
- }
-
- if (isDataAppDev() || !isDataApp()) {
+ if (isActionDefinition(input)) {
return input.action.id;
}
- if (input.copiedActionEntityId === undefined) {
+ if (isDataApp()) {
throw new Error(
- "This action has no copy. Set its `copiedActionEntityId` in `defineAction(...)` in `actions/`, run `npm run write-resources` and `npm run check-resources`, commit, and rebuild.",
+ `Action ${input} was passed to \`useAction\` as a raw id. A data app must pass a \`defineAction(...)\` definition from its typed schema.`,
);
}
- return input.copiedActionEntityId;
+ return input;
}
/**
diff --git a/frontend/src/embedding-sdk-bundle/lib/execute-action.unit.spec.ts b/frontend/src/embedding-sdk-bundle/lib/execute-action.unit.spec.ts
index 08ff6c7f25a..38f49bc8af2 100644
--- a/frontend/src/embedding-sdk-bundle/lib/execute-action.unit.spec.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/execute-action.unit.spec.ts
@@ -31,74 +31,33 @@ describe("executeAction", () => {
activeStore = undefined;
fetchMock.removeRoutes().clearHistory();
EMBEDDING_SDK_CONFIG.isDataApp = false;
- EMBEDDING_SDK_CONFIG.isDataAppDev = false;
});
- describe("an action definition with a copied action", () => {
- const AUTHORED_ID = 51;
- const COPIED_ENTITY_ID = "copiedActionEntity091";
- const definition = {
- action: { id: AUTHORED_ID },
- copiedActionEntityId: COPIED_ENTITY_ID,
- };
-
- const expectExecuted = async (
- id: number | string,
- actionId: Parameters<ReturnType<typeof executeAction>>[0]["actionId"],
- ) => {
- fetchMock.post(`path:/api/action/${id}/execute`, {
+ describe("an action definition", () => {
+ const ACTION_ENTITY_ID = "appActionEntity000091";
+ const definition = { action: { id: ACTION_ENTITY_ID } };
+
+ it("runs the action its definition names in a data app", async () => {
+ EMBEDDING_SDK_CONFIG.isDataApp = true;
+ fetchMock.post(`path:/api/action/${ACTION_ENTITY_ID}/execute`, {
status: 200,
body: { "rows-affected": 1 },
});
- await executeAction(setup())({ actionId });
+ await executeAction(setup())({ actionId: definition });
expect(
- fetchMock.callHistory.calls(`path:/api/action/${id}/execute`),
+ fetchMock.callHistory.calls(
+ `path:/api/action/${ACTION_ENTITY_ID}/execute`,
+ ),
).toHaveLength(1);
- };
-
- it("runs the copy in a production build", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
-
- await expectExecuted(COPIED_ENTITY_ID, definition);
- });
-
- it("runs the authored action in the dev preview", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- await expectExecuted(AUTHORED_ID, definition);
- });
-
- it("runs the authored action outside a data app, where no copy exists", async () => {
- await expectExecuted(AUTHORED_ID, { action: { id: AUTHORED_ID } });
- });
-
- it("refuses a definition without a copied action in a production build", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
-
- await expect(
- executeAction(setup())({ actionId: { action: { id: AUTHORED_ID } } }),
- ).rejects.toThrow(
- "This action has no copy. Set its `copiedActionEntityId` in `defineAction(...)` in `actions/`",
- );
});
it("refuses a raw id inside a data app", async () => {
EMBEDDING_SDK_CONFIG.isDataApp = true;
await expect(
- executeAction(setup())({ actionId: AUTHORED_ID }),
- ).rejects.toThrow("passed to `useAction` as a raw id");
- });
-
- it("refuses a raw id in the dev preview, before it can reach production", async () => {
- EMBEDDING_SDK_CONFIG.isDataApp = true;
- EMBEDDING_SDK_CONFIG.isDataAppDev = true;
-
- await expect(
- executeAction(setup())({ actionId: AUTHORED_ID }),
+ executeAction(setup())({ actionId: NUMERIC_ID }),
).rejects.toThrow("passed to `useAction` as a raw id");
});
});
diff --git a/frontend/src/embedding-sdk-bundle/types/action.ts b/frontend/src/embedding-sdk-bundle/types/action.ts
index 3518f60d034..f3eef8aec4f 100644
--- a/frontend/src/embedding-sdk-bundle/types/action.ts
+++ b/frontend/src/embedding-sdk-bundle/types/action.ts
@@ -4,14 +4,12 @@ import type { SdkEntityId } from "embedding-sdk-bundle/types/entity";
export type SdkActionId = number | SdkEntityId;
/**
- * How a data app names an action: the `defineAction` export, which is what a
- * data app must pass. `copiedActionEntityId` addresses the copy in the app's own
- * collection, the only one its viewers can read. The dev preview runs `action`
- * instead, so an app works before its resources are written.
+ * How a data app names an action: a `defineAction` definition from the app's
+ * typed schema, whose `action.id` is the entity ID of an action in the app's
+ * collection.
*/
export type SdkActionDefinition = {
action: { id: SdkActionId };
- copiedActionEntityId?: SdkEntityId;
};
export type SdkActionInput = SdkActionId | SdkActionDefinition;
diff --git a/frontend/src/embedding-sdk-shared/lib/create-metabase-query/input-guards.ts b/frontend/src/embedding-sdk-shared/lib/create-metabase-query/input-guards.ts
index f7603d0018f..9a16790c30a 100644
--- a/frontend/src/embedding-sdk-shared/lib/create-metabase-query/input-guards.ts
+++ b/frontend/src/embedding-sdk-shared/lib/create-metabase-query/input-guards.ts
@@ -20,7 +20,6 @@ export type TableQueryInput = Omit<TestStageWithSourceSpec, "source"> & {
source: TableSchema;
limit?: number;
enabled?: boolean;
- savedQuestionEntityId?: string;
};
/**
@@ -81,7 +80,9 @@ export const isQuestionInput = (input: unknown): input is QuestionQueryInput =>
isObject(input) && "source" in input && isQuestionReference(input.source);
export const isQuestionReference = (value: unknown): value is QuestionSchema =>
- isObject(value) && typeof value.id === "number" && value.type === "card";
+ isObject(value) &&
+ value.type === "card" &&
+ (typeof value.id === "number" || typeof value.entityId === "string");
export const isQueryInput = (input: unknown): input is QueryInput =>
isTableInput(input) || isQuestionInput(input);
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.ts
index 9e2c26d9ebb..c8956c7067e 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
@@ -60,10 +60,12 @@ export type MetricSchema = {
dimensions?: Record<string, Record<string, FieldSchema>>;
};
+/**
+ * A saved question, by its id or by its entity ID.
+ */
export type QuestionSchema = {
type: "card";
- id: number;
name?: string;
display?: string;
columns?: readonly SchemaColumn[];
-};
+} & ({ id: number; entityId?: string } | { id?: never; entityId: string });
diff --git a/frontend/src/metabase/urls/data-apps.ts b/frontend/src/metabase/urls/data-apps.ts
index 2929bd0684d..f816fb69245 100644
--- a/frontend/src/metabase/urls/data-apps.ts
+++ b/frontend/src/metabase/urls/data-apps.ts
@@ -20,7 +20,7 @@ export const DATA_APP_EMBED_PREFIX = `/embed/${DATA_APP_URL_SEGMENT}`;
* its own CSP granting `'unsafe-eval'`, which is what lets the data-app document
* itself drop that grant.
*/
-export const DATA_APP_REALM_HOST_URL = "/api/apps/sandbox-host";
+export const DATA_APP_REALM_HOST_URL = "/api/ee/apps/sandbox-host";
export function dataApp(name: string) {
return `${DATA_APP_ROOT_URL}/${encodeURIComponent(name)}`;
diff --git a/frontend/test/__support__/server-mocks/data-app.ts b/frontend/test/__support__/server-mocks/data-app.ts
index f89c92c12c8..c4c33d23185 100644
--- a/frontend/test/__support__/server-mocks/data-app.ts
+++ b/frontend/test/__support__/server-mocks/data-app.ts
@@ -3,25 +3,25 @@ import fetchMock from "fetch-mock";
import type { DataApp, DataAppRepoStatus } from "metabase-types/api";
export function setupListDataAppsEndpoint(apps: DataApp[]) {
- fetchMock.get("path:/api/apps", apps);
+ fetchMock.get("path:/api/ee/apps", apps);
}
export function setupListDataAppsEndpointWithError(error: string) {
- fetchMock.get("path:/api/apps", { status: 500, body: error });
+ fetchMock.get("path:/api/ee/apps", { status: 500, body: error });
}
export function setupDataAppRepoStatusEndpoint(status: DataAppRepoStatus) {
- fetchMock.get("path:/api/apps/repo-status", status);
+ fetchMock.get("path:/api/ee/apps/repo-status", status);
}
export function setupDataAppRepoStatusEndpointWithError(error: string) {
- fetchMock.get("path:/api/apps/repo-status", { status: 500, body: error });
+ fetchMock.get("path:/api/ee/apps/repo-status", { status: 500, body: error });
}
export function setupUpdateDataAppEndpoint(app: DataApp) {
- fetchMock.put(`path:/api/apps/${app.name}`, app);
+ fetchMock.put(`path:/api/ee/apps/${app.name}`, app);
}
export function setupDeleteDataAppEndpoint(name: string) {
- fetchMock.delete(`path:/api/apps/${name}`, 204);
+ fetchMock.delete(`path:/api/ee/apps/${name}`, 204);
}
diff --git a/rspack.embedding-sdk-node.config.js b/rspack.embedding-sdk-node.config.js
index 7e0dd253081..0eb0bcf3d37 100644
--- a/rspack.embedding-sdk-node.config.js
+++ b/rspack.embedding-sdk-node.config.js
@@ -70,7 +70,6 @@ const cliConfig = {
library: { type: "commonjs2" },
},
resolve: sharedResolve,
- externals: ["esbuild"],
module: sharedModule,
plugins: [
new rspack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }),
@@ -107,7 +106,6 @@ const dataAppDevConfig = {
"@vitejs/plugin-react",
"vite-plugin-svgr",
"vite-plugin-css-injected-by-js",
- "esbuild",
],
externalsType: "module",
output: {
diff --git a/src/metabase/actions/models.clj b/src/metabase/actions/models.clj
index 96fca1ccaa1..afbf32b29d2 100644
--- a/src/metabase/actions/models.clj
+++ b/src/metabase/actions/models.clj
@@ -166,10 +166,8 @@
(when (some #(contains? (t2/changes <>) %) [:collection_id :model_id])
(check-collection-content <>))
(let [in-data-app? (and (some? (:collection_id <>)) (perms/data-app-collection? (:collection_id <>)))]
- ;; an export leaves out an archived action, and the next pull would then delete it from every instance
(when (and in-data-app? (changed? :archived) (:archived <>))
(throw (ex-info (tru "An action in a data app''s collection can''t be archived.") {:status-code 400})))
- ;; an export writes the link into the action's file, which every pull then refuses
(when (and in-data-app? (changed? :public_uuid) (:public_uuid <>))
(throw (ex-info (tru "An action in a data app''s collection can''t be made public.") {:status-code 400})))))))
@@ -275,7 +273,6 @@
"Throws when `query`, of an action in the Collection with `collection-id`, reads a card outside that collection
while the collection is a data app's."
[collection-id query]
- ;; the next export writes the action into the app's files, and a pull refuses one that reads a card outside
(when (and (some? collection-id)
(perms/data-app-collection? collection-id))
(let [card-ids (query-card-ids query)
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index cd616561298..90051525cf6 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -376,8 +376,6 @@
([instance]
(and (not (default-audit-collection? instance))
(not (is-trash-or-descendant? instance))
- ;; not even an administrator edits a data app's collection: only a pull of its app changes it
- (not= (keyword (:namespace instance)) data-apps-ns)
(mi/current-user-has-full-permissions? :write instance)
(remote-sync/collection-editable? instance)))
([_model pk]
@@ -624,11 +622,8 @@
(defn check-allowed-content
"Checks the content of a collection before saving it. Throws when the collection with `collection-id` can't hold
- content of `model-type`."
+ content of `model-type`; a data app's collection holds only what a remote-sync pull of the app accepts."
[model-type collection-id]
- ;; a data app's collection holds only what a pull of the app accepts; a bookmark is a user's pointer, not content.
- ;; A load checks every question as a possible dashboard question before its dashboard is known, so that is a
- ;; question here.
(when (and collection-id
(some? model-type)
(not= :model/CollectionBookmark model-type)
@@ -1885,7 +1880,6 @@
:new-location new-location})))
(when (= (:type collection) tenant-specific-root-collection-type)
(throw (ex-info "Can't move a tenant collection" {:status-code 400})))
- ;; an export writes a data app's collection under another as a file every pull refuses
(when (perms/data-app-collection? (:id collection))
(throw (ex-info "You cannot move a data app's collection." {:status-code 400})))
;; first move this Collection
@@ -2112,8 +2106,6 @@
(api/check
(not (is-trash? collection-before-updates))
[400 "You cannot modify the Trash Collection."])
- ;; an export leaves out a trashed collection, which would delete the app's resource files, and writes an
- ;; official one into a file every pull refuses
(api/check
(not (and (or (:archived collection-updates) (some? (:authority_level collection-updates)))
(perms/data-app-collection? (:id collection-before-updates))))
diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.clj
index 3a69eef497d..cf761ffa325 100644
--- a/src/metabase/permissions/db.clj
+++ b/src/metabase/permissions/db.clj
@@ -549,11 +549,6 @@
[collection-id :- ::lib.schema.id/collection]
(t2/select-one :model/Collection :id collection-id))
-(mu/defn collection-namespace
- "The namespace of the Collection with `collection-id`."
- [collection-id :- ::lib.schema.id/collection]
- (t2/select-one-fn :namespace :model/Collection :id collection-id))
-
(mu/defn personal-or-descendant-collection-ids
"The IDs among `collection-ids` that are personal Collections, or descendants of one."
[collection-ids :- [:set ::lib.schema.id/collection]]
diff --git a/src/metabase/permissions/models/permissions.clj b/src/metabase/permissions/models/permissions.clj
index ee4c19501f0..5a9bf1b80a0 100644
--- a/src/metabase/permissions/models/permissions.clj
+++ b/src/metabase/permissions/models/permissions.clj
@@ -307,27 +307,13 @@
(derive ::mi/read-policy.full-perms-for-perms-set)
(derive ::mi/write-policy.full-perms-for-perms-set))
-(defn data-apps-namespace-collection?
- "Is `collection-or-id` a collection of the data apps namespace? Reads the namespace from the app DB unless given a
- collection with `:namespace`."
- [collection-or-id]
- (= "data-apps"
- (some-> (if (and (map? collection-or-id) (contains? collection-or-id :namespace))
- (:namespace collection-or-id)
- ;; the root collection has no ID, and is the default namespace's
- (some-> (u/id collection-or-id) permissions.db/collection-namespace))
- name)))
-
(defmethod mi/perms-objects-set :perms/use-parent-collection-perms
[instance read-or-write]
- (let [collection (or (:collection instance) (:collection_id instance))]
- (if (or (= read-or-write :read)
- ;; not even an administrator edits what a data app's collection holds: only a pull of its app changes it
- (and (not (data-apps-namespace-collection? collection))
- (remote-sync/collection-editable? collection)))
- (perms-objects-set-for-parent-collection (:collection_id instance) read-or-write)
- ;; a path that no permissions set holds, not even an administrator's `/`
- #{"___no-write-access"})))
+ (if (or (= read-or-write :read)
+ (remote-sync/collection-editable? (or (:collection instance) (:collection_id instance))))
+ (perms-objects-set-for-parent-collection (:collection_id instance) read-or-write)
+ ;; a path no permissions set holds, not even an admin's `/`, so nobody writes what remote sync keeps read-only
+ #{"___no-remote-sync-access"}))
(methodical/defmethod t2/batched-hydrate [:perms/use-parent-collection-perms :can_write]
[_model k models]
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index 3c4b71891f4..52cf8f28a76 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -996,9 +996,8 @@
(:type card)))
(defn- check-data-app-card
- "Throws unless `card`, when it is in a data app's collection, is what a pull of the app accepts: a question or
- metric that is not archived, public or embedded, reading no card outside the collection. The next export writes
- the card into the app's files, and every pull refuses a file that says otherwise."
+ "Throws unless `card`, when it is in a data app's collection, is a question or metric that is not archived, public
+ or embedded, reading no card outside the collection, which is what a remote-sync pull of the app accepts."
[{:keys [collection_id] :as card}]
(when (and (some? collection_id)
(perms/data-app-collection? collection_id))
diff --git a/src/metabase/request/util.clj b/src/metabase/request/util.clj
index fdf2c0d74ba..6e64859e875 100644
--- a/src/metabase/request/util.clj
+++ b/src/metabase/request/util.clj
@@ -41,10 +41,8 @@
(re-matches #"^/embed/.*$" uri))
(def data-app-url-segment
- "URL path segment identifying data-app routes. Single source of truth on the
- BE: the `/apps` page route, the `/embed/apps` iframe prefix, and the
- `/api/apps` API mount all derive from it (mirrors the FE
- `DATA_APP_URL_SEGMENT`)."
+ "URL path segment of data-app pages: the `/apps` page route and the `/embed/apps` iframe prefix derive from it
+ (mirrors the FE `DATA_APP_URL_SEGMENT`)."
"apps")
(def data-app-embed-prefix
diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.clj
index 43fec63621c..6dba27e9990 100644
--- a/src/metabase/server/middleware/security.clj
+++ b/src/metabase/server/middleware/security.clj
@@ -276,7 +276,7 @@
(str "http://localhost:" cljs-dev-port)])
;; NOTE: data-app routes deliberately do NOT get 'unsafe-eval'.
;; Near-Membrane needs `eval` to run the app bundle, but it runs it
- ;; inside a realm iframe served from `/api/apps/sandbox-host`, whose
+ ;; inside a realm iframe served from `/api/ee/apps/sandbox-host`, whose
;; own CSP grants 'unsafe-eval' for that document alone. Granting it
;; here instead would also hand `eval`/`Function` to anything running
;; in the data-app document itself — including guest code that escaped
diff --git a/test/metabase/actions/models_test.clj b/test/metabase/actions/models_test.clj
index fc462b71e73..53a2e245703 100644
--- a/test/metabase/actions/models_test.clj
+++ b/test/metabase/actions/models_test.clj
@@ -252,8 +252,8 @@
(is (=? {:model_id nil :collection_id app-id}
(action/select-action :id (insert! app-id)))))))))))
-(deftest action-in-a-data-apps-collection-is-read-only-test
- (testing "Nobody writes an action in a collection of the data apps namespace, administrators included"
+(deftest action-in-a-data-apps-collection-is-admin-only-test
+ (testing "Only administrators write an action in a collection of the data apps namespace"
(mt/with-temp [:model/Collection {app-id :id} {:namespace "data-apps"}
:model/Collection {coll-id :id} {:namespace "data-actions"}]
(mt/with-model-cleanup [:model/Action]
@@ -267,14 +267,15 @@
action (action/select-action :id (action/insert! (action-in app-id)))
elsewhere (action/select-action :id (action/insert! (action-in coll-id)))]
(mt/with-test-user :crowberto
+ (is (true? (mi/can-write? action)))
+ (is (true? (:can_write (t2/hydrate action :can_write))))
+ (is (true? (mi/can-create? :model/Action (action-in app-id))))
+ (is (true? (mi/can-update? elsewhere {:collection_id app-id}))))
+ (mt/with-test-user :rasta
(is (false? (mi/can-write? action)))
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."
(api/write-check :model/Action (:id action))))
- (is (false? (:can_write (t2/hydrate action :can_write))))
- (testing "nor creates one there, or moves one in"
- (is (false? (mi/can-create? :model/Action (action-in app-id))))
- (is (true? (mi/can-write? elsewhere)))
- (is (false? (mi/can-update? elsewhere {:collection_id app-id}))))))))))
+ (is (false? (mi/can-create? :model/Action (action-in app-id))))))))))
(deftest implicit-action-requires-model-test
(mt/test-drivers (mt/normal-drivers-with-feature :actions/custom)
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index c8434f8878d..c50e2094631 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -3349,10 +3349,13 @@
(is (true? (mi/can-read? dashboards)))
(is (false? (mi/can-write? dashboards)))))))))
-(deftest data-apps-collection-is-read-only-test
- (testing "Nobody writes a collection of the data apps namespace, administrators included"
+(deftest data-apps-collection-is-admin-only-test
+ (testing "Only administrators write a collection of the data apps namespace"
(mt/with-temp [:model/Collection collection {:namespace "data-apps"}]
(mt/with-test-user :crowberto
+ (is (true? (mi/can-write? collection)))
+ (is (true? (mi/can-write? :model/Collection (:id collection)))))
+ (mt/with-test-user :rasta
(is (false? (mi/can-write? collection)))
(is (false? (mi/can-write? :model/Collection (:id collection))))))))
diff --git a/test/metabase/permissions/models/permissions_test.clj b/test/metabase/permissions/models/permissions_test.clj
index da78f4d9295..fa77a4aa6b1 100644
--- a/test/metabase/permissions/models/permissions_test.clj
+++ b/test/metabase/permissions/models/permissions_test.clj
@@ -308,17 +308,17 @@
(testing "admin can create dashboard in root collection"
(is (true? (mi/can-create? :model/Dashboard {}))))))))
-(deftest content-of-a-data-apps-collection-is-read-only-test
- (testing "Nobody writes what takes its permissions from a collection of the data apps namespace, administrators included"
+(deftest content-of-a-data-apps-collection-is-admin-only-test
+ (testing "Only administrators write what takes its permissions from a collection of the data apps namespace"
(mt/with-temp [:model/Collection {collection-id :id} {:namespace "data-apps"}]
- (mt/with-test-user :crowberto
- ;; unsaved, since the namespace refuses both
- (doseq [model [:model/Dashboard :model/Timeline]
- :let [instance (t2/instance model {:collection_id collection-id})]]
- (testing model
+ (doseq [model [:model/Dashboard :model/Timeline]
+ :let [instance (t2/instance model {:collection_id collection-id})]]
+ (testing model
+ (mt/with-test-user :crowberto
+ (is (true? (mi/can-write? instance))))
+ (mt/with-test-user :rasta
(is (false? (mi/can-write? instance)))
- (testing "while reading still follows the collection's permissions"
- (is (true? (mi/can-read? instance))))))))))
+ (is (false? (mi/can-read? instance)))))))))
(deftest ^:parallel can-read-via-parent-collection?-test
(testing "read perm is true iff the current user has read on the parent collection"
diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.clj
index cb1d64666ff..807a4f7b26a 100644
--- a/test/metabase/queries/models/card_test.clj
+++ b/test/metabase/queries/models/card_test.clj
@@ -1180,16 +1180,18 @@
(is (false? (mi/can-read? card)))
(is (false? (mi/can-write? card)))))))))
-(deftest card-in-a-data-apps-collection-is-read-only-test
- (testing "Nobody writes a card in a collection of the data apps namespace, administrators included"
+(deftest card-in-a-data-apps-collection-is-admin-only-test
+ (testing "Only administrators write a card in a collection of the data apps namespace"
(mt/with-temp [:model/Collection {collection-id :id} {:namespace "data-apps"}
:model/Card card {:collection_id collection-id}]
(mt/with-test-user :crowberto
+ (is (true? (mi/can-write? card)))
+ (is (true? (mi/can-write? :model/Card (:id card))))
+ (is (true? (:can_write (t2/hydrate card :can_write))))
+ (is (true? (mi/can-create? :model/Card {:collection_id collection-id}))))
+ (mt/with-test-user :rasta
(is (false? (mi/can-write? card)))
- (is (false? (mi/can-write? :model/Card (:id card))))
- (is (false? (:can_write (t2/hydrate card :can_write))))
- (testing "nor creates one there"
- (is (false? (mi/can-create? :model/Card {:collection_id collection-id}))))))))
+ (is (false? (mi/can-create? :model/Card {:collection_id collection-id})))))))
(deftest ^:parallel breakouts->identifier->action-fn-test
(are [b1 b2 expected-identifier->action] (=? expected-identifier->action
diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.clj
index dfc45dfe4f0..c9e4c439533 100644
--- a/test/metabase/server/middleware/security_test.clj
+++ b/test/metabase/server/middleware/security_test.clj
@@ -196,7 +196,7 @@
(deftest data-app-unsafe-eval-test
;; Near-Membrane needs `eval` to run the app bundle, but it runs it inside a realm
- ;; iframe served from `/api/apps/sandbox-host`, which carries the grant in its own
+ ;; iframe served from `/api/ee/apps/sandbox-host`, which carries the grant in its own
;; CSP. Granting it on the data-app document too would hand `eval`/`Function` to
;; anything running there — including guest code that escaped the membrane.
(testing "the data-app document does not get 'unsafe-eval'"