super-gizmo / laravel-exodus
Laravel YAML 迁移
Requires
- php: >=7.4.28
- illuminate/filesystem: ^5.8|~6.0|~7.0|~8.0|~9.0|~10.0
- illuminate/support: ^5.8|~6.0|~7.0|~8.0|~9.0|~10.0
- symfony/yaml: ^5.1|~6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-25 20:41:14 UTC
README
将 YAML 转换为实际的 Laravel 迁移文件。
安装
composer require --dev eyf/laravel-exodus
用法
步骤 1: 创建 database/migrations.yaml
文件
定义一个 posts
表
# database/migrations.yaml posts: id: true timestamps: true softDeletes: true slug: string(100).unique title: string content: text excerpt: string.nullable author_id: foreignId.constrained('users')
步骤 2: 制作迁移(命令)
运行 exodus
命令将 yaml
文件转换为实际的 Laravel 迁移文件
php artisan exodus Created Migration: 2020_05_05_100005_create_posts_table
步骤 3: 正常执行 migrate
php artisan migrate
工作流程
Exodus 是一个开发包,旨在简化/加速开发时间。
开发时的正常工作流程可能是
- 创建
migrations.yaml
文件(添加一个posts
表) php artisan exodus
php artisan migrate
- 编辑
migrations.yaml
文件(添加一个users
表) php artisan exodus
php artisan migrate:refresh (--seed)
- 编辑
migrations.yaml
文件 - ...(重复)
exodus.lock
文件
默认情况下,迁移文件名在多次 exodus
运行之间不会改变。
这是因为 Exodus 会跟踪初始迁移文件名在 database/exodus.lock
中(以便提交到您的仓库)。
这确保 git
在整个开发过程中看到相同的迁移文件中的编辑。
force
选项
有时您可能想绕过 exodus.lock
文件(例如,当您想更改表创建顺序时)。
php artisan exodus --force
会发生什么
- 所有“旧”的迁移文件都将被删除(当前
exodus.lock
中的那些) - 将生成新的迁移文件(文件名中的日期最新)
语法
列
任何列都可以像在 Laravel 迁移语法中一样流畅地写出。实际上,Exodus 只是一个将“点表示法” array
转换为实际 PHP 语法的轻量级翻译器。
my_table: my_column_name: string(50).nullable.unique
特殊列
特殊列类型是 Laravel 为提高开发者体验提供的“糖”方法:id()
、timestamps()
、softDeletes()
、rememberToken()
等...
由于这些列类型没有列名(名称遵循约定),只需指定它们的值为 true
my_table: id: true timestamps: true softDeletes: true
关联表
要生成关联表,只需像下面这样使用两个表名
users: id: true name: string posts: id: true title: string "@users @posts": []
这将创建以下关联迁移文件
<?php // database/migrations/2020_05_05_085245_create_post_user_pivot_table.php class CreatePostUserPivotTable extends Migration { public function up() { Schema::create("post_user", function (Blueprint $table) { $table ->foreignId("post_id") ->index() ->constrained(); $table ->foreignId("user_id") ->index() ->constrained(); $table->primary(["post_id", "user_id"]); }); } public function down() { Schema::dropIfExists("post_user"); } }
您甚至可以向关联表提供更多列,就像通常那样
"@users @posts": timestamps: true approved_by: foreignId.nullable.constrained('users') approved_at: timestamp.nullable
哲学
此包旨在加快开发时间。它不打算在生产环境中使用。事实上,此包不提供运行迁移以添加或删除列的方法(它曾经这样做)。
这是有意的。
在开发时,您应该尽可能多地编辑 migrations.yaml
文件,并尽可能频繁地运行 migrate:refresh (--seed)
。这是“正确的方式”。
当您对模式感到满意时,您必须已部署到生产环境中,然后您才可能为添加或删除表中的列创建正常的 Laravel 迁移文件。这就是此包工作的结束。
待办事项:实现安全保护,确保当 exodus
检测到比 exodus.lock
中的更多迁移文件时,无法运行。