From 3e4b0e8cd663c6de21e9de8d86de90025bee8aec Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 26 Apr 2024 14:03:42 +0200 Subject: [PATCH] Handle migrations --- .../server/src/api/routes/tests/table.spec.ts | 20 ++++++++-------- .../server/src/sdk/app/tables/migration.ts | 23 +++++++++++-------- .../types/src/documents/app/table/schema.ts | 12 ---------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 9aabffd8b9..2aa44ce64f 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -496,7 +496,7 @@ describe.each([ oldColumn: table.schema["user relationship"], newColumn: { name: "user column", - type: FieldType.BB_REFERENCE, + type: FieldType.BB_REFERENCE_SINGLE, subtype: BBReferenceFieldSubType.USER, }, }) @@ -515,7 +515,7 @@ describe.each([ expect(migratedRow["user column"]).toBeDefined() expect(migratedRow["user relationship"]).not.toBeDefined() expect(row["user relationship"][0]._id).toEqual( - migratedRow["user column"][0]._id + migratedRow["user column"]._id ) } }) @@ -562,7 +562,7 @@ describe.each([ newColumn: { name: "user column", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }) @@ -614,7 +614,7 @@ describe.each([ newColumn: { name: "user column", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }) @@ -669,7 +669,7 @@ describe.each([ newColumn: { name: "user column", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }) @@ -728,7 +728,7 @@ describe.each([ newColumn: { name: "", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }, { status: 400 } @@ -743,7 +743,7 @@ describe.each([ newColumn: { name: "_id", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }, { status: 400 } @@ -758,7 +758,7 @@ describe.each([ newColumn: { name: "num", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }, { status: 400 } @@ -772,12 +772,12 @@ describe.each([ oldColumn: { name: "not a column", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, newColumn: { name: "new column", type: FieldType.BB_REFERENCE, - subtype: BBReferenceFieldSubType.USERS, + subtype: BBReferenceFieldSubType.USER, }, }, { status: 400 } diff --git a/packages/server/src/sdk/app/tables/migration.ts b/packages/server/src/sdk/app/tables/migration.ts index ae47897289..732773f164 100644 --- a/packages/server/src/sdk/app/tables/migration.ts +++ b/packages/server/src/sdk/app/tables/migration.ts @@ -4,7 +4,6 @@ import { FieldSchema, BBReferenceFieldSubType, InternalTable, - isBBReferenceField, isRelationshipField, LinkDocument, LinkInfo, @@ -12,6 +11,8 @@ import { RelationshipType, Row, Table, + FieldType, + BBReferenceSingleFieldMetadata, } from "@budibase/types" import sdk from "../../../sdk" import { isExternalTableID } from "../../../integrations/utils" @@ -75,11 +76,14 @@ function getColumnMigrator( throw new BadRequestError(`Column "${oldColumn.name}" does not exist`) } - if (!isBBReferenceField(newColumn)) { + if ( + newColumn.type !== FieldType.BB_REFERENCE_SINGLE && + newColumn.type !== FieldType.BB_REFERENCE + ) { throw new BadRequestError(`Column "${newColumn.name}" is not a user column`) } - if (newColumn.subtype !== "user" && newColumn.subtype !== "users") { + if (newColumn.subtype !== BBReferenceFieldSubType.USER) { throw new BadRequestError(`Column "${newColumn.name}" is not a user column`) } @@ -96,7 +100,7 @@ function getColumnMigrator( } if (oldColumn.relationshipType === RelationshipType.ONE_TO_MANY) { - if (newColumn.subtype !== BBReferenceFieldSubType.USER) { + if (newColumn.type !== FieldType.BB_REFERENCE_SINGLE) { throw new BadRequestError( `Column "${oldColumn.name}" is a one-to-many column but "${newColumn.name}" is not a single user column` ) @@ -107,22 +111,23 @@ function getColumnMigrator( oldColumn.relationshipType === RelationshipType.MANY_TO_MANY || oldColumn.relationshipType === RelationshipType.MANY_TO_ONE ) { - if (newColumn.subtype !== BBReferenceFieldSubType.USERS) { + if (newColumn.type !== FieldType.BB_REFERENCE) { throw new BadRequestError( `Column "${oldColumn.name}" is a ${oldColumn.relationshipType} column but "${newColumn.name}" is not a multi user column` ) } + return new MultiUserColumnMigrator(table, oldColumn, newColumn) } throw new BadRequestError(`Unknown migration type`) } -abstract class UserColumnMigrator implements ColumnMigrator { +abstract class UserColumnMigrator implements ColumnMigrator { constructor( protected table: Table, protected oldColumn: RelationshipFieldMetadata, - protected newColumn: BBReferenceFieldMetadata + protected newColumn: T ) {} abstract updateRow(row: Row, linkInfo: LinkInfo): void @@ -192,7 +197,7 @@ abstract class UserColumnMigrator implements ColumnMigrator { } } -class SingleUserColumnMigrator extends UserColumnMigrator { +class SingleUserColumnMigrator extends UserColumnMigrator { updateRow(row: Row, linkInfo: LinkInfo): void { row[this.newColumn.name] = dbCore.getGlobalIDFromUserMetadataID( linkInfo.rowId @@ -200,7 +205,7 @@ class SingleUserColumnMigrator extends UserColumnMigrator { } } -class MultiUserColumnMigrator extends UserColumnMigrator { +class MultiUserColumnMigrator extends UserColumnMigrator { updateRow(row: Row, linkInfo: LinkInfo): void { if (!row[this.newColumn.name]) { row[this.newColumn.name] = [] diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 03d57aced8..38424b26b6 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -214,15 +214,3 @@ export function isManyToOne( ): field is ManyToOneRelationshipFieldMetadata { return field.relationshipType === RelationshipType.MANY_TO_ONE } - -export function isBBReferenceField( - field: FieldSchema -): field is BBReferenceFieldMetadata { - return field.type === FieldType.BB_REFERENCE -} - -export function isAttachmentField( - field: FieldSchema -): field is AttachmentFieldMetadata { - return field.type === FieldType.ATTACHMENTS -}