Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SamStenton's avatar

How would you go about migrating the data from an old system onto a rebuild

I have a website based around Laravel 4. It was initially used as a way for me to learn the framework. Since being released its gained a steady flow of active users. My plan is once I've finished my exams to rebuild the site.

I've chosen to rebuild rather than update because as it stands the code base is horrible. I feel I can produce a better quality product by going from the ground up.

With that however I'm going to need to migrate the current data over to a (probably) fairly different schema. What would be the best way to approach this?

Any advice is appreciated!

0 likes
3 replies
thc1967's avatar
thc1967
Best Answer
Level 8

It sounds like you've learned quite a lot since the initial implementation. You suggest that the old code is pretty bad so I'll assume there are plenty of design patterns that you know how to do much better now.

In that situation, I think I would design (but not necessarily implement) the new version's schema without consideration for the old version's model. Once you have that schema design, go back to compare it to the old schema to decide whether you need to make any changes. When you can see the models side-by-side, you should be able to see how to map the old into the new.

From a technical perspective, unless you have an immense amount of data or absolutely cannot afford any down-time, I don't think I'd try to write code to move the data. I think I'd export the data into something like an Excel workbook, massage it there, and import it into the new schema.

1 like
Drfraker's avatar

You can write DB statements in your migrations. So when you make changes to the schema you can simply write queries to move data where you want it. Here is an example migration from when I moved to the new version of Laravel Cashier and had to migrate some data:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class UpdateStripe extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('clinics', function (Blueprint $table) {
            $table->string('card_brand')->nullable();
            $table->renameColumn('last_four', 'card_last_four');
        });

        Schema::create('subscriptions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('clinic_id');
            $table->string('name');
            $table->string('stripe_id');
            $table->string('stripe_plan');
            $table->integer('coupon_id')->nullable();
            $table->integer('quantity');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamps();
        });

        DB::statement(
            "INSERT INTO `subscriptions` (`clinic_id`, `name`, `stripe_id`, `stripe_plan`, `coupon_id`, `quantity`, `trial_ends_at`, `ends_at`, `created_at`, `updated_at`)
            SELECT `id`, 'membership', `stripe_subscription`, `stripe_plan`, `coupon_id`, '1', `trial_ends_at`, `subscription_ends_at`, `created_at`, `updated_at`
            FROM `clinics` WHERE `stripe_active` = '1'"
        );

        Schema::table('clinics', function (Blueprint $table) {
            $table->dropColumn(['stripe_active', 'stripe_subscription', 'stripe_plan', 'trial_ends_at', 'subscription_ends_at', 'coupon_id']);
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Rebuild the clinics table
        Schema::table('clinics', function (Blueprint $table) {
            $table->tinyInteger('stripe_active')->default(0);
            $table->string('stripe_subscription')->nullable();
            $table->string('stripe_plan', 25)->nullable();
            $table->integer('coupon_id')->nullable();
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('subscription_ends_at')->nullable();
            $table->dropColumn('card_brand');
            $table->renameColumn('card_last_four', 'last_four');
        });


        // Insert the data from the subscriptions table.
        DB::statement(
            'UPDATE clinics, subscriptions
             set clinics.stripe_active = 1,
                 clinics.stripe_subscription = subscriptions.stripe_id,
                 clinics.stripe_plan = subscriptions.stripe_plan,
                 clinics.coupon_id = subscriptions.coupon_id,
                 clinics.trial_ends_at = subscriptions.trial_ends_at,
                 clinics.subscription_ends_at = subscriptions.ends_at
            WHERE clinics.id = subscriptions.clinic_id'
        );

        Schema::drop('subscriptions');
    }
}
1 like
SamStenton's avatar

@thc1967 An Excel document is a great idea! Looks like my largest table is sitting at about 15k rows.

Please or to participate in this conversation.