mylesduncanking / laravel-simple-migration
用于Laravel中的简化迁移的类
Requires
- php: ^7.3|^8.0|^8.1|^8.2
- illuminate/database: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0|^7.0
README
composer require mylesduncanking/laravel-simple-migration
入门
要使用此功能,需要了解Laravel迁移的工作方式。"迁移就像是数据库的版本控制,允许团队定义和共享应用程序的数据库模式定义。如果你曾经告诉队友在从源代码控制中拉取更改后手动添加列到他们的本地数据库模式中,你面临的问题就是数据库迁移解决的问题。" - Laravel文档
要使用简单迁移,创建一个新的迁移文件,使用与默认Laravel artisan迁移相同的语法,但指定你想要一个simple-migration
。例如 php artisan make:simple-migration your_migration_name
在迁移文件中,您将看到一个名为$migration
的新protected array
属性,这是您定义迁移逻辑的地方。
$migration
数组的格式如下
protected array $migration = [ 'TABLE NAME' => [ 'COLUMN NAME' => ['COLUMN MODIFIERS' /** Additional modifiers **/], /* Additional columns */ ], /* Additional tables */ ];
自动追加功能
为了节省时间,在向表中添加多个列时,默认行为已更改为按顺序添加列。这消除了在每个列修改器中添加->after('foobar')
的要求。
您可以通过运行php artisan vendor:publish --tag=simplemigration
并更改config/simplemigration.php > auto_after
为false
来禁用此行为。
自动索引功能
为了节省时间,您可以使用自动索引功能。默认情况下,这将为任何以_id
结尾的列自动添加->index()
修改器。
您可以通过运行php artisan vendor:publish --tag=simplemigration
并更改config/simplemigration.php > auto_index
数组中的值来修改这些规则。注意:这些值是正则表达式格式。
您还可以通过在修饰符数组中传递noIndex
选项来指定一个自动索引列不进行索引。
表命名约定
在$migration
属性中为每个要迁移的表创建一个键。在每个子数组中定义列更改。
如果在列集中定义了id
或uuid
列,则将创建表,否则将更新。您可以通过在表名前加上create:
或update:
来覆盖此行为,具体取决于您想要强制的方法。
您可以通过运行php artisan vendor:publish --tag=simplemigration
并编辑config/simplemigration.php > type_assumptions
来修改这些假设。
例如
protected array $migration = [ /* This table would be created as it contains an 'id' column */ 'table_to_be_created' => [ 'id' 'name', 'date:dob' => ['nullable'], 'timestamps', ], /* This table would be updated as it doesn't contains an 'id' or 'uuid' column */ 'table_to_be_updated' => [ 'name' => ['after:id'] ], /* A table of name "pivot_table" would be created as the method has been defined */ 'create:pivot_table' => [ 'foreignId:key_1' => ['index'], 'foreignId:key_2' => ['index'], ], ];
如何格式化列键
列作为表数组中的键传递。
格式应定义为{ 类型 }:{ 列名 }。例如 integer:quantity
如果您想要向类型方法传递其他参数,可以使用逗号分隔这些参数。如果需要数组,使用竖线|
分隔值。例如 set:eye_color,blue|green|brown|other
如果您没有传递类型,则将假设应使用哪种类型。
您可以通过运行 php artisan vendor:publish --tag=simplemigration
并编辑 config/simplemigration.php > create_triggers
来修改这些假设。注意:这是正则表达式格式。
有关有效的 Laravel 列类型的更多信息,请参阅 Laravel 文档。
如何格式化列修饰符
您传递给每个列的值是一个数组。这个数组可以是空的,也可以定义列的修饰符。
数组中的每个值都应该遵循 { 修饰符 }:{ 参数 } 的格式。例如 after:id
有关有效的 Laravel 列修饰符的更多信息,请参阅 Laravel 文档。
示例迁移
以下迁移创建了一个角色表,并将外键添加到用户表中。
<?php
use MylesDuncanKing\SimpleMigration\SimpleMigration;
class ExampleMigration extends SimpleMigration
{
protected array $migration = [
// Create "roles" table as "id" column is specified
'roles' => [
'id', // $table->id();
'softDeletes', // $table->softDeletes();
'string:role,64', // $table->string('role', 64);
'unique:deleted_at|role,roles_unique_role', // $table->unique(['deleted_at', 'role'], 'roles_unique_role');
],
// Update "users" table as no "id" or "uuid" column is specified
'users' => [
'role_id' => ['after:id', 'nullable'], // ends in _id so $table:foreignId('role_id')->after('id')->nullable()->index();
'foreign:role_id' => ['references:id', 'on:roles'], // $table->foreign('role_id')->references('id')->on('roles')->index();
]
];
}