From ff30d4b47689ab1075edcefdb477a9ded719afe6 Mon Sep 17 00:00:00 2001 From: David Kellner <52860029+kellnerd@users.noreply.github.com> Date: Tue, 9 Jul 2024 20:07:00 +0200 Subject: [PATCH 1/2] fix(imports): Explicitly set the entity BBID of approved imports Not sure whether that is the best solution with Bookshelf.js, but this prevents the following SQL error from crashing the server: > error: insert into "bookbrainz"."author" ("alias_set_id", "begin_area_id", "begin_day", "begin_month", "begin_year", "disambiguation_id", "end_area_id", "end_day", "end_month", "end_year", "ended", "gender_id", "identifier_set_id", "revision_id", "type_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) returning * - null value in column "bbid" violates not-null constraint --- src/func/create-entity.ts | 9 ++++++--- src/func/imports/approve-import.ts | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/func/create-entity.ts b/src/func/create-entity.ts index e194d87d..b619d7fe 100644 --- a/src/func/create-entity.ts +++ b/src/func/create-entity.ts @@ -58,11 +58,11 @@ interface CreateEntityPropsType { entityType: EntityTypeString } -// TODO: function seems to be unused across all BB repos, ignore its errors (and delete it?) +// TODO: function is only used to approve imports, check whether type error below is critical export async function createEntity({ editorId, entityData, orm, transacting }: CreateEntityPropsType) { - const {Revision} = orm; + const {Entity, Revision} = orm; const {aliases, annotation, disambiguation, identifiers, note, type: entityType, ...entitySetData} = entityData; @@ -105,7 +105,7 @@ export async function createEntity({ ); // Get additional props - // @ts-expect-error Not sure why we have this error but this whole function is unused across our repos + // @ts-expect-error Not sure why we have this error const additionalProps = getAdditionalEntityProps(entityData, entityType); // Create entitySets @@ -123,9 +123,12 @@ export async function createEntity({ editorUpdatePromise, notePromise ]); + const newEntity = await new Entity({type: entityType}) + .save(null, {transacting}); const propsToSet = _.extend({ aliasSetId: aliasSetRecord && aliasSetRecord.get('id'), annotationId: annotationRecord && annotationRecord.get('id'), + bbid: newEntity.get('bbid'), disambiguationId: disambiguationRecord && disambiguationRecord.get('id'), identifierSetId: identSetRecord && identSetRecord.get('id'), diff --git a/src/func/imports/approve-import.ts b/src/func/imports/approve-import.ts index c55f84b0..fb438229 100644 --- a/src/func/imports/approve-import.ts +++ b/src/func/imports/approve-import.ts @@ -41,7 +41,7 @@ export async function approveImport( identifierSetId} = importEntity; const {id: aliasSetId} = aliasSet; - const {Revision} = orm; + const {Entity, Revision} = orm; // Increase user edit count const editorUpdatePromise = @@ -74,8 +74,11 @@ export async function approveImport( revisionPromise, notePromise, editorUpdatePromise ]); + const newEntity = await new Entity({type: entityType}) + .save(null, {transacting}); const propsToSet = _.extend({ aliasSetId, + bbid: newEntity.get('bbid'), disambiguationId, identifierSetId, revisionId: revisionRecord && revisionRecord.get('id') From 817ec3fc24d5e7e3f2ccc952dca41c05eeaeb72c Mon Sep 17 00:00:00 2001 From: David Kellner <52860029+kellnerd@users.noreply.github.com> Date: Tue, 9 Jul 2024 20:08:45 +0200 Subject: [PATCH 2/2] refactor(imports): Return the Bookshelf model We need it again to update the search index. --- src/func/create-entity.ts | 2 +- src/func/imports/approve-import.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/func/create-entity.ts b/src/func/create-entity.ts index b619d7fe..d17fcbb1 100644 --- a/src/func/create-entity.ts +++ b/src/func/create-entity.ts @@ -148,5 +148,5 @@ export async function createEntity({ withRelated: ['defaultAlias'] }); - return entity.toJSON(); + return entity; } diff --git a/src/func/imports/approve-import.ts b/src/func/imports/approve-import.ts index fb438229..0a58f79e 100644 --- a/src/func/imports/approve-import.ts +++ b/src/func/imports/approve-import.ts @@ -76,9 +76,10 @@ export async function approveImport( const newEntity = await new Entity({type: entityType}) .save(null, {transacting}); + const bbid = newEntity.get('bbid'); const propsToSet = _.extend({ aliasSetId, - bbid: newEntity.get('bbid'), + bbid, disambiguationId, identifierSetId, revisionId: revisionRecord && revisionRecord.get('id') @@ -95,9 +96,9 @@ export async function approveImport( const entity = await entityModel.refresh({ transacting, withRelated: ['defaultAlias'] - }).then(entityObject => entityObject.toJSON()); + }); - await deleteImport(transacting, importId, entity.bbid); + await deleteImport(transacting, importId, bbid); return entity; }