Skip to content

Conversation

@dereuromark
Copy link
Member

@dereuromark dereuromark commented Nov 3, 2025

Implement cakephp/phinx#123 in a BC way.

  // NEW WAY - Simple and safe!
  $table->updateColumn('email', null, ['null' => true]);

  // OLD WAY - Still works!
  $table->changeColumn('email', 'string', [
      'null' => true,
      'default' => 'test',
      'limit' => 255,
  ]);

An alternative way, if we want more speaking API, could be

  • updateColumnAttributes(field, array options) if we are only interested in changing attributes

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new updateColumn() method for safer column modifications that preserves unspecified attributes by default, while maintaining backward compatibility with the existing changeColumn() method. The changeColumn() method is updated to support optional attribute preservation via a preserve_unspecified flag and now accepts null as the column type to preserve the existing type.

  • Adds updateColumn() as the recommended method for column modifications with automatic attribute preservation
  • Enhances changeColumn() to support null type parameter and optional attribute preservation
  • Implements comprehensive test coverage for both methods and their preservation behavior

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Db/Table.php Adds updateColumn() method, updates changeColumn() to support null type and preservation logic, adds mergeColumnOptions() helper
tests/TestCase/Db/Adapter/MysqlAdapterTest.php Adds comprehensive test cases for column update/change preservation behavior
docs/en/writing-migrations.rst Documents both updateColumn() (recommended) and changeColumn() methods with examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@LordSimal
Copy link
Contributor

as long as schema reflection represents the column properly I don't see a problem supporting this kind of functionality.

@dereuromark dereuromark force-pushed the 5.x-change-column-defaulting branch from ccef79e to 083230c Compare November 9, 2025 08:09
dereuromark added a commit that referenced this pull request Nov 10, 2025
…umn + options behavior

- Remove non-functional enum/set values preservation code
  Schema reflection doesn't populate Column::values, so preserving it has no effect
- Add test documenting that options array is ignored when Column object is passed
  This clarifies the existing behavior where Column objects take full precedence

Addresses: #941 (comment)
Addresses: #941 (comment)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@dereuromark dereuromark force-pushed the 5.x-change-column-defaulting branch from 3196ff9 to cad5475 Compare November 10, 2025 09:14
- Tests that updateColumn() works correctly when passed a Column object
- Addresses review feedback to ensure Column type support is tested

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@dereuromark dereuromark force-pushed the 5.x-change-column-defaulting branch from cad5475 to 7509eff Compare November 10, 2025 09:34
src/Db/Table.php Outdated
Comment on lines 912 to 917
if (!$typeChanging && !isset($options['limit']) && !isset($options['length'])) {
$limit = $existingColumn->getLimit();
if ($limit !== null) {
$existingOptions['limit'] = $limit;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does one remove length with this API?

Copy link
Member Author

@dereuromark dereuromark Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove a length constraint using this API, you would typically need to explicitly set 'length' => null or 'limit' => null in the options array when calling updateColumn(). For example:

 $table->updateColumn('my_column', 'text', ['length' => null]);

I added a test case that shows it works, but from the code above I also see that using isset() instead of array_key_exists() would not set it from the options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants