<?php
declare(strict_types=1);
namespace SitesBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20230824120520 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE sites_bundle__note_type (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(255) NOT NULL, number VARCHAR(32) NOT NULL, color VARCHAR(7) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_1C47A68D96901F54 (number), INDEX IDX_1C47A68DB03A8386 (created_by_id), INDEX IDX_1C47A68D896DBBDE (updated_by_id), INDEX IDX_1C47A68DC76F1F52 (deleted_by_id), INDEX name (name), INDEX number (number), INDEX color (color), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE sites_bundle__note_type_translations (id INT AUTO_INCREMENT NOT NULL, object_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', locale VARCHAR(8) NOT NULL, field VARCHAR(32) NOT NULL, content LONGTEXT DEFAULT NULL, INDEX IDX_2E8081B5232D562B (object_id), FULLTEXT INDEX content (content), UNIQUE INDEX lookup_unique_idx (locale, object_id, field), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE sites_bundle__site (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', customer_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(255) NOT NULL, internal_note LONGTEXT DEFAULT NULL, git_link VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_40572FA39395C3F3 (customer_id), INDEX IDX_40572FA3B03A8386 (created_by_id), INDEX IDX_40572FA3896DBBDE (updated_by_id), INDEX IDX_40572FA3C76F1F52 (deleted_by_id), INDEX name (name), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE sites_bundle__site_note (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', site_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', note_type_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(128) NOT NULL, content LONGTEXT DEFAULT NULL, position INT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_C4C4A711F6BD1646 (site_id), INDEX IDX_C4C4A71144EA4809 (note_type_id), INDEX IDX_C4C4A711B03A8386 (created_by_id), INDEX IDX_C4C4A711896DBBDE (updated_by_id), INDEX IDX_C4C4A711C76F1F52 (deleted_by_id), INDEX name (name), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE sites_bundle__note_type ADD CONSTRAINT FK_1C47A68DB03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__note_type ADD CONSTRAINT FK_1C47A68D896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__note_type ADD CONSTRAINT FK_1C47A68DC76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__note_type_translations ADD CONSTRAINT FK_2E8081B5232D562B FOREIGN KEY (object_id) REFERENCES sites_bundle__note_type (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE sites_bundle__site ADD CONSTRAINT FK_40572FA39395C3F3 FOREIGN KEY (customer_id) REFERENCES customers_bundle__customer (id)');
$this->addSql('ALTER TABLE sites_bundle__site ADD CONSTRAINT FK_40572FA3B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__site ADD CONSTRAINT FK_40572FA3896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__site ADD CONSTRAINT FK_40572FA3C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__site_note ADD CONSTRAINT FK_C4C4A711F6BD1646 FOREIGN KEY (site_id) REFERENCES sites_bundle__site (id)');
$this->addSql('ALTER TABLE sites_bundle__site_note ADD CONSTRAINT FK_C4C4A71144EA4809 FOREIGN KEY (note_type_id) REFERENCES sites_bundle__note_type (id)');
$this->addSql('ALTER TABLE sites_bundle__site_note ADD CONSTRAINT FK_C4C4A711B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__site_note ADD CONSTRAINT FK_C4C4A711896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE sites_bundle__site_note ADD CONSTRAINT FK_C4C4A711C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE projects_bundle__project DROP FOREIGN KEY FK_9317E9CF6BD1646');
$this->addSql('ALTER TABLE sites_bundle__note_type DROP FOREIGN KEY FK_1C47A68DB03A8386');
$this->addSql('ALTER TABLE sites_bundle__note_type DROP FOREIGN KEY FK_1C47A68D896DBBDE');
$this->addSql('ALTER TABLE sites_bundle__note_type DROP FOREIGN KEY FK_1C47A68DC76F1F52');
$this->addSql('ALTER TABLE sites_bundle__note_type_translations DROP FOREIGN KEY FK_2E8081B5232D562B');
$this->addSql('ALTER TABLE sites_bundle__site DROP FOREIGN KEY FK_40572FA39395C3F3');
$this->addSql('ALTER TABLE sites_bundle__site DROP FOREIGN KEY FK_40572FA3B03A8386');
$this->addSql('ALTER TABLE sites_bundle__site DROP FOREIGN KEY FK_40572FA3896DBBDE');
$this->addSql('ALTER TABLE sites_bundle__site DROP FOREIGN KEY FK_40572FA3C76F1F52');
$this->addSql('ALTER TABLE sites_bundle__site_note DROP FOREIGN KEY FK_C4C4A711F6BD1646');
$this->addSql('ALTER TABLE sites_bundle__site_note DROP FOREIGN KEY FK_C4C4A71144EA4809');
$this->addSql('ALTER TABLE sites_bundle__site_note DROP FOREIGN KEY FK_C4C4A711B03A8386');
$this->addSql('ALTER TABLE sites_bundle__site_note DROP FOREIGN KEY FK_C4C4A711896DBBDE');
$this->addSql('ALTER TABLE sites_bundle__site_note DROP FOREIGN KEY FK_C4C4A711C76F1F52');
$this->addSql('DROP TABLE sites_bundle__note_type');
$this->addSql('DROP TABLE sites_bundle__note_type_translations');
$this->addSql('DROP TABLE sites_bundle__site');
$this->addSql('DROP TABLE sites_bundle__site_note');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$now = (new \DateTime())->format('Y-m-d H:i:s');
$languages = $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
->fetchAllAssociative();
$rootLocale = 'en';
$defaultLocale = $this->connection
->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
->fetchOne() ?: 'en';
$noteTypes = [
[
'names' => ['fr' => 'PROD', 'en' => 'PROD'],
'color' => '#f6b3b3',
'number' => '1'
],
[
'names' => ['fr' => 'DÉMO', 'en' => 'DEMO'],
'color' => '#fcc766',
'number' => '2'
],
[
'names' => ['fr' => 'DEV', 'en' => 'DEV'],
'color' => '#b3f6b3',
'number' => '3'
],
[
'names' => ['fr' => 'AUTRES', 'en' => 'OTHERS'],
'color' => '#edf1f2',
'number' => '4'
]
];
foreach ($noteTypes as $noteType) {
$noteTypeId = (new UuidV6())->toBinary();
$this->connection->insert('sites_bundle__note_type', [
'id' => $noteTypeId,
'name' => !empty($noteType['names'][$defaultLocale]) ?
$noteType['names'][$defaultLocale] : $noteType['names'][$rootLocale],
'color' => $noteType['color'],
'number' => $noteType['number'],
'created_at' => $now,
'updated_at' => $now
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($noteType['names'][$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($noteType['names'][$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('sites_bundle__note_type_translations', [
'object_id' => $noteTypeId,
'locale' => $language['locale'],
'field' => 'name',
'content' => $noteType['names'][$localeTo]
]);
}
}
}
}