gogo-gorm

Is there a way to run gorm's AutoMigrate using oldStruct but only migrating the tempStruct?


I have a table user that is really big, which causes AutoMigrate to hangs when I try to use it to create new column or modify the column, which takes down my site for a bit.

One solution I am trying to implement is using a "Zero-Downtime Migration" method, which goes as follow:

  1. Create a user_temp table.
  2. Insert data into the user_temp table from existing user table.
  3. run AutoMigrate on the user_temp table.
  4. Rename current user table to user_old table.
  5. Rename user_temp table to user.
  6. Drop user_old table.

However, since the way I migrate user table is by updating the User struct, and then run

db.AutoMigrate(&User{}).Error

I am not sure how to dynamically create the UserTemp struct from the newly updated User struct in Golang.

I am hoping to either:

a. Dynamically create UserTemp struct that is identical to the newly updated User struct, so i can run db.AutoMigrate(&UserTemp{})

b. use the AutoMigrate func with the newly updated User struct, but only on the user_temp table, and not the user table.

Thank you in advance.


Solution

  • ok, all I had to do was db.Table("user_temp").AutoMigrate(&User{})