fix(GOV-011): 支持 MySQL 5.7 系列健康检查
This commit is contained in:
@@ -23,7 +23,7 @@ describe('基础设施只读健康客户端', () => {
|
|||||||
expect(commands).toEqual(['CONNECT', 'PING']);
|
expect(commands).toEqual(['CONNECT', 'PING']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('MySQL 使用固定元数据查询并校验 5.7 与数据库口径', async () => {
|
it('MySQL 使用固定元数据查询并返回实际 5.7.x 版本', async () => {
|
||||||
const queries: string[] = [];
|
const queries: string[] = [];
|
||||||
const client = new MysqlHealthClient(
|
const client = new MysqlHealthClient(
|
||||||
{
|
{
|
||||||
@@ -32,7 +32,7 @@ describe('基础设施只读健康客户端', () => {
|
|||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
version: '5.7.42-log',
|
version: '5.7.41-log',
|
||||||
databaseName: 'a_share_analysis_dev',
|
databaseName: 'a_share_analysis_dev',
|
||||||
charset: 'utf8mb4',
|
charset: 'utf8mb4',
|
||||||
collation: 'utf8mb4_general_ci',
|
collation: 'utf8mb4_general_ci',
|
||||||
@@ -49,7 +49,7 @@ describe('基础设施只读健康客户端', () => {
|
|||||||
|
|
||||||
await expect(client.check()).resolves.toEqual({
|
await expect(client.check()).resolves.toEqual({
|
||||||
status: 'healthy',
|
status: 'healthy',
|
||||||
detail: 'MySQL 5.7.42 / a_share_analysis_dev / +08:00',
|
detail: 'MySQL 5.7.41 / a_share_analysis_dev / +08:00',
|
||||||
});
|
});
|
||||||
expect(queries).toEqual([MYSQL_HEALTH_QUERY]);
|
expect(queries).toEqual([MYSQL_HEALTH_QUERY]);
|
||||||
});
|
});
|
||||||
@@ -76,14 +76,17 @@ describe('基础设施只读健康客户端', () => {
|
|||||||
'a_share_analysis_dev',
|
'a_share_analysis_dev',
|
||||||
);
|
);
|
||||||
|
|
||||||
await expect(createClient({ version: '8.0.40' }).check()).rejects.toThrow(/MySQL 5.7/);
|
for (const version of ['5.6.51', '8.0.40', '5.7', '5.7.x']) {
|
||||||
await expect(createClient({ version: '5.7.41' }).check()).rejects.toThrow(/5\.7\.42/);
|
await expect(createClient({ version }).check()).rejects.toThrow(/MySQL 5\.7\.x/);
|
||||||
|
}
|
||||||
await expect(createClient({ databaseName: 'a_share_analysis' }).check()).rejects.toThrow(
|
await expect(createClient({ databaseName: 'a_share_analysis' }).check()).rejects.toThrow(
|
||||||
/a_share_analysis_dev/,
|
/a_share_analysis_dev/,
|
||||||
);
|
);
|
||||||
|
await expect(createClient({ charset: 'utf8' }).check()).rejects.toThrow(/utf8mb4/);
|
||||||
await expect(createClient({ collation: 'utf8mb4_0900_ai_ci' }).check()).rejects.toThrow(
|
await expect(createClient({ collation: 'utf8mb4_0900_ai_ci' }).check()).rejects.toThrow(
|
||||||
/utf8mb4_general_ci/,
|
/utf8mb4_general_ci/,
|
||||||
);
|
);
|
||||||
|
await expect(createClient({ timezone: 'SYSTEM' }).check()).rejects.toThrow(/\+08:00/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('MySQL 按当前配置校验生产库名', async () => {
|
it('MySQL 按当前配置校验生产库名', async () => {
|
||||||
|
|||||||
@@ -36,8 +36,9 @@ export class MysqlHealthClient implements OnModuleDestroy {
|
|||||||
const [rows] = await this.client.query(MYSQL_HEALTH_QUERY);
|
const [rows] = await this.client.query(MYSQL_HEALTH_QUERY);
|
||||||
const row = (rows as unknown as MysqlMetadataRow[])[0];
|
const row = (rows as unknown as MysqlMetadataRow[])[0];
|
||||||
if (!row) throw new Error('MySQL 未返回元数据');
|
if (!row) throw new Error('MySQL 未返回元数据');
|
||||||
if (!/^5\.7\.42(?:[-+.]|$)/.test(row.version)) {
|
const versionMatch = /^(5\.7\.\d+)(?:[-+].*)?$/.exec(row.version ?? '');
|
||||||
throw new Error('MySQL 5.7.42 版本要求未满足');
|
if (!versionMatch) {
|
||||||
|
throw new Error('MySQL 5.7.x 版本要求未满足');
|
||||||
}
|
}
|
||||||
if (row.databaseName !== this.expectedDatabase) {
|
if (row.databaseName !== this.expectedDatabase) {
|
||||||
throw new Error(`MySQL 健康检查必须连接 ${this.expectedDatabase}`);
|
throw new Error(`MySQL 健康检查必须连接 ${this.expectedDatabase}`);
|
||||||
@@ -50,7 +51,7 @@ export class MysqlHealthClient implements OnModuleDestroy {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
status: 'healthy',
|
status: 'healthy',
|
||||||
detail: `MySQL 5.7.42 / ${row.databaseName} / ${row.timezone}`,
|
detail: `MySQL ${versionMatch[1]} / ${row.databaseName} / ${row.timezone}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ export class MysqlHealthClient implements OnModuleDestroy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 创建最多两个连接的只读健康池,并显式应用 MySQL 5.7.42 会话时区。 */
|
/** 创建最多两个连接的只读健康池,并显式应用 MySQL 5.7.x 会话时区。 */
|
||||||
export function createMysqlHealthClient(config: MysqlRuntimeConfig): MysqlHealthClient {
|
export function createMysqlHealthClient(config: MysqlRuntimeConfig): MysqlHealthClient {
|
||||||
const pool: Pool = createPool({
|
const pool: Pool = createPool({
|
||||||
host: config.host,
|
host: config.host,
|
||||||
|
|||||||
Reference in New Issue
Block a user