Handle migrations
This commit is contained in:
parent
735572b4bf
commit
3e4b0e8cd6
|
@ -496,7 +496,7 @@ describe.each([
|
||||||
oldColumn: table.schema["user relationship"],
|
oldColumn: table.schema["user relationship"],
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "user column",
|
name: "user column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE_SINGLE,
|
||||||
subtype: BBReferenceFieldSubType.USER,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -515,7 +515,7 @@ describe.each([
|
||||||
expect(migratedRow["user column"]).toBeDefined()
|
expect(migratedRow["user column"]).toBeDefined()
|
||||||
expect(migratedRow["user relationship"]).not.toBeDefined()
|
expect(migratedRow["user relationship"]).not.toBeDefined()
|
||||||
expect(row["user relationship"][0]._id).toEqual(
|
expect(row["user relationship"][0]._id).toEqual(
|
||||||
migratedRow["user column"][0]._id
|
migratedRow["user column"]._id
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -562,7 +562,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "user column",
|
name: "user column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -614,7 +614,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "user column",
|
name: "user column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -669,7 +669,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "user column",
|
name: "user column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -728,7 +728,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "",
|
name: "",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
|
@ -743,7 +743,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "_id",
|
name: "_id",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
|
@ -758,7 +758,7 @@ describe.each([
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "num",
|
name: "num",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
|
@ -772,12 +772,12 @@ describe.each([
|
||||||
oldColumn: {
|
oldColumn: {
|
||||||
name: "not a column",
|
name: "not a column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
newColumn: {
|
newColumn: {
|
||||||
name: "new column",
|
name: "new column",
|
||||||
type: FieldType.BB_REFERENCE,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: BBReferenceFieldSubType.USERS,
|
subtype: BBReferenceFieldSubType.USER,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
|
|
|
@ -4,7 +4,6 @@ import {
|
||||||
FieldSchema,
|
FieldSchema,
|
||||||
BBReferenceFieldSubType,
|
BBReferenceFieldSubType,
|
||||||
InternalTable,
|
InternalTable,
|
||||||
isBBReferenceField,
|
|
||||||
isRelationshipField,
|
isRelationshipField,
|
||||||
LinkDocument,
|
LinkDocument,
|
||||||
LinkInfo,
|
LinkInfo,
|
||||||
|
@ -12,6 +11,8 @@ import {
|
||||||
RelationshipType,
|
RelationshipType,
|
||||||
Row,
|
Row,
|
||||||
Table,
|
Table,
|
||||||
|
FieldType,
|
||||||
|
BBReferenceSingleFieldMetadata,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import { isExternalTableID } from "../../../integrations/utils"
|
import { isExternalTableID } from "../../../integrations/utils"
|
||||||
|
@ -75,11 +76,14 @@ function getColumnMigrator(
|
||||||
throw new BadRequestError(`Column "${oldColumn.name}" does not exist`)
|
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`)
|
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`)
|
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 (oldColumn.relationshipType === RelationshipType.ONE_TO_MANY) {
|
||||||
if (newColumn.subtype !== BBReferenceFieldSubType.USER) {
|
if (newColumn.type !== FieldType.BB_REFERENCE_SINGLE) {
|
||||||
throw new BadRequestError(
|
throw new BadRequestError(
|
||||||
`Column "${oldColumn.name}" is a one-to-many column but "${newColumn.name}" is not a single user column`
|
`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_MANY ||
|
||||||
oldColumn.relationshipType === RelationshipType.MANY_TO_ONE
|
oldColumn.relationshipType === RelationshipType.MANY_TO_ONE
|
||||||
) {
|
) {
|
||||||
if (newColumn.subtype !== BBReferenceFieldSubType.USERS) {
|
if (newColumn.type !== FieldType.BB_REFERENCE) {
|
||||||
throw new BadRequestError(
|
throw new BadRequestError(
|
||||||
`Column "${oldColumn.name}" is a ${oldColumn.relationshipType} column but "${newColumn.name}" is not a multi user column`
|
`Column "${oldColumn.name}" is a ${oldColumn.relationshipType} column but "${newColumn.name}" is not a multi user column`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return new MultiUserColumnMigrator(table, oldColumn, newColumn)
|
return new MultiUserColumnMigrator(table, oldColumn, newColumn)
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new BadRequestError(`Unknown migration type`)
|
throw new BadRequestError(`Unknown migration type`)
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class UserColumnMigrator implements ColumnMigrator {
|
abstract class UserColumnMigrator<T> implements ColumnMigrator {
|
||||||
constructor(
|
constructor(
|
||||||
protected table: Table,
|
protected table: Table,
|
||||||
protected oldColumn: RelationshipFieldMetadata,
|
protected oldColumn: RelationshipFieldMetadata,
|
||||||
protected newColumn: BBReferenceFieldMetadata
|
protected newColumn: T
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
abstract updateRow(row: Row, linkInfo: LinkInfo): void
|
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 {
|
updateRow(row: Row, linkInfo: LinkInfo): void {
|
||||||
row[this.newColumn.name] = dbCore.getGlobalIDFromUserMetadataID(
|
row[this.newColumn.name] = dbCore.getGlobalIDFromUserMetadataID(
|
||||||
linkInfo.rowId
|
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 {
|
updateRow(row: Row, linkInfo: LinkInfo): void {
|
||||||
if (!row[this.newColumn.name]) {
|
if (!row[this.newColumn.name]) {
|
||||||
row[this.newColumn.name] = []
|
row[this.newColumn.name] = []
|
||||||
|
|
|
@ -214,15 +214,3 @@ export function isManyToOne(
|
||||||
): field is ManyToOneRelationshipFieldMetadata {
|
): field is ManyToOneRelationshipFieldMetadata {
|
||||||
return field.relationshipType === RelationshipType.MANY_TO_ONE
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue