phinx icon indicating copy to clipboard operation
phinx copied to clipboard

changeColumn make columns `not null`

Open salarmehr opened this issue 10 years ago • 6 comments

Consider if want to change length of a nullable column. using ['length' => MysqlAdapter::INT_TINY] make the column not null. I have to specify 'null'=>true] options to prevent that. I think Phinx should not change unspecified options.

salarmehr avatar Jul 09 '15 13:07 salarmehr

Similar situation with comment. If it is not specified, comment will be deleted.

decadence avatar Jun 24 '16 07:06 decadence

A reasonable solution would be to get the column description from the DB and populate the change column params with that, and then override them with the user supplied ones.

Not all columns provide/require the same options (http://docs.phinx.org/en/latest/migrations.html#valid-column-options), so each adaptor / column combo would need to be tuned accordingly.

As changColumn is not reversible, the up and down would/should operate in the same way, only changing the attributes that need changing, rather than supplying those that are not changed, but need to be included.

As changing a column is handled by \Phinx\Db\Table::changeColumn(), I wonder how much work to interact with \Phinx\Db\Adapter\AdapterInterface::getColumns() to get the current setup, and then override it with the user supplied one.

Care to have a go at creating a PR for this?

I'd be happy to look it over/help you through it?

rquadling avatar Jun 24 '16 18:06 rquadling

The adapter level getColumns() do not SEEMS to work consistently with regard to comments (first glance at least), so, if it is an issue, would need fixing also.

rquadling avatar Jun 24 '16 18:06 rquadling

This is not optimized but this is what I am doing at the moment since if changeColumn is given a Column Class it will not create a new Column instance.

$table->changeColumn('user_id', $this->getColumn('user_id', ['after' => 'id']));

private function getColumn($columnName, $options)
{
    $columns = new \Cake\Collection\Collection($this->table('Orders')->getColumns());

    $column = $columns->filter(function ($column) use ($columnName){
        return $column->getName() == $columnName;
    })->first();

    $column->setOptions($options);
    return $column;
}

styks1111 avatar Sep 08 '17 21:09 styks1111

Are you able to provide a patch with your suggested changes as PR?

dereuromark avatar Mar 01 '18 20:03 dereuromark

Check out https://github.com/cakephp/migrations/pull/941 - if thats sth to be ported to phinx directly, I think the separate method allows BC here for the attributes.

dereuromark avatar Nov 04 '25 16:11 dereuromark