- /**
- * Delete table column
- *
- * @param string $tableName
- * @param string $columnName
- * @param string $schemaName
- * @return true|\Zend_Db_Statement_Pdo
- */
- public function dropColumn($tableName, $columnName, $schemaName = null)
- {
- if (!$this->tableColumnExists($tableName, $columnName, $schemaName)) {
- return true;
- }
-
- $alterDrop = [];
-
- $foreignKeys = $this->getForeignKeys($tableName, $schemaName);
- foreach ($foreignKeys as $fkProp) {
- if ($fkProp['COLUMN_NAME'] == $columnName) {
- $alterDrop[] = 'DROP FOREIGN KEY ' . $this->quoteIdentifier($fkProp['FK_NAME']);
- }
- }
-
- /* drop index that after column removal would coincide with the existing index by indexed columns */
- foreach ($this->getIndexList($tableName, $schemaName) as $idxData) {
- $idxColumns = $idxData['COLUMNS_LIST'];
- $idxColumnKey = array_search($columnName, $idxColumns);
- if ($idxColumnKey !== false) {
- unset($idxColumns[$idxColumnKey]);
- if ($idxColumns && $this->_getIndexByColumns($tableName, $idxColumns, $schemaName)) {
- $this->dropIndex($tableName, $idxData['KEY_NAME'], $schemaName);
- }
- }
- }
-
- $alterDrop[] = 'DROP COLUMN ' . $this->quoteIdentifier($columnName);
- $sql = sprintf(
- 'ALTER TABLE %s %s',
- $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
- implode(', ', $alterDrop)
- );
-
- $result = $this->rawQuery($sql);
- $this->resetDdlCache($tableName, $schemaName);
-
- return $result;
- }