Fixing an issue with corrupt relationship records referencing rows which don't exist, this is a temporary measure as these relationships should be cleaned up correctly but for now ignore any which reference rows which no longer exist.

This commit is contained in:
mike12345567 2024-10-03 16:10:07 +01:00
parent dfb991b93e
commit a6f6942288
3 changed files with 20 additions and 14 deletions

View File

@ -213,17 +213,21 @@ export class DatabaseImpl implements Database {
async getMultiple<T extends Document>( async getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean; excludeDocs?: boolean }
): Promise<T[]> { ): Promise<T[]> {
// get unique // get unique
ids = [...new Set(ids)] ids = [...new Set(ids)]
const includeDocs = !opts?.excludeDocs
const response = await this.allDocs<T>({ const response = await this.allDocs<T>({
keys: ids, keys: ids,
include_docs: true, include_docs: includeDocs,
}) })
const rowUnavailable = (row: RowResponse<T>) => { const rowUnavailable = (row: RowResponse<T>) => {
// row is deleted - key lookup can return this // row is deleted - key lookup can return this
if (row.doc == null || ("deleted" in row.value && row.value.deleted)) { if (
(includeDocs && row.doc == null) ||
(row.value && "deleted" in row.value && row.value.deleted)
) {
return true return true
} }
return row.error === "not_found" return row.error === "not_found"
@ -237,7 +241,7 @@ export class DatabaseImpl implements Database {
const missingIds = missing.map(row => row.key).join(", ") const missingIds = missing.map(row => row.key).join(", ")
throw new Error(`Unable to get documents: ${missingIds}`) throw new Error(`Unable to get documents: ${missingIds}`)
} }
return rows.map(row => row.doc!) return rows.map(row => (includeDocs ? row.doc! : row.value))
} }
async remove(idOrDoc: string | Document, rev?: string) { async remove(idOrDoc: string | Document, rev?: string) {

View File

@ -211,19 +211,21 @@ class LinkController {
linkedSchema?.type === FieldType.LINK && linkedSchema?.type === FieldType.LINK &&
linkedSchema?.relationshipType === RelationshipType.ONE_TO_MANY linkedSchema?.relationshipType === RelationshipType.ONE_TO_MANY
) { ) {
let links = ( let links = await getLinkDocuments({
await getLinkDocuments({
tableId: field.tableId, tableId: field.tableId,
rowId: linkId, rowId: linkId,
fieldName: linkedSchema.name,
}) })
).filter(
link => // check all the related rows exist
link.id !== row._id && link.fieldName === linkedSchema.name const foundRecords = await this._db.getMultiple(
links.map(l => l.id),
{ allowMissing: true, excludeDocs: true }
) )
// The 1 side of 1:N is already related to something else // The 1 side of 1:N is already related to something else
// You must remove the existing relationship // You must remove the existing relationship
if (links.length > 0) { if (foundRecords.length > 0) {
throw new Error( throw new Error(
`1:N Relationship Error: Record already linked to another.` `1:N Relationship Error: Record already linked to another.`
) )

View File

@ -133,7 +133,7 @@ export interface Database {
exists(docId: string): Promise<boolean> exists(docId: string): Promise<boolean>
getMultiple<T extends Document>( getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean; excludeDocs?: boolean }
): Promise<T[]> ): Promise<T[]>
remove(idOrDoc: Document): Promise<Nano.DocumentDestroyResponse> remove(idOrDoc: Document): Promise<Nano.DocumentDestroyResponse>
remove(idOrDoc: string, rev?: string): Promise<Nano.DocumentDestroyResponse> remove(idOrDoc: string, rev?: string): Promise<Nano.DocumentDestroyResponse>