DoctrineMigrationsBundle

repository·4.0.x·Indexed 26 days ago

https://github.com/doctrine/doctrinemigrationsbundle

A Symfony bundle providing integration for the Doctrine Migrations library to enable versioned database schema management. It includes functionality for generating migrations via diff, executing CLI commands, injecting dependencies into migrations, and managing metadata storage. Documentation covers installation, configuration, and upgrade paths from version 2.x to 3.0.0 and 4.0.0.

Tokens
3.8K
Snippets
9
Records
15
Agent score
88%

What's inside DoctrineMigrationsBundle

  1. Inject dependencies into migrations

    4.0.x

    To use external services (like a Mailer or custom service) inside a migration, you must enable enable_service_migrations in your configuration. If you are not using default autoconfiguration, you must manually tag your migration classes with doctrine_migrations.migration in config/services.yaml.

    # config/packages/doctrine_migrations.yaml
    
    doctrine_migrations:
        enable_service_migrations: true
        migrations_paths:
            'App\Migrations': '%kernel.project_dir%/src/Migrations'
    # config/services.yaml
    
    services:
        DoctrineMigrations\Version20180605025653:
            tags: [ 'doctrine_migrations.migration' ]
            arguments:
                $myService: '@App\Services\MyService'
    // In your migration class
    
    final class Version20180605025653 extends AbstractMigration
    {
        private MyService $myService;
    
        public function __construct(Connection $connection, LoggerInterface $logger, MyService $myService)
        {
            parent::__construct($connection, $logger);
            $this->myService = $myService;
        }
    
        // ...
    }
  2. Migrate Metadata Table Storage Configuration (v2.x to v3.0.0)

    4.0.x

    In version 3.0.0, the configuration for the metadata table definition was moved under a storage.table_storage hierarchy.

    Note on table_name: The default value for table_name changed from migration_versions to doctrine_migration_versions. If you did not explicitly specify a table_name in your previous version, you must declare it explicitly in 3.0.0 to avoid losing access to your migration data.

    # Before 3.0.0
    doctrine_migrations:
        table_name: 'migration_versions'
        column_name: 'version'
        column_length: 14
        executed_at_column_name: 'executed_at'
    
    # After 3.0.0
    doctrine_migrations:
        storage:
            table_storage:
                table_name: 'migration_versions'
                version_column_name: 'version'
                version_column_length: 191
                executed_at_column_name: 'executed_at'
  3. Ignore manual tables from migrations using schema_filter

    4.0.x

    By default, the doctrine:migrations:diff command will attempt to remove any database tables that are not defined in your Doctrine entities. To prevent this for custom tables (e.g., tables managed by other tools), configure a schema_filter in your Doctrine DBAL settings. This filter uses a regular expression to ignore specific tables or sequences at the DBAL level.

    # config/packages/doctrine.yaml
    # Example: Ignore all tables prefixed by `t_`
    doctrine:
        dbal:
            schema_filter: ~^(?!t_)~
  4. Migrate Migration Namespace and Directory Configuration (v2.x to v3.0.0)

    4.0.x

    In version 3.0.0, the configuration structure for migration namespaces and directories changed. Replace the dir_name and namespace keys with the migrations_paths key.

    # Before 3.0.0
    doctrine_migrations:
        dir_name: '%kernel.project_dir%/src/Migrations'
        namespace: DoctrineMigrations
    
    # After 3.0.0
    doctrine_migrations:
        migrations_paths:
            'DoctrineMigrations': '%kernel.project_dir%/src/Migrations'
  5. Install DoctrineMigrationsBundle

    4.0.x

    To install the bundle in a Symfony application, use Composer. If you are not using Symfony Flex, you must manually register the bundle in your config/bundles.php file.

    $ composer require doctrine/doctrine-migrations-bundle "^3.0"
    // config/bundles.php
    
    return [
        // ...
        Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    ];
  6. Upgrade DoctrineMigrationsBundle to 4.0.0

    4.0.x

    When upgrading to version 4.0.0, ensure your environment and dependencies meet the new requirements.

    Dependency Requirements:

    • PHP 8.4 or higher is required (support for < 8.4 is dropped).
    • doctrine/orm 3 or higher is required.
    • doctrine/doctrine-bundle 3 or higher is required.

    Breaking Changes:

    • Final/Internal Classes: Several classes are now marked as final and @internal. They should not be extended or referenced outside of the bundle.
    • Type Declarations: Type declarations have been added to all method signatures and properties. You may need to update your custom code to match these new types.
    • Container-Aware Migrations: Support for container-aware migrations has been removed. Migrations implementing ContainerAwareInterface will no longer have the container injected automatically. The ContainerAwareMigrationFactory class has also been removed.
  7. Skip a single migration

    4.0.x

    You can skip a specific migration by manually adding it to the migration_versions table. This tells Doctrine that the migration has already been executed, so it will be ignored in future runs. Note: You must use single quotes around the class name for the command to work correctly.

    $ php bin/console doctrine:migrations:version 'App\Migrations\Version123' --add
  8. Generate migrations automatically using diff

    4.0.x

    Instead of writing migrations manually, you can compare your Doctrine mapping (entities) with your current database structure to generate migration classes automatically.

    1. Update your entities (e.g., add a new User entity).
    2. Run the diff command to generate a new migration file containing the necessary SQL.
    3. Run the migrate command to apply the changes to your database.

    If you have already created your schema using doctrine:schema:create and want to start using migrations without running all previous migration files, you can tell Doctrine to skip all existing versions.