axelerant / mdtt
数据迁移测试工具
0.2.0-alpha
2022-04-29 05:32 UTC
Requires
- ext-mysqli: *
- guzzlehttp/guzzle: ^7.4
- halaxa/json-machine: ^1.1
- monolog/monolog: ^2.4
- phpunit/phpunit: ^9.5
- sendgrid/sendgrid: ^7.11
- symfony/config: ^6.0
- symfony/console: ^6.0
- symfony/dependency-injection: ^6.0
- symfony/dotenv: ^6.0
- symfony/http-kernel: ^6.0
- symfony/yaml: ^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.7
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.4
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-16 19:24:25 UTC
README
该工具可以帮助您验证迁移数据的质量。
安装
composer require --dev axelerant/mdtt
用法
基本上,您需要遵循以下步骤
- 指定测试规范
- 指定测试定义
- 可选,指定转换插件
- 运行测试
您可以在这里找到工具使用的基礎模板。
测试规范
请在tests/mdtt
目录内指定测试规范。规范必须写入spec.php
文件内。以下是一个示例规范
<?php return [ 'databases' => [ // Source database credentials 'source_db' => [ 'database' => "db_drupal7", 'username' => "db", 'password' => "db", 'host' => "127.0.0.1", 'port' => "59002", ], // Destination database credentials "destination_db" => [ 'database' => "db", 'username' => "db", 'password' => "db", 'host' => "127.0.0.1", 'port' => "59002", ], ], 'http' => [ // JSON datasource basic authentication credetials 'source' => [ 'username' => 'username', 'password' => 'fubar', // Basic authentication protocol: basic, digest, or ntlm. // Protocol is optional. If not mentioned, basic is considered. 'protocol' => 'ntlm', ], 'destination' => [ 'username' => 'username', 'password' => 'puffball', ] ] ];
测试定义
测试定义以yaml
格式编写,并且必须存在于tests/mdtt
目录内。假设您想要验证迁移的用户数据。创建一个名为validate_users.yml
的文件。文件名不重要。
数据库
# Test definition ID id: validate_users # Test definition description description: Validates users # Group to which this test belongs to group: migration_validation # The query used for generating the source dataset source: type: database data: "select * from users" # The source database credentials database: source_db # The query used for generating the destination dataset destination: type: database data: "select * from users_field_data" # The destination database credentials database: destination_db # Test cases tests: - # Field from source datasource used in this comparison sourceField: name # Field from destination datasource used in this comparison destinationField: name - sourceField: mail destinationField: mail
JSON
假设源旧系统以以下格式公开数据
{ "jsonapi": { "version": 1.2, }, "results": [ { "employee": { "id": 3453, "info": { "name": "Aloy", "email": "aloy@nora.tribe", "gender": "female", "status": "active" } } }, { "employee": { "id": 3452, "info": { "name": "Varl", "email": "varl@nora.tribe", "gender": "male", "status": "active" } } }, { "employee": { "id": 3450, "info": { "name": "Rost", "email": "rost@nora.tribe", "gender": "male", "status": "inactive" } } } ] }
假设目标新系统以以下格式公开数据
"jsonapi": { "version": 1.2, }, "results": [ { "id": 3453, "name": "Aloy", "email": "aloy@nora.tribe", "gender": "female", "status": "active" }, { "id": 3452, "name": "Varl", "email": "varl@nora.tribe", "gender": "male", "status": "active" }, { "id": 3450, "name": "Rost", "email": "rost@nora.tribe", "gender": "male", "status": "active" } ] }
id: validate_public_apis description: Validate public apis group: migration_validation # The endpoint that returns the source dataset. source: type: json data: https://dev.legacy-system.com/api/v1/users # The pointer where the list of items resides. Refer https://github.com/halaxa/json-machine#what-is-json-pointer-anyway for examples selector: "/results" # Datasource basic authentication credentials. Note that the key is same as mentioned in the test specification. This is optional if the endpoint is publicly accessible. credential: source # The endpoint that returns the destination dataset. destination: type: json data: https://dev.new-system.com/api/v1/users selector: "/results" credential: destination tests: - # `/` separated field which you want to test. # This is applicable when the value you want to test is in nested format. sourceField: "employee/info/name" destinationField: name - sourceField: "employee/info/email" destinationField: email
转换插件
可能存在这样的情况,数据不是直接从源存储,而是在存储到目标数据库之前必须以某种方式转换(例如,删除空白字符)。QA工程师可以编写自己的插件来验证执行转换的业务逻辑。
测试用例看起来像这样
tests: - sourceField: name transform: trim destinationField: name
QA工程师必须在tests/mdtt/src/Plugin/Transform
内指定插件类。文件名(以及类名)必须与测试用例中提到的插件名相同,首字母大写,即Trim
。插件类必须实现\Mdtt\Transform\Transform
接口。
<?php // File location: tests/mdtt/src/Plugin/Transform/Trim.php. class Trim implements \Mdtt\Transform\Transform { /** * @inheritDoc */ public function name(): string { // Specifies the plugin name. // This must be unique, if you have multiple plugin definitions. // The same plugin name must be mentioned in the test case. return "trim"; } /** * @inheritDoc */ public function process(mixed $data): mixed { // The logic that does the transformation. return trim($data); } }
运行测试
./vendor/bin/mdtt run
详细模式
./vendor/bin/mdtt run -vvv
您可以通过这样做来查看所有可用的选项
./vendor/bin/mdtt run --help
指定测试完成后将发送通知的电子邮件地址
./vendor/bin/mdtt run --email foo@bar.mail
日志
默认情况下,日志文件将创建在名为logs
的目录内。该目录将位于测试套件的根目录下。日志文件的命名基于执行测试时的日期和时间。
功能
支持的数据源类型
- 数据库(MySQL)
- JSON
支持的目标类型
- 数据库(MySQL)
- JSON
在测试完成后通知的渠道类型
- 电子邮件
贡献
将该仓库分叉并进行更改。
运行测试
composer test