ildTableOptions($options); $query .= $this->buildPartitionOptions($options); $sql = [$query]; $engine = 'INNODB'; if (isset($options['engine'])) { $engine = strtoupper(trim($options['engine'])); } // Propagate foreign key constraints only for InnoDB. if (isset($options['foreignKeys']) && $engine === 'INNODB') { foreach ((array) $options['foreignKeys'] as $definition) { $sql[] = $this->getCreateForeignKeySQL($definition, $name); } } return $sql; } public function getDefaultValueDeclarationSQL($column) { // Unset the default value if the given column definition does not allow default values. if ($column['type'] instanceof TextType || $column['type'] instanceof BlobType) { $column['default'] = null; } return parent::getDefaultValueDeclarationSQL($column); } private function buildTableOptions(array $options) { if (isset($options['table_options'])) { return $options['table_options']; } $tableOptions = []; // Charset if (!isset($options['charset'])) { $options['charset'] = 'utf8'; } $tableOptions[] = sprintf('DEFAULT CHARACTER SET %s', $options['charset']); // Collate if (!isset($options['collate'])) { $options['collate'] = $options['charset'] . '_unicode_ci'; } $tableOptions[] = $this->getColumnCollationDeclarationSQL($options['collate']); // Engine if (!isset($options['engine'])) { $options['engine'] = 'InnoDB'; } $tableOptions[] = sprintf('ENGINE = %s', $options['engine']); // Auto increment if (isset($options['auto_increment'])) { $tableOptions[] = sprintf('AUTO_INCREMENT = %s', $options['auto_increment']); } // Comment if (isset($options['comment'])) { $tableOptions[] = sprintf('COMMENT = %s ', $this->quoteStringLiteral($options['comment'])); } // Row format if (isset($options['row_format'])) { $tableOptions[] = sprintf('ROW_FORMAT = %s', $options['row_format']); } return implode(' ', $tableOptions); } private function buildPartitionOptions(array $options) { return isset($options['partition_options']) ? ' ' . $options['partition_options'] : ''; } public function getAlterTableSQL(TableDiff $diff) { $columnSql = []; $queryParts = []; $newName = $diff->getNewName(); if ($newName !== \false) { $queryParts[] = 'RENAME TO ' . $newName->getQuotedName($this); } foreach ($diff->addedColumns as $column) { if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { continue; } $columnArray = array_merge($column->toArray(), ['comment' => $this->getColumnComment($column)]); $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); } foreach ($diff->removedColumns as $column) { if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { continue; } $queryParts[] = 'DROP ' . $column->getQuotedName($this); } foreach ($diff->changedColumns as $columnDiff) { if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { continue; } $column = $columnDiff->column; $columnArray = $column->toArray(); // Don't propagate default value changes for unsupported column types. if ($columnDiff->hasChanged('default') && count($columnDiff->changedProperties) === 1 && ($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType)) { continue; } $columnArray['comment'] = $this->getColumnComment($column); $queryParts[] = 'CHANGE ' . $columnDiff->getOldColumnName()->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); } foreach ($diff->renamedColumns as $oldColumnName => $column) { if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { continue; } $oldColumnName = new Identifier($oldColumnName); $columnArray = $column->toArray(); $columnArray['comment'] = $this->getColumnComment($column); $queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); } if (isset($diff->addedIndexes['primary'])) { $keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns())); $queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')'; unset($diff->addedIndexes['primary']); } elseif (isset($diff->changedIndexes['primary'])) { // Necessary in case the new primary key includes a new auto_increment column foreach ($diff->changedIndexes['primary']->getColumns() as $columnName) { if (isset($diff->addedColumns[$columnName]) && $diff->addedColumns[$columnName]->getAutoincrement()) { $keyColumns = array_unique(array_values($diff->changedIndexes['primary']->getColumns())); $queryParts[] = 'DROP PRIMARY KEY'; $queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')'; unset($diff->changedIndexes['primary']); break; } } } $sql = []; $tableSql = []; if (!$this->onSchemaAlterTable($diff, $tableSql)) { if (count($queryParts) > 0) { $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(', ', $queryParts); } $sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff)); } return array_merge($sql, $tableSql, $columnSql); } protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) { $sql = []; $table = $diff->getName($this)->getQuotedName($this); foreach ($diff->changedIndexes as $changedIndex) { $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex)); } foreach ($diff->removedIndexes as $remKey => $remIndex) { $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex)); foreach ($diff->addedIndexes as $addKey => $addIndex) { if ($remIndex->getColumns() !== $addIndex->getColumns()) { continue; } $indexClause = 'INDEX ' . $addIndex->getName(); if ($addIndex->isPrimary()) { $indexClause = 'PRIMARY KEY'; } elseif ($addIndex->isUnique()) { $indexClause = 'UNIQUE INDEX ' . $addIndex->getName(); } $query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', '; $query .= 'ADD ' . $indexClause; $query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex) . ')'; $sql[] = $query; unset($diff->removedIndexes[$remKey], $diff->addedIndexes[$addKey]); break; } } $engine = 'INNODB'; if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) { $engine = strtoupper(trim($diff->fromTable->getOption('engine'))); } // Suppress foreign key constraint propagation on non-supporting engines. if ($engine !== 'INNODB') { $diff->addedForeignKeys = []; $diff->changedForeignKeys = []; $diff->removedForeignKeys = []; } $sql = array_merge($sql, $this->getPreAlterTableAlterIndexForeignKeySQL($diff), parent::getPreAlterTableIndexForeignKeySQL($diff), $this->getPreAlterTableRenameIndexForeignKeySQL($diff)); return $sql; } private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index) { $sql = []; if (!$index->isPrimary() || !$diff->fromTable instanceof Table) { return $sql; } $tableName = $diff->getName($this)->getQuotedName($this); // Dropping primary keys requires to unset autoincrement attribute on the particular column first. foreach ($index->getColumns() as $columnName) { if (!$diff->fromTable->hasColumn($columnName)) { continue; } $column = $diff->fromTable->getColumn($columnName); if ($column->getAutoincrement() !== \true) { continue; } $column->setAutoincrement(\false); $sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); // original autoincrement information might be needed later on by other parts of the table alteration $column->setAutoincrement(\true); } return $sql; } private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff) { $sql = []; $table = $diff->getName($this)->getQuotedName($this); foreach ($diff->changedIndexes as $changedIndex) { // Changed primary key if (!$changedIndex->isPrimary() || !$diff->fromTable instanceof Table) { continue; } foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) { $column = $diff->fromTable->getColumn($columnName); // Check if an autoincrement column was dropped from the primary key. if (!$column->getAutoincrement() || in_array($columnName, $changedIndex->getColumns())) { continue; } // The autoincrement attribute needs to be removed from the dropped column // before we can drop and recreate the primary key. $column->setAutoincrement(\false); $sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); // Restore the autoincrement attribute as it might be needed later on // by other parts of the table alteration. $column->setAutoincrement(\true); } } return $sql; } protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff) { $sql = []; $tableName = $diff->getName($this)->getQuotedName($this); foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { if (in_array($foreignKey, $diff->changedForeignKeys, \true)) { continue; } $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName); } return $sql; } private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff) { if (empty($diff->renamedIndexes) || !$diff->fromTable instanceof Table) { return []; } $foreignKeys = []; $remainingForeignKeys = array_diff_key($diff->fromTable->getForeignKeys(), $diff->removedForeignKeys); foreach ($remainingForeignKeys as $foreignKey) { foreach ($diff->renamedIndexes as $index) { if ($foreignKey->intersectsIndexColumns($index)) { $foreignKeys[] = $foreignKey; break; } } } return $foreignKeys; } protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff) { return array_merge(parent::getPostAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableRenameIndexForeignKeySQL($diff)); } protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff) { $sql = []; $newName = $diff->getNewName(); if ($newName !== \false) { $tableName = $newName->getQuotedName($this); } else { $tableName = $diff->getName($this)->getQuotedName($this); } foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { if (in_array($foreignKey, $diff->changedForeignKeys, \true)) { continue; } $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName); } return $sql; } protected function getCreateIndexSQLFlags(Index $index) { $type = ''; if ($index->isUnique()) { $type .= 'UNIQUE '; } elseif ($index->hasFlag('fulltext')) { $type .= 'FULLTEXT '; } elseif ($index->hasFlag('spatial')) { $type .= 'SPATIAL '; } return $type; } public function getIntegerTypeDeclarationSQL(array $column) { return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($column); } public function getBigIntTypeDeclarationSQL(array $column) { return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($column); } public function getSmallIntTypeDeclarationSQL(array $column) { return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($column); } public function getFloatDeclarationSQL(array $column) { return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($column); } public function getDecimalTypeDeclarationSQL(array $column) { return parent::getDecimalTypeDeclarationSQL($column) . $this->getUnsignedDeclaration($column); } private function getUnsignedDeclaration(array $columnDef) { return !empty($columnDef['unsigned']) ? ' UNSIGNED' : ''; } protected function _getCommonIntegerTypeDeclarationSQL(array $column) { $autoinc = ''; if (!empty($column['autoincrement'])) { $autoinc = ' AUTO_INCREMENT'; } return $this->getUnsignedDeclaration($column) . $autoinc; } public function getColumnCharsetDeclarationSQL($charset) { return 'CHARACTER SET ' . $charset; } public function getColumnCollationDeclarationSQL($collation) { return 'COLLATE ' . $this->quoteSingleIdentifier($collation); } public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey) { $query = ''; if ($foreignKey->hasOption('match')) { $query .= ' MATCH ' . $foreignKey->getOption('match'); } $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey); return $query; } public function getDropIndexSQL($index, $table = null) { if ($index instanceof Index) { $indexName = $index->getQuotedName($this); } elseif (is_string($index)) { $indexName = $index; } else { throw new InvalidArgumentException(__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'); } if ($table instanceof Table) { $table = $table->getQuotedName($this); } elseif (!is_string($table)) { throw new InvalidArgumentException(__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'); } if ($index instanceof Index && $index->isPrimary()) { // mysql primary keys are always named "PRIMARY", // so we cannot use them in statements because of them being keyword. return $this->getDropPrimaryKeySQL($table); } return 'DROP INDEX ' . $indexName . ' ON ' . $table; } protected function getDropPrimaryKeySQL($table) { return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'; } public function getSetTransactionIsolationSQL($level) { return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); } public function getName() { return 'mysql'; } public function getReadLockSQL() { return 'LOCK IN SHARE MODE'; } protected function initializeDoctrineTypeMappings() { $this->doctrineTypeMapping = ['tinyint' => 'boolean', 'smallint' => 'smallint', 'mediumint' => 'integer', 'int' => 'integer', 'integer' => 'integer', 'bigint' => 'bigint', 'tinytext' => 'text', 'mediumtext' => 'text', 'longtext' => 'text', 'text' => 'text', 'varchar' => 'string', 'string' => 'string', 'char' => 'string', 'date' => 'date', 'datetime' => 'datetime', 'timestamp' => 'datetime', 'time' => 'time', 'float' => 'float', 'double' => 'float', 'real' => 'float', 'decimal' => 'decimal', 'numeric' => 'decimal', 'year' => 'date', 'longblob' => 'blob', 'blob' => 'blob', 'mediumblob' => 'blob', 'tinyblob' => 'blob', 'binary' => 'binary', 'varbinary' => 'binary', 'set' => 'simple_array']; } public function getVarcharMaxLength() { return 65535; } public function getBinaryMaxLength() { return 65535; } protected function getReservedKeywordsClass() { return Keywords\MySQLKeywords::class; } public function getDropTemporaryTableSQL($table) { if ($table instanceof Table) { $table = $table->getQuotedName($this); } elseif (!is_string($table)) { throw new InvalidArgumentException(__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'); } return 'DROP TEMPORARY TABLE ' . $table; } public function getBlobTypeDeclarationSQL(array $column) { if (!empty($column['length']) && is_numeric($column['length'])) { $length = $column['length']; if ($length <= static::LENGTH_LIMIT_TINYBLOB) { return 'TINYBLOB'; } if ($length <= static::LENGTH_LIMIT_BLOB) { return 'BLOB'; } if ($length <= static::LENGTH_LIMIT_MEDIUMBLOB) { return 'MEDIUMBLOB'; } } return 'LONGBLOB'; } public function quoteStringLiteral($str) { $str = str_replace('\\', '\\\\', $str); // MySQL requires backslashes to be escaped aswell. return parent::quoteStringLiteral($str); } public function getDefaultTransactionIsolationLevel() { return TransactionIsolationLevel::REPEATABLE_READ; } public function supportsColumnLengthIndexes() : bool { return \true; } }