Repository navigation
Best Practices for Multi-Database Module Architecture & Testing Challenges #2136
Unanswered
ethancoursestorm
asked this question in
Q&A
Replies: 1 comment
|
We run a similar setup (one connection per bounded context in a modular monolith). A few things that removed most of the friction for us: 1. Don't swap connections in tests — swap the database behind the connectionKeep one connection per module with a stable name, and let the environment decide which database it points at. That's not coupling production code to the environment; it's exactly what env-driven config is for, and there's no conditional anywhere in your code. // Modules/Billing/config/database.php (merged into database.connections)
'billing' => [
'driver' => 'mysql',
'read' => ['host' => [env('BILLING_DB_READ_HOST', env('BILLING_DB_HOST', '127.0.0.1'))]],
'write' => ['host' => [env('BILLING_DB_HOST', '127.0.0.1')]],
'sticky' => true,
'port' => env('BILLING_DB_PORT', '3306'),
'database' => env('BILLING_DB_DATABASE', 'billing'),
'username' => env('BILLING_DB_USERNAME', 'root'),
'password' => env('BILLING_DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
], |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi laravel-modules maintainers,
I'm part of a team building a modular monolith using laravel-modules with separate MySQL databases per module, and I'd love your input on whether our approach aligns with best practices, particularly around testing.
Our Implementation
Context: We have 3 modules, each with its own physically separate MySQL database.
Setup
Module-level database configs:
Modules/{Module}/Config/database.phpService Provider registration:
Base classes enforce connection:
The Testing Challenge 🔴
This is where we need guidance. We have to manually override connections in tests, which feels like a workaround:
Why this is necessary:
BaseMigrationwithprotected $connection = 'orders'BaseModelwithprotected $connection = 'orders''orders_testing'without:App::environment()in production code (bad)Each module's TestCase must:
configure{Module}TestingConnection()increateApplication()refreshDatabase()to configure connection before migrations runmodule:migrate --freshinstead of standard Laravel migrationsQuestions
Is runtime connection swapping the recommended pattern for module testing with separate databases? Or is there a cleaner way we're missing?
Migration tracking: With separate database connections, each module's migrations are tracked independently in their respective migrations tables. Is this expected behavior? Should we centralize migration tracking?
Any other general guidance/tips would be most appreciated
Environment
Thanks for the amazing package!
All reactions