aedart / laravel-database
为 Laravel 的数据库包提供各种实用工具和助手,其中包括包迁移助手。
Requires
- php: >=5.5.9
- aedart/laravel-detector: 1.1.*
- aedart/model-table-name: 1.*
- aedart/model-vendor-path: 1.*
- illuminate/database: 5.1.*
- symfony/finder: 2.7.*
Requires (Dev)
- aedart/license: 1.*
- aedart/license-file-manager: 1.*
- aedart/testing-laravel: 1.1.*
- codeception/codeception: 2.0.*
- fzaninotto/faker: 1.5.*
- mockery/mockery: 0.9.*
This package is auto-updated.
Last update: 2022-02-01 12:46:22 UTC
README
为 Laravel 的数据库包提供各种实用工具和助手,其中包括包迁移助手。
内容
[目录]
如何安装
对于 Laravel 版本 5.0.x
#!console
composer require aedart/laravel-database
此包使用 composer。如果您不知道它是做什么的或者如何工作,建议您在尝试使用此包之前先了解一些相关信息。
包迁移助手
这是什么?
包迁移助手是一个实用工具,允许您从 vendor
目录直接运行迁移。它作为 Laravel 原生迁移器的替代品。
尽管如此,我仍然建议您使用 Laravel 的 服务提供者 来 发布您的包迁移,而不是仅从 vendor
文件夹迁移。
先决条件
您应该在 Laravel 框架内运行您的应用程序
或者
您正在使用某种测试框架,如 orchestral-testbench,这使得 Laravel 应用程序可在您的测试中使用
Composer classmap
在您使用助手之前,您应该声明您的包迁移所在位置的 classmap
- 除非给定的包已经声明了此类,或者您的迁移文件是命名空间化的。
示例
#!json
{
"autoload": {
"classmap": [
"vendor/acme/db/src/migrations",
"vendor/acme/plugin/src/myCustomMigrations",
"vendor/acme/runner/src/db-migrations"
]
}
}
如果您不这样做,那么您可能无法回滚已执行的迁移
MigratorHelper 和 MigratorHelperTrait
如果您有需要从多个位置执行的迁移,那么您可以使用 MigratorHelper
和或 MigratorHelperTrait
来帮助您迁移。
从默认的 vendor 目录
以下示例假设您有一个默认的 composer 配置,其中 vendor
目录只是称为 vendor!
#!php
<?php
use Aedart\Laravel\Database\Migrations\Packages\Traits\MigratorHelperTrait;
// Example class
class MyMigrationInvoker {
use MigratorHelperTrait;
public function run(){
// Run the migrations, recursively find all migration files in nested directories
// from /vendor/...
$this->getMigratorHelper()->runPackageMigrations([
'acme/db/src/migrations',
'acme/plugin/src/MyMigrations',
]);
}
}
自定义 vendor 目录
如果您在顶级 composer 文件中配置了不同的 vendor
目录,那么您可以使用 setVendorPath
方法指定其位置。
#!php
<?php
use Aedart\Laravel\Database\Migrations\Packages\Traits\MigratorHelperTrait;
// Example class
class MyMigrationInvoker {
use MigratorHelperTrait;
public function run(){
// Run the migrations, recursively find all migration files in nested directories
// from /home/etc/lib/myCustomVendorLib/...
$this->getMigratorHelper()->setVendorPath('/home/etc/lib/myCustomVendorLib')
$this->getMigratorHelper()->runPackageMigrations([
'acme/db/src/migrations',
'acme/plugin/src/MyMigrations',
]);
}
}
回滚最后一条迁移和回滚所有迁移
您可以使用 rollBackLastMigration
方法回滚最后执行的迁移。
注意: 确保迁移文件已通过 composer autoload
进行类映射,否则您可能无法回滚任何之前执行的迁移。
#!php
<?php
use Aedart\Laravel\Database\Migrations\Packages\Traits\MigratorHelperTrait;
// Example class
class MyMigrationInvoker {
use MigratorHelperTrait;
public function back(){
// Roll back last migration
$this->getMigratorHelper()->rollBackLastMigration();
}
}
或者...您可以使用 rollBackAllMigrations
方法回滚所有执行的迁移。
#!php
<?php
use Aedart\Laravel\Database\Migrations\Packages\Traits\MigratorHelperTrait;
// Example class
class MyMigrationInvoker {
use MigratorHelperTrait;
public function back(){
// Roll back last migration
$this->getMigratorHelper()->rollBackAllMigrations();
}
}
递归与非递归
MigratorHelper
提供的所有 run-migration
方法默认是递归的!
这意味着您可以按需组织您的迁移文件在嵌套的子目录中。
如果您不想递归运行迁移,那么您可以在所需的方法上指定 $recursive
选项。
示例结构
+ vendor
+ acme
+ sales
+ src
+ migrations
+ products
- 2015_04_120000_create_products_table.php
+ sales
- 2015_04_10_121000_create_statistics_table.php
- 2015_04_120500_create_sales_table.php
给定上述迁移文件结构,您可以递归地运行迁移,或者您可以将第二个参数设置为 false
(即 $recursive
选项),对于 "运行迁移" 方法。
#!php
<?php
use Aedart\Laravel\Database\Migrations\Packages\MigratorHelper;
$helper = new MigratorHelper();
// By default, migrations are executed recursively
//$helper->runPackageMigration('acme/sales/src/migrations');
// This method does NOT execute all the migration files - it doesn't run recursively
$helper->runPackageMigration('acme/sales/src/migrations', false);
有关更多信息,请参阅 Aedart\Laravel\Database\Migrations\Packages\Interfaces\IMigratorHelper
接口。
感谢
Taylor Otwell 等人为有史以来最好的PHP框架之一。
许可证
BSD-3-Clause,请阅读此包中包含的LICENSE文件