riverqueue / river

Fast and reliable background jobs in Go
https://riverqueue.com
Mozilla Public License 2.0
3.34k stars 89 forks source link

Add migration that brings `line` column to the migration table #435

Closed brandur closed 2 months ago

brandur commented 2 months ago

Here, add a new River migration (version 005) that brings a line column to the river_migration table, allowing non-main lines to be supported. We also teach the migrator how to use it.

This one is a little trickier than it sounds (partly thanks to sqlc) because the migrator needs to behave somewhat differently depending on whether the line column exits yet. So for example, when migrating to 004 it needs to upsert migration records that do not include a line field, but then when migrating to 005 or beyond, a line value is included. The same consideration goes for performing a down migration, or checking the existing state of migrations in the database.

This is implemented by pairing driver functions related to migrations into one for pre-line and another for post-line. e.g.

// MigrationDeleteAssumingMainMany deletes many migrations assuming
// everything is on the main line. This is suitable for use in databases on
// a version before the `line` column exists.
MigrationDeleteAssumingMainMany(ctx context.Context, versions []int) ([]*Migration, error)

// MigrationDeleteByLineAndVersionMany deletes many migration versions on a
// particular line.
MigrationDeleteByLineAndVersionMany(ctx context.Context, line string, versions []int) ([]*Migration, error)

We don't yet pull line support into the CLI, but this should make alternate lines fully supported up to that point. The CLI needs a little more thought because it might involve building a separate binary.

brandur commented 2 months ago

Thanks!