Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class ManageSQLBuilder extends Builder {
}

/**
* @param {import('../index').ManageBuilderOptions} options
* @param {import('./migration').ManageBuilderOptions} options
*/
createTable(options) {
_validate(options, {
Expand Down
8 changes: 7 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ const createPool = (options, name = null) => {
const key = name ? name :
`${options.host}:${options.port}:${options.user}:${options.password}:${options.database}`;
if (clients[key]) {
return clients[key];
const existingPool = clients[key];

if (existingPool._closed) {
delete clients[key];
} else {
return existingPool;
}
}
const pool = mysql.createPool(options);
clients[key] = pool;
Expand Down
76 changes: 76 additions & 0 deletions tests/client.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

/**
* @type {Chai.ExpectStatic}
*/
let expect = null;
const { createPool } = require('../src/client');

describe('client test case', () => {
before(async function () {
const chai = await import('chai');
expect = chai.expect;
});

it('should create new pool when existing pool is closed', () => {
const options = {
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test_db',
connectionLimit: 10
};

// 创建第一个 pool
const pool1 = createPool(options, 'test_pool');
expect(pool1).to.not.be.null;

// 模拟 pool 被关闭
pool1._closed = true;

// 再次调用 createPool,应该创建新的 pool
const pool2 = createPool(options, 'test_pool');
expect(pool2).to.not.be.null;
expect(pool2).to.not.equal(pool1); // 应该是不同的实例
expect(pool2._closed).to.not.be.true; // 新 pool 应该是活跃的
});

it('should return existing pool when pool is active', () => {
const options = {
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test_db2',
connectionLimit: 10
};

// 创建第一个 pool
const pool1 = createPool(options, 'test_pool2');
expect(pool1).to.not.be.null;

// 再次调用 createPool,应该返回相同的 pool
const pool2 = createPool(options, 'test_pool2');
expect(pool2).to.equal(pool1); // 应该是相同的实例
});

it('should handle pool without name parameter', () => {
const options = {
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test_db3',
connectionLimit: 10
};

// 创建 pool 不指定名称
const pool1 = createPool(options);
expect(pool1).to.not.be.null;

// 再次调用应该返回相同的 pool
const pool2 = createPool(options);
expect(pool2).to.equal(pool1);
});
});