Handle renames

This commit is contained in:
Adria Navarro 2023-08-09 16:25:05 +03:00
parent a17f81ffb3
commit 3bf794c6ba
4 changed files with 103 additions and 14 deletions

View File

@ -233,7 +233,8 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
tableToSave.views[view] = sdk.views.syncSchema(
oldTable!.views![view] as ViewV2,
tableToSave.schema
tableToSave.schema,
renamed
)
}

View File

@ -108,7 +108,8 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
if (sdk.views.isV2(tableView)) {
tableToSave.views[view] = sdk.views.syncSchema(
oldTable!.views![view] as ViewV2,
tableToSave.schema
tableToSave.schema,
_rename
)
continue
}

View File

@ -1,9 +1,14 @@
import { HTTPError, context } from "@budibase/backend-core"
import { FieldSchema, TableSchema, View, ViewV2 } from "@budibase/types"
import {
FieldSchema,
RenameColumn,
TableSchema,
View,
ViewV2,
} from "@budibase/types"
import sdk from "../../../sdk"
import * as utils from "../../../db/utils"
import merge from "lodash/merge"
export async function get(viewId: string): Promise<ViewV2 | undefined> {
const { tableId } = utils.extractViewInfoFromID(viewId)
@ -107,7 +112,22 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) {
}
}
export function syncSchema(view: ViewV2, schema: TableSchema): ViewV2 {
export function syncSchema(
view: ViewV2,
schema: TableSchema,
renameColumn: RenameColumn | undefined
): ViewV2 {
if (renameColumn) {
if (view.columns) {
view.columns[view.columns.indexOf(renameColumn.old)] =
renameColumn.updated
}
if (view.schemaUI) {
view.schemaUI[renameColumn.updated] = view.schemaUI[renameColumn.old]
delete view.schemaUI[renameColumn.old]
}
}
if (view.schemaUI) {
for (const fieldName of Object.keys(view.schemaUI)) {
if (!schema[fieldName]) {
@ -122,5 +142,6 @@ export function syncSchema(view: ViewV2, schema: TableSchema): ViewV2 {
}
view.columns = view.columns?.filter(x => schema[x])
return view
}

View File

@ -1,5 +1,5 @@
import _ from "lodash"
import { FieldType, Table, ViewV2 } from "@budibase/types"
import { FieldType, Table, TableSchema, ViewV2 } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import { enrichSchema, syncSchema } from ".."
@ -296,7 +296,11 @@ describe("table sdk", () => {
...basicView,
columns: ["name", "id", "description"],
}
const result = syncSchema(_.cloneDeep(view), basicTable.schema)
const result = syncSchema(
_.cloneDeep(view),
basicTable.schema,
undefined
)
expect(result).toEqual(view)
})
@ -320,7 +324,7 @@ describe("table sdk", () => {
},
}
const result = syncSchema(_.cloneDeep(view), newTableSchema)
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
schemaUI: undefined,
@ -334,13 +338,37 @@ describe("table sdk", () => {
}
const { name, description, ...newTableSchema } = basicTable.schema
const result = syncSchema(_.cloneDeep(view), newTableSchema)
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
schemaUI: undefined,
})
})
it("renaming mapped columns will update the view column mapping", () => {
const view = {
...basicView,
columns: ["name", "id", "description"],
}
const { description, ...newTableSchema } = {
...basicTable.schema,
updatedDescription: {
...basicTable.schema.description,
name: "updatedDescription",
},
} as TableSchema
const result = syncSchema(_.cloneDeep(view), newTableSchema, {
old: "description",
updated: "updatedDescription",
})
expect(result).toEqual({
...view,
columns: ["name", "id", "updatedDescription"],
schemaUI: undefined,
})
})
})
describe("view with schema", () => {
@ -355,7 +383,11 @@ describe("table sdk", () => {
hiddenField: { visible: false },
},
}
const result = syncSchema(_.cloneDeep(view), basicTable.schema)
const result = syncSchema(
_.cloneDeep(view),
basicTable.schema,
undefined
)
expect(result).toEqual(view)
})
@ -385,7 +417,7 @@ describe("table sdk", () => {
},
}
const result = syncSchema(_.cloneDeep(view), newTableSchema)
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
schemaUI: {
@ -409,7 +441,7 @@ describe("table sdk", () => {
}
const { name, description, ...newTableSchema } = basicTable.schema
const result = syncSchema(_.cloneDeep(view), newTableSchema)
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
@ -439,9 +471,9 @@ describe("table sdk", () => {
name: "newField1",
visible: true,
},
} as any
} as TableSchema
const result = syncSchema(_.cloneDeep(view), newTableSchema)
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
@ -453,6 +485,40 @@ describe("table sdk", () => {
},
})
})
it("renaming mapped columns will update the view column mapping and it's schema", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
name: { visible: true },
id: { visible: true },
description: { visible: true, width: 150, icon: "ic-any" },
hiddenField: { visible: false },
},
}
const { description, ...newTableSchema } = {
...basicTable.schema,
updatedDescription: {
...basicTable.schema.description,
name: "updatedDescription",
},
} as TableSchema
const result = syncSchema(_.cloneDeep(view), newTableSchema, {
old: "description",
updated: "updatedDescription",
})
expect(result).toEqual({
...view,
columns: ["name", "id", "updatedDescription"],
schemaUI: {
...view.schemaUI,
description: undefined,
updatedDescription: { visible: true, width: 150, icon: "ic-any" },
},
})
})
})
})
})