OurBG's avatar
Level 1

Migrating database to new structure and converting data.

We are planning a major database restructure and instead of "Create" all the migrations are "Alter"-ing already existing tables.

For example:

There's table users that holds a column role.

After running migrations this column will be dropped and table "Roles" will be created with a hasOne relationship (just an example).

How should I approach converting the old data into the new structure as seamlessly as possible?

0 likes
2 replies
CodeNathan's avatar
  • create a folder inside migrations called database refactoring or something

  • within that folder create to new folders called addition and altering

  • write a custom artisan command which does the following 3 in their respective order

  1. to first run all the addition of the new database tables and columns ( PHP artisan migrate has a path option where you can specify this)
  2. then I would write a script to all the data across
  3. I would drop the columns no longer necessary ( again use the path option for this using artisan migrate)

As usual backup db before attempting

Tray2's avatar

I would do something along the line of

  • Create a new database.
  • Run all the migrations for the new structure in the new database
  • Script the data from the old database into the new one.
1 like

Please or to participate in this conversation.