如何将 Laravel 个迁移字段克隆到另一个迁移中?
How to clone Laravel migration fields into another migration?
我有这个 Laravel 迁移结构:
class CreateWarehouseProductTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('warehouse_products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->default(0);
$table->integer('warehouse_id');
$table->integer('free_amount')->default(0);
$table->integer('booked_amount')->default(0);
// ...
$table->timestamps();
});
}
// ...
}
我需要从仓库产品创建一个每日备份,为此我需要创建一个新的 table,它与 warehouse_products
完全相同,并且包含一个额外的备份日期字段。
有什么最佳做法吗?我想是这样的:
class CreateWarehouseProductBackupTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('warehouse_product_backups', function (Blueprint $table) {
CreateWarehouseProductTable->cloneFieldsToHere();
$table->date('date_of_backup');
});
}
}
从现有迁移中克隆字段是否有类似的好的做法?
我找到了一个解决方案,我认为它并不优雅,但它有效:
class CreateWarehouseProductTable extends Migration
{
public function up()
{
Schema::create('warehouse_products', function (Blueprint $table) {
$this->setWarehouseProductColumns($table);
});
Schema::create('warehouse_product_backups', function (Blueprint $table) {
$this->setWarehouseProductColumns($table);
$table->date('date_of_backup')->nullable();
});
}
public function setWarehouseProductColumns(Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->default(0);
$table->integer('warehouse_id');
$table->integer('free_amount')->default(0);
$table->integer('booked_amount')->default(0);
// ...
$table->timestamps();
}
}
我有这个 Laravel 迁移结构:
class CreateWarehouseProductTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('warehouse_products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->default(0);
$table->integer('warehouse_id');
$table->integer('free_amount')->default(0);
$table->integer('booked_amount')->default(0);
// ...
$table->timestamps();
});
}
// ...
}
我需要从仓库产品创建一个每日备份,为此我需要创建一个新的 table,它与 warehouse_products
完全相同,并且包含一个额外的备份日期字段。
有什么最佳做法吗?我想是这样的:
class CreateWarehouseProductBackupTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('warehouse_product_backups', function (Blueprint $table) {
CreateWarehouseProductTable->cloneFieldsToHere();
$table->date('date_of_backup');
});
}
}
从现有迁移中克隆字段是否有类似的好的做法?
我找到了一个解决方案,我认为它并不优雅,但它有效:
class CreateWarehouseProductTable extends Migration
{
public function up()
{
Schema::create('warehouse_products', function (Blueprint $table) {
$this->setWarehouseProductColumns($table);
});
Schema::create('warehouse_product_backups', function (Blueprint $table) {
$this->setWarehouseProductColumns($table);
$table->date('date_of_backup')->nullable();
});
}
public function setWarehouseProductColumns(Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->default(0);
$table->integer('warehouse_id');
$table->integer('free_amount')->default(0);
$table->integer('booked_amount')->default(0);
// ...
$table->timestamps();
}
}