Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration CLI: Add migrate-get command and --dry-run/--show-sql options #273

Merged
merged 1 commit into from
Mar 17, 2024

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Mar 16, 2024

This one's in pursuit of resolving #209, which I've decided to tackle
now because we're going to have to cut a CLI release across River
package versions for #258 anyway, so this'll reuse some work.

A new river migrate-get command becomes available, whose only job is
to dump SQL from River migrations so that it can easily be plugged into
other migration frameworks. Its use looks like:

river migrate-get --version 3 --down > version3.down.sql
river migrate-get --version 3 --up > version3.up.sql

It can also take multiple versions:

river migrate-get --version 3,2,1 --down > river.down.sql
river migrate-get --version 1,2.3 --up > river.up.sql

It can also dump all migrations, which will be useful in cases where
users want to avoid River's internal migration framework completely, and
use their own:

river migrate-get --all --exclude-version 1 --up > river_all.up.sql
river migrate-get --all --exclude-version 1 --down > river_all.down.sql

Along with that, migrate-down and migrate-up get a few new useful
options:

  • --dry-run: Prints information on migrations that would be run, but
    doesn't modify the database in any way.

  • --show-sql: Prints SQL for each migration step that was applied.

This gives users an easy way to, after a River upgrade, run the CLI to
see what commands would be run were they to migrate, but without
actually performing the migration, likely a step that most production
users would perform to be cautious:

river migrate-up --dry-run --show-sql

I've also done a little cleanup around the River CLI's main.go. The
--verbose and --debug commands added in #258 are now promoted to
persistent flag configuration so they're available for all commands, and
we now have one standardized way of initializing an appropriate logger.

Fixes #209.

},
}
cmd.Flags().BoolVar(&opts.Down, "down", false, "print down migration")
cmd.Flags().BoolVar(&opts.Up, "up", false, "print up migration")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little odd that this command takes --down and --up options, when the migration commands are migrate-down and migrate-up, but I tried to consider all the alternatives, and I think this is the least bad. Having two full commands for migrate-get-down and migrate-get-up feels too heavy handed and unwieldy.

@brandur brandur force-pushed the brandur-dump-sql branch 3 times, most recently from b505679 to dc121f5 Compare March 17, 2024 00:02
@brandur brandur requested a review from bgentry March 17, 2024 00:05
@brandur brandur force-pushed the brandur-dump-sql branch 4 times, most recently from 6945ab6 to 8f9bb03 Compare March 17, 2024 04:45

require.Equal(t, "-- River migration 001 [down]", migrationComment(1, rivermigrate.DirectionDown))
require.Equal(t, "-- River migration 002 [up]", migrationComment(2, rivermigrate.DirectionUp))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I wish I could write more tests for the CLI, but I'm getting hard blocked by the fact that none of our internal test utilities are available to this module. Something to look into separately, but punting on it for now.

@brandur brandur force-pushed the brandur-fast-completer branch 3 times, most recently from e5d4268 to 71abf47 Compare March 17, 2024 20:22
Base automatically changed from brandur-fast-completer to master March 17, 2024 20:25
… options

This one's in pursuit of resolving #209, which I've decided to tackle
now because we're going to have to cut a CLI release across River
package versions for #258 anyway, so this'll reuse some work.

A new `river migrate-get` command becomes available, whose only job is
to dump SQL from River migrations so that it can easily be plugged into
other migration frameworks. Its use looks like:

    river migrate-get --version 3 --down > version3.down.sql
    river migrate-get --version 3 --up > version3.up.sql

It can also take multiple versions:

    river migrate-get --version 3,2,1 --down > river.down.sql
    river migrate-get --version 1,2.3 --up > river.up.sql

It can also dump _all_ migrations, which will be useful in cases where
users want to avoid River's internal migration framework completely, and
use their own:

    river migrate-get --all --exclude-version 1 --up > river_all.up.sql
    river migrate-get --all --exclude-version 1 --down > river_all.down.sql

Along with that, `migrate-down` and `migrate-up` get a few new useful
options:

* `--dry-run`: Prints information on migrations that would be run, but
  doesn't modify the database in any way.

* `--show-sql`: Prints SQL for each migration step that was applied.

This gives users an easy way to, after a River upgrade, run the CLI to
see what commands would be run were they to migrate, but without
actually performing the migration, likely a step that most production
users would perform to be cautious:

    river migrate-up --dry-run --show-sql

I've also done a little cleanup around the River CLI's `main.go`. The
`--verbose` and `--debug` commands added in #258 are now promoted to
persistent flag configuration so they're available for all commands, and
we now have one standardized way of initializing an appropriate logger.

Fixes #209.
@brandur
Copy link
Contributor Author

brandur commented Mar 17, 2024

thx!

@brandur brandur merged commit e68e430 into master Mar 17, 2024
10 checks passed
@brandur brandur deleted the brandur-dump-sql branch March 17, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

River Schema Migration Improvement
2 participants