DEV Community

Michael Heap
Michael Heap

Posted on • Originally published at michaelheap.com on

Dedicated test databases with Laravel Sail

I’ve been playing around with Laravel Sail on a project that I’ve just started and wanted to be able to run my tests on a separate database and leave my main DB intact for development.

Adding a second database for tests involves adding a second container and configuring your tests to point at the newly created host.

Add the following to docker-compose.yml under the services: key:

mysql_test:
  image: "mysql:8.0"
  environment:
    MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
    MYSQL_DATABASE: "${DB_DATABASE}"
    MYSQL_USER: "${DB_USERNAME}"
    MYSQL_PASSWORD: "${DB_PASSWORD}"
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  networks:
    - sail
Enter fullscreen mode Exit fullscreen mode

Next, configure your .env.testing file to point at the mysql_test host (Docker Compose uses the service name as the host name for networking):

# Don't forget to set APP_KEY too
DB_HOST=mysql_test # This is the important bit
DB_DATABASE=
DB_USERNAME=root
Enter fullscreen mode Exit fullscreen mode

Then finally, run your tests:

./vendor/bin/sail test
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay