Fix postgres tests.

This commit is contained in:
Sam Rose 2024-11-27 13:14:42 +00:00
parent cb7c1dd6d4
commit 332505388d
No known key found for this signature in database
11 changed files with 34 additions and 39 deletions

View File

@ -23,7 +23,7 @@ import {
Table,
RowValue,
DynamicVariable,
QueryJson,
QueryJsonRequest,
} from "@budibase/types"
import sdk from "../../sdk"
import { builderSocket } from "../../websockets"
@ -299,7 +299,7 @@ export async function find(ctx: UserCtx) {
}
// dynamic query functionality
export async function query(ctx: UserCtx<QueryJson>) {
export async function query(ctx: UserCtx<QueryJsonRequest>) {
const queryJson = ctx.request.body
try {
ctx.body = await makeExternalQuery(queryJson)

View File

@ -137,7 +137,7 @@ function cleanupConfig(config: RunConfig, table: Table): RunConfig {
function getEndpoint(tableId: string, operation: Operation) {
const { datasourceId, tableName } = breakExternalTableId(tableId)
return { datasourceId, entityId: tableName, operation }
return { datasource: datasourceId, entityId: tableName, operation }
}
function isOneSide(
@ -706,7 +706,7 @@ export class ExternalRequest<T extends Operation> {
let json: QueryJson = {
endpoint: {
datasourceId: this.datasource._id!,
datasource: this.datasource,
entityId: table.name,
operation,
},

View File

@ -15,7 +15,7 @@ export async function makeTableRequest(
) {
const json: QueryJson = {
endpoint: {
datasourceId: datasource._id!,
datasource,
entityId: table._id!,
operation,
},

View File

@ -837,7 +837,7 @@ if (descriptions.length) {
const res = await config.api.datasource.query({
endpoint: {
datasourceId: datasource._id!,
datasource: datasource._id!,
operation: Operation.READ,
entityId,
},

View File

@ -23,5 +23,5 @@ export async function makeExternalQuery(
}
const integration = new Integration(enrichedJson.datasource.config)
return integration.query(json)
return integration.query(enrichedJson)
}

View File

@ -4,8 +4,6 @@ import {
QueryJson,
SourceName,
SqlQuery,
Table,
TableSourceType,
SqlClient,
EnrichedQueryJson,
} from "@budibase/types"
@ -15,17 +13,6 @@ import { generator } from "@budibase/backend-core/tests"
import sdk from "../../sdk"
const Sql = sql.Sql
// this doesn't exist strictly
const TABLE: Table = {
type: "table",
sourceType: TableSourceType.EXTERNAL,
sourceId: "SOURCE_ID",
schema: {},
name: "tableName",
primary: ["id"],
}
const AliasTables = sdk.rows.AliasTables
function multiline(sql: string) {
@ -249,7 +236,7 @@ describe("Captures of real examples", () => {
function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson {
return {
endpoint: { datasourceId: "", entityId: "", operation: op },
endpoint: { datasource: "", entityId: "", operation: op },
resource: {
fields,
},

View File

@ -358,7 +358,7 @@ export async function search(
const request: QueryJson = {
endpoint: {
// not important, we query ourselves
datasourceId: SQS_DATASOURCE_INTERNAL,
datasource: SQS_DATASOURCE_INTERNAL,
entityId: table._id!,
operation: Operation.READ,
},

View File

@ -9,11 +9,8 @@ import {
SqlClient,
Table,
} from "@budibase/types"
import { SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core"
import { getSQLClient } from "./utils"
import { cloneDeep } from "lodash"
import datasources from "../datasources"
import { BudibaseInternalDB } from "../../../db/utils"
import { dataFilters } from "@budibase/shared-core"
type PerformQueryFunction = (
@ -185,14 +182,12 @@ export default class AliasTables {
json: EnrichedQueryJson,
queryFn: PerformQueryFunction
): Promise<DatasourcePlusQueryResponse> {
const datasourceId = json.endpoint.datasourceId
const isSqs = datasourceId === SQS_DATASOURCE_INTERNAL
let aliasingEnabled: boolean, datasource: Datasource
const datasource = json.datasource
const isSqs = datasource === undefined
let aliasingEnabled: boolean
if (isSqs) {
aliasingEnabled = this.isAliasingEnabled(json)
datasource = BudibaseInternalDB
} else {
datasource = await datasources.get(datasourceId)
aliasingEnabled = this.isAliasingEnabled(json, datasource)
}

View File

@ -23,6 +23,8 @@ import { isSQL } from "../../../integrations/utils"
import { docIds, sql, SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core"
import { getTableFromSource } from "../../../api/controllers/row/utils"
import env from "../../../environment"
import { breakExternalTableId } from "@budibase/backend-core/src/sql/utils"
import { isDatasourceId } from "@budibase/backend-core/src/docIds"
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
[SourceName.POSTGRES]: SqlClient.POSTGRES,
@ -88,8 +90,18 @@ export async function enrichQueryJson(
json: QueryJson
): Promise<EnrichedQueryJson> {
let datasource: Datasource | undefined = undefined
if (json.endpoint.datasourceId !== SQS_DATASOURCE_INTERNAL) {
datasource = await sdk.datasources.get(json.endpoint.datasourceId)
let entityId = json.endpoint.entityId
if (typeof json.endpoint.datasource === "string") {
if (json.endpoint.datasource !== SQS_DATASOURCE_INTERNAL) {
datasource = await sdk.datasources.get(json.endpoint.datasource, {
enriched: true,
})
}
} else {
datasource = json.endpoint.datasource
if (isDatasourceId(entityId)) {
entityId = breakExternalTableId(entityId).tableName
}
}
let tables: Record<string, Table>
@ -99,7 +111,7 @@ export async function enrichQueryJson(
tables = processInternalTables(await sdk.tables.getAllInternalTables())
}
const table = tables[json.endpoint.entityId]
const table = tables[entityId]
return {
table,

View File

@ -4,7 +4,7 @@ import {
Datasource,
FetchDatasourceInfoResponse,
FieldType,
QueryJson,
QueryJsonRequest,
RelationshipType,
UpdateDatasourceRequest,
UpdateDatasourceResponse,
@ -69,10 +69,7 @@ export class DatasourceAPI extends TestAPI {
return await this._get<Datasource[]>(`/api/datasources`, { expectations })
}
query = async (
query: Omit<QueryJson, "meta"> & Partial<Pick<QueryJson, "meta">>,
expectations?: Expectations
) => {
query = async (query: QueryJsonRequest, expectations?: Expectations) => {
return await this._post<any>(`/api/datasources/query`, {
body: query,
expectations,

View File

@ -158,7 +158,7 @@ export interface ManyToManyRelationshipJson {
export interface QueryJson {
endpoint: {
datasourceId: string
datasource: string | Datasource
entityId: string
operation: Operation
schema?: string
@ -189,6 +189,10 @@ export interface EnrichedQueryJson extends QueryJson {
datasource?: Datasource
}
export interface QueryJsonRequest extends Omit<QueryJson, "endpoint"> {
endpoint: QueryJson["endpoint"] & { datasource: string }
}
export interface QueryOptions {
disableReturning?: boolean
disableBindings?: boolean