Fixing PR comments.

This commit is contained in:
mike12345567 2023-10-19 12:02:49 +01:00
parent 9bd67595ec
commit 54d976f750
4 changed files with 25 additions and 35 deletions

View File

@ -41,9 +41,10 @@ export async function save(
...update, ...update,
} }
const tableId = opts?.tableId || update._id
let oldTable: Table | undefined let oldTable: Table | undefined
if (opts?.tableId) { if (tableId) {
oldTable = await getTable(opts.tableId) oldTable = await getTable(tableId)
} }
if (hasTypeChanged(tableToSave, oldTable)) { if (hasTypeChanged(tableToSave, oldTable)) {
@ -114,6 +115,11 @@ export async function save(
relatedTable, relatedTable,
relationType relationType
) )
if (fkTable.schema[foreignKey] != null) {
throw new Error(
`Unable to generate foreign key - column ${foreignKey} already in use.`
)
}
fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey) fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey)
if (fkTable.constrained == null) { if (fkTable.constrained == null) {
fkTable.constrained = [] fkTable.constrained = []
@ -132,7 +138,7 @@ export async function save(
cleanupRelationships(tableToSave, tables, oldTable) cleanupRelationships(tableToSave, tables, oldTable)
const operation = oldTable ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE const operation = tableId ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE
await makeTableRequest( await makeTableRequest(
datasource, datasource,
operation, operation,

View File

@ -52,7 +52,7 @@ export function cleanupRelationships(
} }
} }
export function otherRelationshipType(type?: string) { export function otherRelationshipType(type: RelationshipType) {
if (type === RelationshipType.MANY_TO_MANY) { if (type === RelationshipType.MANY_TO_MANY) {
return RelationshipType.MANY_TO_MANY return RelationshipType.MANY_TO_MANY
} }
@ -68,7 +68,10 @@ export function generateManyLinkSchema(
relatedTable: Table relatedTable: Table
): Table { ): Table {
if (!table.primary || !relatedTable.primary) { if (!table.primary || !relatedTable.primary) {
throw new Error("Unable to generate many link schema, no primary keys") const noPrimaryName = !table.primary ? table.name : relatedTable.name
throw new Error(
`Unable to generate many link schema, "${noPrimaryName}" does not have a primary key`
)
} }
const primary = table.name + table.primary[0] const primary = table.name + table.primary[0]
const relatedPrimary = relatedTable.name + relatedTable.primary[0] const relatedPrimary = relatedTable.name + relatedTable.primary[0]

View File

@ -31,16 +31,12 @@ export async function getAllInternalTables(db?: Database): Promise<Table[]> {
if (!db) { if (!db) {
db = context.getAppDB() db = context.getAppDB()
} }
const internalTables = await db.allDocs( const internalTables = await db.allDocs<Table[]>(
getTableParams(null, { getTableParams(null, {
include_docs: true, include_docs: true,
}) })
) )
return internalTables.rows.map((tableDoc: any) => ({ return processInternalTables(internalTables)
...tableDoc.doc,
type: "internal",
sourceId: tableDoc.doc.sourceId || BudibaseInternalDB._id,
}))
} }
async function getAllExternalTables(): Promise<Table[]> { async function getAllExternalTables(): Promise<Table[]> {
@ -63,7 +59,7 @@ export async function getExternalTable(
return entities[tableName] return entities[tableName]
} }
export async function getTable(tableId: any): Promise<Table> { export async function getTable(tableId: string): Promise<Table> {
const db = context.getAppDB() const db = context.getAppDB()
if (isExternalTable(tableId)) { if (isExternalTable(tableId)) {
let { datasourceId, tableName } = breakExternalTableId(tableId) let { datasourceId, tableName } = breakExternalTableId(tableId)
@ -80,7 +76,7 @@ export async function getAllTables() {
getAllInternalTables(), getAllInternalTables(),
getAllExternalTables(), getAllExternalTables(),
]) ])
return [...internal, external] return [...internal, ...external]
} }
export async function getExternalTablesInDatasource( export async function getExternalTablesInDatasource(

View File

@ -58,17 +58,6 @@ export async function save(
}) })
table = await tableSaveFunctions.before(table) table = await tableSaveFunctions.before(table)
// make sure that types don't change of a column, have to remove
// the column if you want to change the type
if (oldTable && oldTable.schema) {
for (const propKey of Object.keys(table.schema)) {
let oldColumn = oldTable.schema[propKey]
if (oldColumn && oldColumn.type === FieldTypes.INTERNAL) {
oldTable.schema[propKey].type = FieldTypes.AUTO
}
}
}
let renaming = opts?.renaming let renaming = opts?.renaming
if (renaming && renaming.old === renaming.updated) { if (renaming && renaming.old === renaming.updated) {
renaming = undefined renaming = undefined
@ -104,17 +93,13 @@ export async function save(
} }
// update linked rows // update linked rows
try { const linkResp: any = await updateLinks({
const linkResp: any = await updateLinks({ eventType: oldTable ? EventType.TABLE_UPDATED : EventType.TABLE_SAVE,
eventType: oldTable ? EventType.TABLE_UPDATED : EventType.TABLE_SAVE, table: table,
table: table, oldTable: oldTable,
oldTable: oldTable, })
}) if (linkResp != null && linkResp._rev) {
if (linkResp != null && linkResp._rev) { table._rev = linkResp._rev
table._rev = linkResp._rev
}
} catch (err) {
throw new Error(err as string)
} }
// don't perform any updates until relationships have been // don't perform any updates until relationships have been