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

IvanBernatovic's avatar

How to add missing migrations and new data model to production application

So, I got to work on Laravel Spark application that's not finished yet but it's used by customers on production. Quality of the code is poor and it seems that previous developers weren't familiar with Laravel. So in result nothing works completely and it's extremely hard to fix things as there were no conventions followed regarding anything (no code styling, no comments, no SOLID principles on mind, 200+ lines for a method, no taste in general).

However, biggest issue with the app is that there are no migrations for database and database scheme is not usable at the moment. So I started to redesign database model and created all missing migrations and some of the Eloquent models. Now problem is that I need to find a way how to apply new scheme and use migrations. For instance, some tables already exist so migration will fail.

My plan is this:

  1. Extract all of the data from tables that have no migration file to some easily readable format (can be SQL dump, Laravel seed file or even CSV); let's call it old_scheme_backup
  2. Drop those tables
  3. Create new tables using migrations
  4. Write a script that will populate newly created tables with the data from old_scheme_backup
  5. Problem solved

So, do you have any suggestion what's the best approach for problems like this?

0 likes
3 replies
Borisu's avatar

Yes your approach is good. If you have a dump of the old data you can do whatever you want basically. If you are changing table structure though, you will have to consider a mapping method for your data.

You could do something like this:

$file = fopen('some_table.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
    $fields = array ( 'first_name', 'last_name' );
    $arr = array_combine ( $fields, explode ( ",", $line ) );
    User::create(['first_name' => $arr['first_name'], 'last_name' => $arr['last_name']]);
}
fclose($file);

And so forth... Maybe there's a better method for this

EventFellows's avatar

When listening to what you describe it might be worth a few seconds of your time to think about re-starting it all with a fresh spark install in a SOLID way and then then migrate the data over (in the fashion you described above).

that way you might end up with a different datastrucutre all together but have clean setup.

But that clearly depends on how much of your existing code would need rework.

Please or to participate in this conversation.