Skip to content

Commit 68e6c15

Browse files
Vitalii4ashackolade-bot
andauthored
HCK-10900: filter list of schemas by database from connection settings if provided, take all data if shcema is not provided (#186)
Co-authored-by: hackolade-bot <[email protected]>
1 parent 5a723e5 commit 68e6c15

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

reverse_engineering/api.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ const getDbCollectionsNames = async (connectionInfo, logger, cb) => {
6060
try {
6161
logger.clear();
6262
await snowflakeHelper.connect(logger, connectionInfo);
63-
const schemasInfo = await snowflakeHelper.getSchemasInfo();
63+
const databaseName = connectionInfo.databaseName;
64+
const schemasInfo = await snowflakeHelper.getSchemasInfo({ databaseName });
6465
logger.log('info', { schemas: schemasInfo }, 'Found schemas');
65-
const namesBySchemas = await snowflakeHelper.getEntitiesNames({ logger });
66+
const namesBySchemas = await snowflakeHelper.getEntitiesNames({ databaseName, logger });
6667

6768
logger.log('info', { entities: namesBySchemas }, 'Found entities');
6869

reverse_engineering/connection_settings_modal/connectionSettingsModalConfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
"inputType": "text",
5959
"inputTooltip": "Optionally specify the active/current warehouse for the session",
6060
"defaultValue": ""
61+
},
62+
{
63+
"inputLabel": "Database name",
64+
"inputKeyword": "databaseName",
65+
"inputType": "text",
66+
"inputTooltip": "Optionally specify the database name."
6167
}
6268
]
6369
},

reverse_engineering/helpers/snowflakeHelper.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ const connect = async (
4040
name,
4141
cloudPlatform,
4242
queryRequestTimeout,
43+
databaseName,
4344
},
4445
) => {
46+
if (connection) {
47+
logger.log('info', 'connection already exists', 'Connection');
48+
49+
return connection;
50+
}
51+
4552
const account = getAccount(host);
4653
const accessUrl = getAccessUrl(account);
4754
const timeout = _.toNumber(queryRequestTimeout) || 2 * 60 * 1000;
@@ -54,7 +61,8 @@ const connect = async (
5461
`Auth type: ${authType}\n` +
5562
`Username: ${username}\n` +
5663
`Warehouse: ${warehouse}\n` +
57-
`Role: ${role}`,
64+
`Role: ${role}\n` +
65+
`Database name: ${databaseName}`,
5866
'Connection',
5967
);
6068

@@ -483,15 +491,20 @@ const showTablesByDatabases = async databases =>
483491
databases.map(database => execute(`SHOW TABLES IN DATABASE "${removeQuotes(database.name)}";`)),
484492
);
485493

494+
const showSchemasByDatabase = async databaseName =>
495+
databaseName ? showSchemasInDatabase(databaseName) : showSchemas();
496+
486497
const showDatabases = () => execute('SHOW DATABASES;');
487498

488499
const showSchemas = () => execute('SHOW SCHEMAS;');
489500

490-
const showExternalTables = () => execute('SHOW EXTERNAL TABLES;');
501+
const showSchemasInDatabase = databaseName => execute(`SHOW SCHEMAS IN DATABASE "${removeQuotes(databaseName)}";`);
502+
503+
const showExternalTables = ({ options = '' } = {}) => execute(`SHOW EXTERNAL TABLES${options};`);
491504

492-
const showViews = () => execute('SHOW VIEWS;');
505+
const showViews = ({ options = '' } = {}) => execute(`SHOW VIEWS${options};`);
493506

494-
const showMaterializedViews = () => execute('SHOW MATERIALIZED VIEWS;');
507+
const showMaterializedViews = ({ options = '' } = {}) => execute(`SHOW MATERIALIZED VIEWS${options};`);
495508

496509
const showIcebergTables = ({ options = '' } = {}) => execute(`SHOW ICEBERG TABLES${options};`);
497510

@@ -507,8 +520,8 @@ const splitEntityNames = names => {
507520

508521
const isView = name => name.slice(-4) === ' (v)';
509522

510-
const getSchemasInfo = async () => {
511-
const schemas = await showSchemas().catch(err => [{ status: 'error', message: err.message }]);
523+
const getSchemasInfo = async ({ databaseName }) => {
524+
const schemas = await showSchemasByDatabase(databaseName).catch(err => [{ status: 'error', message: err.message }]);
512525

513526
if (schemas[0]?.status === 'error') {
514527
return schemas;
@@ -592,19 +605,19 @@ const logTablesMeta = async ({ logger, tables = [], icebergTables = [] }) => {
592605
logger.log('info', combinedMeta, 'Tables metadata');
593606
};
594607

595-
const getEntitiesNames = async ({ logger }) => {
608+
const getEntitiesNames = async ({ databaseName, logger }) => {
596609
const logError = logErrorAndReturnEmptyArray({ logger, query: 'SHOW' });
597-
598-
const databases = await showDatabases().catch(logError);
610+
const databaseQueryOptions = databaseName ? ` IN DATABASE "${removeQuotes(databaseName)}"` : '';
611+
const databases = databaseName ? [{ name: databaseName }] : await showDatabases().catch(logError);
599612
const tablesRows = await showTablesByDatabases(databases).catch(logError);
600613
const flatTableRows = tablesRows.flatMap(row => row.value).filter(Boolean);
601-
const icebergTables = await showIcebergTables().catch(logError);
614+
const icebergTables = await showIcebergTables({ options: databaseQueryOptions }).catch(logError);
602615

603616
await logTablesMeta({ logger, tables: flatTableRows, icebergTables });
604617

605-
const externalTableRows = await showExternalTables().catch(logError);
606-
const viewsRows = await showViews().catch(logError);
607-
const materializedViewsRows = await showMaterializedViews().catch(logError);
618+
const externalTableRows = await showExternalTables({ options: databaseQueryOptions }).catch(logError);
619+
const viewsRows = await showViews({ options: databaseQueryOptions }).catch(logError);
620+
const materializedViewsRows = await showMaterializedViews({ options: databaseQueryOptions }).catch(logError);
608621

609622
const entitiesRows = [
610623
...flatTableRows,

0 commit comments

Comments
 (0)