postgres - readonly connection

This commit is contained in:
Jan Prochazka 2022-03-17 13:27:33 +01:00
parent 8f1d76fd2a
commit 29591a613a
2 changed files with 13 additions and 7 deletions

View File

@ -6,9 +6,9 @@ const Analyser = require('./Analyser');
const pg = require('pg'); const pg = require('pg');
const { createBulkInsertStreamBase, makeUniqueColumnNames } = require('dbgate-tools'); const { createBulkInsertStreamBase, makeUniqueColumnNames } = require('dbgate-tools');
pg.types.setTypeParser(1082, 'text', (val) => val); // date pg.types.setTypeParser(1082, 'text', val => val); // date
pg.types.setTypeParser(1114, 'text', (val) => val); // timestamp without timezone pg.types.setTypeParser(1114, 'text', val => val); // timestamp without timezone
pg.types.setTypeParser(1184, 'text', (val) => val); // timestamp pg.types.setTypeParser(1184, 'text', val => val); // timestamp
function extractPostgresColumns(result) { function extractPostgresColumns(result) {
if (!result || !result.fields) return []; if (!result || !result.fields) return [];
@ -31,7 +31,7 @@ const drivers = driverBases.map(driverBase => ({
...driverBase, ...driverBase,
analyserClass: Analyser, 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; let options = null;
if (engine == 'redshift@dbgate-plugin-postgres') { if (engine == 'redshift@dbgate-plugin-postgres') {
@ -67,6 +67,11 @@ const drivers = driverBases.map(driverBase => ({
const client = new pg.Client(options); const client = new pg.Client(options);
await client.connect(); await client.connect();
if (isReadOnly) {
await this.query(client, 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY');
}
return client; return client;
}, },
async close(pool) { async close(pool) {

View File

@ -41,15 +41,16 @@ const postgresDriverBase = {
// showConnectionField: (field, values) => // showConnectionField: (field, values) =>
// ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field), // ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field),
getQuerySplitterOptions: () => postgreSplitterOptions, getQuerySplitterOptions: () => postgreSplitterOptions,
readOnlySessions: true,
databaseUrlPlaceholder: 'e.g. postgresql://user:password@localhost:5432/default_database', databaseUrlPlaceholder: 'e.g. postgresql://user:password@localhost:5432/default_database',
showConnectionField: (field, values) => { showConnectionField: (field, values) => {
if (field == 'useDatabaseUrl') return true; if (field == 'useDatabaseUrl') return true;
if (values.useDatabaseUrl) { 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 => { beforeConnectionSave: connection => {
@ -153,7 +154,7 @@ const redshiftDriver = {
title: 'Amazon Redshift', title: 'Amazon Redshift',
defaultPort: 5439, defaultPort: 5439,
databaseUrlPlaceholder: 'e.g. redshift-cluster-1.xxxx.redshift.amazonaws.com:5439/dev', 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 => { beforeConnectionSave: connection => {
const { databaseUrl } = connection; const { databaseUrl } = connection;
if (databaseUrl) { if (databaseUrl) {