Implement the check as part of the integration

This commit is contained in:
Adria Navarro 2023-05-12 12:29:52 +02:00
parent 721492e76d
commit 8a1564ef07
2 changed files with 20 additions and 20 deletions

View File

@ -22,7 +22,7 @@ import { MySQLColumn } from "./base/types"
import mysql from "mysql2/promise"
export interface MySQLConfig extends mysql.ConnectionOptions {
interface MySQLConfig extends mysql.ConnectionOptions {
database: string
rejectUnauthorized: boolean
}
@ -152,6 +152,18 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
}
}
async testConnection() {
try {
const [result] = await this.internalQuery(
{ sql: "SELECT 1+1 AS checkRes" },
{ connect: true }
)
return result?.checkRes == 2
} catch (e: any) {
return { error: e.message as string }
}
}
getBindingIdentifier(): string {
return "?"
}
@ -286,21 +298,7 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
}
}
async function validateConnection(config: MySQLConfig) {
const integration = new MySQLIntegration(config)
try {
const [result] = await integration.internalQuery(
{ sql: "SELECT 1+1 AS checkRes" },
{ connect: true }
)
return result?.checkRes == 2
} catch (e: any) {
return { error: e.message as string }
}
}
export default {
schema: SCHEMA,
integration: MySQLIntegration,
validateConnection,
}

View File

@ -1,5 +1,6 @@
import { GenericContainer } from "testcontainers"
import postgres from "../../../../packages/server/src/integrations/postgres"
import mysql from "../../../../packages/server/src/integrations/mysql"
jest.unmock("pg")
jest.unmock("mysql2/promise")
@ -53,8 +54,6 @@ describe("datasource validators", () => {
})
describe("mysql", () => {
const validator = integrations.getValidator[SourceName.MYSQL]!
let host: string
let port: number
@ -72,7 +71,7 @@ describe("datasource validators", () => {
})
it("test valid connection string", async () => {
const result = await validator({
const integration = new mysql.integration({
host,
port,
user: "user",
@ -80,11 +79,12 @@ describe("datasource validators", () => {
password: "password",
rejectUnauthorized: true,
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
it("test invalid database", async () => {
const result = await validator({
const integration = new mysql.integration({
host,
port,
user: "user",
@ -92,13 +92,14 @@ describe("datasource validators", () => {
password: "password",
rejectUnauthorized: true,
})
const result = await integration.testConnection()
expect(result).toEqual({
error: "Access denied for user 'user'@'%' to database 'test'",
})
})
it("test invalid password", async () => {
const result = await validator({
const integration = new mysql.integration({
host,
port,
user: "root",
@ -106,6 +107,7 @@ describe("datasource validators", () => {
password: "wrong",
rejectUnauthorized: true,
})
const result = await integration.testConnection()
expect(result).toEqual({
error:
"Access denied for user 'root'@'172.17.0.1' (using password: YES)",