Skip to content

Commit

Permalink
Pass md token as part of database url
Browse files Browse the repository at this point in the history
  • Loading branch information
whscullin committed May 21, 2024
1 parent b9df596 commit 5c030bc
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/malloy-db-duckdb/src/duckdb_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,25 @@ export class DuckDBConnection extends DuckDBCommon {
resolve();
} else {
if (this.isMotherDuck) {
if (this.motherDuckToken) {
process.env['motherduck_token'] = this.motherDuckToken;
process.env['MOTHERDUCK_TOKEN'] = this.motherDuckToken;
}
if (
!this.motherDuckToken &&
!process.env['motherduck_token'] &&
!process.env['MOTHERDUCK_TOKEN']
) {
this.setupError = new Error('Please set your MotherDuck Token');
// Resolve instead of error because errors cannot be caught.
return resolve();
}
if (this.motherDuckToken) {
try {
const dbUrl = new URL(this.databasePath);
dbUrl.searchParams.set('motherduck_token', this.motherDuckToken);
this.databasePath = dbUrl.toString();
} catch {
// eslint-disable-next-line no-console
console.warn('Unable to update MotherDuck URL with token');
}
}
}
const database = new Database(
this.databasePath,
Expand All @@ -154,17 +161,13 @@ export class DuckDBConnection extends DuckDBCommon {
this.setupError = err;
} else {
this.connection = database.connect();
// Don't cache MotherDuck connections so they can
// pick up fresh credentials if necessary.
if (!this.isMotherDuck) {
const activeDB: ActiveDB = {
database,
connections: [],
};
DuckDBConnection.activeDBs[this.databasePath] = activeDB;
const activeDB: ActiveDB = {
database,
connections: [],
};
DuckDBConnection.activeDBs[this.databasePath] = activeDB;

activeDB.connections.push(this.connection);
}
activeDB.connections.push(this.connection);
}
resolve();
}
Expand Down

0 comments on commit 5c030bc

Please sign in to comment.