From 8ccdef9acee94792c0d4c93d1ab7cf77ac96870f Mon Sep 17 00:00:00 2001 From: Vitalii Yarmus Date: Wed, 1 Dec 2021 15:22:15 +0200 Subject: [PATCH 1/2] RE: fixed issue when user have not enough priviligies to read view select statement from information_schema --- .../helpers/postgresHelpers/viewHelper.js | 10 ++++++++-- reverse_engineering/helpers/postgresService.js | 15 ++++++++++++++- reverse_engineering/helpers/queryConstants.js | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/reverse_engineering/helpers/postgresHelpers/viewHelper.js b/reverse_engineering/helpers/postgresHelpers/viewHelper.js index 338af58..a62f4a5 100644 --- a/reverse_engineering/helpers/postgresHelpers/viewHelper.js +++ b/reverse_engineering/helpers/postgresHelpers/viewHelper.js @@ -13,8 +13,14 @@ const isViewByName = name => _.endsWith(name, VIEW_SUFFIX); const removeViewNameSuffix = name => name.slice(0, -VIEW_SUFFIX.length); const setViewSuffix = name => `${name}${VIEW_SUFFIX}`; -const generateCreateViewScript = (viewName, viewData) => { - return `CREATE VIEW ${viewName} AS ${viewData.view_definition}`; +const generateCreateViewScript = (viewName, viewData, viewDefinitionFallback = {}) => { + const selectStatement = _.trim(viewData.view_definition || viewDefinitionFallback.definition || ''); + + if (!selectStatement) { + return '' + } + + return `CREATE VIEW ${viewName} AS ${selectStatement}`; }; const prepareViewData = (viewData, viewOptions) => { diff --git a/reverse_engineering/helpers/postgresService.js b/reverse_engineering/helpers/postgresService.js index b9929b1..ead40d5 100644 --- a/reverse_engineering/helpers/postgresService.js +++ b/reverse_engineering/helpers/postgresService.js @@ -315,11 +315,24 @@ module.exports = { viewName = removeViewNameSuffix(viewName); const viewData = await db.query(queryConstants.GET_VIEW_DATA, [viewName, schemaName], true); + const viewDefinitionFallback = + !viewData.view_definition && + (await db.queryTolerant(queryConstants.GET_VIEW_SELECT_STMT_FALLBACK, [viewName, schemaName], true)); const viewOptions = await db.queryTolerant(queryConstants.GET_VIEW_OPTIONS, [viewName, schemaOid], true); - const script = generateCreateViewScript(viewName, viewData); + const script = generateCreateViewScript(viewName, viewData, viewDefinitionFallback); const data = prepareViewData(viewData, viewOptions); + if (!script) { + logger.info('View select statement was not retrieved', { schemaName, viewName }); + + return { + name: viewName, + data, + jsonSchema: {}, + }; + } + return { name: viewName, data, diff --git a/reverse_engineering/helpers/queryConstants.js b/reverse_engineering/helpers/queryConstants.js index 93d69e8..5dea378 100644 --- a/reverse_engineering/helpers/queryConstants.js +++ b/reverse_engineering/helpers/queryConstants.js @@ -150,6 +150,7 @@ const queryConstants = { JOIN pg_catalog.pg_namespace AS foreign_table_namespace ON (pc_foreign_table.relnamespace = foreign_table_namespace.oid) WHERE pcon.conrelid = $1 AND pcon.contype = 'f';`, GET_VIEW_DATA: `SELECT * FROM information_schema.views WHERE table_name = $1 AND table_schema = $2;`, + GET_VIEW_SELECT_STMT_FALLBACK: `SELECT definition FROM pg_views WHERE viewname = $1 AND schemaname = $2;`, GET_VIEW_OPTIONS: ` SELECT reloptions AS view_options, relpersistence AS persistence, From a2368f6510d62476866086d16fea777bf86c6b04 Mon Sep 17 00:00:00 2001 From: Vitalii Yarmus Date: Wed, 1 Dec 2021 15:27:07 +0200 Subject: [PATCH 2/2] RE: fixed issue with jsonSchema when the select statement was not retrieved --- reverse_engineering/helpers/postgresService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reverse_engineering/helpers/postgresService.js b/reverse_engineering/helpers/postgresService.js index ead40d5..84f1b67 100644 --- a/reverse_engineering/helpers/postgresService.js +++ b/reverse_engineering/helpers/postgresService.js @@ -329,7 +329,7 @@ module.exports = { return { name: viewName, data, - jsonSchema: {}, + jsonSchema: { properties: [] }, }; }