fndmiranda/data-migration

同步和迁移应用与数据库之间的数据

1.4.6 2019-09-20 13:43 UTC

This package is auto-updated.

Last update: 2024-09-21 00:29:30 UTC


README

Total Downloads Latest Stable Version License

Laravel的数据迁移

该软件包简化了应用和数据库之间数据迁移和同步的过程,允许您控制例如设置或权限列表。提供资源来查看状态和尚未执行的改变,迁移和同步数据。

安装

composer require fndmiranda/data-migration

使用

您可以通过 data-migration:make Artisan 命令生成数据迁移

php artisan data-migration:make PermissionDataMigration

此命令将在 app/DataMigrations/PermissionDataMigration.php 生成数据迁移。数据迁移将包含 modeldataoptions 方法。

<?php

namespace App\DataMigrations;

use Fndmiranda\DataMigration\Contracts\DataMigration;

class PermissionDataMigration implements DataMigration
{
    /**
     * Order to execute this data-migration.
     *
     * @var int
     */
    protected $order = 0;
    
    /**
     * Tag to filter on data-migrations search.
     *
     * @var string
     */
    protected $tag = 'production';

    /**
     * Get the model being used by the data migration.
     *
     * @return string
     */
    public function model()
    {
        //
    }

    /**
     * Get the data being used by the data migration.
     *
     * @return mixed
     */
    public function data()
    {
        //
    }

    /**
     * Get the data options being used by the data migration.
     *
     * @return mixed
     */
    public function options()
    {
        //
    }
}

属性顺序(可选)

顺序属性定义了数据迁移类执行的顺序,默认值为 0

属性标签(可选)

标签属性用于数据迁移搜索的筛选,默认值为 production,如果设置了其他值,则在执行命令时需要传递给标签参数。

模型方法

指定数据迁移类绑定的模型的方法。

/**
 * Get the model being used by the data migration.
 *
 * @return string
 */
public function model()
{
    return \App\Permission::class;
}

数据方法

指定要迁移的数据的方法。

/**
 * Get the data being used by the data migration.
 *
 * @return mixed
 */
public function data()
{
    return [
       ['name' => 'product.products.index', 'title' => 'List products', 'group' => 'Product'],
       ['name' => 'product.products.show', 'title' => 'Show product', 'group' => 'Product'],
       ['name' => 'product.products.store', 'title' => 'Create product', 'group' => 'Product'],
       ['name' => 'product.products.update', 'title' => 'Update product', 'group' => 'Product'],
       ['name' => 'product.products.destroy', 'title' => 'Delete product', 'group' => 'Product'],

       ['name' => 'product.brands.index', 'title' => 'List brands', 'group' => 'Product'],
       ['name' => 'product.brands.show', 'title' => 'Show brand', 'group' => 'Product'],
       ['name' => 'product.brands.store', 'title' => 'Create brand', 'group' => 'Product'],
       ['name' => 'product.brands.update', 'title' => 'Update brand', 'group' => 'Product'],
       ['name' => 'product.brands.destroy', 'title' => 'Delete brand', 'group' => 'Product'],
   ];
}

选项方法

选项方法用于指定迁移中要使用的参数。

/**
 * Get the data options being used by the data migration.
 *
 * @return mixed
 */
public function options()
{
    return [
       'identifier' => 'name',
       'show' => ['name', 'title'],
   ];
}

以下键作为选项可用

运行数据迁移

您可以通过命令或外观运行数据迁移。

使用 data-migration:status Artisan 命令显示每个数据与数据库的状态

php artisan data-migration:status

指定搜索数据迁移的路径

php artisan data-migration:status --path=path/with/data/migrations --path=other/path/with/data/migrations

指定搜索数据迁移的标签

php artisan data-migration:status --tag=staging --path=local

仅指定特定数据迁移的 data-migration:status

php artisan data-migration:status App\\DataMigrations\\PermissionDataMigration

