Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ const getDbCollectionsNames = async (connectionInfo, logger, cb) => {
try {
logger.clear();
await snowflakeHelper.connect(logger, connectionInfo);
const schemasInfo = await snowflakeHelper.getSchemasInfo();
const databaseName = connectionInfo.databaseName;
const schemasInfo = await snowflakeHelper.getSchemasInfo({ databaseName });
logger.log('info', { schemas: schemasInfo }, 'Found schemas');
const namesBySchemas = await snowflakeHelper.getEntitiesNames({ logger });
const namesBySchemas = await snowflakeHelper.getEntitiesNames({ databaseName, logger });

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
"inputType": "text",
"inputTooltip": "Optionally specify the active/current warehouse for the session",
"defaultValue": ""
},
{
"inputLabel": "Database name",
"inputKeyword": "databaseName",
"inputType": "text",
"inputTooltip": "Optionally specify the database name."
}
]
},
Expand Down
39 changes: 26 additions & 13 deletions reverse_engineering/helpers/snowflakeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ const connect = async (
name,
cloudPlatform,
queryRequestTimeout,
databaseName,
},
) => {
if (connection) {
logger.log('info', 'connection already exists', 'Connection');

return connection;
}

const account = getAccount(host);
const accessUrl = getAccessUrl(account);
const timeout = _.toNumber(queryRequestTimeout) || 2 * 60 * 1000;
Expand All @@ -54,7 +61,8 @@ const connect = async (
`Auth type: ${authType}\n` +
`Username: ${username}\n` +
`Warehouse: ${warehouse}\n` +
`Role: ${role}`,
`Role: ${role}\n` +
`Database name: ${databaseName}`,
'Connection',
);

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

const showSchemasByDatabase = async databaseName =>
databaseName ? showSchemasInDatabase(databaseName) : showSchemas();

const showDatabases = () => execute('SHOW DATABASES;');

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

const showExternalTables = () => execute('SHOW EXTERNAL TABLES;');
const showSchemasInDatabase = databaseName => execute(`SHOW SCHEMAS IN DATABASE "${removeQuotes(databaseName)}";`);

const showExternalTables = ({ options = '' } = {}) => execute(`SHOW EXTERNAL TABLES${options};`);

const showViews = () => execute('SHOW VIEWS;');
const showViews = ({ options = '' } = {}) => execute(`SHOW VIEWS${options};`);

const showMaterializedViews = () => execute('SHOW MATERIALIZED VIEWS;');
const showMaterializedViews = ({ options = '' } = {}) => execute(`SHOW MATERIALIZED VIEWS${options};`);

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

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

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

const getSchemasInfo = async () => {
const schemas = await showSchemas().catch(err => [{ status: 'error', message: err.message }]);
const getSchemasInfo = async ({ databaseName }) => {
const schemas = await showSchemasByDatabase(databaseName).catch(err => [{ status: 'error', message: err.message }]);

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

const getEntitiesNames = async ({ logger }) => {
const getEntitiesNames = async ({ databaseName, logger }) => {
const logError = logErrorAndReturnEmptyArray({ logger, query: 'SHOW' });

const databases = await showDatabases().catch(logError);
const databaseQueryOptions = databaseName ? ` IN DATABASE "${removeQuotes(databaseName)}"` : '';
const databases = databaseName ? [{ name: databaseName }] : await showDatabases().catch(logError);
const tablesRows = await showTablesByDatabases(databases).catch(logError);
const flatTableRows = tablesRows.flatMap(row => row.value).filter(Boolean);
const icebergTables = await showIcebergTables().catch(logError);
const icebergTables = await showIcebergTables({ options: databaseQueryOptions }).catch(logError);

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

const externalTableRows = await showExternalTables().catch(logError);
const viewsRows = await showViews().catch(logError);
const materializedViewsRows = await showMaterializedViews().catch(logError);
const externalTableRows = await showExternalTables({ options: databaseQueryOptions }).catch(logError);
const viewsRows = await showViews({ options: databaseQueryOptions }).catch(logError);
const materializedViewsRows = await showMaterializedViews({ options: databaseQueryOptions }).catch(logError);

const entitiesRows = [
...flatTableRows,
Expand Down