column name, for the columns an early version of the mapping left in camelCase. */ private const COLUMNS = [ 'targetType' => 'target_type', 'profileCriteria' => 'profile_criteria', 'painPoints' => 'pain_points', 'valueProposition' => 'value_proposition', ]; protected function isApplicable(Schema $schema): bool { if (!$schema->hasTable($this->concatPrefix(Plan::TABLE_NAME))) { return false; } if (!$schema->hasTable($this->concatPrefix(Target::TABLE_NAME)) || !$schema->hasTable($this->concatPrefix('plan_target_stages'))) { return true; } // Repair mode: missing foreign keys or camelCase columns. return [] !== $this->getColumnsToRename($schema) || !$this->hasForeignKeys($schema); } protected function up(): void { $schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema(); if (!$schema->hasTable($this->concatPrefix(Target::TABLE_NAME))) { $this->addSql($this->getCreateTableSql(Target::TABLE_NAME)); } if (!$schema->hasTable($this->concatPrefix('plan_target_stages'))) { $this->addSql($this->getCreateTableSql('plan_target_stages')); } foreach ($this->getColumnsToRename($schema) as $field => $column) { $this->addSql(sprintf( 'ALTER TABLE %s CHANGE %s %s', $this->concatPrefix(Target::TABLE_NAME), $field, $this->getColumnDefinition($field, $column) )); } if (!$this->hasForeignKeys($schema)) { $this->addSql($this->generateAlterTableForeignKeyStatement( Target::TABLE_NAME, ['plan_id'], Plan::TABLE_NAME, ['id'], 'ON DELETE CASCADE' )); } } /** * @return array field name => column name, for the camelCase columns still present */ private function getColumnsToRename(Schema $schema): array { if (!$schema->hasTable($this->concatPrefix(Target::TABLE_NAME))) { return []; } $table = $schema->getTable($this->concatPrefix(Target::TABLE_NAME)); $rename = []; foreach (self::COLUMNS as $field => $column) { if ($table->hasColumn($field) && !$table->hasColumn($column)) { $rename[$field] = $column; } } return $rename; } private function hasForeignKeys(Schema $schema): bool { $targets = $this->concatPrefix(Target::TABLE_NAME); if (!$schema->hasTable($targets)) { return false; } foreach ($schema->getTable($targets)->getForeignKeys() as $foreignKey) { if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) { return true; } } return false; } /** * The MySQL column definition matching the entity mapping, for a CHANGE that renames the column. */ private function getColumnDefinition(string $field, string $column): string { $definitions = [ 'targetType' => sprintf("%s VARCHAR(30) NOT NULL DEFAULT '%s'", $column, TargetTypeEnum::Persona->value), 'profileCriteria' => $column.' LONGTEXT DEFAULT NULL', 'painPoints' => $column.' LONGTEXT DEFAULT NULL', 'valueProposition' => $column.' LONGTEXT DEFAULT NULL', ]; return $definitions[$field] ?? $column; } /** * The CREATE TABLE statement for one of this migration's tables, from Doctrine's SchemaTool. */ private function getCreateTableSql(string $table): string { $schemaTool = new SchemaTool($this->entityManager); $prefix = 'CREATE TABLE '.$this->concatPrefix($table).' '; foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata(Target::class)]) as $sql) { if (str_starts_with($sql, $prefix)) { return $sql; } } throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.$table.'.'); } }