migrations icon indicating copy to clipboard operation
migrations copied to clipboard

Missing foreign keys from dump-schema command

Open clement-michelet opened this issue 3 years ago • 10 comments

Bug Report

Q A
BC Break no
Version 2.3.3

Summary

Original issue : https://github.com/doctrine/migrations/issues/820

While a solution was proposed to generate a diff from empty schema, in my use case, I cannot generate a schema from an empty diff.

Use case :

  • Production database had some changes on foreign key names (done by external tools as a side effect of huge DB migrations)
  • No migrations were generated to backport those changes to the basecode (because there is about 300+ FK/constraints changed)
  • New migrations generate invalid changes when we want to do changes on constraints (like dropping FK)

I need to :

  • Generate a schema from the existing database with dump-schema which contains all constraints including FK
  • Execute a rollup to have clean database state on every application environments (non-prod environments are reset with a prod-like anonymized dump)

Current behavior

Foreign keys are missing from the schema dumped and DB diff generate foreign keys

How to reproduce

Expected behavior

Foreign keys are dumped into the migration file

Workaround

Dump structure from MySQL and create the migration manually

clement-michelet avatar May 26 '21 11:05 clement-michelet

were those FK defined in your ORM mappings?

dump-schema does not read your database schema but your ORM metadata and generates the needed SQL to get what is defined there.

goetas avatar Jul 04 '21 07:07 goetas

were those FK defined in your ORM mappings?

They are defined on entities (annotations) but with the autogenerated name.

dump-schema does not read your database schema but your ORM metadata and generates the needed SQL to get what is defined there.

That's odd because when I look on the dump-schema command / SchemaDumper service, it depends on Schema Manager. I thought It was using the database state with information schema and so on (not digged enough to check that).

If it's not supposed to rely on database schema, maybe it should be added to the command description and service descriptions because it is a little misleading as of now :

MigrationsDumpSchemaDoctrineCommand.php

Command for dumping your current database schema to a migration.

SchemaDumper.php

The SchemaDumper class is responsible for dumping the current state of your database schema to a migration. This is to be used in conjunction with the Rollup class.

When I saw "current database schema" and "current state of your database schema", I thought it was refering to the database, not the database mapping.

clement-michelet avatar Jul 12 '21 09:07 clement-michelet

SchemaDumper is indeed not dumping the foreign keys. It only dumps the table definitions.

Instead of creating the SQL queries itself, it should reuse the DBAL machinery for that (or use DiffGenerator with fromEmptySchema: true which already does it)

stof avatar Nov 23 '21 11:11 stof

Please add an option to set AbstractPlatform::CREATE_FOREIGNKEYS in lib/Doctrine/Migrations/SchemaDumper.php:91 Foreign keys are part of schema.

loco8878 avatar May 20 '22 14:05 loco8878

Well, the proper fix is described in my previous comment. But we need someone to implement it.

stof avatar May 20 '22 15:05 stof

Probably the best way now is to use migrations:diff --from-empty-schema instead. It does similar thing as migrations:dump-schema but it will contain foreign keys.

jazithedev avatar Oct 12 '22 16:10 jazithedev

sorry, what's a migrations:diff?

image

jacekkarczmarczyk avatar May 27 '23 06:05 jacekkarczmarczyk

@jacekkarczmarczyk bin/console doctrine:migrations:diff --from-empty-schema

gsjurebus avatar Jul 17 '23 10:07 gsjurebus

bin/console doctrine:migrations:diff --from-empty-schema

@gsjurebus thanks, but this looks like a Symfony solution, I don't use Symfony, I'd expect a native doctrine solution without using external libs

jacekkarczmarczyk avatar Jul 17 '23 11:07 jacekkarczmarczyk

Faced this today with doctrine/doctrine-migrations-bundle 3.3.0.

I would have expected doctrine:migrations:dump-schema and doctrine:migrations:diff --from-empty-schema to behave the same. It seems like a rather big issue to dump a database schema without foreign keys... ?

Not a blocking issue as one of the commands does generate the expected output (thanks for the workaround!), but still... is there a reason to keep the current behavior of doctrine:migrations:dump-schema exporting everything except foreign keys?

(posted similar comment on linked issue: https://github.com/doctrine/migrations/issues/820)

pacproduct avatar May 17 '24 13:05 pacproduct