Validate mysql
This commit is contained in:
parent
f10a66cc42
commit
721492e76d
|
@ -85,8 +85,6 @@ const SCHEMA: Integration = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const TimezoneAwareDateTypes = ["timestamp"]
|
|
||||||
|
|
||||||
function bindingTypeCoerce(bindings: any[]) {
|
function bindingTypeCoerce(bindings: any[]) {
|
||||||
for (let i = 0; i < bindings.length; i++) {
|
for (let i = 0; i < bindings.length; i++) {
|
||||||
const binding = bindings[i]
|
const binding = bindings[i]
|
||||||
|
@ -113,7 +111,7 @@ function bindingTypeCoerce(bindings: any[]) {
|
||||||
|
|
||||||
class MySQLIntegration extends Sql implements DatasourcePlus {
|
class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
private config: MySQLConfig
|
private config: MySQLConfig
|
||||||
private client: any
|
private client?: mysql.Connection
|
||||||
public tables: Record<string, Table> = {}
|
public tables: Record<string, Table> = {}
|
||||||
public schemaErrors: Record<string, string> = {}
|
public schemaErrors: Record<string, string> = {}
|
||||||
|
|
||||||
|
@ -167,7 +165,7 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
}
|
}
|
||||||
|
|
||||||
async disconnect() {
|
async disconnect() {
|
||||||
await this.client.end()
|
await this.client!.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
async internalQuery(
|
async internalQuery(
|
||||||
|
@ -186,10 +184,10 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
? baseBindings
|
? baseBindings
|
||||||
: bindingTypeCoerce(baseBindings)
|
: bindingTypeCoerce(baseBindings)
|
||||||
// Node MySQL is callback based, so we must wrap our call in a promise
|
// Node MySQL is callback based, so we must wrap our call in a promise
|
||||||
const response = await this.client.query(query.sql, bindings)
|
const response = await this.client!.query(query.sql, bindings)
|
||||||
return response[0]
|
return response[0]
|
||||||
} finally {
|
} finally {
|
||||||
if (opts?.connect) {
|
if (opts?.connect && this.client) {
|
||||||
await this.disconnect()
|
await this.disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,7 +286,21 @@ 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 {
|
export default {
|
||||||
schema: SCHEMA,
|
schema: SCHEMA,
|
||||||
integration: MySQLIntegration,
|
integration: MySQLIntegration,
|
||||||
|
validateConnection,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { GenericContainer } from "testcontainers"
|
||||||
import postgres from "../../../../packages/server/src/integrations/postgres"
|
import postgres from "../../../../packages/server/src/integrations/postgres"
|
||||||
|
|
||||||
jest.unmock("pg")
|
jest.unmock("pg")
|
||||||
|
jest.unmock("mysql2/promise")
|
||||||
|
|
||||||
describe("datasource validators", () => {
|
describe("datasource validators", () => {
|
||||||
describe("postgres", () => {
|
describe("postgres", () => {
|
||||||
|
@ -50,4 +51,65 @@ describe("datasource validators", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("mysql", () => {
|
||||||
|
const validator = integrations.getValidator[SourceName.MYSQL]!
|
||||||
|
|
||||||
|
let host: string
|
||||||
|
let port: number
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const container = await new GenericContainer("mysql")
|
||||||
|
.withExposedPorts(3306)
|
||||||
|
.withEnv("MYSQL_ROOT_PASSWORD", "admin")
|
||||||
|
.withEnv("MYSQL_DATABASE", "db")
|
||||||
|
.withEnv("MYSQL_USER", "user")
|
||||||
|
.withEnv("MYSQL_PASSWORD", "password")
|
||||||
|
.start()
|
||||||
|
|
||||||
|
host = container.getContainerIpAddress()
|
||||||
|
port = container.getMappedPort(3306)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("test valid connection string", async () => {
|
||||||
|
const result = await validator({
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
user: "user",
|
||||||
|
database: "db",
|
||||||
|
password: "password",
|
||||||
|
rejectUnauthorized: true,
|
||||||
|
})
|
||||||
|
expect(result).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("test invalid database", async () => {
|
||||||
|
const result = await validator({
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
user: "user",
|
||||||
|
database: "test",
|
||||||
|
password: "password",
|
||||||
|
rejectUnauthorized: true,
|
||||||
|
})
|
||||||
|
expect(result).toEqual({
|
||||||
|
error: "Access denied for user 'user'@'%' to database 'test'",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("test invalid password", async () => {
|
||||||
|
const result = await validator({
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
user: "root",
|
||||||
|
database: "test",
|
||||||
|
password: "wrong",
|
||||||
|
rejectUnauthorized: true,
|
||||||
|
})
|
||||||
|
expect(result).toEqual({
|
||||||
|
error:
|
||||||
|
"Access denied for user 'root'@'172.17.0.1' (using password: YES)",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue