Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-db snowflake #11485

Merged
merged 1 commit into from
Mar 19, 2025
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
6 changes: 6 additions & 0 deletions connectors/src/connectors/snowflake/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const snowflake = async ({
}

case "fetch-schemas": {
if (args.database === undefined) {
throw new Error("Database is required for fetching schemas");
}
const schemas = await fetchSchemas({
credentials,
fromDatabase: args.database,
Expand All @@ -63,6 +66,9 @@ export const snowflake = async ({
}

case "fetch-tables": {
if (args.database === undefined && args.schema === undefined) {
throw new Error("Database or Schema is required for fetching tables");
}
const tables = await fetchTables({
credentials,
fromDatabase: args.database,
Expand Down
47 changes: 30 additions & 17 deletions connectors/src/connectors/snowflake/lib/snowflake_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,19 @@ export const fetchDatabases = async ({
};

/**
* Fetch the tables available in the Snowflake account. We have no guarantee to get all the schemas
* from all the DBs so we generaly want to have fromDatabase defined.
* Fetch the tables available in the Snowflake account. `fromDatabase` is required because there is
* no guarantee to get all schemas otherwise.
*/
export const fetchSchemas = async ({
credentials,
fromDatabase,
connection,
}: {
credentials: SnowflakeCredentials;
fromDatabase?: string;
fromDatabase: string;
connection?: Connection;
}): Promise<Result<Array<RemoteDBSchema>, Error>> => {
const query = fromDatabase
? `SHOW SCHEMAS IN DATABASE ${fromDatabase}`
: "SHOW SCHEMAS";
const query = `SHOW SCHEMAS IN DATABASE ${fromDatabase}`;
return _fetchRows<RemoteDBSchema>({
credentials,
query,
Expand Down Expand Up @@ -251,25 +249,40 @@ export const fetchTree = async (
(db) => !EXCLUDE_DATABASES.includes(db.name)
);

const schemasRes = await fetchSchemas({ credentials, connection });
if (schemasRes.isErr()) {
return schemasRes;
const allSchemas: RemoteDBSchema[] = [];
const allTables: RemoteDBTable[] = [];

for (const db of databases) {
const schemasRes = await fetchSchemas({
credentials,
fromDatabase: db.name,
connection,
});
if (schemasRes.isErr()) {
return schemasRes;
}
allSchemas.push(...schemasRes.value);

const tablesRes = await fetchTables({
credentials,
fromDatabase: db.name,
connection,
});
if (tablesRes.isErr()) {
return tablesRes;
}
allTables.push(...tablesRes.value);
}
const schemas = schemasRes.value.filter(
(s) => !EXCLUDE_SCHEMAS.includes(s.name)
);

const schemas = allSchemas.filter((s) => !EXCLUDE_SCHEMAS.includes(s.name));
localLogger.info(
{
schemasCount: schemas.length,
},
"Found schemas in Snowflake"
);

const tablesRes = await fetchTables({ credentials, connection });
if (tablesRes.isErr()) {
return tablesRes;
}
const tables = tablesRes.value;
const tables = allTables;
localLogger.info(
{
tablesCount: tables.length,
Expand Down