concatPrefix(Task::TABLE_NAME); $table = $this->entityManager->getConnection()->createSchemaManager()->introspectTable($tasks); // New columns. if (!$table->hasColumn('task_kind')) { $this->addSql(sprintf( "ALTER TABLE %s ADD task_kind VARCHAR(20) NOT NULL DEFAULT '%s'", $tasks, \MauticPlugin\ProgramBundle\Entity\TaskKind::Task->value )); } if (!$table->hasColumn('definition_of_done')) { $this->addSql("ALTER TABLE {$tasks} ADD definition_of_done LONGTEXT DEFAULT NULL"); } if (!$table->hasColumn('planned_work')) { $this->addSql("ALTER TABLE {$tasks} ADD planned_work INT UNSIGNED DEFAULT NULL"); } // Dates: DATETIME -> DATE, truncating the time part. $start = $table->getColumn('start_date'); $end = $table->getColumn('end_date'); if ('datetime' === $start->getType()->getName()) { $this->addSql("ALTER TABLE {$tasks} MODIFY start_date DATE DEFAULT NULL"); $this->addSql("UPDATE {$tasks} SET start_date = DATE(start_date)"); } if ('datetime' === $end->getType()->getName()) { $this->addSql("ALTER TABLE {$tasks} MODIFY end_date DATE DEFAULT NULL"); $this->addSql("UPDATE {$tasks} SET end_date = DATE(end_date)"); } // Drop the parent/child tree. if ($table->hasColumn('parent_id')) { $this->addSql("ALTER TABLE {$tasks} DROP COLUMN parent_id"); } // New tables. Each is created when missing on its own, so a partial earlier run is repaired. if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskDependency::TABLE_NAME))) { foreach ($this->getCreateTableSqls(TaskDependency::class, TaskDependency::TABLE_NAME) as $sql) { $this->addSql($sql); } } if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskRaci::TABLE_NAME))) { foreach ($this->getCreateTableSqls(TaskRaci::class, TaskRaci::TABLE_NAME) as $sql) { $this->addSql($sql); } } // The RACI join table comes with the TaskRaci entity's SQL (CREATE TABLE initiative_task_raci_stakeholders), // but it may be missing while initiative_task_raci exists (a partial earlier run). if (!$this->introspectSchema()->hasTable($this->concatPrefix(TaskRaci::JOIN_TABLE_NAME))) { foreach ($this->getCreateTableSqls(TaskRaci::class, TaskRaci::JOIN_TABLE_NAME) as $sql) { $this->addSql($sql); } } } private function introspectSchema(): Schema { return $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); } /** * The CREATE TABLE statements of one of this migration's entities, from Doctrine's SchemaTool — * the entity's table and, for TaskRaci, its join table initiative_task_raci_stakeholders too. * * @param class-string $entityClass * * @return string[] */ private function getCreateTableSqls(string $entityClass, string $table): array { $schemaTool = new SchemaTool($this->entityManager); $prefix = 'CREATE TABLE '; $prefixes = [$prefix.$this->concatPrefix($table).' ', $prefix.$this->concatPrefix($table).'_']; $statements = []; foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata($entityClass)]) as $sql) { foreach ($prefixes as $candidate) { if (str_starts_with($sql, $candidate)) { $statements[] = $sql; } } } if ([] === $statements) { throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.$table.'.'); } return $statements; } }