doctrine-ormsymfony-3.4doctrine-migrations

Doctrine Migration from string to Entity


I've an apparently simple task to perform, i have to convert several tables column from a string to a new entity (integer FOREIGN KEY) value.

I have DB 10 tables with a column called "app_version" which atm are VARCHAR columns type. Since i'm going to have a little project refactor i'd like to convert those VARCHAR columns to a new column which contains an ID representing the newly mapped value so:

V1 -> ID: 1 V2 -> ID: 2

and so on

I've prepared a Doctrine Migration (i'm using symfony 3.4) which performs the conversion by DROPPING the old column and adding the new id column for the AppVersion table. Of course i need to preserve my current existing data.

I know about preUp and postUp but i can't figure how to do it w/o hitting the DB performance too much. I can collect the data via SELECT in the preUp, store them in some PHP vars to use later on inside postUp to write new values to DB but since i have 10 tables with many rows this become a disaster real fast.

Do you guys have any suggestion i could apply to make this smooth and easy?

Please do not ask why i have to do this refactor now and i didn't setup the DB correctly in the first time. :D

Keywords for ideas: transaction? bulk query? avoid php vars storage? write sql file? everything can be good


Solution

  • I feel dumb but the solution was much more simple, i created a custom migration with all the "ALTER TABLE [table_name] DROP app_version" to be executed AFTER one that simply does:

    UPDATE [table_name] SET app_version_id = 1 WHERE app_version = "V1"