symfonydoctrine-ormdoctrine-migrations

How to Exclude pre-existing Tables from Doctrine Migrations in Symfony?


I am working on a Symfony project using Doctrine, and I've encountered an issue with the migration generation process. My database has pre-existing tables, and I only want to manage a subset of these tables (those prefixed with pp_) with Doctrine. However, when I run php bin/console make:migration, the migration files include SQL statements for all tables, not just those prefixed with pp_.

Here's what I've tried so far:

Ensuring my Entity folder only includes entities for pp_ prefixed tables (it's currently empty). Clearing the Doctrine cache (php bin/console doctrine:cache:clear-metadata). Reviewing the Doctrine configuration to disable auto_mapping and ensure it's not causing automatic mapping of existing tables. Despite these efforts, make:migration still generates migrations for all tables, including those without the pp_ prefix. Here's my current doctrine.yaml configuration for reference:

doctrine.yaml:

doctrine:
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                auto_mapping: false
                # ... other configurations ...

  1. How can I configure Doctrine or Symfony to exclude non-prefixed tables (those without pp_) from the migration generation process?
  2. Is there a way to customize the make:migration command to ignore these tables, or perhaps a recommended approach to handle this scenario in Symfony/Doctrine? Any insights or suggestions would be greatly appreciated!

Solution

  • To configure Doctrine to generate migrations only for tables with a specific prefix (in your case, pp_), you can use the table filtering option in the Doctrine configuration. This is achieved by specifying a schema filter in your doctrine.yaml file. Here's how you can do it:

    doctrine:
      dbal:
        #  ...
        schema_filter: ~^(pp_)~
    

    This regular expression tells Doctrine to consider only tables starting with "pp_". By adding this filter, Doctrine will ignore tables without this prefix when generating migrations. For more detailed information, please refer to the DoctrineBundle documentation on Symfony's website: DoctrineBundle Documentation.