<?php
declare(strict_types=1);
namespace CustomersBundleMigrations;
use DateTime;
use Doctrine\DBAL\Schema\Schema;
use App\Services\ObjectSerialize;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20221202084059 extends AbstractMigration
{
private ObjectSerialize $objectSerialize;
public function setObjectSerialize(ObjectSerialize $objectSerialize)
{
$this->objectSerialize = $objectSerialize;
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE customers_bundle__customer (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', customer_type_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', customer_group_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', country_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', country_state_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', parent_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', context_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', currency_id BINARY(16) DEFAULT 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, firstname VARCHAR(128) DEFAULT NULL, lastname VARCHAR(128) DEFAULT NULL, nature INT DEFAULT NULL, address VARCHAR(35) DEFAULT NULL, address_2 VARCHAR(35) DEFAULT NULL, postcode VARCHAR(12) DEFAULT NULL, city VARCHAR(50) DEFAULT NULL, vat_number VARCHAR(32) DEFAULT NULL, siret VARCHAR(16) DEFAULT NULL, phone VARCHAR(32) DEFAULT NULL, phone_mobile VARCHAR(32) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, website VARCHAR(128) DEFAULT NULL, internal_note LONGTEXT DEFAULT NULL, blocked TINYINT(1) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_1FD6BD95D991282D (customer_type_id), INDEX IDX_1FD6BD95D2919A68 (customer_group_id), INDEX IDX_1FD6BD95F92F3E70 (country_id), INDEX IDX_1FD6BD9536126D7C (country_state_id), INDEX IDX_1FD6BD95727ACA70 (parent_id), INDEX IDX_1FD6BD956B00C1CF (context_id), INDEX IDX_1FD6BD9538248176 (currency_id), INDEX IDX_1FD6BD95B03A8386 (created_by_id), INDEX IDX_1FD6BD95896DBBDE (updated_by_id), INDEX IDX_1FD6BD95C76F1F52 (deleted_by_id), INDEX name (name), INDEX firstname (firstname), INDEX lastname (lastname), INDEX postcode (postcode), INDEX city (city), INDEX phone (phone), INDEX phone_mobile (phone_mobile), INDEX email (email), 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 customers_bundle__customer_group_customer (customer_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', customer_group_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', INDEX IDX_85C42ABD9395C3F3 (customer_id), INDEX IDX_85C42ABDD2919A68 (customer_group_id), PRIMARY KEY(customer_id, customer_group_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE customers_bundle__customer_group (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(32) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_5641E104B03A8386 (created_by_id), INDEX IDX_5641E104896DBBDE (updated_by_id), INDEX IDX_5641E104C76F1F52 (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 customers_bundle__customer_group_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_2CA9AD1F232D562B (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 customers_bundle__customer_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(32) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_635B5E09B03A8386 (created_by_id), INDEX IDX_635B5E09896DBBDE (updated_by_id), INDEX IDX_635B5E09C76F1F52 (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 customers_bundle__customer_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_AF7CDEC7232D562B (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('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95D991282D FOREIGN KEY (customer_type_id) REFERENCES customers_bundle__customer_type (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95D2919A68 FOREIGN KEY (customer_group_id) REFERENCES customers_bundle__customer_group (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95F92F3E70 FOREIGN KEY (country_id) REFERENCES country (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD9536126D7C FOREIGN KEY (country_state_id) REFERENCES country_state (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95727ACA70 FOREIGN KEY (parent_id) REFERENCES customers_bundle__customer (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD956B00C1CF FOREIGN KEY (context_id) REFERENCES context (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD9538248176 FOREIGN KEY (currency_id) REFERENCES currency (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer ADD CONSTRAINT FK_1FD6BD95C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_group_customer ADD CONSTRAINT FK_85C42ABD9395C3F3 FOREIGN KEY (customer_id) REFERENCES customers_bundle__customer (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customers_bundle__customer_group_customer ADD CONSTRAINT FK_85C42ABDD2919A68 FOREIGN KEY (customer_group_id) REFERENCES customers_bundle__customer_group (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customers_bundle__customer_group ADD CONSTRAINT FK_5641E104B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_group ADD CONSTRAINT FK_5641E104896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_group ADD CONSTRAINT FK_5641E104C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_group_translations ADD CONSTRAINT FK_2CA9AD1F232D562B FOREIGN KEY (object_id) REFERENCES customers_bundle__customer_group (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customers_bundle__customer_type ADD CONSTRAINT FK_635B5E09B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_type ADD CONSTRAINT FK_635B5E09896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_type ADD CONSTRAINT FK_635B5E09C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE customers_bundle__customer_type_translations ADD CONSTRAINT FK_AF7CDEC7232D562B FOREIGN KEY (object_id) REFERENCES customers_bundle__customer_type (id) ON DELETE CASCADE');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$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';
$customerTypes = [
['fr' => 'Société', 'en' => 'Company'],
['fr' => 'SARL', 'en' => 'LLC'],
['fr' => 'M.', 'en' => 'Mr.'],
['fr' => 'Mme.', 'en' => 'Ms.'],
['fr' => 'Mlle.', 'en' => 'Miss.']
];
$this->connection->insert('configuration', [
'id' => (new UuidV6())->toBinary(),
'name' => 'customers_bundle__shared_customers',
'context_id' => null,
'value' => $this->objectSerialize->add((object) ['value' => true]),
'created_at' => (new DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new DateTime())->format('Y-m-d H:i:s')
]);
foreach ($customerTypes as $customerType) {
$customerTypeId = (new UuidV6())->toBinary();
$this->connection->insert('customers_bundle__customer_type', [
'id' => $customerTypeId,
'name' => !empty($customerType[$defaultLocale]) ?
$customerType[$defaultLocale] : $customerType[$rootLocale],
'created_at' => (new DateTime())->format('Y-m-d H:i:s'),
'updated_at' => (new DateTime())->format('Y-m-d H:i:s')
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($customerType[$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($customerType[$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('customers_bundle__customer_type_translations', [
'object_id' => $customerTypeId,
'locale' => $language['locale'],
'field' => 'name',
'content' => $customerType[$localeTo]
]);
}
}
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95D991282D');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95D2919A68');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95F92F3E70');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD9536126D7C');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95727ACA70');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD956B00C1CF');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD9538248176');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95B03A8386');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95896DBBDE');
$this->addSql('ALTER TABLE customers_bundle__customer DROP FOREIGN KEY FK_1FD6BD95C76F1F52');
$this->addSql('ALTER TABLE customers_bundle__customer_group_customer DROP FOREIGN KEY FK_85C42ABD9395C3F3');
$this->addSql('ALTER TABLE customers_bundle__customer_group_customer DROP FOREIGN KEY FK_85C42ABDD2919A68');
$this->addSql('ALTER TABLE customers_bundle__customer_group DROP FOREIGN KEY FK_5641E104B03A8386');
$this->addSql('ALTER TABLE customers_bundle__customer_group DROP FOREIGN KEY FK_5641E104896DBBDE');
$this->addSql('ALTER TABLE customers_bundle__customer_group DROP FOREIGN KEY FK_5641E104C76F1F52');
$this->addSql('ALTER TABLE customers_bundle__customer_group_translations DROP FOREIGN KEY FK_2CA9AD1F232D562B');
$this->addSql('ALTER TABLE customers_bundle__customer_type DROP FOREIGN KEY FK_635B5E09B03A8386');
$this->addSql('ALTER TABLE customers_bundle__customer_type DROP FOREIGN KEY FK_635B5E09896DBBDE');
$this->addSql('ALTER TABLE customers_bundle__customer_type DROP FOREIGN KEY FK_635B5E09C76F1F52');
$this->addSql('ALTER TABLE customers_bundle__customer_type_translations DROP FOREIGN KEY FK_AF7CDEC7232D562B');
$this->addSql('DROP TABLE customers_bundle__customer');
$this->addSql('DROP TABLE customers_bundle__customer_group_customer');
$this->addSql('DROP TABLE customers_bundle__customer_group');
$this->addSql('DROP TABLE customers_bundle__customer_group_translations');
$this->addSql('DROP TABLE customers_bundle__customer_type');
$this->addSql('DROP TABLE customers_bundle__customer_type_translations');
}
}