输出

+--------------------------+------------------------+--------+
| name                     | title                  | status |
+--------------------------+------------------------+--------+
| product.products.index   | List products          | Create |
| product.products.show    | Show product           | OK     |
| product.products.store   | Create product updated | Update |
| product.products.destroy | Delete product         | OK     |
| product.brands.show      | Show brand             | Create |
| product.brands.store     | Create brand updated   | Update |
| product.brands.update    | Update brand           | OK     |
| product.brands.destroy   | Delete brand           | OK     |
| product.products.update  | Update product         | Delete |
| product.brands.index     | List brands            | Delete |
+--------------------------+------------------------+--------+

或使用 DataMigration 外观

$status = DataMigration::status(\App\DataMigrations\PermissionDataMigration::class);

使用 data-migration:diff Artisan 命令显示数据迁移与数据库之间的更改

php artisan data-migration:diff

指定搜索数据迁移的路径

php artisan data-migration:diff --path=path/with/data/migrations --path=other/path/with/data/migrations

指定搜索数据迁移的标签

php artisan data-migration:diff --tag=staging --path=local

仅指定特定数据迁移的 data-migration:diff

php artisan data-migration:diff App\\DataMigrations\\PermissionDataMigration

输出

+--------------------------+------------------------+--------+
| name                     | title                  | status |
+--------------------------+------------------------+--------+
| product.products.index   | List products          | Create |
| product.products.store   | Create product updated | Update |
| product.brands.show      | Show brand             | Create |
| product.brands.store     | Create brand updated   | Update |
| product.products.update  | Update product         | Delete |
| product.brands.index     | List brands            | Delete |
+--------------------------+------------------------+--------+

或使用 DataMigration 外观

$diff = DataMigration::diff(\App\DataMigrations\PermissionDataMigration::class);

使用 data-migration:migrate Artisan 命令从数据迁移迁移数据到数据库。只有状态为创建的必要操作将执行

php artisan data-migration:migrate

指定搜索数据迁移的路径

php artisan data-migration:migrate --path=path/with/data/migrations --path=other/path/with/data/migrations

指定搜索数据迁移的标签

php artisan data-migration:migrate --tag=staging --path=local

仅指定特定数据迁移的 data-migration:migrate

php artisan data-migration:migrate App\\DataMigrations\\PermissionDataMigration

输出

+--------------------------+------------------------+--------+
| name                     | title                  | status |
+--------------------------+------------------------+--------+
| product.products.index   | List products          | Create |
| product.brands.show      | Show brand             | Create |
+--------------------------+------------------------+--------+

或使用 DataMigration 外观

$migrated = DataMigration::migrate(\App\DataMigrations\PermissionDataMigration::class);

使用 data-migration:sync Artisan 命令从数据迁移与数据库同步数据。所有必要的 createupdatedelete 操作都将执行

php artisan data-migration:sync

指定搜索数据迁移的路径

php artisan data-migration:sync --path=path/with/data/migrations --path=other/path/with/data/migrations

指定搜索数据迁移的标签

php artisan data-migration:sync --tag=staging --path=local

仅指定特定数据迁移的 data-migration:sync

php artisan data-migration:sync App\\DataMigrations\\PermissionDataMigration

输出

+--------------------------+------------------------+--------+
| name                     | title                  | status |
+--------------------------+------------------------+--------+
| product.products.index   | List products          | Create |
| product.products.store   | Create product updated | Update |
| product.brands.show      | Show brand             | Create |
| product.brands.store     | Create brand updated   | Update |
| product.products.update  | Update product         | Delete |
| product.brands.index     | List brands            | Delete |
+--------------------------+------------------------+--------+

或使用 DataMigration 外观

$synchronized = DataMigration::sync(\App\DataMigrations\PermissionDataMigration::class);

使用 data-migration:list Artisan 命令显示数据迁移列表

php artisan data-migration:list

指定搜索数据迁移的路径

