From 586dd8fea74726963177af3118806fe4eafd6288 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 3 Oct 2024 18:10:58 +0100 Subject: [PATCH] Fixing an issue with newlines between coalesce statements in Postgres - we were escaping newlines even if they were valid when given a list of JSON operations to perform. --- .../routes/tests/queries/generic-sql.spec.ts | 29 +++++++++++++++++++ packages/server/src/integrations/postgres.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts index e6da4693eb..0979f8bed3 100644 --- a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts +++ b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts @@ -28,6 +28,7 @@ describe.each( const config = setup.getConfig() const isOracle = dbName === DatabaseName.ORACLE const isMsSQL = dbName === DatabaseName.SQL_SERVER + const isPostgres = dbName === DatabaseName.POSTGRES let rawDatasource: Datasource let datasource: Datasource @@ -47,6 +48,9 @@ describe.each( transformer: "return data", readable: true, } + if (query.fields?.sql && typeof query.fields.sql !== "string") { + throw new Error("Unable to create with knex structure in 'sql' field") + } return await config.api.query.save( { ...defaultQuery, ...query }, expectations @@ -207,6 +211,31 @@ describe.each( expect(prodQuery.parameters).toBeUndefined() expect(prodQuery.schema).toBeDefined() }) + + isPostgres && + it("should be able to handle a JSON aggregate with newlines", async () => { + const jsonStatement = `COALESCE(json_build_object('name', name),'{"name":{}}'::json)` + const query = await createQuery({ + fields: { + sql: client("test_table") + .select([ + "*", + client.raw( + `${jsonStatement} as json,\n${jsonStatement} as json2` + ), + ]) + .toString(), + }, + }) + const res = await config.api.query.execute( + query._id!, + {}, + { + status: 200, + } + ) + expect(res).toBeDefined() + }) }) }) diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 3652864991..28400f616f 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -41,7 +41,7 @@ if (types) { types.setTypeParser(1184, (val: any) => val) // timestampz } -const JSON_REGEX = /'{.*}'::json/s +const JSON_REGEX = /'{\s*.*?\s*}'::json/gs const Sql = sql.Sql interface PostgresConfig {