Skip to content

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

Remove redundant table id from segment and measure API (#77716)

Alexander Polyankin30e421908239
diff --git a/e2e/support/helpers/e2e-table-metadata-helpers.js b/e2e/support/helpers/e2e-table-metadata-helpers.js
index d10a5921bea..051ea67fbf2 100644
--- a/e2e/support/helpers/e2e-table-metadata-helpers.js
+++ b/e2e/support/helpers/e2e-table-metadata-helpers.js
@@ -1,21 +1,14 @@
/**
* @param {object} segment
* @param {string} segment.name
- * @param {TableId} segment.table_id
* @param {string|null} [segment.description]
* @param {object} segment.definition
*/
-export const createSegment = ({
- name,
- table_id,
- definition,
- description = null,
-}) => {
+export const createSegment = ({ name, definition, description = null }) => {
cy.log(`Create a segment: ${name}`);
return cy.request("POST", "/api/segment", {
name,
description,
- table_id,
definition,
});
};
@@ -23,21 +16,14 @@ export const createSegment = ({
/**
* @param {object} measure
* @param {string} measure.name
- * @param {TableId} measure.table_id
* @param {string|null} [measure.description]
* @param {object} measure.definition
*/
-export const createMeasure = ({
- name,
- table_id,
- definition,
- description = null,
-}) => {
+export const createMeasure = ({ name, definition, description = null }) => {
cy.log(`Create a measure: ${name}`);
return cy.request("POST", "/api/measure", {
name,
description,
- table_id,
definition,
});
};
@@ -46,21 +32,19 @@ export const createMeasure = ({
* @param {object} measure
* @param {MeasureId} measure.id
* @param {string} [measure.name]
- * @param {TableId} [measure.table_id]
* @param {string|null} [measure.description]
* @param {object} [measure.definition]
* @param {object} [options]
* @param {string|null} [options.revision_message]
*/
export const updateMeasure = (
- { id, name, table_id, definition, description = null },
+ { id, name, definition, description = null },
{ revision_message = "Update measure" } = {},
) => {
cy.log(`Update a measure: ${id}`);
return cy.request("PUT", `/api/measure/${id}`, {
name,
description,
- table_id,
definition,
revision_message,
});
diff --git a/e2e/test/scenarios/admin/databases.cy.spec.js b/e2e/test/scenarios/admin/databases.cy.spec.js
index 5dbe9b0e5bc..79711e62827 100644
--- a/e2e/test/scenarios/admin/databases.cy.spec.js
+++ b/e2e/test/scenarios/admin/databases.cy.spec.js
@@ -674,7 +674,6 @@ describe("scenarios > admin > databases > sample database", () => {
H.createSegment({
name: "Small orders",
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/admin/datamodel/reproductions.cy.spec.ts b/e2e/test/scenarios/admin/datamodel/reproductions.cy.spec.ts
index ff7479bde1f..f762ea7c584 100644
--- a/e2e/test/scenarios/admin/datamodel/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/admin/datamodel/reproductions.cy.spec.ts
@@ -243,7 +243,6 @@ describe("issues 55617, 55618", () => {
cy.intercept("GET", "/api/segment").as("getSegments");
H.createSegment({
name: "My segment",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/admin/datamodel/segments.cy.spec.ts b/e2e/test/scenarios/admin/datamodel/segments.cy.spec.ts
index 926ae0e5cae..6f82f53810d 100644
--- a/e2e/test/scenarios/admin/datamodel/segments.cy.spec.ts
+++ b/e2e/test/scenarios/admin/datamodel/segments.cy.spec.ts
@@ -101,7 +101,6 @@ describe("scenarios > admin > datamodel > segments", () => {
H.createSegment({
name: SEGMENT_NAME,
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: 1,
@@ -346,7 +345,6 @@ describe("scenarios > admin > datamodel > segments", () => {
H.createSegment({
name: "Foo",
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: 1,
@@ -403,7 +401,6 @@ describe("scenarios > admin > datamodel > segments", () => {
H.createSegment({
name: SEGMENT_IN_ORDERS,
description: "Hey oh",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: 1,
@@ -417,7 +414,6 @@ describe("scenarios > admin > datamodel > segments", () => {
H.createSegment({
name: SEGMENT_IN_PEOPLE,
description: "This is a segment in the PEOPLE table",
- table_id: PEOPLE_ID,
definition: {
type: "query",
database: 1,
diff --git a/e2e/test/scenarios/custom-column/custom-column-reproductions-1.cy.spec.js b/e2e/test/scenarios/custom-column/custom-column-reproductions-1.cy.spec.js
index 712df8f07f9..effd5cb1865 100644
--- a/e2e/test/scenarios/custom-column/custom-column-reproductions-1.cy.spec.js
+++ b/e2e/test/scenarios/custom-column/custom-column-reproductions-1.cy.spec.js
@@ -636,7 +636,6 @@ describe("issue 24922", () => {
const segmentDetails = {
name: "OrdersSegment",
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js b/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js
index 0e95f9292c5..1c60f11ca20 100644
--- a/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js
+++ b/e2e/test/scenarios/custom-column/custom-column-reproductions-2.cy.spec.js
@@ -305,7 +305,6 @@ describe("issue 55300", () => {
H.createSegment({
name: "now",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
@@ -315,7 +314,6 @@ describe("issue 55300", () => {
H.createSegment({
name: "Count",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/dashboard/dashboard-reproductions.cy.spec.js b/e2e/test/scenarios/dashboard/dashboard-reproductions.cy.spec.js
index 580cbcac622..66288e1580c 100644
--- a/e2e/test/scenarios/dashboard/dashboard-reproductions.cy.spec.js
+++ b/e2e/test/scenarios/dashboard/dashboard-reproductions.cy.spec.js
@@ -841,7 +841,6 @@ describe("issue 31697", () => {
const segmentDetails = {
name: "Orders segment",
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/data-studio/data-model/segments-data-studio.cy.spec.ts b/e2e/test/scenarios/data-studio/data-model/segments-data-studio.cy.spec.ts
index 2750ae9eb68..1b9fb654cb5 100644
--- a/e2e/test/scenarios/data-studio/data-model/segments-data-studio.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/data-model/segments-data-studio.cy.spec.ts
@@ -404,7 +404,6 @@ describe(
cy.log("create segment on Products table");
H.createSegment({
name: "Expensive Products",
- table_id: PRODUCTS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -441,7 +440,6 @@ describe(
cy.log("create segment based on segment");
H.createSegment({
name: "High Value Recent Orders",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -475,7 +473,6 @@ describe(
cy.log("create Segment A");
H.createSegment({
name: "Segment A",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -488,7 +485,6 @@ describe(
cy.log("create Segment B that depends on A");
H.createSegment({
name: "Segment B",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -739,7 +735,6 @@ function createTestSegment(
H.createSegment({
name,
description,
- table_id: tableId,
definition: {
type: "query",
database: SAMPLE_DB_ID,
diff --git a/e2e/test/scenarios/data-studio/data-model/source-replacement.cy.spec.ts b/e2e/test/scenarios/data-studio/data-model/source-replacement.cy.spec.ts
index a569d7a5b31..22b67f4e9c3 100644
--- a/e2e/test/scenarios/data-studio/data-model/source-replacement.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/data-model/source-replacement.cy.spec.ts
@@ -1053,7 +1053,6 @@ function createHighAmountSegment() {
(amountFieldId) =>
H.createSegment({
name: "High amount",
- table_id: sourceTableId,
definition: {
type: "query",
database: WRITABLE_DB_ID,
@@ -1073,7 +1072,6 @@ function createSourceTotalAmountMeasure() {
(amountFieldId) =>
H.createMeasure({
name: "Total amount",
- table_id: sourceTableId,
definition: {
"source-table": sourceTableId,
aggregation: [["sum", ["field", amountFieldId, null]]],
diff --git a/e2e/test/scenarios/data-studio/measures/measures-data-studio.cy.spec.ts b/e2e/test/scenarios/data-studio/measures/measures-data-studio.cy.spec.ts
index f85f039cb7c..f911e02539a 100644
--- a/e2e/test/scenarios/data-studio/measures/measures-data-studio.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/measures/measures-data-studio.cy.spec.ts
@@ -529,7 +529,6 @@ function createTestMeasure(
H.createMeasure({
name,
description,
- table_id: tableId,
definition: {
type: "query",
database: SAMPLE_DB_ID,
diff --git a/e2e/test/scenarios/data-studio/measures/measures-published-tables.cy.spec.ts b/e2e/test/scenarios/data-studio/measures/measures-published-tables.cy.spec.ts
index 5a0002caae5..8e4f5eafd16 100644
--- a/e2e/test/scenarios/data-studio/measures/measures-published-tables.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/measures/measures-published-tables.cy.spec.ts
@@ -140,7 +140,6 @@ function createTestMeasure(opts: { name?: string; description?: string } = {}) {
H.createMeasure({
name,
description,
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
diff --git a/e2e/test/scenarios/data-studio/measures/measures-queries.cy.spec.ts b/e2e/test/scenarios/data-studio/measures/measures-queries.cy.spec.ts
index 99dcf945f6d..0beaa968329 100644
--- a/e2e/test/scenarios/data-studio/measures/measures-queries.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/measures/measures-queries.cy.spec.ts
@@ -107,7 +107,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should create a measure based on a segment", () => {
H.createSegment({
name: "TotalSegment",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
filter: ["<", ["field", ORDERS.TOTAL, null], 100],
@@ -129,7 +128,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should create a measure based on another measure with an identity expression", () => {
H.createMeasure({
name: "TotalMeasure",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -151,7 +149,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should create a measure based on another measure", () => {
H.createMeasure({
name: "TotalMeasure",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -173,7 +170,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should create a measure based on another measure with an identity expression", () => {
H.createMeasure({
name: "TotalMeasure",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -254,7 +250,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to create measures with filters like CountIf based on segments", () => {
H.createSegment({
name: "LargeTotal",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -280,7 +275,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to create measures with filters like based on segments that are nested", () => {
H.createSegment({
name: "LargeTotal",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -292,7 +286,6 @@ describe("scenarios > data studio > measures > queries", () => {
}).then(({ body: segment }) => {
H.createSegment({
name: "NestedSegment",
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
@@ -323,7 +316,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to offset a measure in a query", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -349,7 +341,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to order by an aggregation using a measure directly", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -375,7 +366,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to order by an aggregation using a custom expression based on a measure", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -409,7 +399,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to use results of a measure in follow up stages", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -449,7 +438,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to join on a measure in a follow up stage", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -478,7 +466,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to join on a measure in a follow up stage with a custom expression", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -512,7 +499,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to rename a measure without breaking queries that reference it", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -533,7 +519,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to rename an aggregation expression based on a measure without breaking it", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -556,7 +541,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("changing the top-level aggregation expression in a measure might break queries that reference it in follow up stages", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
@@ -613,7 +597,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to use a measure in a pivot table", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -660,7 +643,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to use measure results in a saved question as source for a follow up question", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -714,7 +696,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible to use measure results in a saved question as source for a follow up model", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["sum", ["field", ORDERS.TOTAL, null]]],
@@ -769,7 +750,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible x-ray a question containing a measure", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
@@ -803,7 +783,6 @@ describe("scenarios > data studio > measures > queries", () => {
it("should be possible x-ray a model containing a measure", () => {
H.createMeasure({
name: MEASURE_NAME,
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/data-studio/published-tables-segments.cy.spec.ts b/e2e/test/scenarios/data-studio/published-tables-segments.cy.spec.ts
index df8a6548a94..fec0d459b2b 100644
--- a/e2e/test/scenarios/data-studio/published-tables-segments.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/published-tables-segments.cy.spec.ts
@@ -144,7 +144,6 @@ function createTestSegment(opts: { name?: string; description?: string } = {}) {
H.createSegment({
name,
description,
- table_id: ORDERS_ID,
definition: {
type: "query",
database: SAMPLE_DB_ID,
diff --git a/e2e/test/scenarios/data-studio/table-collection-permissions.cy.spec.ts b/e2e/test/scenarios/data-studio/table-collection-permissions.cy.spec.ts
index 1a11502dce4..2f4944fe134 100644
--- a/e2e/test/scenarios/data-studio/table-collection-permissions.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/table-collection-permissions.cy.spec.ts
@@ -130,7 +130,6 @@ describe("scenarios > data studio > table collection permissions", () => {
it("should create a question with a table segment", () => {
H.createSegment({
name: "ID segment",
- table_id: PRODUCTS_ID,
definition: {
database: SAMPLE_DB_ID,
type: "query",
diff --git a/e2e/test/scenarios/dependencies/dependency-graph.cy.spec.ts b/e2e/test/scenarios/dependencies/dependency-graph.cy.spec.ts
index 1d4ec56bb36..b7dd6687f37 100644
--- a/e2e/test/scenarios/dependencies/dependency-graph.cy.spec.ts
+++ b/e2e/test/scenarios/dependencies/dependency-graph.cy.spec.ts
@@ -1100,7 +1100,6 @@ function createTableBasedSegment({ tableId }: { tableId: TableId }) {
return H.createSegment({
name: TABLE_BASED_SEGMENT_NAME,
description: "Segment description",
- table_id: tableId,
definition: {
"source-table": tableId,
filter: ["=", 1, 1],
@@ -1118,7 +1117,6 @@ function createSegmentBasedSegment({
return H.createSegment({
name: SEGMENT_BASED_SEGMENT_NAME,
description: "Segment description",
- table_id: tableId,
definition: {
"source-table": tableId,
filter: ["segment", segmentId],
@@ -1228,7 +1226,6 @@ function createDocumentWithTableBasedQuestion({
function createTableBasedMeasure({ tableId }: { tableId: TableId }) {
return H.createMeasure({
name: TABLE_BASED_MEASURE_NAME,
- table_id: tableId,
definition: {
"source-table": tableId,
aggregation: [["count"]],
@@ -1245,7 +1242,6 @@ function createSegmentBaseMeasure({
}) {
return H.createMeasure({
name: SEGMENT_BASED_MEASURE_NAME,
- table_id: tableId,
definition: {
"source-table": tableId,
aggregation: [["count-where", ["segment", segmentId]]],
@@ -1341,7 +1337,6 @@ function createMeasureBasedMeasure({
}) {
return H.createMeasure({
name: MEASURE_BASED_MEASURE_NAME,
- table_id: tableId,
definition: {
"source-table": tableId,
aggregation: ["+", 1, ["measure", measureId]],
diff --git a/e2e/test/scenarios/dependencies/dependency-unreferenced-list.cy.spec.ts b/e2e/test/scenarios/dependencies/dependency-unreferenced-list.cy.spec.ts
index f8d387211be..71faf42ede8 100644
--- a/e2e/test/scenarios/dependencies/dependency-unreferenced-list.cy.spec.ts
+++ b/e2e/test/scenarios/dependencies/dependency-unreferenced-list.cy.spec.ts
@@ -949,7 +949,6 @@ function createSegmentWithTableDataSource({
}) {
return H.createSegment({
name,
- table_id: tableId,
definition: {
"source-table": tableId,
filter: [["=", "A", "A"]],
@@ -968,7 +967,6 @@ function createSegmentWithSegmentClause({
}) {
return H.createSegment({
name,
- table_id: tableId,
definition: {
"source-table": tableId,
filter: ["segment", segmentId],
diff --git a/e2e/test/scenarios/filters/filter-bulk.cy.spec.js b/e2e/test/scenarios/filters/filter-bulk.cy.spec.js
index 05be77929bc..fc34e7e9297 100644
--- a/e2e/test/scenarios/filters/filter-bulk.cy.spec.js
+++ b/e2e/test/scenarios/filters/filter-bulk.cy.spec.js
@@ -254,7 +254,6 @@ describe("scenarios > filters > bulk filtering", () => {
H.createSegment({
name: SEGMENT_1_NAME,
description: "All orders with a total under $100.",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
@@ -265,7 +264,6 @@ describe("scenarios > filters > bulk filtering", () => {
H.createSegment({
name: SEGMENT_2_NAME,
description: "All orders with a discount",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
aggregation: [["count"]],
diff --git a/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts b/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
index 0dbad7e3287..d9dbcf342a6 100644
--- a/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
+++ b/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
@@ -2009,7 +2009,6 @@ describe("scenarios > metrics > explorer", () => {
H.createSegment({
name: SEGMENT_NAME,
description: "Orders with a total over $100",
- table_id: ORDERS_ID,
definition: {
"source-table": ORDERS_ID,
filter: [">", ["field", ORDERS.TOTAL, null], 100],
@@ -2387,7 +2386,6 @@ function createTestMeasure(
H.createMeasure({
name,
description,
- table_id: tableId,
definition: {
type: "query",
database: SAMPLE_DB_ID,
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index 1d1e4e8cf8a..6fb6fee29fe 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -999,14 +999,12 @@
;; Create measure A (base measure) via API
{measure-a-id :id} (mt/user-http-request :crowberto :post 200 "measure"
{:name "Measure A"
- :table_id products-id
:definition (-> (lib/query mp products)
(lib/aggregate (lib/sum price)))})
mp' (mt/metadata-provider)
;; Create measure B that depends on measure A
{measure-b-id :id} (mt/user-http-request :crowberto :post 200 "measure"
{:name "Measure B"
- :table_id products-id
:definition (-> (lib/query mp' products)
(lib/aggregate (lib/* (lib.metadata/measure mp' measure-a-id)
2)))})
@@ -1014,7 +1012,6 @@
;; Create measure C that depends on measure B
{measure-c-id :id} (mt/user-http-request :crowberto :post 200 "measure"
{:name "Measure C"
- :table_id products-id
:definition (-> (lib/query mp'' products)
(lib/aggregate (lib/+ (lib.metadata/measure mp'' measure-b-id)
100)))})]
diff --git a/frontend/src/metabase-types/api/measure.ts b/frontend/src/metabase-types/api/measure.ts
index a955667916a..8420eee8519 100644
--- a/frontend/src/metabase-types/api/measure.ts
+++ b/frontend/src/metabase-types/api/measure.ts
@@ -55,7 +55,6 @@ export interface Measure {
export interface CreateMeasureRequest {
name: string;
- table_id: TableId;
definition: DatasetQuery;
description?: string;
}
diff --git a/frontend/src/metabase-types/api/segment.ts b/frontend/src/metabase-types/api/segment.ts
index 403ad6ae4a2..39cd10a09d4 100644
--- a/frontend/src/metabase-types/api/segment.ts
+++ b/frontend/src/metabase-types/api/segment.ts
@@ -25,7 +25,6 @@ export interface Segment {
export interface CreateSegmentRequest {
name: string;
- table_id: TableId;
definition: DatasetQuery;
description?: string;
}
diff --git a/frontend/src/metabase/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx b/frontend/src/metabase/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
index e51a01a374d..993bc554bff 100644
--- a/frontend/src/metabase/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
+++ b/frontend/src/metabase/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
@@ -77,7 +77,6 @@ export function NewMeasurePage({
}
const { data: measure, error } = await createMeasure({
name: name.trim(),
- table_id: table.id,
definition: definition,
description: description.trim() || undefined,
});
diff --git a/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.tsx b/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.tsx
index 81bbcdbdf3e..04b5f69fb39 100644
--- a/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.tsx
+++ b/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.tsx
@@ -73,7 +73,6 @@ export function NewSegmentPage({
}
const { data: segment, error } = await createSegment({
name: name.trim(),
- table_id: table.id,
definition: definition,
description: description.trim() || undefined,
});
diff --git a/src/metabase/measures/api.clj b/src/metabase/measures/api.clj
index b3a59e33db2..1e67362bf4d 100644
--- a/src/metabase/measures/api.clj
+++ b/src/metabase/measures/api.clj
@@ -9,6 +9,7 @@
[metabase.metrics.core :as metrics]
[metabase.models.interface :as mi]
[metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
@@ -37,40 +38,50 @@
Accepts MBQL4 definitions for Cypress e2e test support:
- MBQL5 full queries (passed through)
- MBQL4 full queries (converted to MBQL5)
- - MBQL4 fragments (wrapped in full query, then converted to MBQL5)"
- [definition table-id database-id]
+ - MBQL4 fragments (wrapped in full query, then converted to MBQL5); the fragment must
+ include `:source-table` so the table can be derived"
+ [definition]
(if (seq definition)
(-> (case (lib/normalized-mbql-version definition)
(:mbql-version/mbql5 :mbql-version/legacy)
definition
;; default: MBQL4 fragment - wrap it in a full query
- {:database database-id
- :type :query
- :query (merge {:source-table table-id} definition)})
+ (let [table-id (:source-table definition)
+ _ (api/check-400 (pos-int? table-id)
+ (tru "Measure definition must specify a source table."))
+ database-id (t2/select-one-fn :db_id :model/Table :id table-id)]
+ {:database database-id
+ :type :query
+ :query definition}))
lib-be/normalize-query)
{}))
+(defn- definition-table-id
+ "Derive the source table ID from a normalized measure definition, or throw a 400 if it has none."
+ [normalized-definition]
+ (api/check-400 (when (seq normalized-definition)
+ (lib/primary-source-table-id normalized-definition))
+ (tru "Measure definition must specify a source table.")))
+
(api.macros/defendpoint :post "/" :- ::measure
- "Create a new `Measure`."
+ "Create a new `Measure`. The Measure's table is derived from its `definition`."
[_route-params
_query-params
- {:keys [name description table_id definition], :as body} :- [:map
- [:name ms/NonBlankString]
- [:table_id ms/PositiveInt]
- [:definition ms/Map]
- [:description {:optional true} [:maybe :string]]]]
- (api/create-check :model/Measure body)
- (let [database-id (t2/select-one-fn :db_id :model/Table :id table_id)
- normalized-definition (normalize-input-definition definition table_id database-id)
- measure (api/check-500
- (first (t2/insert-returning-instances! :model/Measure
- :table_id table_id
- :creator_id api/*current-user-id*
- :name name
- :description description
- :definition normalized-definition)))]
- (events/publish-event! :event/measure-create {:object measure :user-id api/*current-user-id*})
- (t2/hydrate measure :creator)))
+ {:keys [name description definition], :as body} :- [:map
+ [:name ms/NonBlankString]
+ [:definition ms/Map]
+ [:description {:optional true} [:maybe :string]]]]
+ (let [normalized-definition (normalize-input-definition definition)
+ table-id (definition-table-id normalized-definition)]
+ (api/create-check :model/Measure (assoc body :table_id table-id))
+ (let [measure (api/check-500
+ (first (t2/insert-returning-instances! :model/Measure
+ :creator_id api/*current-user-id*
+ :name name
+ :description description
+ :definition normalized-definition)))]
+ (events/publish-event! :event/measure-create {:object measure :user-id api/*current-user-id*})
+ (t2/hydrate measure :creator))))
(mu/defn- hydrated-measure [id :- ms/PositiveInt]
(api/read-check (t2/select-one :model/Measure :id id))
@@ -101,14 +112,19 @@
:present #{:description}
:non-nil #{:archived :definition :name})
new-def (when-let [def (:definition clean-body)]
- (let [table-id (:table_id existing)
- database-id (t2/select-one-fn :db_id :model/Table :id table-id)]
- (normalize-input-definition def table-id database-id)))
+ (normalize-input-definition def))
new-body (merge
(dissoc clean-body :revision_message)
(when new-def {:definition new-def}))
changes (when-not (= new-body existing)
new-body)]
+ ;; An updated definition must still specify a source table; if it implicitly moves the Measure to a different
+ ;; table, the write-check above checked the old table, so also make sure the user could create a Measure on the
+ ;; new one.
+ (when new-def
+ (let [new-table-id (definition-table-id new-def)]
+ (when (not= new-table-id (:table_id existing))
+ (api/create-check :model/Measure {:table_id new-table-id}))))
(when changes
(t2/update! :model/Measure id changes))
(u/prog1 (hydrated-measure id)
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index 25ae76fa505..1e45c13dbd9 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -346,25 +346,35 @@
(mu/defn populate-query-fields
"Lift `database_id`, `table_id`, `query_type`, and `source_card_id` fields
- from query definition when inserting/updating a Card."
- [{query :dataset_query, :as card} :- ::queries.schema/card]
- (merge
- card
- (when (and (seq query) (map? query))
- (merge
- ;; This used to be conditional on not nilling source-card-id, changed due to #68080.
- {:source_card_id (source-card-id query)}
- (when-let [{:keys [database-id table-id]} (query/query->database-and-table-ids query)]
- ;; TODO -- not sure `query_type` is actually used for anything important anyway
- (let [query-type (if (query/query-is-native? query)
- :native
- :query)]
- (merge
- {:query_type (keyword query-type)}
- (when database-id
- {:database_id database-id})
- (when table-id
- {:table_id table-id}))))))))
+ from query definition when inserting/updating a Card.
+
+ `clear-stale-table-id?` (default true) controls whether a nil derived table id overwrites an existing `:table_id`.
+ Pass false when the query itself is not changing, so that an unrelated update (rename, archive, ...) doesn't wipe
+ a previously-valid table_id just because the derivation can no longer resolve it (e.g. the source card was
+ deleted)."
+ ([card]
+ (populate-query-fields card true))
+ ([{query :dataset_query, :as card} :- ::queries.schema/card
+ clear-stale-table-id? :- :boolean]
+ (merge
+ card
+ (when (and (seq query) (map? query))
+ (merge
+ ;; This used to be conditional on not nilling source-card-id, changed due to #68080.
+ {:source_card_id (source-card-id query)}
+ (when-let [{:keys [database-id table-id]} (query/query->database-and-table-ids query)]
+ ;; TODO -- not sure `query_type` is actually used for anything important anyway
+ (let [query-type (if (query/query-is-native? query)
+ :native
+ :query)]
+ (merge
+ {:query_type (keyword query-type)}
+ ;; a stale table_id gets cleared when the query no longer has a source table (e.g. it was converted to
+ ;; native SQL), same as :source_card_id above
+ (when (or table-id clear-stale-table-id?)
+ {:table_id table-id})
+ (when database-id
+ {:database_id database-id})))))))))
;;; TODO -- move this to [[metabase.query-processor.card]] or Lib so the logic can be shared between the backend and
;;; frontend (?)
@@ -855,8 +865,9 @@
(apply-dashboard-question-updates changes)
(m/update-existing :dataset_query lib-be/normalize-query)
(populate-result-metadata changes verified-result-metadata?)
- ;; populate-query-fields must run before pre-update in case source_card_id should be nilled
- populate-query-fields
+ ;; populate-query-fields must run before pre-update in case source_card_id should be nilled.
+ ;; Only allow it to nil out a stale table_id when the query itself is changing.
+ (populate-query-fields (contains? changes :dataset_query))
(pre-update changes)
maybe-populate-initially-published-at)))
diff --git a/src/metabase/segments/api.clj b/src/metabase/segments/api.clj
index 87e899b36e7..192efc85024 100644
--- a/src/metabase/segments/api.clj
+++ b/src/metabase/segments/api.clj
@@ -4,8 +4,11 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.events.core :as events]
+ [metabase.lib-be.core :as lib-be]
+ [metabase.lib.core :as lib]
[metabase.models.interface :as mi]
[metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -15,27 +18,41 @@
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
+(defn- definition-table-id
+ "Derive the source table ID from a segment definition, or throw a 400 if it has none. Handles MBQL5 and legacy full
+ queries as well as MBQL4 fragments (which carry `:source-table` directly)."
+ [definition]
+ (api/check-400 (when (seq definition)
+ (case (lib/normalized-mbql-version definition)
+ (:mbql-version/mbql5 :mbql-version/legacy)
+ (lib/primary-source-table-id (lib-be/normalize-query definition))
+ ;; default: MBQL4 fragment
+ (let [table-id (:source-table definition)]
+ (when (pos-int? table-id)
+ table-id))))
+ (tru "Segment definition must specify a source table.")))
+
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :post "/"
- "Create a new `Segment`."
+ "Create a new `Segment`. The Segment's table is derived from its `definition`."
[_route-params
_query-params
- {:keys [name description table_id definition], :as body} :- [:map
- [:name ms/NonBlankString]
- [:table_id ms/PositiveInt]
- [:definition ms/Map]
- [:description {:optional true} [:maybe :string]]]]
+ {:keys [name description definition], :as body} :- [:map
+ [:name ms/NonBlankString]
+ [:definition ms/Map]
+ [:description {:optional true} [:maybe :string]]]]
;; TODO - why can't we set other properties like `show_in_getting_started` when we create the Segment?
- (api/create-check :model/Segment body)
- (let [segment (api/check-500
- (first (t2/insert-returning-instances! :model/Segment
- :table_id table_id
- :creator_id api/*current-user-id*
- :name name
- :description description
- :definition definition)))]
- (events/publish-event! :event/segment-create {:object segment :user-id api/*current-user-id*})
- (t2/hydrate segment :creator)))
+ (let [table-id (definition-table-id definition)]
+ (api/create-check :model/Segment (assoc body :table_id table-id))
+ (let [segment (api/check-500
+ (first (t2/insert-returning-instances! :model/Segment
+ :table_id table-id
+ :creator_id api/*current-user-id*
+ :name name
+ :description description
+ :definition definition)))]
+ (events/publish-event! :event/segment-create {:object segment :user-id api/*current-user-id*})
+ (t2/hydrate segment :creator))))
(mu/defn- hydrated-segment [id :- ms/PositiveInt]
(-> (api/read-check (t2/select-one :model/Segment :id id))
@@ -75,6 +92,13 @@
new-body (dissoc clean-body :revision_message)
changes (when-not (= new-body existing)
new-body)]
+ ;; An updated definition must still specify a source table; if it implicitly moves the Segment to a different
+ ;; table, the write-check above checked the old table, so also make sure the user could create a Segment on the
+ ;; new one.
+ (when-let [definition (:definition new-body)]
+ (let [new-table-id (definition-table-id definition)]
+ (when (not= new-table-id (:table_id existing))
+ (api/create-check :model/Segment {:table_id new-table-id}))))
(when changes
(t2/update! :model/Segment id changes))
(u/prog1 (hydrated-segment id)
diff --git a/test/metabase/measures/api_test.clj b/test/metabase/measures/api_test.clj
index df6b0bc2d18..b3a214e50fb 100644
--- a/test/metabase/measures/api_test.clj
+++ b/test/metabase/measures/api_test.clj
@@ -49,31 +49,32 @@
(testing "POST /api/measure"
(testing "Test security. Requires superuser perms."
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "measure" {:name "abc"
- :table_id (mt/id :venues)
- :definition {}}))))))
+ (mt/user-http-request :rasta :post 403 "measure"
+ {:name "abc"
+ :definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))}))))))
(deftest create-measure-input-validation-test
(testing "POST /api/measure"
(is (=? {:errors {:name "value must be a non-blank string."}}
(mt/user-http-request :crowberto :post 400 "measure" {})))
- (is (=? {:errors {:table_id "value must be an integer greater than zero."}}
- (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"})))
- (is (=? {:errors {:table_id "value must be an integer greater than zero."}}
- (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"
- :table_id "foobar"})))
(is (=? {:errors {:definition "Value must be a map."}}
- (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"
- :table_id 123})))
+ (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"})))
(is (=? {:errors {:definition "Value must be a map."}}
(mt/user-http-request :crowberto :post 400 "measure" {:name "abc"
- :table_id 123
- :definition "foobar"})))))
+ :definition "foobar"})))
+ (testing "definition must specify a source table"
+ (is (= "Measure definition must specify a source table."
+ (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"
+ :definition {}})))
+ (is (= "Measure definition must specify a source table."
+ (mt/user-http-request :crowberto :post 400 "measure" {:name "abc"
+ :definition {:aggregation [[:count]]}}))))))
(deftest create-measure-test
(testing "POST /api/measure"
(is (=? {:name "A Measure"
:description "I did it!"
+ :table_id (mt/id :venues)
:creator_id (mt/user->id :crowberto)
:creator {:id (mt/user->id :crowberto)}
:entity_id string?
@@ -84,7 +85,6 @@
(mt/user-http-request :crowberto :post 200 "measure"
{:name "A Measure"
:description "I did it!"
- :table_id (mt/id :venues)
:definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))})))))
;; ## PUT /api/measure
@@ -114,6 +114,24 @@
:revision_message "123"
:definition "foobar"})))))
+(deftest update-definition-table-test
+ (testing "PUT /api/measure/:id"
+ (testing "an updated definition must still specify a source table"
+ (mt/with-temp [:model/Measure {:keys [id]} {:table_id (mt/id :venues)
+ :definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))}]
+ (is (= "Measure definition must specify a source table."
+ (mt/user-http-request :crowberto :put 400 (str "measure/" id)
+ {:revision_message "no more source table"
+ :definition {}})))))
+ (testing "a definition that moves the Measure to another table keeps table_id in sync"
+ (mt/with-temp [:model/Measure {:keys [id]} {:table_id (mt/id :venues)
+ :definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))}]
+ (mt/user-http-request :crowberto :put 200 (str "measure/" id)
+ {:revision_message "move to checkins"
+ :definition (mbql5-measure-definition (mt/id :checkins) (mt/id :checkins :user_id))})
+ (is (= (mt/id :checkins)
+ (t2/select-one-fn :table_id :model/Measure :id id)))))))
+
(deftest update-test
(testing "PUT /api/measure/:id"
(mt/with-temp [:model/Measure {:keys [id]} {:table_id (mt/id :venues)
@@ -132,7 +150,6 @@
{:id id
:name "Updated Measure"
:description nil
- :table_id (mt/id :venues)
:revision_message "I got me some revisions"
:definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))}))))))
@@ -195,7 +212,6 @@
(is (= "You don't have permissions to do that."
(mt/user-http-request :lucky :post 403 "measure"
{:name "New Measure"
- :table_id (mt/id :venues)
:definition (mbql5-measure-definition (mt/id :venues) (mt/id :venues :price))}))))
(testing "PUT /api/measure/:id is still blocked"
(is (= "You don't have permissions to do that."
@@ -304,7 +320,6 @@
(let [response (mt/user-http-request
:crowberto :post 200 "measure"
{:name "Fragment Measure"
- :table_id (mt/id :venues)
:definition (mbql4-fragment-definition (mt/id :venues) [[:count]])})]
(is (some? (:id response)))
(is (mbql5-definition? (:definition response))
@@ -313,7 +328,6 @@
(let [response (mt/user-http-request
:crowberto :post 200 "measure"
{:name "Full Query Measure"
- :table_id (mt/id :venues)
:definition (legacy-mbql-measure-definition (mt/id) (mt/id :venues) [[:count]])})]
(is (some? (:id response)))
(is (mbql5-definition? (:definition response))
@@ -346,7 +360,6 @@
(let [{measure-id :id} (mt/user-http-request
:crowberto :post 200 "measure"
{:name "Venue Count"
- :table_id (mt/id :venues)
:definition (mbql4-fragment-definition (mt/id :venues) [[:count]])})
measure-query (mt/mbql-query venues {:aggregation [[:measure measure-id]]})
direct-query (mt/mbql-query venues {:aggregation [[:count]]})]
diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.clj
index 12cc5b2b9c2..adf42aac8f6 100644
--- a/test/metabase/queries/models/card_test.clj
+++ b/test/metabase/queries/models/card_test.clj
@@ -1314,6 +1314,36 @@
(testing "converted question should survive"
(is (t2/exists? :model/Card question-id))))))
+(deftest table-id-cleared-on-conversion-to-native-test
+ (testing "table_id should be set to nil when a question is converted to native SQL"
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/mbql-query venues)}]
+ (is (= (mt/id :venues) (t2/select-one-fn :table_id :model/Card :id card-id)))
+ (t2/update! :model/Card card-id
+ {:dataset_query (mt/native-query {:query "SELECT * FROM venues"})})
+ (is (nil? (t2/select-one-fn :table_id :model/Card :id card-id)))))
+ (testing "table_id should be set to nil when the source becomes a native model with no table"
+ (mt/with-temp [:model/Card {model-id :id} {:type :model
+ :dataset_query (mt/native-query {:query "SELECT 1"})}
+ :model/Card {card-id :id} {:dataset_query (mt/mbql-query venues)}]
+ (t2/update! :model/Card card-id
+ {:dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (str "card__" model-id)}}})
+ (let [card (t2/select-one :model/Card :id card-id)]
+ (is (= model-id (:source_card_id card)))
+ (is (nil? (:table_id card))))))
+ (testing "table_id should be preserved on updates that don't touch the query, even when it can no longer be derived"
+ (mt/with-temp [:model/Card {model-id :id} {:type :model
+ :dataset_query (mt/mbql-query venues)}
+ :model/Card {card-id :id} {:dataset_query {:database (mt/id)
+ :type :query
+ :query {:source-table (str "card__" model-id)}}}]
+ (is (= (mt/id :venues) (t2/select-one-fn :table_id :model/Card :id card-id)))
+ ;; hard-delete the source card so the table can no longer be derived from the query
+ (t2/delete! :model/Card :id model-id)
+ (t2/update! :model/Card card-id {:name "Renamed, query untouched"})
+ (is (= (mt/id :venues) (t2/select-one-fn :table_id :model/Card :id card-id))))))
+
(deftest assert-no-source-card-id-for-native-query-test
(testing "assertion fires if native query has source_card_id set"
(with-redefs [card/populate-query-fields identity]
diff --git a/test/metabase/segments/api_test.clj b/test/metabase/segments/api_test.clj
index 3c6c3d82bf7..21690384cd8 100644
--- a/test/metabase/segments/api_test.clj
+++ b/test/metabase/segments/api_test.clj
@@ -60,26 +60,26 @@
(testing "POST /api/segment"
(testing "Test security. Requires superuser perms."
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "segment" {:name "abc"
- :table_id (mt/id :users)
- :definition {}}))))))
+ (mt/user-http-request :rasta :post 403 "segment"
+ {:name "abc"
+ :definition (mbql4-segment-definition (mt/id :users) (mt/id :users :id) 20)}))))))
(deftest create-segment-input-validation-test
(testing "POST /api/segment"
(is (=? {:errors {:name "value must be a non-blank string."}}
(mt/user-http-request :crowberto :post 400 "segment" {})))
- (is (=? {:errors {:table_id "value must be an integer greater than zero."}}
- (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"})))
- (is (=? {:errors {:table_id "value must be an integer greater than zero."}}
- (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"
- :table_id "foobar"})))
(is (=? {:errors {:definition "Value must be a map."}}
- (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"
- :table_id 123})))
+ (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"})))
(is (=? {:errors {:definition "Value must be a map."}}
(mt/user-http-request :crowberto :post 400 "segment" {:name "abc"
- :table_id 123
- :definition "foobar"})))))
+ :definition "foobar"})))
+ (testing "definition must specify a source table"
+ (is (= "Segment definition must specify a source table."
+ (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"
+ :definition {}})))
+ (is (= "Segment definition must specify a source table."
+ (mt/user-http-request :crowberto :post 400 "segment" {:name "abc"
+ :definition {:filter [:> [:field (mt/id :users :id) nil] 0]}}))))))
(deftest create-segment-test
(doseq [[format-name definition-fn] {"MBQL4" (partial mbql4-segment-definition (mt/id :users))
@@ -103,10 +103,19 @@
:show_in_getting_started false
:caveats nil
:points_of_interest nil
- :table_id (mt/id :users)
:definition (definition-fn (mt/id :users :id) 20)})
segment-response))))))
+(deftest create-segment-derives-table-id-test
+ (testing "POST /api/segment derives table_id from the definition"
+ (doseq [[format-name definition-fn] {"MBQL4" (partial mbql4-segment-definition (mt/id :users))
+ "MBQL5" (partial mbql5-segment-definition (mt/id :users))}]
+ (testing format-name
+ (is (=? {:table_id (mt/id :users)}
+ (mt/user-http-request :crowberto :post 200 "segment"
+ {:name "A Segment"
+ :definition (definition-fn (mt/id :users :id) 20)})))))))
+
;; ## PUT /api/segment
(deftest update-permissions-test
@@ -133,6 +142,24 @@
:revision_message "123"
:definition "foobar"})))))
+(deftest update-definition-table-test
+ (testing "PUT /api/segment/:id"
+ (testing "an updated definition must still specify a source table"
+ (mt/with-temp [:model/Segment {:keys [id]} {:table_id (mt/id :users)
+ :definition (mbql4-segment-definition (mt/id :users) (mt/id :users :name) "cans")}]
+ (is (= "Segment definition must specify a source table."
+ (mt/user-http-request :crowberto :put 400 (str "segment/" id)
+ {:revision_message "no more source table"
+ :definition {}})))))
+ (testing "a definition that moves the Segment to another table keeps table_id in sync"
+ (mt/with-temp [:model/Segment {:keys [id]} {:table_id (mt/id :users)
+ :definition (mbql4-segment-definition (mt/id :users) (mt/id :users :name) "cans")}]
+ (mt/user-http-request :crowberto :put 200 (str "segment/" id)
+ {:revision_message "move to venues"
+ :definition (mbql4-segment-definition (mt/id :venues) (mt/id :venues :name) "cans")})
+ (is (= (mt/id :venues)
+ (t2/select-one-fn :table_id :model/Segment :id id)))))))
+
(deftest update-test
(testing "PUT /api/segment/:id"
(mt/with-temp [:model/Segment {:keys [id]} {:table_id (mt/id :users)
@@ -160,7 +187,6 @@
:show_in_getting_started false
:caveats nil
:points_of_interest nil
- :table_id (mt/id :users)
:revision_message "I got me some revisions"
:definition (eq-fn (mt/id :users :name) "cans")})
segment-response))))))))
@@ -172,8 +198,7 @@
;; just make sure API call doesn't barf
(is (some? (mt/user-http-request :crowberto :put 200 (str "segment/" (u/the-id segment))
{:name "Cool name"
- :revision_message "WOW HOW COOL"
- :definition {}})))))))
+ :revision_message "WOW HOW COOL"})))))))
(deftest update-with-full-legacy-query-test
(testing "PUT /api/segment/:id"
diff --git a/test/metabase/xrays/automagic_dashboards/core_test.clj b/test/metabase/xrays/automagic_dashboards/core_test.clj
index 84b36d633cc..2d2d2d24efd 100644
--- a/test/metabase/xrays/automagic_dashboards/core_test.clj
+++ b/test/metabase/xrays/automagic_dashboards/core_test.clj
@@ -1613,7 +1613,9 @@
[:model/Card {native-card-id :id :as native-card} (merge (mt/card-with-source-metadata-for-query native-query)
{:table_id nil
:name "15655"})
- :model/Card card {:table_id (mt/id :orders) ; this is wrong (!)
+ ;; a wrong table_id used to be part of this repro (set by the FE, see #15655); table_id is now always
+ ;; derived from the query, so this card gets table_id nil (its source is a native card)
+ :model/Card card {:table_id (mt/id :orders) ; ignored: cleared since the query has no source table
:dataset_query {:query {:source-table (format "card__%s" native-card-id)
:aggregation [[:count]]
:breakout [[:field "SOURCE" {:base-type :type/Text}]]}
@@ -1647,16 +1649,16 @@
transient_name))
(is (= "Automatically generated comparison dashboard comparing Number of 15655 where SOURCE is Affiliate and \"15655\", all 15655"
comparison-description))
- (is (= [{:group-name nil, :card-name "Number of 15655 per SOURCE"}
- {:group-name nil, :card-name "Number of 15655 per SOURCE"}
- {:group-name nil, :card-name "Number of 15655 per CITY"}
- {:group-name nil, :card-name "Number of 15655 per CITY"}
- {:group-name nil, :card-name "Number of 15655 per NAME"}
- {:group-name nil, :card-name "Number of 15655 per NAME"}
- {:group-name nil, :card-name "Number of 15655 per SOURCE over time"}
- {:group-name nil, :card-name "Number of 15655 per SOURCE over time"}
- {:group-name nil, :card-name "Number of 15655 per CITY over time"}
- {:group-name nil, :card-name "Number of 15655 per CITY over time"}]
+ (is (= [{:group-name nil, :card-name "SOURCE by CITY"}
+ {:group-name nil, :card-name "SOURCE by CITY"}
+ {:group-name nil, :card-name "SOURCE by NAME"}
+ {:group-name nil, :card-name "SOURCE by NAME"}
+ {:group-name "### How the SOURCE fields is distributed", :card-name nil}
+ {:group-name nil, :card-name "Distinct values"}
+ {:group-name nil, :card-name "Distinct values"}
+ {:group-name nil, :card-name "How the SOURCE is distributed (Number of 15655 where SOURCE is Affiliate)"}
+ {:group-name nil, :card-name "Null values"}
+ {:group-name nil, :card-name "Null values"}]
(->> comparison-dashcards
(take 10)
(map (fn [dashcard]