Issue
I have this Laravel migration structure:
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();
});
}
// ...
}
I need to create a daily backup from warehouse products and for this I need to create a new table what is exactly same as warehouse_products
and contains one more extra field for the backup's date.
Is there any best practice to do this? I think something like this:
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');
});
}
}
Is there something similar good practice to clone fields from an existing migration?
Solution
I found a solution, I think not elegant, but it's working:
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();
}
}
Answered By - netdjw Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.