Skip to content

Commit c5d47b1

Browse files
committed
fix: convert SQL integration credentials to normalized format for toolkit
1 parent 915fa56 commit c5d47b1

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/notebooks/deepnote/integrations/sqlIntegrationEnvironmentVariablesProvider.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,40 @@ function getSqlEnvVarName(integrationId: string): string {
2121

2222
/**
2323
* Converts integration configuration to the JSON format expected by the SQL execution code.
24+
* The format must match what deepnote_toolkit expects:
25+
* {
26+
* "url": "sqlalchemy_connection_url",
27+
* "params": {},
28+
* "param_style": "qmark" | "format" | etc.
29+
* }
2430
*/
2531
function convertIntegrationConfigToJson(config: IntegrationConfig): string {
2632
switch (config.type) {
27-
case IntegrationType.Postgres:
33+
case IntegrationType.Postgres: {
34+
// Build PostgreSQL connection URL
35+
// Format: postgresql://username:password@host:port/database
36+
const encodedUsername = encodeURIComponent(config.username);
37+
const encodedPassword = encodeURIComponent(config.password);
38+
const url = `postgresql://${encodedUsername}:${encodedPassword}@${config.host}:${config.port}/${config.database}`;
39+
2840
return JSON.stringify({
29-
type: 'postgres',
30-
host: config.host,
31-
port: config.port,
32-
database: config.database,
33-
username: config.username,
34-
password: config.password,
35-
ssl: config.ssl ?? false
41+
url: url,
42+
params: config.ssl ? { sslmode: 'require' } : {},
43+
param_style: 'format'
3644
});
45+
}
3746

38-
case IntegrationType.BigQuery:
47+
case IntegrationType.BigQuery: {
48+
// BigQuery uses a special URL format
3949
return JSON.stringify({
40-
type: 'bigquery',
41-
project_id: config.projectId,
42-
credentials: JSON.parse(config.credentials) // Parse the JSON string to an object
50+
url: 'bigquery://?user_supplied_client=true',
51+
params: {
52+
project_id: config.projectId,
53+
credentials: JSON.parse(config.credentials)
54+
},
55+
param_style: 'format'
4356
});
57+
}
4458

4559
default:
4660
throw new Error(`Unsupported integration type: ${(config as IntegrationConfig).type}`);

src/notebooks/deepnote/integrations/sqlIntegrationEnvironmentVariablesProvider.unit.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,9 @@ suite('SqlIntegrationEnvironmentVariablesProvider', () => {
107107
// Check that the environment variable is set
108108
assert.property(envVars, 'SQL_MY_POSTGRES_DB');
109109
const credentialsJson = JSON.parse(envVars['SQL_MY_POSTGRES_DB']!);
110-
assert.deepStrictEqual(credentialsJson, {
111-
type: 'postgres',
112-
host: 'localhost',
113-
port: 5432,
114-
database: 'mydb',
115-
username: 'user',
116-
password: 'pass',
117-
ssl: true
118-
});
110+
assert.strictEqual(credentialsJson.url, 'postgresql://user:pass@localhost:5432/mydb');
111+
assert.deepStrictEqual(credentialsJson.params, { sslmode: 'require' });
112+
assert.strictEqual(credentialsJson.param_style, 'format');
119113
});
120114

121115
test('Returns environment variable for BigQuery integration', async () => {
@@ -144,11 +138,12 @@ suite('SqlIntegrationEnvironmentVariablesProvider', () => {
144138
// Check that the environment variable is set
145139
assert.property(envVars, 'SQL_MY_BIGQUERY');
146140
const credentialsJson = JSON.parse(envVars['SQL_MY_BIGQUERY']!);
147-
assert.deepStrictEqual(credentialsJson, {
148-
type: 'bigquery',
141+
assert.strictEqual(credentialsJson.url, 'bigquery://?user_supplied_client=true');
142+
assert.deepStrictEqual(credentialsJson.params, {
149143
project_id: 'my-project',
150144
credentials: { type: 'service_account', project_id: 'my-project' }
151145
});
146+
assert.strictEqual(credentialsJson.param_style, 'format');
152147
});
153148

154149
test('Handles multiple SQL cells with same integration', async () => {

0 commit comments

Comments
 (0)