Precondition checks to make sure the migration is from the right column to the right column.
This commit is contained in:
parent
3634687442
commit
5747f30b5f
|
@ -11,11 +11,16 @@ import {
|
||||||
BulkImportRequest,
|
BulkImportRequest,
|
||||||
BulkImportResponse,
|
BulkImportResponse,
|
||||||
FetchTablesResponse,
|
FetchTablesResponse,
|
||||||
|
InternalTable,
|
||||||
|
MigrateRequest,
|
||||||
|
MigrateResponse,
|
||||||
SaveTableRequest,
|
SaveTableRequest,
|
||||||
SaveTableResponse,
|
SaveTableResponse,
|
||||||
Table,
|
Table,
|
||||||
TableResponse,
|
TableResponse,
|
||||||
UserCtx,
|
UserCtx,
|
||||||
|
isBBReferenceField,
|
||||||
|
isRelationshipField,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import { jsonFromCsvString } from "../../../utilities/csv"
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
FieldSchema,
|
||||||
Row,
|
Row,
|
||||||
Table,
|
Table,
|
||||||
TableRequest,
|
TableRequest,
|
||||||
|
@ -33,3 +34,13 @@ export interface BulkImportRequest {
|
||||||
export interface BulkImportResponse {
|
export interface BulkImportResponse {
|
||||||
message: string
|
message: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface MigrateRequest {
|
||||||
|
tableId: string
|
||||||
|
oldColumn: FieldSchema
|
||||||
|
newColumn: FieldSchema
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MigrateResponse {
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
|
@ -164,3 +164,33 @@ export type FieldSchema =
|
||||||
export interface TableSchema {
|
export interface TableSchema {
|
||||||
[key: string]: FieldSchema
|
[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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue