shanejansen / touchstone

Touchstone is a testing framework for your services that focuses on component, end-to-end, and exploratory testing.
17 stars 2 forks source link

order of MySQL defaults and migrations #46

Open ScottFreeCode opened 2 years ago

ScottFreeCode commented 2 years ago

I believe the order of effects when starting up the mocks and services with mysql set to snapshot DBs is:

  1. Defaults file applied on mocks start.
  2. Migrations may be applied by the service on service start.
  3. The DB snapshot is saved.

(The same order is used without snapshotting DBs, with similar questions/issues but it's eclipsed by the migrations getting wiped so with migrations one pretty much always wants to snapshot DBs.)

This has a couple limitations/oddities:

  1. The migrations may expect a clean DB, but if you add anything to the defaults that may not be the case.
  2. You cannot add default data to the service's tables without having to create the tables the migrations should create and therefore running into problem 1.

In other words, you can have migrations or defaults but effectively it's hard to use both, e.g. to populate default data in tables the service creates.

It would make sense in my opinion to defer running the defaults until after the services start, to the same point when the DB is snapshotted (presumably before the snapshot, although at that point as long as the defaults don't contain nondeterministic values/behavior there is only performance difference between "defaults once then snapshot" versus "snapshot migrations, reapply snapshot in place of wiping DB and reapply defaults after each wipe or snapshot application".)

ScottFreeCode commented 2 years ago

One thing that may need to be used from the defaults file before starting the service is to create the named DB in the first place, as services typically connect to a specific named DB and may fail if it is not present. This isn't even SQL, of course.