1
0
Fork 0
mirror of synced 2024-09-12 23:43:09 +12:00

Handle migrations

This commit is contained in:
Adria Navarro 2024-04-26 14:03:42 +02:00
parent 735572b4bf
commit 3e4b0e8cd6
3 changed files with 24 additions and 31 deletions

View file

@ -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 }

View file

@ -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<T> 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<BBReferenceSingleFieldMetadata> {
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<BBReferenceFieldMetadata> {
updateRow(row: Row, linkInfo: LinkInfo): void {
if (!row[this.newColumn.name]) {
row[this.newColumn.name] = []

View file

@ -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
}