Reputation: 29932
I've searched pretty much everywhere but I wasn't able to find anything.
Is there a command or a procedure to change the name of a table (so inside doctrine annotation) without loosing data?
Basically, something that will produce something like
RENAME TABLE old_table TO new_table;
or
ALTER TABLE old_table RENAME new_table;
MySQL commands taken from here
Should I write manually the migration file with doctrine:migrations:generate
?
Upvotes: 12
Views: 21398
Reputation: 2267
I found the best possible way is to use a two-step / two-migration approach. With the 1st migration rename the table only, and with the 2nd migration take care of the renaming parts, e.g. FKs.
The steps I took:
App\Entity\OldName
to App\Entity\NewName
symfony console make:migration
up()
with ALTER TABLE old_name RENAME new_name;
and down()
with ALTER TABLE new_name RENAME old_name;
. In other words, this migration only renames the table itself, without FKs, indexes, etc.symfony console doctrine:migrations:migrate -n
symfony console make:migration
. The generated code for this migration handles all the FKs, columns rename (if you did any in the PHP entity), indexes, etc.symfony console doctrine:migrations:migrate -n
Upvotes: 3
Reputation: 44396
Change table name for given entity.
/** @Entity @Table(name="new_table_name") */
class MyEntity { ... }
Generate a new migration.
up()
and down()
methods and replace them with custom SQL (ALTER TABLE ... RENAME TO ...
).Keep in mind that migration generator is meant as utility/semi-automatic tool.
Upvotes: 19