marcelohoffmeister / migrator
为 Laravel 6.0+ 提供命名空间迁移
2.0.1
2020-04-29 22:25 UTC
This package is auto-updated.
Last update: 2024-09-29 05:29:07 UTC
README
此包是 Laravel 默认数据库迁移器的定制版本,它旨在在服务提供器上注册迁移并支持命名空间。
由于运行顺序基于您注册迁移的方式,因此没有时间戳预览。
警告
此包从 Laravel 5.2 开始支持,到最新稳定版本。
安装
为了安装 Migrator,请在您的 Laravel 6.0+ 项目中运行以下命令
composer require marcelohoffmeister/migrator
安装包后,您现在可以将提供器注册到您的 config/app.php 文件中
'providers' => [ // other providers omitted. Migrator\MigrationServiceProvider::class, ]
并发布配置:使用
php artisan vendor:publish --provider="Migrator\MigrationServiceProvider"
用法
作为默认的 Laravel 迁移器,这个迁移器有所有原始命令,要列出所有可用选项,可以使用 php artisan
命令查看所有可用选项。
migrator Run the database migrations
migrator:fresh Drop all tables and re-run all migrations
migrator:install Create the migration repository
migrator:make Create a new migration file
migrator:refresh Reset and re-run all migrations
migrator:reset Rollback all database migrations
migrator:rollback Rollback the last database migration
migrator:status Show the status of each migration
创建迁移
为了生成空迁移,请向迁移器提供完全限定的类名,例如。
php artisan migrator:make 'MyApp\MyModule\Database\Migrations\CreateOrdersTable' --create=orders
这将创建一个迁移类到正确的目录,生成的文件与默认 Laravel 生成的文件略有不同
<?php namespace MyApp\MyModule\Database\Migrations; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateOrdersTable extends Migration { /** * @var \Illuminate\Database\Schema\Builder */ protected $schema; /** * Migration constructor. */ public function __construct() { $this->schema = app('db')->connection()->getSchemaBuilder(); } /** * Run the migrations. * * @return void */ public function up() { $this->schema->create('orders', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { $this->schema->drop('orders'); } }
要声明您的表字段,只需遵循通常的架构构建惯例,此包在这方面没有做出任何不同。
作为正常的迁移器,您可以使用 --table
选项而不是 --create
选项来生成更新迁移而不是创建迁移。您还可以创建不传递任何选项的空迁移。
在此分支中,您可以为 fresh 命令传递 --path
选项。这将在特定路径中执行命令。
注册迁移。
在您选择的任何服务提供器中(通常在您存储迁移的同一名空间中),您可以使用 Migrator\MigratorTrait
简单地注册迁移。
<?php namespace MyApp\MyModule\Providers; use Illuminate\Support\ServiceProvider; use Migrator\MigratorTrait; use MyApp\MyModule\Database\Migrations\CreateOrdersTable; use MyApp\MyModule\Database\Migrations\CreateProductsTable; class MyModuleServiceProvider extends ServiceProvider { use MigratorTrait; public function register() { $this->migrations([ CreateOrdersTable::class, CreateProductsTable::class, ]); } }