From 3cc4b71561b0d93e8417edc5e6127f346ddae9ec Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 7 Jun 2024 17:59:18 +0100 Subject: [PATCH] Fixing some issues highlighted by test case. --- .../src/api/routes/tests/datasource.spec.ts | 7 ++++++- .../server/src/integrations/utils/utils.ts | 20 ++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index f7838b1d39..8fa84ac05d 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -258,11 +258,12 @@ describe("/datasources", () => { }) ) + const stringName = "string" const fullSchema: { [type in SupportedSqlTypes]: FieldSchema & { type: type } } = { [FieldType.STRING]: { - name: "string", + name: stringName, type: FieldType.STRING, constraints: { presence: true, @@ -353,6 +354,10 @@ describe("/datasources", () => { ), schema: Object.entries(table.schema).reduce( (acc, [fieldName, field]) => { + // the constraint will be unset - as the DB doesn't recognise it as not null + if (fieldName === stringName) { + field.constraints = {} + } acc[fieldName] = expect.objectContaining({ ...field, }) diff --git a/packages/server/src/integrations/utils/utils.ts b/packages/server/src/integrations/utils/utils.ts index 41e62f8975..b97782ce7e 100644 --- a/packages/server/src/integrations/utils/utils.ts +++ b/packages/server/src/integrations/utils/utils.ts @@ -289,19 +289,25 @@ function copyExistingPropsOver( externalType: existingTableSchema[key].externalType || table.schema[key]?.externalType, - autocolumn: !!fetchedColumnDefinition?.autocolumn, + autocolumn: fetchedColumnDefinition?.autocolumn, } as FieldSchema // check constraints which can be fetched from the DB (they could be updated) if (fetchedColumnDefinition?.constraints) { // inclusions are the enum values (select/options) - const fetchedInclusion = fetchedColumnDefinition.constraints.inclusion - const oldInclusion = table.schema[key].constraints?.inclusion + const fetchedConstraints = fetchedColumnDefinition.constraints + const oldConstraints = table.schema[key].constraints table.schema[key].constraints = { ...table.schema[key].constraints, - presence: !!fetchedColumnDefinition.constraints?.presence, - inclusion: fetchedInclusion?.length - ? fetchedInclusion - : oldInclusion, + inclusion: fetchedConstraints.inclusion?.length + ? fetchedConstraints.inclusion + : oldConstraints?.inclusion, + } + // true or undefined - consistent with old API + if (fetchedConstraints.presence) { + table.schema[key].constraints!.presence = + fetchedConstraints.presence + } else if (oldConstraints?.presence === true) { + delete table.schema[key].constraints?.presence } } }