Precondition checks to make sure the migration is from the right column to the right column.

This commit is contained in:
Sam Rose 2023-10-18 12:04:50 +01:00
parent 3634687442
commit 5747f30b5f
No known key found for this signature in database
3 changed files with 96 additions and 1 deletions

View File

@ -11,11 +11,16 @@ import {
BulkImportRequest,
BulkImportResponse,
FetchTablesResponse,
InternalTable,
MigrateRequest,
MigrateResponse,
SaveTableRequest,
SaveTableResponse,
Table,
TableResponse,
UserCtx,
isBBReferenceField,
isRelationshipField,
} from "@budibase/types"
import sdk from "../../../sdk"
import { jsonFromCsvString } from "../../../utilities/csv"
@ -159,4 +164,53 @@ export async function validateExistingTableImport(ctx: UserCtx) {
}
}
export async function migrate(ctx: UserCtx) {}
function error(ctx: UserCtx, message: string, status = 400) {
ctx.status = status
ctx.body = { message }
}
export async function migrate(ctx: UserCtx<MigrateRequest, MigrateResponse>) {
const { tableId, oldColumn, newColumn } = ctx.request.body
// For now we're only supporting migrations of user relationships to user
// columns in internal tables. In future we may want to support other
// migrations but for now return an error if we aren't migrating a user
// relationship.
if (isExternalTable(tableId)) {
return error(ctx, "External tables cannot be migrated")
}
const table = await sdk.tables.getTable(tableId)
if (!(oldColumn.name in table.schema)) {
return error(
ctx,
`Column "${oldColumn.name}" does not exist on table "${table.name}"`
)
}
if (newColumn.name in table.schema) {
return error(
ctx,
`Column "${newColumn.name}" already exists on table "${table.name}"`
)
}
if (!isBBReferenceField(newColumn)) {
return error(ctx, `Column "${newColumn.name}" is not a user column`)
}
if (newColumn.subtype !== "user" && newColumn.subtype !== "users") {
return error(ctx, `Column "${newColumn.name}" is not a user column`)
}
if (!isRelationshipField(oldColumn)) {
return error(ctx, `Column "${oldColumn.name}" is not a user relationship`)
}
if (oldColumn.tableId !== InternalTable.USER_METADATA) {
return error(ctx, `Column "${oldColumn.name}" is not a user relationship`)
}
let rows = await sdk.rows.fetch(tableId)
}

View File

@ -1,4 +1,5 @@
import {
FieldSchema,
Row,
Table,
TableRequest,
@ -33,3 +34,13 @@ export interface BulkImportRequest {
export interface BulkImportResponse {
message: string
}
export interface MigrateRequest {
tableId: string
oldColumn: FieldSchema
newColumn: FieldSchema
}
export interface MigrateResponse {
message: string
}

View File

@ -164,3 +164,33 @@ export type FieldSchema =
export interface TableSchema {
[key: string]: FieldSchema
}
export function isRelationshipField(
field: FieldSchema
): field is RelationshipFieldMetadata {
return field.type === FieldType.LINK
}
export function isManyToMany(
field: RelationshipFieldMetadata
): field is ManyToManyRelationshipFieldMetadata {
return field.relationshipType === RelationshipType.MANY_TO_MANY
}
export function isOneToMany(
field: RelationshipFieldMetadata
): field is OneToManyRelationshipFieldMetadata {
return field.relationshipType === RelationshipType.ONE_TO_MANY
}
export function isManyToOne(
field: RelationshipFieldMetadata
): field is ManyToOneRelationshipFieldMetadata {
return field.relationshipType === RelationshipType.MANY_TO_ONE
}
export function isBBReferenceField(
field: FieldSchema
): field is BBReferenceFieldMetadata {
return field.type === FieldType.BB_REFERENCE
}