Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

it-is-all-about-laravel's avatar

testing json and enum column types with sqlite

Hey Everyone, I attempted to switch to sqlite (in memory) to speed up running phpunit testing with the RefreshDatabase trait. Currently I'm using mysql and found that RefreshDatabase adds about 2 seconds to the run time to process the migrations.

I discovered sqlite doesn't seem to support column types enum or json.

Can you recommend a way around this with sqlite, or any alternate approaches to speed up testing execution on mysql?

Thanks

0 likes
3 replies
Talinon's avatar

Do you have a very good reason for using ENUM data types? They can be completely avoided under most scenarios. I would consider dropping them and going with a more standard approach using a reference table.

I could list a bunch of web links that speak about why you should avoid using ENUMs, but a google search would do that for you.

If all the reasons against using ENUM are not enough, then the fact you can't use an in-memory SQLite database should be reason enough to ditch them.

1 like
it-is-all-about-laravel's avatar

thanks for the advice. i've reworked the application to replace all the database enum columns with reference tables and other data types. i can run sqlite now, but sqlite causes issues with php unit testing because of the way it retrieves integers so I'm still interested in seeing how i can speed things up using my sql.

i tried implementing the solution @fastsol mentioned my code is below. but i'm not seeing the performance benefit, i think this is because my overiding method is not being called by the trait. do you have any suggestions on how to correct it?

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use RefreshDatabase;

    protected function refreshTestDatabase()
    {

        if (! RefreshDatabaseState::$migrated) {
            DB::unprepared(file_get_contents(database_path('migrations_2019_06_19.sql')));

            $this->artisan('migrate');

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        }

        $this->beginDatabaseTransaction();
    }

}

Please or to participate in this conversation.