CLab's avatar
Level 3

Run different migration in test

I have a migration which is altering a column in a table in Postgres (dev) to JSON. This is a custom query like.

            DB::statement('ALTER TABLE users ALTER COLUMN options TYPE JSON USING (options)::json');

However due to this migration my tests are failing as I am using SQLite in testing. How can I run a different alteration command for test vs dev? I am looking for something like:

if ($testEnvironment) {
	// run something
} else {
	// run above command
}
0 likes
6 replies
CLab's avatar
Level 3

For anyone else here is the solution

if (App::environment() == 'testing') {
	// run something
} else {
	// run above command
}
martinbean's avatar

@clab You’re not really testing your application if you run different code paths in tests that your application does for real.

If you’re relying on Postgres-specific features then consider using a test Postgres database. Migrations are meant to be database engine-agnostic.

CLab's avatar
Level 3

@martinbean the issue with using Postgres as DB for testing is that it is really slow for tests compared to SQLite. Any suggestions for not doing this?

automica's avatar

@CLab I'd recommend you look at why your tests are running slowly.

If your live application runs on Postgres then your testing should be done on postgres too. Otherwise theres no guarantees your application will work the same when you publish to live.

Please or to participate in this conversation.