The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Keep entity identity stable when a write changes nothing (#80976)
Romeo Van Snick0a601f07c6d8
diff --git a/frontend/src/metabase/redux/entities/entities.unit.spec.ts b/frontend/src/metabase/redux/entities/entities.unit.spec.tsnew file mode 100644index 00000000000..a5812f8bcee--- /dev/null+++ b/frontend/src/metabase/redux/entities/entities.unit.spec.ts@@ -0,0 +1,56 @@+import { reducer } from "./index";++const update = (fields: Record<string, unknown>) => ({+ type: "metabase/entities/UPDATE",+ payload: { entities: { fields } },+});++const FIELD = {+ id: 1,+ name: "ID",+ display_name: "ID",+ base_type: "type/BigInteger",+};++describe("entities reducer", () => {+ it("keeps the slice and its entries identical when a write changes nothing", () => {+ const before = reducer(undefined, update({ 1: FIELD }));+ const after = reducer(before, update({ 1: { ...FIELD } }));++ expect(after).toBe(before);+ expect(after.fields).toBe(before.fields);+ expect(after.fields[1]).toBe(before.fields[1]);+ });++ it("keeps untouched entries identical when a sibling changes", () => {+ const before = reducer(+ undefined,+ update({ 1: FIELD, 2: { ...FIELD, id: 2, name: "TOTAL" } }),+ );+ const after = reducer(before, update({ 2: { id: 2, name: "SUBTOTAL" } }));++ expect(after.fields).not.toBe(before.fields);+ expect(after.fields[1]).toBe(before.fields[1]);+ expect(after.fields[2]).not.toBe(before.fields[2]);+ expect(after.fields[2]).toMatchObject({ id: 2, name: "SUBTOTAL" });+ });++ it("shallow-merges so a partial write does not drop existing keys", () => {+ const before = reducer(undefined, update({ 1: FIELD }));+ const after = reducer(before, update({ 1: { id: 1, description: "pk" } }));++ expect(after.fields[1]).toMatchObject({+ ...FIELD,+ description: "pk",+ });+ });++ it("deletes an entry written as null, and only then copies the slice", () => {+ const before = reducer(undefined, update({ 1: FIELD }));+ const unchanged = reducer(before, update({ 999: null }));+ const deleted = reducer(before, update({ 1: null }));++ expect(unchanged.fields).toBe(before.fields);+ expect(deleted.fields[1]).toBeUndefined();+ });+});diff --git a/frontend/src/metabase/redux/entities/index.ts b/frontend/src/metabase/redux/entities/index.tsindex 6b317db36f9..e3a23ce81b2 100644--- a/frontend/src/metabase/redux/entities/index.ts+++ b/frontend/src/metabase/redux/entities/index.ts@@ -3,7 +3,9 @@ import { getIn } from "icepick";import { tablesReducer } from "./tables-reducer";-type SliceState = Record<string, unknown>;+/** One normalized record. Its keys are whatever the endpoint returned. */+type Entity = Record<string, unknown>;+type SliceState = Record<string, Entity>;type SliceAction = { type: string; payload?: any };type SliceReducer = Reducer<SliceState>;@@ -28,28 +30,55 @@ const ENTITY_SLICE_NAMES = [const ACTION_PATTERN = /^metabase\/entities\//;+function hasSameValues(before: Entity, after: Entity): boolean {+ const keys = Object.keys(after);+ return (+ keys.length === Object.keys(before).length &&+ keys.every((key) => before[key] === after[key])+ );+}+/*** Merges newEntities into entities, deleting keys whose value is nullish.* Existing entries are shallow-merged so partial entities don't overwrite* full ones.+ *+ * Returns the same slice, and the same entries within it, when the merge+ * changes nothing. Endpoints re-normalize the same records often, and a fresh+ * object each time would rebuild `getMetadata` and every metabase-lib metadata+ * provider derived from it.*/function mergeEntities(entities: SliceState,- newEntities: Record<string, unknown>,+ newEntities: Record<string, Entity | null>,): SliceState {- const result = { ...entities };+ let result = entities;++ const copyOnce = () => {+ if (result === entities) {+ result = { ...entities };+ }+ return result;+ };+for (const id of Object.keys(newEntities)) {const entry = newEntities[id];+if (entry == null) {- delete result[id];- } else {- result[id] = {- ...(result[id] ?? {}),- // Unjustified type cast. FIXME- ...(entry as Record<string, unknown>),- };+ if (id in result) {+ delete copyOnce()[id];+ }+ continue;}++ const existing: Entity | undefined = result[id];+ const merged = { ...existing, ...entry };+ if (existing != null && hasSameValues(existing, merged)) {+ continue;+ }+ copyOnce()[id] = merged;}+return result;}@@ -64,9 +93,10 @@ function makeSliceReducer(): SliceReducer {return (state = {}, action: SliceAction) => {let nextState = state;- // Unjustified type cast. FIXME+ // `getIn` walks an untyped action payload, so it can only return+ // `unknown`. The shape is the normalizr output for this slice.const entities = getIn(action, ["payload", "entities", sliceName]) as- | Record<string, unknown>+ | Record<string, Entity | null>| undefined;if (ACTION_PATTERN.test(action.type) && entities) {nextState = mergeEntities(nextState, entities);diff --git a/frontend/src/metabase/redux/entities/tables-reducer.ts b/frontend/src/metabase/redux/entities/tables-reducer.tsindex 5aa90f17e8d..db837dbee0f 100644--- a/frontend/src/metabase/redux/entities/tables-reducer.ts+++ b/frontend/src/metabase/redux/entities/tables-reducer.ts@@ -101,6 +101,11 @@ export function tablesReducer() {let nextState = state;for (const updated of Object.values<any>(payload.entities.fields)) {+ // A null entry is a delete, so there is nothing to sync into+ // `original_fields`.+ if (updated == null) {+ continue;+ }const tableId = updated.table_id;const table = nextState[tableId];if (!table?.original_fields) {diff --git a/frontend/src/metabase/redux/entities/tables-reducer.unit.spec.ts b/frontend/src/metabase/redux/entities/tables-reducer.unit.spec.tsindex 305d2b95e63..f8092246ef1 100644--- a/frontend/src/metabase/redux/entities/tables-reducer.unit.spec.ts+++ b/frontend/src/metabase/redux/entities/tables-reducer.unit.spec.ts@@ -176,6 +176,22 @@ describe("tablesReducer", () => {]);});+ it("skips a field written as null, which is a delete", () => {+ const state = {+ 1: {+ id: 1,+ original_fields: [{ id: 10, display_name: "Old" }],+ },+ };++ const nextState = tablesReducer(+ state,+ getEntitiesUpdateAction({ 10: null }),+ );++ expect(nextState).toBe(state);+ });+it("leaves state untouched when the table has no original_fields", () => {const state = { 1: { id: 1, name: "Orders" } };