distinctm/laravel-data-sync

此包已被废弃且不再维护。作者建议使用 nullthoughts/laravel-data-sync 包。

Laravel工具,用于通过源控制在不同环境之间同步记录

v3.0 2022-05-17 12:02 UTC

README

Total Downloads Latest Stable Version Travis CI Build Status: Master

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,
    ]);