-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced `DatabaseForker` for efficient database forking, particularly useful in unit testing environments. - Added configuration options for database forking in various modules and tests. - Enhanced `MysqlDataSource` with new properties for improved database connection handling. - **Documentation** - Updated README with instructions for unit test database configuration. - **Tests** - Adjusted database initialization and cleanup processes in unit tests to utilize `DatabaseForker`. - Modified database configurations in test fixtures to support new forking functionality. - **Refactor** - Refactored database connection and forking logic across multiple modules to integrate `DatabaseForker`. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
13 changed files
with
142 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { DataSourceOptions } from './MySqlDataSource'; | ||
import { RDSClient } from '@eggjs/rds'; | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
import assert from 'node:assert'; | ||
|
||
export class DatabaseForker { | ||
private readonly env: string; | ||
private readonly options: DataSourceOptions; | ||
|
||
constructor(env: string, options: DataSourceOptions) { | ||
this.env = env; | ||
this.options = options; | ||
} | ||
|
||
shouldFork() { | ||
return this.env === 'unittest' && this.options.forkDb; | ||
} | ||
|
||
async forkDb(dalDir: string) { | ||
assert(this.shouldFork(), 'fork db only run in unittest'); | ||
// 尽早判断不应该 fork,避免对 rds pool 配置造成污染 | ||
try { | ||
await fs.access(dalDir); | ||
} catch (_) { | ||
return; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { name, initSql, forkDb, database, ...mysqlOptions } = this.options; | ||
const client = new RDSClient(Object.assign(mysqlOptions)); | ||
const conn = await client.getConnection(); | ||
await this.doCreateUtDb(conn); | ||
await this.forkTables(conn, dalDir); | ||
conn.release(); | ||
await client.end(); | ||
} | ||
|
||
private async forkTables(conn, dalDir: string) { | ||
const sqlDir = path.join(dalDir, 'structure'); | ||
const structureFiles = await fs.readdir(sqlDir); | ||
const sqlFiles = structureFiles.filter(t => t.endsWith('.sql')); | ||
for (const sqlFile of sqlFiles) { | ||
await this.doForkTable(conn, path.join(sqlDir, sqlFile)); | ||
} | ||
} | ||
|
||
private async doForkTable(conn, sqlFileName: string) { | ||
const sqlFile = await fs.readFile(sqlFileName, 'utf8'); | ||
const sqls = sqlFile.split(';').filter(t => !!t.trim()); | ||
for (const sql of sqls) { | ||
await conn.query(sql); | ||
} | ||
} | ||
|
||
private async doCreateUtDb(conn) { | ||
await conn.query(`CREATE DATABASE IF NOT EXISTS ${this.options.database};`); | ||
await conn.query(`use ${this.options.database};`); | ||
} | ||
|
||
async destroy() { | ||
assert(this.shouldFork(), 'fork db only run in unittest'); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { name, initSql, forkDb, database, ...mysqlOptions } = this.options; | ||
const client = new RDSClient(Object.assign(mysqlOptions)); | ||
await client.query(`DROP DATABASE ${database}`); | ||
await client.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ module.exports = function() { | |
security: { | ||
csrf: { | ||
ignoreJSON: false, | ||
} | ||
}, | ||
}, | ||
}; | ||
return config; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
dataSource: | ||
foo: | ||
connectionLimit: 100 | ||
database: 'test' | ||
database: 'test_dal_plugin' | ||
host: '127.0.0.1' | ||
user: root | ||
port: 3306 | ||
timezone: '+08:00' | ||
forkDb: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
dataSource: | ||
foo: | ||
connectionLimit: 100 | ||
database: 'test' | ||
database: 'test_dal_standalone' | ||
host: '127.0.0.1' | ||
user: root | ||
port: 3306 | ||
timezone: '+08:00' | ||
forkDb: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters