/** * changeColumn * * @param string $oldName * @param string|Column $newName * @param string $type * @param bool $signed * @param bool $allowNull * @param string $default * @param string $comment * @param array $options * * @return static */ public function changeColumn($oldName, $newName, $type = 'text', $signed = true, $allowNull = true, $default = '', $comment = '', $options = array()) { $column = $name = $newName; if ($column instanceof Column) { $name = $column->getName(); $type = $column->getType(); $length = $column->getLength(); $allowNull = $column->getAllowNull(); $default = $column->getDefault(); $comment = $column->getComment(); } $type = PostgresqlType::getType($type); $length = isset($length) ? $length : PostgresqlType::getLength($type); $length = PostgresqlType::noLength($type) ? null : $length; $length = $length ? '(' . $length . ')' : null; $query = $this->db->getQuery(true); // Type $sql = PostgresqlQueryBuilder::build('ALTER TABLE ' . $query->quoteName($this->table), 'ALTER COLUMN', $query->quoteName($oldName), 'TYPE', $type . $length, $this->usingTextToNumeric($oldName, $type)); // Not NULL $sql .= ";\n" . PostgresqlQueryBuilder::build('ALTER TABLE ' . $query->quoteName($this->table), 'ALTER COLUMN', $query->quoteName($oldName), $allowNull ? 'DROP' : 'SET', 'NOT NULL'); // Default if (!is_null($default)) { $sql .= ";\n" . PostgresqlQueryBuilder::build('ALTER TABLE ' . $query->quoteName($this->table), 'ALTER COLUMN', $query->quoteName($oldName), 'SET DEFAULT' . $query->quote($default)); } // Comment $sql .= ";\n" . PostgresqlQueryBuilder::comment('COLUMN', $this->table, $oldName, $comment); // Rename $sql .= ";\n" . PostgresqlQueryBuilder::renameColumn($this->table, $oldName, $name); DatabaseHelper::batchQuery($this->db, $sql); return $this; }
/** * Method to test renameColumn(). * * @return void * * @covers Windwalker\Query\Postgresql\PostgresqlQueryBuilder::renameColumn */ public function testRenameColumn() { $expected = "ALTER TABLE {$this->qn('foo')} RENAME {$this->qn('bar')} TO {$this->qn('yoo')}"; $actual = PostgresqlQueryBuilder::renameColumn('foo', 'bar', 'yoo'); $this->assertEquals($this->format($expected), $this->format($actual)); }