Create failing test.
This commit is contained in:
parent
6e9c643fce
commit
6ae5451fdf
|
@ -162,7 +162,11 @@ export async function validateExistingTableImport(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function migrate(ctx: UserCtx<MigrateRequest, MigrateResponse>) {
|
export async function migrate(ctx: UserCtx<MigrateRequest, MigrateResponse>) {
|
||||||
const { tableId, oldColumn, newColumn } = ctx.request.body
|
const { oldColumn, newColumn } = ctx.request.body
|
||||||
|
let tableId = ctx.params.tableId as string
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
await sdk.tables.migrate(table, oldColumn, newColumn)
|
await sdk.tables.migrate(table, oldColumn, newColumn)
|
||||||
|
|
||||||
|
ctx.status = 200
|
||||||
|
ctx.body = { message: `Column ${oldColumn.name} migrated.` }
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import {
|
||||||
Table,
|
Table,
|
||||||
ViewCalculation,
|
ViewCalculation,
|
||||||
AutoFieldSubTypes,
|
AutoFieldSubTypes,
|
||||||
|
InternalTable,
|
||||||
|
FieldSubtype,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { checkBuilderEndpoint } from "./utilities/TestFunctions"
|
import { checkBuilderEndpoint } from "./utilities/TestFunctions"
|
||||||
import * as setup from "./utilities"
|
import * as setup from "./utilities"
|
||||||
|
@ -417,4 +419,46 @@ describe("/tables", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("migrate", () => {
|
||||||
|
it("should successfully migrate a user relationship to a user column", async () => {
|
||||||
|
const users = await config.api.row.fetch(InternalTable.USER_METADATA)
|
||||||
|
const table = await config.api.table.create({
|
||||||
|
name: "table",
|
||||||
|
type: "table",
|
||||||
|
schema: {
|
||||||
|
"user relationship": {
|
||||||
|
type: FieldType.LINK,
|
||||||
|
fieldName: "test",
|
||||||
|
name: "user relationship",
|
||||||
|
constraints: {
|
||||||
|
type: "array",
|
||||||
|
presence: false,
|
||||||
|
},
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
tableId: InternalTable.USER_METADATA,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.api.row.save(table._id!, {
|
||||||
|
"user relationship": users,
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.api.table.migrate(table._id!, {
|
||||||
|
oldColumn: table.schema["user relationship"],
|
||||||
|
newColumn: {
|
||||||
|
name: "user column",
|
||||||
|
type: FieldType.BB_REFERENCE,
|
||||||
|
subtype: FieldSubtype.USER,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const migratedTable = await config.api.table.get(table._id!)
|
||||||
|
expect(migratedTable.schema["user column"]).toBeDefined()
|
||||||
|
|
||||||
|
const rows = await config.api.row.fetch(table._id!)
|
||||||
|
expect(rows[0]["user column"]).toBeDefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
import * as links from "./links"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
...links,
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { context } from "@budibase/backend-core"
|
||||||
|
import { isTableId } from "@budibase/backend-core/src/docIds"
|
||||||
|
import { LinkDocument, LinkDocumentValue } from "@budibase/types"
|
||||||
|
import { ViewName, getQueryIndex } from "../../../../src/db/utils"
|
||||||
|
|
||||||
|
export async function fetch(tableId: string): Promise<LinkDocumentValue[]> {
|
||||||
|
if (!isTableId(tableId)) {
|
||||||
|
throw new Error(`Invalid tableId: ${tableId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = context.getAppDB()
|
||||||
|
const params: any = { startkey: [tableId], endkey: [tableId, {}] }
|
||||||
|
const linkRows = (await db.query(getQueryIndex(ViewName.LINK), params)).rows
|
||||||
|
return linkRows.map(row => row.value as LinkDocumentValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchWithDocument(
|
||||||
|
tableId: string
|
||||||
|
): Promise<LinkDocument[]> {
|
||||||
|
if (!isTableId(tableId)) {
|
||||||
|
throw new Error(`Invalid tableId: ${tableId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = context.getAppDB()
|
||||||
|
const params: any = {
|
||||||
|
startkey: [tableId],
|
||||||
|
endkey: [tableId, {}],
|
||||||
|
include_docs: true,
|
||||||
|
}
|
||||||
|
const linkRows = (await db.query(getQueryIndex(ViewName.LINK), params)).rows
|
||||||
|
return linkRows.map(row => row.doc as LinkDocument)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { SearchFilters, SearchParams, Row } from "@budibase/types"
|
import { SearchFilters, SearchParams } from "@budibase/types"
|
||||||
import { isExternalTable } from "../../../integrations/utils"
|
import { isExternalTable } from "../../../integrations/utils"
|
||||||
import * as internal from "./search/internal"
|
import * as internal from "./search/internal"
|
||||||
import * as external from "./search/external"
|
import * as external from "./search/external"
|
||||||
|
@ -45,7 +45,7 @@ export async function exportRows(
|
||||||
return pickApi(options.tableId).exportRows(options)
|
return pickApi(options.tableId).exportRows(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetch(tableId: string): Promise<Row[]> {
|
export async function fetch(tableId: string) {
|
||||||
return pickApi(tableId).fetch(tableId)
|
return pickApi(tableId).fetch(tableId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +53,6 @@ export async function fetchView(
|
||||||
tableId: string,
|
tableId: string,
|
||||||
viewName: string,
|
viewName: string,
|
||||||
params: ViewParams
|
params: ViewParams
|
||||||
): Promise<Row[]> {
|
) {
|
||||||
return pickApi(tableId).fetchView(viewName, params)
|
return pickApi(tableId).fetchView(viewName, params)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import {
|
||||||
isBBReferenceField,
|
isBBReferenceField,
|
||||||
isRelationshipField,
|
isRelationshipField,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { isExternalTable } from "src/integrations/utils"
|
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
|
import { isExternalTable } from "../../../../src/integrations/utils"
|
||||||
|
|
||||||
export async function migrate(
|
export async function migrate(
|
||||||
table: Table,
|
table: Table,
|
||||||
|
@ -80,5 +80,6 @@ class UserColumnMigrator implements ColumnMigrator {
|
||||||
|
|
||||||
async doMigration() {
|
async doMigration() {
|
||||||
let rows = await sdk.rows.fetch(this.table._id!)
|
let rows = await sdk.rows.fetch(this.table._id!)
|
||||||
|
let links = await sdk.links.fetchWithDocument(this.table._id!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { default as applications } from "./app/applications"
|
||||||
import { default as datasources } from "./app/datasources"
|
import { default as datasources } from "./app/datasources"
|
||||||
import { default as queries } from "./app/queries"
|
import { default as queries } from "./app/queries"
|
||||||
import { default as rows } from "./app/rows"
|
import { default as rows } from "./app/rows"
|
||||||
|
import { default as links } from "./app/links"
|
||||||
import { default as users } from "./users"
|
import { default as users } from "./users"
|
||||||
import { default as plugins } from "./plugins"
|
import { default as plugins } from "./plugins"
|
||||||
import * as views from "./app/views"
|
import * as views from "./app/views"
|
||||||
|
@ -22,6 +23,7 @@ const sdk = {
|
||||||
plugins,
|
plugins,
|
||||||
views,
|
views,
|
||||||
permissions,
|
permissions,
|
||||||
|
links,
|
||||||
}
|
}
|
||||||
|
|
||||||
// default export for TS
|
// default export for TS
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
import { SaveTableRequest, SaveTableResponse, Table } from "@budibase/types"
|
import {
|
||||||
|
MigrateRequest,
|
||||||
|
MigrateResponse,
|
||||||
|
SaveTableRequest,
|
||||||
|
SaveTableResponse,
|
||||||
|
Table,
|
||||||
|
} from "@budibase/types"
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
|
|
||||||
|
@ -42,4 +48,18 @@ export class TableAPI extends TestAPI {
|
||||||
.expect(expectStatus)
|
.expect(expectStatus)
|
||||||
return res.body
|
return res.body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
migrate = async (
|
||||||
|
tableId: string,
|
||||||
|
data: MigrateRequest,
|
||||||
|
{ expectStatus } = { expectStatus: 200 }
|
||||||
|
): Promise<MigrateResponse> => {
|
||||||
|
const res = await this.request
|
||||||
|
.post(`/api/tables/${tableId}/migrate`)
|
||||||
|
.send(data)
|
||||||
|
.set(this.config.defaultHeaders())
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(expectStatus)
|
||||||
|
return res.body
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ export interface BulkImportResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MigrateRequest {
|
export interface MigrateRequest {
|
||||||
tableId: string
|
|
||||||
oldColumn: FieldSchema
|
oldColumn: FieldSchema
|
||||||
newColumn: FieldSchema
|
newColumn: FieldSchema
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue