Merge upstream.

This commit is contained in:
Sam Rose 2024-11-25 17:18:36 +00:00
commit 98717de61f
No known key found for this signature in database
2 changed files with 71 additions and 22 deletions

View File

@ -3496,18 +3496,18 @@ if (descriptions.length) {
}) })
}) })
!isInternal && if (!isInternal) {
describe("bigint ids", () => { describe("bigint ids", () => {
let table: Table let table: Table, relatedTable: Table
let relatedTable: Table let tableName: string, relatedTableName: string
beforeAll(async () => { beforeAll(async () => {
const tableName = generator.guid().substring(0, 10) tableName = generator.guid().substring(0, 10)
await client!.schema.createTable(tableName, table => { await client!.schema.createTable(tableName, table => {
table.bigIncrements("id").primary() table.bigIncrements("id").primary()
}) })
const relatedTableName = generator.guid().substring(0, 10) relatedTableName = generator.guid().substring(0, 10)
await client!.schema.createTable(relatedTableName, table => { await client!.schema.createTable(relatedTableName, table => {
table.increments("id").primary() table.increments("id").primary()
table table
@ -3525,17 +3525,16 @@ if (descriptions.length) {
table = tables.find(t => t.name === tableName)! table = tables.find(t => t.name === tableName)!
relatedTable = tables.find(t => t.name === relatedTableName)! relatedTable = tables.find(t => t.name === relatedTableName)!
await config.api.table.save({ await config.api.datasource.addExistingRelationship({
...table, one: {
schema: { tableId: relatedTable._id!,
...table.schema, relationshipName: "one",
related: { foreignKey: "tableid",
name: "related", },
type: FieldType.LINK, many: {
tableId: relatedTable._id!, tableId: table._id!,
fieldName: "tableid", relationshipName: "many",
relationshipType: RelationshipType.ONE_TO_MANY, primaryKey: "id",
},
}, },
}) })
}) })
@ -3560,6 +3559,7 @@ if (descriptions.length) {
]) ])
}) })
}) })
}
} }
) )
} }

View File

@ -1,14 +1,17 @@
import { import {
Datasource,
VerifyDatasourceRequest,
CreateDatasourceResponse,
UpdateDatasourceResponse,
UpdateDatasourceRequest,
QueryJson,
BuildSchemaFromSourceResponse, BuildSchemaFromSourceResponse,
CreateDatasourceResponse,
Datasource,
FetchDatasourceInfoResponse, FetchDatasourceInfoResponse,
FieldType,
QueryJson,
RelationshipType,
UpdateDatasourceRequest,
UpdateDatasourceResponse,
VerifyDatasourceRequest,
} from "@budibase/types" } from "@budibase/types"
import { Expectations, TestAPI } from "./base" import { Expectations, TestAPI } from "./base"
import { sql } from "@budibase/backend-core"
export class DatasourceAPI extends TestAPI { export class DatasourceAPI extends TestAPI {
create = async ( create = async (
@ -103,4 +106,50 @@ export class DatasourceAPI extends TestAPI {
} }
) )
} }
addExistingRelationship = async (
{
one,
many,
}: {
one: { tableId: string; relationshipName: string; foreignKey: string }
many: { tableId: string; relationshipName: string; primaryKey: string }
},
expectations?: Expectations
) => {
const oneTableInfo = sql.utils.breakExternalTableId(one.tableId),
manyTableInfo = sql.utils.breakExternalTableId(many.tableId)
if (oneTableInfo.datasourceId !== manyTableInfo.datasourceId) {
throw new Error(
"Tables are in different datasources, cannot create relationship."
)
}
const datasource = await this.get(oneTableInfo.datasourceId)
const oneTable = datasource.entities?.[oneTableInfo.tableName],
manyTable = datasource.entities?.[manyTableInfo.tableName]
if (!oneTable || !manyTable) {
throw new Error(
"Both tables not found in datasource, cannot create relationship."
)
}
manyTable.schema[many.relationshipName] = {
type: FieldType.LINK,
name: many.relationshipName,
tableId: oneTable._id!,
relationshipType: RelationshipType.MANY_TO_ONE,
fieldName: one.foreignKey,
foreignKey: many.primaryKey,
main: true,
}
oneTable.schema[one.relationshipName] = {
type: FieldType.LINK,
name: one.relationshipName,
tableId: manyTable._id!,
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: many.primaryKey,
foreignKey: one.foreignKey,
}
return await this.update(datasource, expectations)
}
} }