Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing foreign keys from dump-schema command #1163

Open
clement-michelet opened this issue May 26, 2021 · 9 comments
Open

Missing foreign keys from dump-schema command #1163

clement-michelet opened this issue May 26, 2021 · 9 comments

Comments

@clement-michelet
Copy link

Bug Report

Q A
BC Break no
Version 2.3.3

Summary

Original issue : #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

@goetas
Copy link
Member

goetas commented Jul 4, 2021

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.

@clement-michelet
Copy link
Author

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.

@stof
Copy link
Member

stof commented Nov 23, 2021

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)

@loco8878
Copy link

loco8878 commented May 20, 2022

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

@stof
Copy link
Member

stof commented May 20, 2022

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

@jazithedev
Copy link

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.

@jacekkarczmarczyk
Copy link

sorry, what's a migrations:diff?

image

@gsjurebus
Copy link

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

@jacekkarczmarczyk
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants