nullthoughts / laravel-data-sync
Laravel工具,用于通过源代码控制保持环境间记录同步
v3.0
2022-05-17 12:02 UTC
Requires
- php: ^8.0
- ext-json: *
Requires (Dev)
- orchestra/testbench: ^7.0
This package is auto-updated.
Last update: 2024-09-19 22:37:48 UTC
README
Laravel Data Sync
Laravel工具,用于通过源代码控制保持环境间记录同步
V3.1分支 备注(进行中)
- 在
config/data-sync.php中添加对Laravel 8+模型目录的支持
'namespace' => '\\App\\Models\\',
- 添加将现有模型导出到数据同步文件的能力
php artisan data:export User --criteria=name --criteria=email --except=id
这会生成
[
{
"_name": "Cameron Frye",
"properties->title": "Best Friend",
"phone_numbers->mobile": "555-555-5556",
"_email": "noreply@buellerandco.com",
}
]
- 需要进一步工作以支持远程磁盘。为测试和开发新的V3.1功能,请使用
composer require nullthoughts/laravel-data-sync:v3.1.x-dev
安装
您可以通过composer安装此包
composer require nullthoughts/laravel-data-sync
或在您的 composer.json 中的 require 部分添加此行
{
"require": {
"nullthoughts/laravel-data-sync": "^1.0",
}
}
然后运行 composer install
用法
- 运行
php artisan vendor:publish --provider="nullthoughts\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"以发布配置文件。指定同步数据文件的目录(默认是项目根目录中的新同步目录) - 为每个模型创建一个JSON文件,使用模型名称作为文件名。例如:Product.json 将更新Product模型
- 使用嵌套数组代替硬编码的ID来表示关系
- 运行
php artisan data:sync(或php artisan data:sync --model={model}使用模型标志来指定模型)
可选
如果使用Laravel Forge,您可以在部署时自动运行数据同步。在“站点 -> 应用”中编辑您的部署脚本,包括
if [ -f artisan ]
then
php artisan migrate --force
php artisan data:sync
fi
备注
- 请使用大写字母开头为模型名称的关系作为JSON键(例如:'option_group' => 'OptionGroup')。这对于大小写敏感的文件系统很重要。
- 空值将被跳过
- updateOrCreate的准则/属性以带前导下划线的方式标识
- 嵌套值表示关系,并使用where($key, $value)->first()->id返回
- 可以在 config/data-sync.php 中使用数组设置导入顺序
return [
'path' => base_path('sync'),
'order' => [
'Role',
'Supervisor',
]
];
示例
User.json
[
{
"name": "Ferris Bueller",
"properties->title": "Leisure Consultant",
"phone_numbers->mobile": "555-555-5555",
"phone_numbers->office": "",
"_email": "ferris@buellerandco.com",
"department": {
"name": "Management",
"location": {
"name": "Chicago"
}
}
}
]
翻译为...
User::updateOrCreate([ 'email' => 'ferris@buellerandco.com', ],[ 'name' => 'Ferris Bueller', 'properties->title' => 'Leisure Consultant', 'phone_numbers->mobile' => '555-555-5555', 'department_id' => Department::where('name', 'Management') ->where('location_id', Location::where('name', 'Chicago')->first()->id) ->first() ->id, ]);
Role.json
[
{
"_slug": "update-student-records"
},
{
"_slug": "borrow-ferrari"
},
{
"_slug": "destroy-ferrari"
}
]
翻译为...
Role::updateOrCreate(['slug' => 'update-student-records']); Role::updateOrCreate(['slug' => 'borrow-ferrari']); Role::updateOrCreate(['slug' => 'destroy-ferrari']);
RoleUser.json(带模型的中继表)
[
{
"_user": {
"email": "ferris@buellerandco.com"
},
"_role": {
"slug": "update-student-records"
}
},
{
"_user": {
"email": "ferris@buellerandco.com"
},
"_role": {
"slug": "borrow-ferrari"
}
},
{
"_user": {
"email": "ferris@buellerandco.com"
},
"_role": {
"slug": "destroy-ferrari"
}
}
]
翻译为...
RoleUser::updateOrCreate([ 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id, 'role_id' => Role::where('slug', 'update-student-records')->first()->id, ]); RoleUser::updateOrCreate([ 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id, 'role_id' => Role::where('slug', 'borrow-ferrari')->first()->id, ]); RoleUser::updateOrCreate([ 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id, 'role_id' => Role::where('slug', 'destroy-ferrari')->first()->id, ]);