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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,8 +4,6 @@ import {
QueryJson, QueryJson,
SourceName, SourceName,
SqlQuery, SqlQuery,
Table,
TableSourceType,
SqlClient, SqlClient,
EnrichedQueryJson, EnrichedQueryJson,
} from "@budibase/types" } from "@budibase/types"
@ -15,17 +13,6 @@ import { generator } from "@budibase/backend-core/tests"
import sdk from "../../sdk" import sdk from "../../sdk"
const Sql = sql.Sql 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 const AliasTables = sdk.rows.AliasTables
function multiline(sql: string) { function multiline(sql: string) {
@ -249,7 +236,7 @@ describe("Captures of real examples", () => {
function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson { function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson {
return { return {
endpoint: { datasourceId: "", entityId: "", operation: op }, endpoint: { datasource: "", entityId: "", operation: op },
resource: { resource: {
fields, fields,
}, },

View File

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

View File

@ -9,11 +9,8 @@ import {
SqlClient, SqlClient,
Table, Table,
} from "@budibase/types" } from "@budibase/types"
import { SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core"
import { getSQLClient } from "./utils" import { getSQLClient } from "./utils"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import datasources from "../datasources"
import { BudibaseInternalDB } from "../../../db/utils"
import { dataFilters } from "@budibase/shared-core" import { dataFilters } from "@budibase/shared-core"
type PerformQueryFunction = ( type PerformQueryFunction = (
@ -185,14 +182,12 @@ export default class AliasTables {
json: EnrichedQueryJson, json: EnrichedQueryJson,
queryFn: PerformQueryFunction queryFn: PerformQueryFunction
): Promise<DatasourcePlusQueryResponse> { ): Promise<DatasourcePlusQueryResponse> {
const datasourceId = json.endpoint.datasourceId const datasource = json.datasource
const isSqs = datasourceId === SQS_DATASOURCE_INTERNAL const isSqs = datasource === undefined
let aliasingEnabled: boolean, datasource: Datasource let aliasingEnabled: boolean
if (isSqs) { if (isSqs) {
aliasingEnabled = this.isAliasingEnabled(json) aliasingEnabled = this.isAliasingEnabled(json)
datasource = BudibaseInternalDB
} else { } else {
datasource = await datasources.get(datasourceId)
aliasingEnabled = this.isAliasingEnabled(json, datasource) 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 { docIds, sql, SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core"
import { getTableFromSource } from "../../../api/controllers/row/utils" import { getTableFromSource } from "../../../api/controllers/row/utils"
import env from "../../../environment" 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> = { const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
[SourceName.POSTGRES]: SqlClient.POSTGRES, [SourceName.POSTGRES]: SqlClient.POSTGRES,
@ -88,8 +90,18 @@ export async function enrichQueryJson(
json: QueryJson json: QueryJson
): Promise<EnrichedQueryJson> { ): Promise<EnrichedQueryJson> {
let datasource: Datasource | undefined = undefined let datasource: Datasource | undefined = undefined
if (json.endpoint.datasourceId !== SQS_DATASOURCE_INTERNAL) { let entityId = json.endpoint.entityId
datasource = await sdk.datasources.get(json.endpoint.datasourceId) 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> let tables: Record<string, Table>
@ -99,7 +111,7 @@ export async function enrichQueryJson(
tables = processInternalTables(await sdk.tables.getAllInternalTables()) tables = processInternalTables(await sdk.tables.getAllInternalTables())
} }
const table = tables[json.endpoint.entityId] const table = tables[entityId]
return { return {
table, table,

View File

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

View File

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