1 min read

Testing with SQLite in memory: when it lies to you

An in-memory SQLite database makes a Laravel test suite run in seconds. Here are five bugs it let through to production for us, and how we catch them now.

Colorful source code on a dark monitor

SQLite is a fantastic database. It is just not the database you run in production, and the differences show up in exactly the places where bugs hurt most.

Five differences that matter

  1. Types: SQLite stores '42abc' in an integer column without complaining.
  2. String comparison: MySQL compares case-insensitively by default, SQLite does not.
  3. Foreign keys: they are off unless you enable them.
  4. Locking: lockForUpdate() does nothing useful.
  5. JSON: the JSON functions and their results differ in edge cases.

Run both

We keep SQLite for the fast local loop and run the full suite against MySQL in CI. A tiny trait marks the tests that only make sense on the real database.

php
trait RequiresMysql
{
    protected function setUpRequiresMysql(): void
    {
        if (DB::getDriverName() !== 'mysql') {
            $this->markTestSkipped('Needs MySQL locking semantics');
        }
    }
}