php artisan data-migration:list --path=path/with/data/migrations --path=other/path/with/data/migrations

指定搜索数据迁移的标签

php artisan data-migration:list --tag=staging --path=local

与关系使用

权限模型的示例,具有类型为 belongsToMany 的依赖关系的 pivot_example_1 和 pivot_example_2,以及类型为 belongsTo 的品牌关系,以演示数据迁移。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'title', 'group', 'brand_id',
    ];

    /**
     * The dependencies that belong to the permission.
     */
    public function dependencies()
    {
        return $this->belongsToMany(Permission::class)->withPivot(['pivot_example_1', 'pivot_example_2']);
    }

    /**
     * Get the brand of the permission.
     */
    public function brand()
    {
        return $this->belongsTo(Brand::class);
    }
}

数据方法与关系

数据方法用于指定具有关系的迁移数据。

/**
 * Get the data being used by the data migration.
 *
 * @return mixed
 */
public function data()
{
    return [
       ['name' => 'product.products.index', 'title' => 'List products', 'group' => 'Product', 'brand' => ['name' => 'Brand test 1']],
       ['name' => 'product.products.show', 'title' => 'Show product', 'group' => 'Product'],
       ['name' => 'product.products.store', 'title' => 'Create product', 'group' => 'Product', 'dependencies' => [
           ['name' => 'product.brands.index', 'pivot_example_1' => 'Pivot value 1'], ['name' => 'product.categories.index'],
       ], 'brand' => ['name' => 'Brand test 2']],
       ['name' => 'product.products.update', 'title' => 'Update product', 'group' => 'Product', 'dependencies' => [
           ['name' => 'product.brands.index'], ['name' => 'product.categories.index', 'pivot_example_2' => 'Pivot value 2'],
       ]],
       ['name' => 'product.products.destroy', 'title' => 'Delete product', 'group' => 'Product'],

       ['name' => 'product.brands.index', 'title' => 'List brands', 'group' => 'Product', 'brand' => ['name' => 'Brand test 1']],
       ['name' => 'product.brands.show', 'title' => 'Show brand', 'group' => 'Product'],
       ['name' => 'product.brands.store', 'title' => 'Create brand', 'group' => 'Product'],
       ['name' => 'product.brands.update', 'title' => 'Update brand', 'group' => 'Product', 'brand' => ['name' => 'Brand test 2']],
       ['name' => 'product.brands.destroy', 'title' => 'Delete brand', 'group' => 'Product'],
   ];
}

关系选项方法

关系选项方法用于指定数据迁移中要使用的参数。

/**
 * Get the data options being used by the data migration.
 *
 * @return mixed
 */
public function options()
{
    return [
       'identifier' => 'name',
       'show' => ['name', 'title'],
       'relations' => [
           [
               'type' => 'belongsToMany',
               'relation' => 'dependencies',
               'identifier' => 'name',
               'show' => ['name'],
           ],
           [
               'type' => 'belongsTo',
               'relation' => 'brand',
           ],
       ],
   ];
}

以下键作为关系选项可用

事件

在运行 data-migration:migratedata-migration:sync Artisan 命令时,开始和完成事件可用于 data-migration:migrate

onStartMigrate

在您的数据迁移中创建 onStartMigrate 方法,在执行 data-migration:migrate Artisan 命令之前调用。

onFinishMigrate

在您的数据迁移中创建 onFinishMigrate 方法,在执行 data-migration:migrate Artisan 命令之后调用。

onStartSync

在数据迁移中创建 onStartSync 方法,在执行 data-migration:sync Artisan 命令之前调用。

onFinishSync

在数据迁移中创建 onFinishSync 方法,在执行 data-migration:sync Artisan 命令之后调用。

测试

composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件 fndmiranda@gmail.com 反馈,而不是使用问题跟踪器。

许可证

MIT 许可证 (MIT)。更多信息请参阅 许可证文件