Rename link relationship fields
This commit is contained in:
parent
ae0e3cdb59
commit
74eb0357a4
|
@ -116,6 +116,9 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
|||
} else {
|
||||
await events.table.updated(savedTable)
|
||||
}
|
||||
if (renaming) {
|
||||
await sdk.views.renameLinkedViews(savedTable, renaming)
|
||||
}
|
||||
if (isImport) {
|
||||
await events.table.imported(savedTable)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {
|
||||
FieldType,
|
||||
RelationSchemaField,
|
||||
RelationshipFieldMetadata,
|
||||
RenameColumn,
|
||||
Table,
|
||||
TableSchema,
|
||||
|
@ -252,3 +253,50 @@ export function syncSchema(
|
|||
|
||||
return view
|
||||
}
|
||||
|
||||
export async function renameLinkedViews(table: Table, renaming: RenameColumn) {
|
||||
const relatedLinks: Record<string, RelationshipFieldMetadata[]> = {}
|
||||
|
||||
for (const field of Object.values(table.schema)) {
|
||||
if (field.type !== FieldType.LINK) {
|
||||
continue
|
||||
}
|
||||
|
||||
relatedLinks[field.tableId] ??= []
|
||||
relatedLinks[field.tableId].push(field)
|
||||
}
|
||||
|
||||
const relatedTables = await sdk.tables.getTables(Object.keys(relatedLinks))
|
||||
for (const relatedTable of relatedTables) {
|
||||
let toSave = false
|
||||
const viewsV2 = Object.values(relatedTable.views || {}).filter(
|
||||
sdk.views.isV2
|
||||
)
|
||||
if (!viewsV2) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const view of viewsV2) {
|
||||
for (const relField of Object.keys(view.schema || {}).filter(f => {
|
||||
const tableField = relatedTable.schema[f]
|
||||
if (tableField.type !== FieldType.LINK) {
|
||||
return false
|
||||
}
|
||||
|
||||
return tableField.tableId === table._id
|
||||
})) {
|
||||
const columns = view.schema && view.schema[relField]?.columns
|
||||
|
||||
if (columns && columns[renaming.old]) {
|
||||
columns[renaming.updated] = columns[renaming.old]
|
||||
delete columns[renaming.old]
|
||||
toSave = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (toSave) {
|
||||
await sdk.tables.saveTable(relatedTable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue