Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Dbal Schema Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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,
    ],
    
  2. 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);
    
  3. 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());
    

Implementation Patterns

Common Workflows

  1. 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);
    
  2. 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;
        }
    }
    
  3. 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`).
    
  4. 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));
    }
    

Integration Tips

  1. 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)
        );
    });
    
  2. 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);
    
  3. 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);
    

Gotchas and Tips

Pitfalls

  1. Connection Configuration

    • Ensure your Doctrine DBAL connection is properly configured in config/database.php.
    • The package relies on the connection’s SchemaManager, so misconfigurations (e.g., wrong driver) will cause failures.
  2. Platform-Specific SQL

    • The generated SQL is platform-specific (MySQL, PostgreSQL, etc.). Always specify the correct platform:
      $platform = $schemaManager->getDatabasePlatform();
      // Or manually:
      $platform = \Doctrine\DBAL\Platforms\MySqlPlatform::class;
      
  3. Schema Locking

    • Generating schema SQL on a production database with active connections may cause locks or timeouts. Test in staging first.
  4. Foreign Key Constraints

    • The package may not handle complex foreign key constraints perfectly. Validate generated SQL manually for critical schemas.

Debugging Tips

  1. 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());
        }
    );
    
  2. 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));
    
  3. Use SchemaDiff for Troubleshooting The SchemaDiff tool can highlight differences between schemas:

    $diff = new \Doctrine\DBAL\Schema\SchemaDiff($liveSchema, $referenceSchema);
    $sql = $diff->toSql($platform);
    

Extension Points

  1. 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');
        }
    );
    
  2. 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;
    }
    
  3. 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();
    });
    
  4. 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!');
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor