Laravel 8 - 禁用特定包的迁移
Laravel 8 - Disable migrations for specific package
场景如下:
我正在构建一个软件,其中前端和执行工作的程序彼此分离。为了让前端能够使用 worker-programm 的模型,我构建了一个包含它们的包。该包为其提供者提供以下代码:
public function boot()
{
if($this->app->runningInConsole()){
$this->loadMigrationsFrom(__DIR__ . '/MyPackageMigrations');
}
}
现在我的问题是,当工作程序中的 artisan migrate
命令为 运行 而不是 UI 时,我希望这些迁移为 运行 -programm,因为两个数据库也彼此分开。有没有办法在 UI-programm 中抑制这些迁移?
问候
与其在 ServiceProvider
的 boot()
方法中使用 loadMigrationsFrom()
,不如使用 $this->puiblishes()
,这样您就可以控制发布迁移而不是让它们自动加载.
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../database/migrations/create_models_table.php.stub'
=> database_path('migrations/' . date('Y_m_d_His', time()) . 'create_models_table.php'),
// you can add any number of migrations here
], 'migrations');
}
}
然后您需要做的就是在要使用迁移的项目中发布迁移:
php artisan vendor:publish --provider="Husky110\Husky110Package\Husky110PackageServiceProvider" --tag="migrations"
更新
您可以通过执行以下操作来检查是否存在迁移:
if (!class_exists('CreateModelsTable')) {
// perform migration
}
如果您有改变 table 结构的迁移,您可以这样做:
if (!class_exists('AlterModelsTableAddField')) {
// perform migration
}
场景如下:
我正在构建一个软件,其中前端和执行工作的程序彼此分离。为了让前端能够使用 worker-programm 的模型,我构建了一个包含它们的包。该包为其提供者提供以下代码:
public function boot()
{
if($this->app->runningInConsole()){
$this->loadMigrationsFrom(__DIR__ . '/MyPackageMigrations');
}
}
现在我的问题是,当工作程序中的 artisan migrate
命令为 运行 而不是 UI 时,我希望这些迁移为 运行 -programm,因为两个数据库也彼此分开。有没有办法在 UI-programm 中抑制这些迁移?
问候
与其在 ServiceProvider
的 boot()
方法中使用 loadMigrationsFrom()
,不如使用 $this->puiblishes()
,这样您就可以控制发布迁移而不是让它们自动加载.
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../database/migrations/create_models_table.php.stub'
=> database_path('migrations/' . date('Y_m_d_His', time()) . 'create_models_table.php'),
// you can add any number of migrations here
], 'migrations');
}
}
然后您需要做的就是在要使用迁移的项目中发布迁移:
php artisan vendor:publish --provider="Husky110\Husky110Package\Husky110PackageServiceProvider" --tag="migrations"
更新
您可以通过执行以下操作来检查是否存在迁移:
if (!class_exists('CreateModelsTable')) {
// perform migration
}
如果您有改变 table 结构的迁移,您可以这样做:
if (!class_exists('AlterModelsTableAddField')) {
// perform migration
}