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

feat: goose validate command #449

Merged
merged 10 commits into from
Feb 25, 2023
Merged

feat: goose validate command #449

merged 10 commits into from
Feb 25, 2023

Conversation

mfridman
Copy link
Collaborator

@mfridman mfridman commented Jan 22, 2023

Fixes #448

  • what about .go files, should they be displayed?

rajeshkarnena

This comment was marked as outdated.

@VojtechVitek
Copy link
Collaborator

So this is a sanity check for the SQL migrations, right?

Do the numbers represent count of SQL statements?

Go migrations would be nice to support too, I guess we could print func vs. <nil>. And tx ✔️ based on the func signature.

Thinking of better names:
goose check
or
goose inspect

@mfridman
Copy link
Collaborator Author

mfridman commented Jan 31, 2023

Yep, intended to be a command users can run to sanity-check their migration files.

I noticed some users only find out about malformed migration files (especially down migrations) when it's too late. Here's a real-world example, where migration 35 is missing a semicolon.

https://github.com/smartcontractkit/chainlink/blob/develop/core/store/migrate/migrations/0035_create_feeds_managers.sql#L13

And running this proposed command would catch this error:

goose verify: failed to parse down "core/store/migrate/migrations/0035_create_feeds_managers.sql": error: failed to parse migration: state 4, direction: down: unexpected unfinished SQL query: "DROP TABLE feeds_managers": missing semicolon?

A lot of projects don't have a robust set of tests like we have, i.e., up, down, up then drop schema and compare it to the checked-in schema.

Great idea to include the .go migrations!

@mfridman mfridman changed the title feat: goose verify command feat: goose check command Feb 3, 2023
@mfridman
Copy link
Collaborator Author

mfridman commented Feb 4, 2023

wrote a simple parser for .go files using the std lib ast parser packages.

Type  Txn  Up  Down  Name
────  ───  ──  ────  ────
sql   ✔    1   1     00001_create_tables_repos_stargazers.sql
sql   ✔    1   1     00002_update_respos_add_owner_info.sql
sql   ✔    1   1     00003_update_respos_add_owner_type.sql
sql   ✔    1   1     00004_add_seq_id_stargazers.sql
sql   ✘    2   1     00005_add_index_stargazers.sql
sql   ✔    1   1     00006_create_matview_stars_day.sql
sql   ✔    1   1     00007_create_matview_stars_month.sql
sql   ✔    1   1     00008_create_matview_stars_year.sql
go    ✔    1   0     00009_fix_description_emojis.go. <--------- go migration, 1 up and nil down
sql   ✔    1   1     00010_add_mat_view_all.sql

@mfridman
Copy link
Collaborator Author

mfridman commented Feb 5, 2023

Still can't think of a good name for this command, cc @VojtechVitek what do you think? Suggestions welcome...

I think goose validate resonates the most.

goose validate
goose check
goose ls-files
goose vet
goose lint
goose verify
goose ...

Other examples

The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc.

https://developer.hashicorp.com/terraform/cli/commands/validate

@VojtechVitek
Copy link
Collaborator

validate is a pretty good name :)

@mfridman mfridman changed the title feat: goose check command feat: goose validate command Feb 6, 2023
"text/template"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/internal/cfg"
"github.com/pressly/goose/v3/internal/migrationstats"
Copy link
Collaborator Author

@mfridman mfridman Feb 25, 2023

Choose a reason for hiding this comment

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

It's possible folks might want this functionality exposed in the goose package, if so, we can move this to the top level or create a pkg with helpful utilities unrelated to migration logic per se.

@mfridman mfridman changed the title feat: goose validate command feat: goose validate command Feb 25, 2023
@mfridman
Copy link
Collaborator Author

mfridman commented Feb 25, 2023

Pretty happy with how this turned out, been finding this command awfully useful.

Some nice-to-have for the future:

  • output as json
  • additional Walk implementations (probably taking []*Migration)
  • maybe filtering and/or exclusion, although if we support json then that can be left to the caller with jq
  • move migrationstats out of ./internal

@mfridman mfridman merged commit 8c25e3b into master Feb 25, 2023
@mfridman mfridman deleted the gh-448 branch February 25, 2023 19:33
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.

feat: goose validate command
3 participants