jorge07 / symfony-6-es-cqrs-boilerplate

Symfony 6 DDD ES CQRS backend boilerplate.
MIT License
1.07k stars 187 forks source link

Question about generated migrations #215

Closed Romaixn closed 3 years ago

Romaixn commented 3 years ago

How do you generate new migration ? (I'm new in DDD and CQRS concept, sorry if it's dumb question)

jorge07 commented 3 years ago

Hi @Romaixn

I use doctrine migrations bundle, which is included in the orm-pack dependency.

From inside the container, ones your doctrine entities are modified, execute /bin/console doctrine:migrations:generate and it will create a new migration file.

Happy to add it to the makefile if helps.

Romaixn commented 3 years ago

Doesn't it generate migrations automatically like the Symfony maker?

jorge07 commented 3 years ago

I don't have experience with symfony maker, but it probably use doctrine bundle under the hood https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

jorge07 commented 3 years ago

If you can provide a bit more context about the work flow you want to accomplish, I could help you better

Romaixn commented 3 years ago

Yes of course, for the moment I just want to change MySQL to PostgreSQL to my project. So I deleted Migrations and wanted to regenerate them. (automatically)

But the command /bin/console doctrine:migrations:generate generate me an empty Migration file.

Did you write this line by hand in the Migration file ? $this->addSql('CREATE TABLE users (uuid BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid_binary)\', created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', credentials_email VARCHAR(255) NOT NULL COMMENT \'(DC2Type:email)\', credentials_password VARCHAR(255) NOT NULL COMMENT \'(DC2Type:hashed_password)\', UNIQUE INDEX UNIQ_1483A5E9299C9369 (credentials_email), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4 COLLATEutf8mb4_unicode_ciENGINE = InnoDB');

jorge07 commented 3 years ago

Let me open a branch to test this use case. It can be a configuration issue. Will be back to you ASAP

jorge07 commented 3 years ago

I've test it and it works to for me: I've deleted the user table migration and in the container runned:

ae07feeaf91b:/app# ./bin/console doctrine:migrations:diff

 Generated new migration class to "/app/src/App/Shared/Infrastructure/Persistence/Doctrine/Migrations/Version20210528143745.php"

 To run just this migration for testing purposes, you can use migrations:execute --up 'App\\Shared\\Infrastructure\\Persistence\\Doctrine\\Migrations\\Version20210528143745'

 To revert the migration you can use migrations:execute --down 'App\\Shared\\Infrastructure\\Persistence\\Doctrine\\Migrations\\Version20210528143745'

The result:

<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\Persistence\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20210528143745 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE users (uuid BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid_binary)\', created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', credentials_email VARCHAR(255) NOT NULL COMMENT \'(DC2Type:email)\', credentials_password VARCHAR(255) NOT NULL COMMENT \'(DC2Type:hashed_password)\', UNIQUE INDEX UNIQ_1483A5E9299C9369 (credentials_email), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('DROP TABLE users');
    }
}
jorge07 commented 3 years ago

Added this one. Maybe some lines in the readme if helps #216

Romaixn commented 3 years ago

Thanks it's worked ! Thanks for your work ! 👍🏻

jorge07 commented 3 years ago

Great. I added few lines about doctrine migrations on the Projections section in the documentation.