<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
use PaperKite\MyCmcmApi\Entity\Enum\Method2FaType;
use Symfony\Component\Uid\Uuid;
final class Version20250428900000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create mycmcm_user_2fa table';
}
public function up(Schema $schema): void
{
$this->skipIf(
true === $schema->hasTable('mycmcm_user_2fa'),
'The mycmcm_user_2fa table already exists!'
);
$table = $schema->createTable('mycmcm_user_2fa');
$table->addColumn('id', Types::GUID)->setNotnull(true);
$table->addColumn('code', Types::STRING, ['notnull' => false, 'length' => 6]);
$table->addColumn('code_expires_at', Types::DATETIME_IMMUTABLE, ['notnull' => false]);
$table->addColumn('code_created_at', Types::DATETIME_IMMUTABLE, ['notnull' => false]);
$table->addColumn('method', Types::STRING, ['notnull' => true, 'length' => 20, 'default' => Method2FaType::TYPE_EMAIL]);
$table->addColumn('my_cmcm_user_id', Types::GUID)->setNotnull(true);
$table->setPrimaryKey(['id']);
$myCmcmUserTable = $schema->getTable('mycmcm_user');
$table->addForeignKeyConstraint(
$myCmcmUserTable,
['my_cmcm_user_id'],
['id'],
[],
'fk_2fa_my_cmcm_user'
);
$table->addIndex(['my_cmcm_user_id'], 'idx_2fa_my_cmcm_user_id');
}
public function postUp(Schema $schema): void
{
$connection = $this->connection;
$users = $connection->fetchAllAssociative('SELECT id FROM mycmcm_user');
foreach ($users as $user) {
$connection->insert('mycmcm_user_2fa', [
'id' => Uuid::v4(),
'my_cmcm_user_id' => $user['id'],
'method' => Method2FaType::TYPE_EMAIL,
]);
}
}
public function down(Schema $schema): void
{
$this->skipIf(
false === $schema->hasTable('mycmcm_user_2fa'),
'The mycmcm_user_2fa table is already removed !'
);
$schema->dropTable('mycmcm_user_2fa');
}
}