goose icon indicating copy to clipboard operation
goose copied to clipboard

feat: Expand Store interface; add GetLatestVersion method in Provider

Open mfridman opened this issue 2 years ago • 0 comments

For historic reasons, adding a GetLatestVersion query was challenging because goose did not hard-delete rows and instead marked them with is_applied = false. Which means you'd end up with something like this:

| id | version_id | is_applied |
|----|------------|------------|
|  1 |          0 | t          |
|  2 |          1 | t          |
|  3 |          2 | t          |
|  4 |          2 | f          |

See https://github.com/pressly/goose/pull/131#pullrequestreview-178409168 where we changed the underlying implementation to hard-delete the row.

This is why we list versions (in reverse order by id, or in rare circumstances the timestamp) and then get the highest version that has is_applied = true and that hasn't been previously deleted. But this is wasteful, since we query the entire table and discard 99% of the results on the client. Granted we probably could have done this with SQL, but this is easy to mess up across implementations.

In the new Provider, this functionality does not exist, so we can expand the interface to accommodate for a "get latest" query to avoid listing all versions.

Note

Latest version can mean 2 different things:

  1. Latest version is the max by version_id
  2. Latest version is the last applied migration by id or timestamp, irrespective of the version. This becomes especially important for out-of-order migrations.

mfridman avatar Dec 18 '23 00:12 mfridman