phparrot / parrot
基于规则的通过采样操作数据库生成测试数据库。由 maple-syrup-group/dbsampler 分支而来
Requires
- doctrine/dbal: ^2.5
- fakerphp/faker: ^1.13
- monolog/monolog: ^1.22||^2.0.0
- pimple/pimple: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^9
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.7
README
一种通用工具,用于从数据库中提取和清理选定的表作为测试数据。在json配置文件的控制下,将数据库的子集从一台数据库复制到另一台。然后可以将后者数据库导出到SQL文件作为测试数据文件。
为什么选择PHParrot?
小时候我们去探望祖母的时候,我和我哥哥会等到所有的成年人离开房间,然后不停地对我们祖母的宠物鹦鹉重复我们所知道的(虽然非常温和)脏话,希望它会模仿我们。
因为这个工具(鹦鹉)从一处复制(文本)到另一处(并且因为这似乎是给项目取一个包含“PHP”的项目名的绝佳机会),所以“PHParrot”似乎很合适。
顺便说一句:祖母的鹦鹉从未重复过我们试图教它的任何词,但它学会了打嗝(我叔叔的影响)。
用法
- 创建您希望填充的目标数据库。工具只会输出到现有数据库。目标数据库的内容将被删除。
- 使用数据库服务器配置创建
config/credentials.json - 创建
config/*.db.json文件以定义每个所需数据库的映射 - 运行
bin/dbsampler.php
配置格式
所有配置文件都位于 config 子目录下。文件 不能 包含注释,因为JSON格式不支持此功能,但作为惯例,如果可能,将忽略名为“comment”的字段。
credentials.json
"driver": "pdo_mysql" 目前假定,但这可能在将来改变。
MySQL
参见 config/credentials.dist.json
{
"driver": "pdo_mysql",
"dbUser": "root",
"dbPassword": "SOMEPASSWORD",
"dbHost": "127.0.0.1"
}
如果您需要不同的源和目标服务器,则变为:
{
"source": {
"driver": "pdo_mysql",
"dbUser": "root",
"dbPassword": "SOMEPASSWORD",
"dbHost": "sourceDB.example.com"
},
"dest" : {
"driver": "pdo_mysql",
"dbUser": "root",
"dbPassword": "SOMEPASSWORD",
"dbHost": "127.0.0.1"
}
}
如果您需要准备连接,请添加一个 initialSql 章节来准备连接
{
"source": {
"driver": "pdo_mysql",
"dbUser": "root",
"dbPassword": "SOMEPASSWORD",
"dbHost": "sourceDB.example.com",
"initialSql": [
"SET NAMES UTF8"
]
},
"dest": {
"driver": "pdo_mysql",
"dbUser": "root",
"dbPassword": "SOMEPASSWORD",
"dbHost": "127.0.0.1",
"initialSql": [
"SET NAMES UTF8",
"SET foreign_key_checks = 0"
]
}
}
Sqlite
参见 config/credentials.dist.json
{
"driver": "pdo_sqlite",
"directory": "..\/path\/to\/sqlite-dbs"
}
路径默认相对于配置文件,除非它们以“/”开头。要迁移的Sqlite数据库默认为此目录中的 *.sqlite 文件
dbname.db.json
{
"name": "small-sqlite-test", # Configuration name
"sourceDb": "small-source", # Name of the source DB
"destDb": "small-dest", # Name of the destination DB. This DB will get trashed
"tables": { # A set of tables to be copied over. Each table is defined as "table": config
# Every config stanza requires a sampler field. For now, look these up in
# \PHParrot\Parrot\MigrationConfigProcessor::$samplerMap
# All other fields depend on the specific sampler being used; these should
# all be documented in their own class files in src/Sample
"fruits": {
"sampler": "matched",
"constraints": {
"name": [
"apple",
"pear"
]
},
"remember": {
"id": "fruit_ids" # Cross-referencing is supported by "remember" stanzas
# These take the field name of which the values are to be remembered
# matched to a variable name in which the values will be stored
# Note: Variable declarations do not include a '$' symbol
# References MUST be 'remember'ed before being used, there is no
} # dependency resolution here, so order your config appropriately
},
"vegetables": {
"sampler": "NewestById",
"idField": "id",
"quantity": 2
},
"fruit_x_basket": {
"sampler": "matched",
"constraints": {
"fruit_id": "$fruit_ids" # Remembered variables, with $ sign, can be used as cross-references
# This will expand to all ids of the fruits table matched above
},
"where" : [
"basket_id > 1" # The matched sampler can also accept a list of arbitrary WHERE clauses
],
"remember": {
"basket_id": "basket_ids"
}
},
"baskets": {
"sampler": "matched", # samplers support field cleaners that are defined in
# \PHParrot\Parrot\FieldCleanerProvider::getCleanerByName
# They modify or replace the content of the field that they are keyed to
"constraints": {
"id": "$basket_ids"
},
"cleanFields": {
"name": "fakefullname"
}
}
},
"views": [ # view support is experimental
"some_view" # views are specified as name only but format may change
] # The destination's CURRENT_USER() is used as the DEFINER for MySQL DBs
}
"Faker" 清理器
任何不需要参数的 'faker' (fzaninotto/faker) 生成器都可以通过使用 "name": "faker:GENERATOR" 在 cleanFields 章节中直接使用,例如:
"cleanFields": {
"ip": "faker:ipv4"
},
扩展项目
该工具的设计主要是通过添加自定义采样器(必须实现 \PHParrot\Parrot\Sampler\Sampler)和清理器(必须实现 \PHParrot\Parrot\Cleaner\FieldCleaner 并使用 \PHParrot\Parrot\Cleaner\RowCleaner::registerCleaner 进行注册)来扩展。
目前,只支持mysql和sqlite数据库,但这也可能被扩展。
此工具不转换数据库驱动程序。这意味着如果您的源数据库是MySQL,则目标数据库也必须是MySQL。