ezsystems/doctrine-dbal-schema
Doctrine DBAL schema utility package for eZ Platform/eZ Systems projects. Provides tools to define, compare and update database schemas using Doctrine DBAL, helping manage schema changes and migrations consistently across environments.
Installation Add the package via Composer:
composer require ezsystems/doctrine-dbal-schema
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
EzSystems\DoctrineDbalSchema\DoctrineDbalSchemaServiceProvider::class,
],
Basic Usage The package provides a schema manager for Doctrine DBAL. To generate a schema SQL file:
use EzSystems\DoctrineDbalSchema\SchemaManager;
$schemaManager = app(SchemaManager::class);
$schema = $schemaManager->getSchema();
$sql = $schemaManager->getDatabasePlatform()->getCreateSchemaSql($schema);
file_put_contents('schema.sql', $sql);
First Use Case Use it to dump your current database schema for version control or migrations:
$schemaManager = app(SchemaManager::class);
$schema = $schemaManager->getSchema();
$schema->drop(); // Optional: Reset DB before generating
$schema->create($schemaManager->getConnection()->getSchemaManager()->createSchema());
Schema Comparison & Updates Compare live schema with a reference (e.g., from migrations):
$liveSchema = $schemaManager->getSchema();
$referenceSchema = $schemaManager->getSchemaFromFile('path/to/reference.sql');
$differ = new \Doctrine\DBAL\Schema\Comparator();
$diffs = $differ->diff($referenceSchema, $liveSchema);
Custom Schema Generation Extend the schema manager to include custom tables or logic:
class CustomSchemaManager extends \EzSystems\DoctrineDbalSchema\SchemaManager
{
public function getSchema()
{
$schema = parent::getSchema();
$table = $schema->createTable('custom_table');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
return $schema;
}
}
Integration with Laravel Migrations Use the package to generate migrations from an existing schema:
$schema = $schemaManager->getSchema();
$platform = $schemaManager->getDatabasePlatform();
$sql = $platform->getCreateSchemaSql($schema);
// Parse SQL into Laravel migrations (e.g., with `doctrine/dbal-migrations-bundle`).
Schema Validation Validate a live database against a known schema:
$schema = $schemaManager->getSchema();
$validator = new \Doctrine\DBAL\Schema\SchemaValidator($schemaManager->getConnection());
$errors = $validator->validate($schema);
if (!empty($errors)) {
throw new \RuntimeException("Schema validation failed: " . implode(', ', $errors));
}
Leverage Laravel’s Service Container
Bind the SchemaManager to the container for easy access:
$this->app->bind(SchemaManager::class, function ($app) {
return new SchemaManager(
$app->make(\Doctrine\DBAL\Connection::class),
$app->make(\Doctrine\DBAL\Platforms\AbstractPlatform::class)
);
});
Combine with Doctrine Migrations Use the package to bootstrap migrations from a live schema:
$schema = $schemaManager->getSchema();
$migration = new \Doctrine\DBAL\Migrations\AbstractMigration();
$migration->up($schemaManager->getConnection(), $schema);
Schema Snapshots Store schema snapshots in version control for rollback:
$schema = $schemaManager->getSchema();
$snapshot = $schema->toSql($schemaManager->getDatabasePlatform());
file_put_contents('schema_snapshots/' . date('Y-m-d') . '.sql', $snapshot);
Connection Configuration
config/database.php.SchemaManager, so misconfigurations (e.g., wrong driver) will cause failures.Platform-Specific SQL
$platform = $schemaManager->getDatabasePlatform();
// Or manually:
$platform = \Doctrine\DBAL\Platforms\MySqlPlatform::class;
Schema Locking
Foreign Key Constraints
Enable Doctrine DBAL Logging
Add this to config/logging.php to debug schema operations:
'channels' => [
'doctrine' => [
'driver' => 'single',
'handler' => 'stream',
'path' => storage_path('logs/doctrine.log'),
],
],
Then configure DBAL to use the channel:
$connection->getEventManager()->addEventListener(
\Doctrine\DBAL\Events::onSchemaCreate,
function ($eventArgs) {
\Log::channel('doctrine')->info('Schema create event', $eventArgs->getSchema());
}
);
Compare Schemas Manually Dump both live and reference schemas to files and diff them:
file_put_contents('live_schema.sql', $liveSchema->toSql($platform));
file_put_contents('reference_schema.sql', $referenceSchema->toSql($platform));
Use SchemaDiff for Troubleshooting
The SchemaDiff tool can highlight differences between schemas:
$diff = new \Doctrine\DBAL\Schema\SchemaDiff($liveSchema, $referenceSchema);
$sql = $diff->toSql($platform);
Custom Schema Events Extend the package by listening to schema events:
$schemaManager->getConnection()->getEventManager()->addEventListener(
\Doctrine\DBAL\Events::onSchemaCreate,
function ($eventArgs) {
// Modify the schema before creation
$eventArgs->getSchema()->createTable('audit_log');
}
);
Override Schema Generation
Subclass EzSystems\DoctrineDbalSchema\SchemaManager and override methods like:
public function getSchema()
{
$schema = parent::getSchema();
// Add custom tables or modify existing ones
return $schema;
}
Add Custom Platform Support
If using an unsupported platform (e.g., SQLite), extend AbstractPlatform and bind it:
$this->app->bind(\Doctrine\DBAL\Platforms\Platform::class, function () {
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
});
Integrate with Laravel Artisan Create a custom Artisan command for schema operations:
use EzSystems\DoctrineDbalSchema\SchemaManager;
use Illuminate\Console\Command;
class SchemaDumpCommand extends Command
{
protected $signature = 'schema:dump {--path=schema.sql}';
protected $description = 'Dump the current database schema';
public function handle(SchemaManager $schemaManager)
{
$sql = $schemaManager->getDatabasePlatform()->getCreateSchemaSql(
$schemaManager->getSchema()
);
file_put_contents($this->option('path'), $sql);
$this->info('Schema dumped!');
}
}
How can I help you explore Laravel packages today?