Minimal fix for issue involving JSON views and newlines with postgres, this fix couldn't be more over-arching as it risked breaking new lines across the board. Have included a script for setting up the test scenario as well. This fixes issue #2612.
This commit is contained in:
parent
ae0a4a14f8
commit
1f474ca106
|
@ -0,0 +1,28 @@
|
||||||
|
version: "3.8"
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
container_name: postgres-json
|
||||||
|
image: postgres
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: root
|
||||||
|
POSTGRES_PASSWORD: root
|
||||||
|
POSTGRES_DB: main
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
volumes:
|
||||||
|
#- pg_data:/var/lib/postgresql/data/
|
||||||
|
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||||
|
|
||||||
|
pgadmin:
|
||||||
|
container_name: pgadmin-json
|
||||||
|
image: dpage/pgadmin4
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
PGADMIN_DEFAULT_EMAIL: root@root.com
|
||||||
|
PGADMIN_DEFAULT_PASSWORD: root
|
||||||
|
ports:
|
||||||
|
- "5050:80"
|
||||||
|
|
||||||
|
#volumes:
|
||||||
|
# pg_data:
|
|
@ -0,0 +1,22 @@
|
||||||
|
SELECT 'CREATE DATABASE main'
|
||||||
|
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
|
||||||
|
CREATE TABLE jsonTable (
|
||||||
|
id character varying(32),
|
||||||
|
data jsonb,
|
||||||
|
text text
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO jsonTable (id, data) VALUES ('1', '{"id": 1, "age": 1, "name": "Mike", "newline": "this is text with a\n newline in it"}');
|
||||||
|
|
||||||
|
CREATE VIEW jsonView AS SELECT
|
||||||
|
x.id,
|
||||||
|
x.age,
|
||||||
|
x.name,
|
||||||
|
x.newline
|
||||||
|
FROM
|
||||||
|
jsonTable c,
|
||||||
|
LATERAL jsonb_to_record(c.data) x (id character varying(32),
|
||||||
|
age BIGINT,
|
||||||
|
name TEXT,
|
||||||
|
newline TEXT
|
||||||
|
);
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
docker-compose down
|
||||||
|
docker volume prune -f
|
|
@ -13,6 +13,9 @@ module PostgresModule {
|
||||||
const Sql = require("./base/sql")
|
const Sql = require("./base/sql")
|
||||||
const { FieldTypes } = require("../constants")
|
const { FieldTypes } = require("../constants")
|
||||||
const { buildExternalTableId, convertType, copyExistingPropsOver } = require("./utils")
|
const { buildExternalTableId, convertType, copyExistingPropsOver } = require("./utils")
|
||||||
|
const { escapeDangerousCharacters } = require("../utilities")
|
||||||
|
|
||||||
|
const JSON_REGEX = /'{.*}'::json/s
|
||||||
|
|
||||||
interface PostgresConfig {
|
interface PostgresConfig {
|
||||||
host: string
|
host: string
|
||||||
|
@ -94,6 +97,15 @@ module PostgresModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function internalQuery(client: any, query: SqlQuery) {
|
async function internalQuery(client: any, query: SqlQuery) {
|
||||||
|
// need to handle a specific issue with json data types in postgres,
|
||||||
|
// new lines inside the JSON data will break it
|
||||||
|
const matches = query.sql.match(JSON_REGEX)
|
||||||
|
if (matches && matches.length > 0) {
|
||||||
|
for (let match of matches) {
|
||||||
|
const escaped = escapeDangerousCharacters(match)
|
||||||
|
query.sql = query.sql.replace(match, escaped)
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return await client.query(query.sql, query.bindings || [])
|
return await client.query(query.sql, query.bindings || [])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -106,3 +106,14 @@ exports.deleteEntityMetadata = async (appId, type, entityId) => {
|
||||||
await db.remove(id, rev)
|
await db.remove(id, rev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.escapeDangerousCharacters = string => {
|
||||||
|
return string
|
||||||
|
.replace(/[\\]/g, "\\\\")
|
||||||
|
.replace(/[\/]/g, "\\/")
|
||||||
|
.replace(/[\b]/g, "\\b")
|
||||||
|
.replace(/[\f]/g, "\\f")
|
||||||
|
.replace(/[\n]/g, "\\n")
|
||||||
|
.replace(/[\r]/g, "\\r")
|
||||||
|
.replace(/[\t]/g, "\\t")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue