natepisarski's avatar

Should I make special migrations just for tests?

Background

So, I have a suite of migrations in my app. These migrations run locally for the developers, on our testing server (develop), and on production to build the database up.

Recently I've been trying to repair our migrations so that they run on both SQLite (for feature tests) and MySQL (for the running instances). It was tough. SQLite just isn't built for alter commands.

Anyway, I've been talking about approaches to make it easier. I have heard online and from other developers that most projects don't run their full suite of migrations for tests. This is surprising to me. It seems like if you made migrations that only run on the phpunit database, then you're not really testing the app's flow at all.

Again, I'm not used to the flow of having totally different migrations between Feature Tests and the regular app. In node, C#, and Rust we run the same migrations during feature testing and production.

My Question

  1. Do you use your app's migrations for testing, or do you make special migrations just for testing?

  2. Since most project apparently have test-specific migrations, what should it look like? How much of my app's schema should I reproduce in these new migrations?

  3. How can I be sure that something will work in my test-migration-schema AND my app-migration-schema? If I make a mistake, it seems like something could work during testing but blow up in production (like I name a column different in the testing migrations).

0 likes
3 replies
Tray2's avatar

You should not use special migrations for testing since that kinda defeats the purpose.

However sometimes you might need to compensate for the differences between the different databases.

If you have issues with SQLite you can always set up a testing database in MySQL.

1 like
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

I use the same migrations, but I have few migrations which acts differently based on database driver (SQLite and MySQL).

Recently, I've discovered a bug in production which was related to incompatibility between SQLite and MySQL, so I've decided to run my full test suite on MySQL before deploying to production. And surprisingly running the whole test suite on MySQL is faster than SQLite, because my project has ~40 migrations.

https://alexvanderbist.com/posts/2019/how-migrations-might-be-slowing-down-your-laravel-tests

1 like
natepisarski's avatar

Interesting article, thank you. I like the strategy of just exporting a "snapshot" of your database to test with instead of running through the migrations.

Please or to participate in this conversation.