Handle renames
This commit is contained in:
parent
a17f81ffb3
commit
3bf794c6ba
|
@ -233,7 +233,8 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
|
|
||||||
tableToSave.views[view] = sdk.views.syncSchema(
|
tableToSave.views[view] = sdk.views.syncSchema(
|
||||||
oldTable!.views![view] as ViewV2,
|
oldTable!.views![view] as ViewV2,
|
||||||
tableToSave.schema
|
tableToSave.schema,
|
||||||
|
renamed
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,8 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
if (sdk.views.isV2(tableView)) {
|
if (sdk.views.isV2(tableView)) {
|
||||||
tableToSave.views[view] = sdk.views.syncSchema(
|
tableToSave.views[view] = sdk.views.syncSchema(
|
||||||
oldTable!.views![view] as ViewV2,
|
oldTable!.views![view] as ViewV2,
|
||||||
tableToSave.schema
|
tableToSave.schema,
|
||||||
|
_rename
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
import { HTTPError, context } from "@budibase/backend-core"
|
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 sdk from "../../../sdk"
|
||||||
import * as utils from "../../../db/utils"
|
import * as utils from "../../../db/utils"
|
||||||
import merge from "lodash/merge"
|
|
||||||
|
|
||||||
export async function get(viewId: string): Promise<ViewV2 | undefined> {
|
export async function get(viewId: string): Promise<ViewV2 | undefined> {
|
||||||
const { tableId } = utils.extractViewInfoFromID(viewId)
|
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) {
|
if (view.schemaUI) {
|
||||||
for (const fieldName of Object.keys(view.schemaUI)) {
|
for (const fieldName of Object.keys(view.schemaUI)) {
|
||||||
if (!schema[fieldName]) {
|
if (!schema[fieldName]) {
|
||||||
|
@ -122,5 +142,6 @@ export function syncSchema(view: ViewV2, schema: TableSchema): ViewV2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
view.columns = view.columns?.filter(x => schema[x])
|
view.columns = view.columns?.filter(x => schema[x])
|
||||||
|
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import _ from "lodash"
|
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 { generator } from "@budibase/backend-core/tests"
|
||||||
import { enrichSchema, syncSchema } from ".."
|
import { enrichSchema, syncSchema } from ".."
|
||||||
|
|
||||||
|
@ -296,7 +296,11 @@ describe("table sdk", () => {
|
||||||
...basicView,
|
...basicView,
|
||||||
columns: ["name", "id", "description"],
|
columns: ["name", "id", "description"],
|
||||||
}
|
}
|
||||||
const result = syncSchema(_.cloneDeep(view), basicTable.schema)
|
const result = syncSchema(
|
||||||
|
_.cloneDeep(view),
|
||||||
|
basicTable.schema,
|
||||||
|
undefined
|
||||||
|
)
|
||||||
expect(result).toEqual(view)
|
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({
|
expect(result).toEqual({
|
||||||
...view,
|
...view,
|
||||||
schemaUI: undefined,
|
schemaUI: undefined,
|
||||||
|
@ -334,13 +338,37 @@ describe("table sdk", () => {
|
||||||
}
|
}
|
||||||
const { name, description, ...newTableSchema } = basicTable.schema
|
const { name, description, ...newTableSchema } = basicTable.schema
|
||||||
|
|
||||||
const result = syncSchema(_.cloneDeep(view), newTableSchema)
|
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
...view,
|
...view,
|
||||||
columns: ["id"],
|
columns: ["id"],
|
||||||
schemaUI: undefined,
|
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", () => {
|
describe("view with schema", () => {
|
||||||
|
@ -355,7 +383,11 @@ describe("table sdk", () => {
|
||||||
hiddenField: { visible: false },
|
hiddenField: { visible: false },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const result = syncSchema(_.cloneDeep(view), basicTable.schema)
|
const result = syncSchema(
|
||||||
|
_.cloneDeep(view),
|
||||||
|
basicTable.schema,
|
||||||
|
undefined
|
||||||
|
)
|
||||||
expect(result).toEqual(view)
|
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({
|
expect(result).toEqual({
|
||||||
...view,
|
...view,
|
||||||
schemaUI: {
|
schemaUI: {
|
||||||
|
@ -409,7 +441,7 @@ describe("table sdk", () => {
|
||||||
}
|
}
|
||||||
const { name, description, ...newTableSchema } = basicTable.schema
|
const { name, description, ...newTableSchema } = basicTable.schema
|
||||||
|
|
||||||
const result = syncSchema(_.cloneDeep(view), newTableSchema)
|
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
...view,
|
...view,
|
||||||
columns: ["id"],
|
columns: ["id"],
|
||||||
|
@ -439,9 +471,9 @@ describe("table sdk", () => {
|
||||||
name: "newField1",
|
name: "newField1",
|
||||||
visible: true,
|
visible: true,
|
||||||
},
|
},
|
||||||
} as any
|
} as TableSchema
|
||||||
|
|
||||||
const result = syncSchema(_.cloneDeep(view), newTableSchema)
|
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
...view,
|
...view,
|
||||||
columns: ["id"],
|
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" },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue