From 29591a613a3eb4efe9d4e3dae5be10de01403075 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 17 Mar 2022 13:27:33 +0100 Subject: [PATCH] postgres - readonly connection --- .../dbgate-plugin-postgres/src/backend/drivers.js | 13 +++++++++---- .../dbgate-plugin-postgres/src/frontend/drivers.js | 7 ++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/plugins/dbgate-plugin-postgres/src/backend/drivers.js b/plugins/dbgate-plugin-postgres/src/backend/drivers.js index 397273af..6aaa7aa6 100644 --- a/plugins/dbgate-plugin-postgres/src/backend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/backend/drivers.js @@ -6,9 +6,9 @@ const Analyser = require('./Analyser'); const pg = require('pg'); const { createBulkInsertStreamBase, makeUniqueColumnNames } = require('dbgate-tools'); -pg.types.setTypeParser(1082, 'text', (val) => val); // date -pg.types.setTypeParser(1114, 'text', (val) => val); // timestamp without timezone -pg.types.setTypeParser(1184, 'text', (val) => val); // timestamp +pg.types.setTypeParser(1082, 'text', val => val); // date +pg.types.setTypeParser(1114, 'text', val => val); // timestamp without timezone +pg.types.setTypeParser(1184, 'text', val => val); // timestamp function extractPostgresColumns(result) { if (!result || !result.fields) return []; @@ -31,7 +31,7 @@ const drivers = driverBases.map(driverBase => ({ ...driverBase, analyserClass: Analyser, - async connect({ engine, server, port, user, password, database, databaseUrl, useDatabaseUrl, ssl }) { + async connect({ engine, server, port, user, password, database, databaseUrl, useDatabaseUrl, ssl, isReadOnly }) { let options = null; if (engine == 'redshift@dbgate-plugin-postgres') { @@ -67,6 +67,11 @@ const drivers = driverBases.map(driverBase => ({ const client = new pg.Client(options); await client.connect(); + + if (isReadOnly) { + await this.query(client, 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY'); + } + return client; }, async close(pool) { diff --git a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js index 9e0cd102..29b8b0c9 100644 --- a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js @@ -41,15 +41,16 @@ const postgresDriverBase = { // showConnectionField: (field, values) => // ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field), getQuerySplitterOptions: () => postgreSplitterOptions, + readOnlySessions: true, databaseUrlPlaceholder: 'e.g. postgresql://user:password@localhost:5432/default_database', showConnectionField: (field, values) => { if (field == 'useDatabaseUrl') return true; if (values.useDatabaseUrl) { - return ['databaseUrl'].includes(field); + return ['databaseUrl', 'isReadOnly'].includes(field); } - return ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field); + return ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field); }, beforeConnectionSave: connection => { @@ -153,7 +154,7 @@ const redshiftDriver = { title: 'Amazon Redshift', defaultPort: 5439, databaseUrlPlaceholder: 'e.g. redshift-cluster-1.xxxx.redshift.amazonaws.com:5439/dev', - showConnectionField: (field, values) => ['databaseUrl', 'user', 'password'].includes(field), + showConnectionField: (field, values) => ['databaseUrl', 'user', 'password', 'isReadOnly'].includes(field), beforeConnectionSave: connection => { const { databaseUrl } = connection; if (databaseUrl) {