<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\Uuid;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20251119103818 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 channel_company (id INT AUTO_INCREMENT NOT NULL, channel_id VARCHAR(36) NOT NULL, created_by_id VARCHAR(36) DEFAULT NULL, updated_by_id VARCHAR(36) DEFAULT NULL, name VARCHAR(255) NOT NULL, siret VARCHAR(14) DEFAULT NULL, representative_last_name VARCHAR(255) DEFAULT NULL, representative_first_name VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, phone VARCHAR(255) DEFAULT NULL, address LONGTEXT DEFAULT NULL, address_street VARCHAR(255) DEFAULT NULL, address_post_code VARCHAR(15) DEFAULT NULL, address_city VARCHAR(255) DEFAULT NULL, address_country VARCHAR(2) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME DEFAULT NULL, INDEX IDX_F8FC364E72F5A1AA (channel_id), INDEX IDX_F8FC364EB03A8386 (created_by_id), INDEX IDX_F8FC364E896DBBDE (updated_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE channel_company_channel_user_data (id INT AUTO_INCREMENT NOT NULL, company_id INT NOT NULL, channel_user_data_id VARCHAR(36) NOT NULL, job VARCHAR(255) DEFAULT NULL, INDEX IDX_2A27B7A6979B1AD6 (company_id), INDEX IDX_2A27B7A623887597 (channel_user_data_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE channel_user_data_cursus_internship (id INT AUTO_INCREMENT NOT NULL, cursus_id INT NOT NULL, company_id INT DEFAULT NULL, tutor_id VARCHAR(36) DEFAULT NULL, created_by_id VARCHAR(36) DEFAULT NULL, updated_by_id VARCHAR(36) DEFAULT NULL, start_at DATE NOT NULL, end_at DATE NOT NULL, duration INT DEFAULT NULL, internship_address LONGTEXT DEFAULT NULL, address_street VARCHAR(255) DEFAULT NULL, address_post_code VARCHAR(15) DEFAULT NULL, address_city VARCHAR(255) DEFAULT NULL, address_country VARCHAR(2) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME DEFAULT NULL, INDEX IDX_1398A1AD40AEF4B9 (cursus_id), INDEX IDX_1398A1AD979B1AD6 (company_id), INDEX IDX_1398A1AD208F64F1 (tutor_id), INDEX IDX_1398A1ADB03A8386 (created_by_id), INDEX IDX_1398A1AD896DBBDE (updated_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE channel_company ADD CONSTRAINT FK_F8FC364E72F5A1AA FOREIGN KEY (channel_id) REFERENCES channel_channels (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE channel_company ADD CONSTRAINT FK_F8FC364EB03A8386 FOREIGN KEY (created_by_id) REFERENCES account_users (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE channel_company ADD CONSTRAINT FK_F8FC364E896DBBDE FOREIGN KEY (updated_by_id) REFERENCES account_users (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE channel_company_channel_user_data ADD CONSTRAINT FK_2A27B7A6979B1AD6 FOREIGN KEY (company_id) REFERENCES channel_company (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE channel_company_channel_user_data ADD CONSTRAINT FK_2A27B7A623887597 FOREIGN KEY (channel_user_data_id) REFERENCES channel_user_data (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship ADD CONSTRAINT FK_1398A1AD40AEF4B9 FOREIGN KEY (cursus_id) REFERENCES channel_user_data_cursus (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship ADD CONSTRAINT FK_1398A1AD979B1AD6 FOREIGN KEY (company_id) REFERENCES channel_company (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship ADD CONSTRAINT FK_1398A1AD208F64F1 FOREIGN KEY (tutor_id) REFERENCES channel_user_data (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship ADD CONSTRAINT FK_1398A1ADB03A8386 FOREIGN KEY (created_by_id) REFERENCES account_users (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship ADD CONSTRAINT FK_1398A1AD896DBBDE FOREIGN KEY (updated_by_id) REFERENCES account_users (id) ON DELETE SET NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE channel_company DROP FOREIGN KEY FK_F8FC364E72F5A1AA');
$this->addSql('ALTER TABLE channel_company DROP FOREIGN KEY FK_F8FC364EB03A8386');
$this->addSql('ALTER TABLE channel_company DROP FOREIGN KEY FK_F8FC364E896DBBDE');
$this->addSql('ALTER TABLE channel_company_channel_user_data DROP FOREIGN KEY FK_2A27B7A6979B1AD6');
$this->addSql('ALTER TABLE channel_company_channel_user_data DROP FOREIGN KEY FK_2A27B7A623887597');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship DROP FOREIGN KEY FK_1398A1AD40AEF4B9');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship DROP FOREIGN KEY FK_1398A1AD979B1AD6');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship DROP FOREIGN KEY FK_1398A1AD208F64F1');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship DROP FOREIGN KEY FK_1398A1ADB03A8386');
$this->addSql('ALTER TABLE channel_user_data_cursus_internship DROP FOREIGN KEY FK_1398A1AD896DBBDE');
$this->addSql('DROP TABLE channel_company');
$this->addSql('DROP TABLE channel_company_channel_user_data');
$this->addSql('DROP TABLE channel_user_data_cursus_internship');
}
/**
* @throws \Throwable
* @throws Exception
*/
public function postUp(Schema $schema): void
{
$this->connection->beginTransaction();
try {
$tutors = $this->connection->fetchAllAssociative('
SELECT t.email, t.first_name, t.last_name, cc.id AS channel_id
FROM `channel_user_data_cursus_founder_tutor` t
JOIN channel_user_data_cursus_company cudcc ON cudcc.id = t.company_id
JOIN channel_user_data_cursus cudc ON cudc.id = cudcc.cursus_id
JOIN channel_user_data cud ON cud.id = cudc.channel_user_data_id
JOIN channel_channels cc ON cc.id = cud.channel_id
WHERE t.email IS NOT NULL
');
foreach ($tutors as $tutor) {
$email = $tutor['email'];
$firstName = $tutor['first_name'];
$lastName = $tutor['last_name'];
$channelId = $tutor['channel_id'];
$existingUser = $this->connection->fetchAssociative(
'SELECT id FROM account_users WHERE email = :email',
['email' => $email]
);
if (!$existingUser) {
$userId = Uuid::v4()->toRfc4122();
$this->connection->insert('account_users', [
'id' => $userId,
'email' => $email,
'password' => password_hash(bin2hex(random_bytes(6)), PASSWORD_BCRYPT),
'enabled' => 1,
'is_verified' => 1,
'first_name' => $firstName,
'last_name' => $lastName,
'roles' => json_encode([]),
'created_at' => (new \DateTime())->format('Y-m-d H:i:s'),
]);
}
else {
$userId = $existingUser['id'];
}
$existingChannelUserData = $this->connection->fetchAssociative(
'SELECT id FROM channel_user_data WHERE user_id = :uid AND channel_id = :cid',
[
'uid' => $userId,
'cid' => $channelId,
]
);
if (!$existingChannelUserData) {
$channelUserDataId = Uuid::v4()->toRfc4122();
$this->connection->insert('channel_user_data', [
'id' => $channelUserDataId,
'user_id' => $userId,
'channel_id' => $channelId,
'is_learner' => 0,
]);
}
}
} catch (\Throwable $e) {
$this->connection->rollBack();
throw $e;
}
}
}