Skip to content

Add connection-targeted migrations and seeders#152

Merged
usernane merged 1 commit into
mainfrom
dev
May 13, 2026
Merged

Add connection-targeted migrations and seeders#152
usernane merged 1 commit into
mainfrom
dev

Conversation

@usernane
Copy link
Copy Markdown
Member

Summary

Add the ability to restrict migrations and seeders to specific named database connections, enabling multi-database architectures where certain schema changes should only apply to specific databases.

Motivation

In multi-tenant applications or systems with multiple databases (e.g., one for user data, another for reporting), not all migrations should apply to every database. Currently, running migrations applies all discovered changes regardless of which database they belong to.

This feature follows the same pattern as the existing getEnvironments() method for environment-based filtering, extending it to connection-based filtering.

Changes

  • Added DatabaseChange::getTargetConnections(): array — declares which connections a change applies to. Empty array (default) means all connections.
  • Added SchemaRunner::shouldRunForConnection() — filters changes by connection name at apply-time
  • Filtering integrated into: apply(), applyOne(), getPendingChanges(), skipAll(), skipNext(), skipUpTo()
  • Skipped changes are recorded with reason Connection mismatch in DatabaseChangeResult
  • Added example migration (CreateReportsTableMigration.php) demonstrating the feature
  • Updated migrations example to show connection targeting in action
  • Added documentation to README

How to Test / Verify

php vendor/bin/phpunit -c tests/phpunit10.xml --testsuite "Schema Tests" --filter "ConnectionTarget"

11 tests pass covering: default behavior, matching/mismatched connections, multiple targets, mixed targeting, seeder filtering, getPendingChanges, and applyOne.

Full schema suite (162 tests) passes with no regressions.

Breaking Changes and Migration Steps

None. Default getTargetConnections() returns [] (all connections), so existing migrations are unaffected.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • The title of the pull request follows Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed)
  • I ran lint/cs-fixer (if applicable)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #149
Follow-up for CLI integration: WebFiori/framework#326

Add getTargetConnections() to DatabaseChange, allowing migrations and
seeders to declare which named connections they apply to. Empty array
(default) means all connections, preserving backward compatibility.

SchemaRunner filters changes by connection name at apply-time, consistent
with the existing getEnvironments() pattern. Skipped changes are recorded
with reason 'Connection mismatch'.

Filtering is applied in: apply(), applyOne(), getPendingChanges(),
skipAll(), skipNext(), and skipUpTo().

Closes #149
Ref: WebFiori/framework#326
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.08%. Comparing base (3f25b82) to head (002aac3).
⚠️ Report is 33 commits behind head on main.

Files with missing lines Patch % Lines
WebFiori/Database/Schema/SchemaRunner.php 77.27% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #152      +/-   ##
============================================
+ Coverage     75.06%   75.08%   +0.01%     
- Complexity     2174     2185      +11     
============================================
  Files            54       54              
  Lines          5403     5427      +24     
============================================
+ Hits           4056     4075      +19     
- Misses         1347     1352       +5     
Flag Coverage Δ
php-8.1 75.80% <79.16%> (+0.01%) ⬆️
php-8.2 75.58% <79.16%> (?)
php-8.3 74.89% <79.16%> (+0.01%) ⬆️
php-8.4 74.89% <79.16%> (+0.01%) ⬆️
php-8.5 74.89% <79.16%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud
Copy link
Copy Markdown

@usernane usernane merged commit 7df8fda into main May 13, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Connection-targeted migrations and seeders

1 participant