Skip to content

Commit

Permalink
Merge commit 'refs/pull/7/head' of https://github.com/hackolade/Postg…
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed Dec 1, 2021
2 parents 1a2dcf7 + a2368f6 commit 78074b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions reverse_engineering/helpers/postgresHelpers/viewHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
15 changes: 14 additions & 1 deletion reverse_engineering/helpers/postgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: { properties: [] },
};
}

return {
name: viewName,
data,
Expand Down
1 change: 1 addition & 0 deletions reverse_engineering/helpers/queryConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 78074b9

Please sign in to comment.