migrations/Version20260429120000.php line 1

  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. final class Version20260429120000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Add certificate templates, rules, conditions and generated registration certificates';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         $this->abortIf(
  15.             !$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySQL57Platform,
  16.             "Migration can only be executed safely on '\Doctrine\DBAL\Platforms\MySQL57Platform'."
  17.         );
  18.         $this->addSql('CREATE TABLE certificate_template (
  19.             id INT AUTO_INCREMENT NOT NULL,
  20.             title VARCHAR(255) NOT NULL,
  21.             body LONGTEXT NOT NULL,
  22.             active TINYINT(1) DEFAULT 1 NOT NULL,
  23.             created_at DATETIME NOT NULL,
  24.             updated_at DATETIME NOT NULL,
  25.             PRIMARY KEY(id)
  26.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  27.         $this->addSql('CREATE TABLE certificate_rule (
  28.             id INT AUTO_INCREMENT NOT NULL,
  29.             template_id INT NOT NULL,
  30.             created_by_id INT DEFAULT NULL,
  31.             updated_by_id INT DEFAULT NULL,
  32.             name VARCHAR(255) NOT NULL,
  33.             active TINYINT(1) DEFAULT 1 NOT NULL,
  34.             requires_signature TINYINT(1) DEFAULT 0 NOT NULL,
  35.             created_at DATETIME NOT NULL,
  36.             updated_at DATETIME NOT NULL,
  37.             INDEX IDX_CERTIFICATE_RULE_TEMPLATE (template_id),
  38.             INDEX IDX_CERTIFICATE_RULE_CREATED_BY (created_by_id),
  39.             INDEX IDX_CERTIFICATE_RULE_UPDATED_BY (updated_by_id),
  40.             PRIMARY KEY(id)
  41.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  42.         $this->addSql('CREATE TABLE certificate_condition (
  43.             id INT AUTO_INCREMENT NOT NULL,
  44.             rule_id INT NOT NULL,
  45.             qualification_type VARCHAR(30) NOT NULL,
  46.             required_result VARCHAR(20) DEFAULT \'APTO\' NOT NULL,
  47.             position INT DEFAULT 1 NOT NULL,
  48.             INDEX IDX_CERTIFICATE_CONDITION_RULE (rule_id),
  49.             PRIMARY KEY(id)
  50.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  51.         $this->addSql('CREATE TABLE registration_certificate (
  52.             id INT AUTO_INCREMENT NOT NULL,
  53.             registration_id INT NOT NULL,
  54.             event_id INT NOT NULL,
  55.             student_id INT NOT NULL,
  56.             rule_id INT DEFAULT NULL,
  57.             generated_by_id INT DEFAULT NULL,
  58.             version INT NOT NULL,
  59.             file_path VARCHAR(500) NOT NULL,
  60.             file_hash VARCHAR(64) NOT NULL,
  61.             active TINYINT(1) DEFAULT 1 NOT NULL,
  62.             generated_at DATETIME NOT NULL,
  63.             annulled_at DATETIME DEFAULT NULL,
  64.             annulment_reason LONGTEXT DEFAULT NULL,
  65.             signed TINYINT(1) DEFAULT 0 NOT NULL,
  66.             signature_status VARCHAR(30) DEFAULT \'NOT_REQUIRED\' NOT NULL,
  67.             signature_error LONGTEXT DEFAULT NULL,
  68.             signature_certificate_fingerprint VARCHAR(128) DEFAULT NULL,
  69.             signature_signed_at DATETIME DEFAULT NULL,
  70.             INDEX IDX_REGISTRATION_CERTIFICATE_REGISTRATION (registration_id),
  71.             INDEX idx_registration_certificate_event_active (event_id, active),
  72.             INDEX IDX_REGISTRATION_CERTIFICATE_STUDENT (student_id),
  73.             INDEX IDX_REGISTRATION_CERTIFICATE_RULE (rule_id),
  74.             INDEX IDX_REGISTRATION_CERTIFICATE_GENERATED_BY (generated_by_id),
  75.             INDEX idx_registration_certificate_active (active),
  76.             PRIMARY KEY(id)
  77.         ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  78.         $this->addSql('ALTER TABLE certificate_rule ADD CONSTRAINT FK_CERTIFICATE_RULE_TEMPLATE FOREIGN KEY (template_id) REFERENCES certificate_template (id) ON DELETE CASCADE');
  79.         $this->addSql('ALTER TABLE certificate_rule ADD CONSTRAINT FK_CERTIFICATE_RULE_CREATED_BY FOREIGN KEY (created_by_id) REFERENCES user (id) ON DELETE SET NULL');
  80.         $this->addSql('ALTER TABLE certificate_rule ADD CONSTRAINT FK_CERTIFICATE_RULE_UPDATED_BY FOREIGN KEY (updated_by_id) REFERENCES user (id) ON DELETE SET NULL');
  81.         $this->addSql('ALTER TABLE certificate_condition ADD CONSTRAINT FK_CERTIFICATE_CONDITION_RULE FOREIGN KEY (rule_id) REFERENCES certificate_rule (id) ON DELETE CASCADE');
  82.         $this->addSql('ALTER TABLE registration_certificate ADD CONSTRAINT FK_REGISTRATION_CERTIFICATE_REGISTRATION FOREIGN KEY (registration_id) REFERENCES registrations (id) ON DELETE CASCADE');
  83.         $this->addSql('ALTER TABLE registration_certificate ADD CONSTRAINT FK_REGISTRATION_CERTIFICATE_EVENT FOREIGN KEY (event_id) REFERENCES events (id) ON DELETE CASCADE');
  84.         $this->addSql('ALTER TABLE registration_certificate ADD CONSTRAINT FK_REGISTRATION_CERTIFICATE_STUDENT FOREIGN KEY (student_id) REFERENCES user (id) ON DELETE CASCADE');
  85.         $this->addSql('ALTER TABLE registration_certificate ADD CONSTRAINT FK_REGISTRATION_CERTIFICATE_RULE FOREIGN KEY (rule_id) REFERENCES certificate_rule (id) ON DELETE SET NULL');
  86.         $this->addSql('ALTER TABLE registration_certificate ADD CONSTRAINT FK_REGISTRATION_CERTIFICATE_GENERATED_BY FOREIGN KEY (generated_by_id) REFERENCES user (id) ON DELETE SET NULL');
  87.     }
  88.     public function down(Schema $schema): void
  89.     {
  90.         $this->abortIf(
  91.             !$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySQL57Platform,
  92.             "Migration can only be executed safely on '\Doctrine\DBAL\Platforms\MySQL57Platform'."
  93.         );
  94.         $this->addSql('ALTER TABLE registration_certificate DROP FOREIGN KEY FK_REGISTRATION_CERTIFICATE_REGISTRATION');
  95.         $this->addSql('ALTER TABLE registration_certificate DROP FOREIGN KEY FK_REGISTRATION_CERTIFICATE_EVENT');
  96.         $this->addSql('ALTER TABLE registration_certificate DROP FOREIGN KEY FK_REGISTRATION_CERTIFICATE_STUDENT');
  97.         $this->addSql('ALTER TABLE registration_certificate DROP FOREIGN KEY FK_REGISTRATION_CERTIFICATE_RULE');
  98.         $this->addSql('ALTER TABLE registration_certificate DROP FOREIGN KEY FK_REGISTRATION_CERTIFICATE_GENERATED_BY');
  99.         $this->addSql('ALTER TABLE certificate_condition DROP FOREIGN KEY FK_CERTIFICATE_CONDITION_RULE');
  100.         $this->addSql('ALTER TABLE certificate_rule DROP FOREIGN KEY FK_CERTIFICATE_RULE_TEMPLATE');
  101.         $this->addSql('ALTER TABLE certificate_rule DROP FOREIGN KEY FK_CERTIFICATE_RULE_CREATED_BY');
  102.         $this->addSql('ALTER TABLE certificate_rule DROP FOREIGN KEY FK_CERTIFICATE_RULE_UPDATED_BY');
  103.         $this->addSql('DROP TABLE registration_certificate');
  104.         $this->addSql('DROP TABLE certificate_condition');
  105.         $this->addSql('DROP TABLE certificate_rule');
  106.         $this->addSql('DROP TABLE certificate_template');
  107.     }
  108. }