Skip to content

Add fluent Table API for check constraints#1052

Merged
dereuromark merged 2 commits into5.nextfrom
add-check-constraint-table-api
Mar 15, 2026
Merged

Add fluent Table API for check constraints#1052
dereuromark merged 2 commits into5.nextfrom
add-check-constraint-table-api

Conversation

@dereuromark
Copy link
Member

@dereuromark dereuromark commented Mar 15, 2026

Summary

  • Add addCheckConstraint() and dropCheckConstraint() methods to the Table class
  • Allow managing check constraints with the same fluent API pattern used for indexes and foreign keys
  • The adapter already had the underlying implementation - this adds the user-facing Table API

Before (direct adapter access)

public function change(): void
{
    // Awkward - need to get adapter and create objects manually
    $table = $this->table('users');
    $table->addColumn('age', 'integer')->create();
    
    $adapter = $this->getAdapter();
    $constraint = new CheckConstraint('age_check', 'age >= 18');
    $adapter->addCheckConstraint($table->getTable(), $constraint);
}

After (fluent Table API)

public function change(): void
{
    $this->table('users')
        ->addColumn('age', 'integer')
        ->addCheckConstraint('age >= 18', ['name' => 'age_check'])
        ->create();

    // Or with CheckConstraint object
    $constraint = new CheckConstraint('price_positive', 'price > 0');
    $this->table('products')
        ->addCheckConstraint($constraint)
        ->update();

    // Drop a check constraint
    $this->table('users')
        ->dropCheckConstraint('age_check')
        ->update();

    // Check if constraint exists
    if ($this->table('users')->hasCheckConstraint('age_check')) {
        // ...
    }
}

Changes

  • New AddCheckConstraint action class
  • New DropCheckConstraint action class
  • Table::addCheckConstraint() method
  • Table::dropCheckConstraint() method
  • Table::hasCheckConstraint() method
  • Updated Plan.php to handle check constraint actions in gatherConstraints()
  • Updated AbstractAdapter::executeActions() to handle the new action types
  • Unit tests for the new methods

Add addCheckConstraint() and dropCheckConstraint() methods to the Table
class, allowing users to manage check constraints using the same fluent
API pattern used for indexes and foreign keys.

The adapter already had the underlying implementation, this adds:
- AddCheckConstraint action class
- DropCheckConstraint action class
- Table::addCheckConstraint() method
- Table::dropCheckConstraint() method
- Table::hasCheckConstraint() method
- Plan.php updated to handle check constraint actions
- Unit tests for the new methods
Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Update examples to use the correct API signature:
- addCheckConstraint($expression, $options) not addCheckConstraint($name, $expression)
- Replace non-existent checkConstraint() fluent builder with CheckConstraint object
- Fix all examples throughout the check constraints section
@dereuromark
Copy link
Member Author

Added docs.

@dereuromark dereuromark merged commit d752038 into 5.next Mar 15, 2026
14 checks passed
@dereuromark dereuromark deleted the add-check-constraint-table-api branch March 15, 2026 03:18
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.

2 participants