1

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?

netdjw
  • 5,419
  • 21
  • 88
  • 162
  • I don't quite understand the use case for this. Why not just do a daily export as CSV? – BlindPenguin May 28 '20 at 11:36
  • Because it's not a CSV backup, but a database backup to create statistics later about product amounts. In my app the product amount has many states in the warehouses. We have free_amount, booked_amount, under_shipping_amount, wait_for_shipping_amount, etc... more than 20 states... I need to create a daily backup, later create a chart about stock amount changes of a product. – netdjw May 28 '20 at 11:45
  • You could also consider something like [spatie/laravel-backup](https://github.com/spatie/laravel-backup). – Tpojka May 28 '20 at 19:57
  • But I don't want to create a backup from my app. I just want to copy datas from on table into another and for this operation I need to create a table where the columns are sync. – netdjw May 29 '20 at 09:20

1 Answers1

0

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();
    }
}
netdjw
  • 5,419
  • 21
  • 88
  • 162