Skip to content

Commit

Permalink
syncing up to a938f998fbdf3364caf6ed9ae03252dfc75b8c9f
Browse files Browse the repository at this point in the history
Co-authored-by: Joey Greco <57115019+joeyagreco@users.noreply.github.com>
  • Loading branch information
superblocksadmin and joeyagreco committed Jan 7, 2025
1 parent d7e04ad commit 8559bdc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## vNext

- ...
- Updated Snowflake integration to not include `INFORMATION_SCHEMA` data in metadata

## v1.18.0

Expand Down
25 changes: 3 additions & 22 deletions workers/javascript/packages/plugins/snowflake/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@superblocks/shared';
import { isEmpty } from 'lodash';
import { Snowflake } from './Snowflake';
import { connectionOptionsFromDatasourceConfiguration } from './util';
import { connectionOptionsFromDatasourceConfiguration, getMetadataQuery } from './util';

export default class SnowflakePlugin extends DatabasePluginPooled<Snowflake, SnowflakeDatasourceConfiguration> {
protected readonly parameterType = '?';
Expand Down Expand Up @@ -63,11 +63,11 @@ export default class SnowflakePlugin extends DatabasePluginPooled<Snowflake, Sno
const schema = auth?.custom?.schema?.value;
try {
rows = await this.executeQuery(() => {
return client.execute(this.getMetadataQuery(database, schema));
return client.execute(getMetadataQuery(database, schema));
});
} catch (err) {
rows = await this.executeQuery(() => {
return client.execute(this.getMetadataQuery(database, schema, false));
return client.execute(getMetadataQuery(database, schema, false));
});
}
} catch (err) {
Expand Down Expand Up @@ -119,25 +119,6 @@ export default class SnowflakePlugin extends DatabasePluginPooled<Snowflake, Sno
await client.destroy();
}

private getMetadataQuery(database: string, schema?: string, dbNameQuoted = true) {
let query: string;
if (dbNameQuoted) {
query = `select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM "${database}"."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN "${database}"."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME `;
} else {
query = `select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM ${database}."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN ${database}."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME `;
}
if (schema) {
query += ` WHERE c.TABLE_SCHEMA ILIKE '${schema}'`;
}
query += ` ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION ASC`;

return query;
}

private getTestQuery(database?: string, schema?: string, dbNameQuoted = true) {
if (!database && !schema) {
return 'SHOW TABLES LIMIT 1;';
Expand Down
34 changes: 33 additions & 1 deletion workers/javascript/packages/plugins/snowflake/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SnowflakeDatasourceConfiguration } from '@superblocks/shared';

import { connectionOptionsFromDatasourceConfiguration } from './util';
import { connectionOptionsFromDatasourceConfiguration, getMetadataQuery } from './util';

describe('connectionOptionsFromDatasourceConfiguration', () => {
it('works for fields when connectionType is not given (backwards compatible)', async () => {
Expand Down Expand Up @@ -247,3 +247,35 @@ oBHJaYtVCXd3VBWCVLcfnw==
);
});
});

describe('getMetadataQuery', () => {
it('without schema, not quoted', async () => {
const metadataQuery = getMetadataQuery('db');

expect(metadataQuery).toEqual(
`select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM "db"."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN "db"."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME WHERE c.TABLE_SCHEMA != 'INFORMATION_SCHEMA' ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION ASC;`
);
});

it('with schema, not quoted', async () => {
const metadataQuery = getMetadataQuery('db', 'schema');

expect(metadataQuery).toEqual(
`select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM "db"."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN "db"."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME WHERE c.TABLE_SCHEMA ILIKE 'schema' ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION ASC;`
);
});

it('quoted', async () => {
const metadataQuery = getMetadataQuery('"db"', 'schema', false);

expect(metadataQuery).toEqual(
`select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM "db"."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN "db"."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME WHERE c.TABLE_SCHEMA ILIKE 'schema' ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION ASC;`
);
});
});
21 changes: 21 additions & 0 deletions workers/javascript/packages/plugins/snowflake/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ export function connectionOptionsFromDatasourceConfiguration(datasourceConfigura
}
}

export function getMetadataQuery(database: string, schema?: string, dbNameQuoted = true): string {
let query: string;
if (dbNameQuoted) {
query = `select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM "${database}"."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN "${database}"."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME`;
} else {
query = `select c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.ORDINAL_POSITION, c.DATA_TYPE, t.TABLE_TYPE
FROM ${database}."INFORMATION_SCHEMA"."COLUMNS" as c
LEFT JOIN ${database}."INFORMATION_SCHEMA"."TABLES" AS t ON t.TABLE_NAME = c.TABLE_NAME`;
}
if (schema) {
query += ` WHERE c.TABLE_SCHEMA ILIKE '${schema}'`;
} else {
query += ` WHERE c.TABLE_SCHEMA != 'INFORMATION_SCHEMA' `;
}
query += ` ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION ASC;`;

return query;
}

function handleMissingFields(missingFields: string[]) {
if (missingFields.length > 0) {
throw new IntegrationError(`Missing required fields: ${missingFields}`, ErrorCode.INTEGRATION_MISSING_REQUIRED_FIELD);
Expand Down

0 comments on commit 8559bdc

Please sign in to comment.