smario10 / 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 16:26:44 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 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.lock
中的迁移文件更多时,无法运行 exodus
。