a5sys/doctrine-migration-tools-bundle
Installation
composer require a5sys/doctrine-migration-tools-bundle
Ensure DoctrineMigrationsBundle is already installed (this package extends its functionality).
First Use Case
Generate a migration from a schema file (e.g., schema.yml or schema.xml) instead of the database:
php bin/console doctrine:migrations:diff-file
src/Migrations/ (default) and dumps the current schema to:
app/DoctrineMigrations/SchemaVersion/.Where to Look First
php bin/console list → Filter for doctrine:migrations:diff-file.config/packages/a5sys_doctrine_migration_tools.yaml (if it exists) for custom paths or defaults.config/schema.yml) is up-to-date and matches your intended database structure.Schema-Driven Migrations
schema.yml) with new tables/columns.doctrine:migrations:diff-file to generate a migration comparing the schema file to the current database state.YYYYMMDDHHMMSS_UpdateSchema.php) and commit it.php bin/console doctrine:migrations:migrate
CI/CD Integration
--check flag to a CI pipeline to fail builds if migrations are needed:
php bin/console doctrine:migrations:diff-file --check
Exit code 1 indicates pending migrations (useful for enforcing schema consistency).Multi-Environment Sync
# Generate migration for staging (schema file vs. staging DB)
php bin/console doctrine:migrations:diff-file --env=staging
# config/packages/a5sys_doctrine_migration_tools.yaml
a5sys_doctrine_migration_tools:
schema_file: '%kernel.project_dir%/config/custom_schema.yml'
StoDoctrineExtensionsBundle (e.g., for behaviors like timestamps), ensure your schema file includes these extensions to avoid migration conflicts.SchemaVersion/) to track schema evolution alongside migrations.Schema File Desync
schema.yml) drifts from the actual database schema due to manual migrations or third-party changes.php bin/console doctrine:schema:update --dump-sql --complete
Then update schema.yml manually or use a diff tool to reconcile.False Positives with --check
--check flag may return 1 (error) even if no changes are needed, due to:
schema.yml vs. Schema.yml).SchemaVersion/.ls -la app/DoctrineMigrations/SchemaVersion/
Migration Generation Conflicts
diff-file after manual DB changes may produce incorrect migrations.php bin/console doctrine:schema:update --force
Namespace/Autoloading Issues
src/Migrations/ directory isn’t properly configured.src/Migrations/ is listed in composer.json:
"autoload": {
"psr-4": {
"App\\Migrations\\": "src/Migrations/"
}
}
-v or -vv to debug the diff process:
php bin/console doctrine:migrations:diff-file -vv
php bin/console doctrine:schema:update --dry-run
Custom Diff Logic Override the diff command by extending the bundle’s service:
# config/services.yaml
a5sys_doctrine_migration_tools.diff_command:
class: App\Command\CustomDiffCommand
arguments:
- '@a5sys_doctrine_migration_tools.diff_command.inner'
tags: ['console.command']
Implement App\Command\CustomDiffCommand to modify diff behavior (e.g., ignore specific tables).
Post-Generation Hooks Use Symfony’s event system to act after migration generation:
// src/EventListener/MigrationGeneratedListener.php
class MigrationGeneratedListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'a5sys_doctrine_migration_tools.migration_generated' => 'onMigrationGenerated',
];
}
public function onMigrationGenerated(MigrationGeneratedEvent $event)
{
// Example: Slack notification or Git hook trigger
}
}
SchemaVersion/ path is hardcoded to app/DoctrineMigrations/SchemaVersion/. To change it:
a5sys_doctrine_migration_tools:
schema_version_dir: '%kernel.project_dir%/var/schema_backups'
YYYYMMDDHHMMSS_ prefix. To customize:
Override the MigrationGenerator service (advanced; requires deep bundle knowledge).schema.yml over schema.xml for readability and easier version control diffs.php bin/console doctrine:schema:update --complete --dump-sql | mysql -u user -p db_name
SchemaVersion/ periodically to audit schema history:
tar -czvf schema_backup_$(date +%Y-%m-%d).tar.gz app/DoctrineMigrations/SchemaVersion/
How can I help you explore Laravel packages today?