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

Programmatically support checking if there are any pending migrations #225

Open
wendorf opened this issue Aug 13, 2020 · 4 comments
Open
Assignees

Comments

@wendorf
Copy link

wendorf commented Aug 13, 2020

In my CI pipeline, I want to fail a code deploy if there are any pending migrations so a human can look at them manually. I currently do that by running ! goose status 2>&1 | grep Pending > /dev/null. If that returns 0, then I know I have no pending migrations and can continue with my deploy.

This works, but it feels a bit brittle to grep for a string. I'd prefer to have explicit support in goose for programmatically knowing if migrations are pending.

Some possible solutions:

  • Provide a flag that sets the exit code to non-zero if any are pending: goose status --fail-if-pending
  • Provide a flag that outputs status in JSON so it can be parsed: goose status --json | jq '.migrations[] | select(.pending)' -e
@mfridman
Copy link
Collaborator

@wendorf Interesting suggestion, I like it. Just thinking out-loud, would something like this work, pseudo based on your suggestion?

{
    "migrations": [
        {
            "name": "000001_baseline_schema.sql",
            "applied_at": "2020-08-24T19:40:36-04:00",
            "is_applied": true,
            "version_id": 0
        },
        {
            "name": "000057_drop_a_table.go",
            "applied_at": "2020-08-24T19:40:40-04:00",
            "is_applied": true,
            "version_id": 45
        },
        {
            "name": "20200808122156_backfill_data.go",
            "applied_at": "",
            "is_applied": false,
            "version_id": 20200808122156
        }
    ]
}

@wendorf
Copy link
Author

wendorf commented Aug 25, 2020

That would work quite well :) To bikeshed, I might omit the applied_at field when is_applied is false, but I don't think it matters much.

@ezk84
Copy link

ezk84 commented May 23, 2022

Would be useful to have this also accessible from the go package's interface.

In our case we use goose programmatically to set up the database for integration tests, and it would be good to be able to know is there are pending migrations when a test suite is first run.

something like:

goose.DetailedStatus(db *sql.DB, dir string, opts ...goose.OptionsFunc) ([]goose.MigrationStatus, error)

@mfridman
Copy link
Collaborator

mfridman commented Apr 21, 2024

Would be useful to have this also accessible from the go package's interface.

The goose provider now has a .HasPending() method which enables you to check whether there are any pending migrations. A bit more details in this PR, #751

func (p *Provider) HasPending(context.Context) (bool, error) {}

I'm going to keep this issue open until there is --json support in the CLI to allow folks to programmatically check for pending migrations with goose ... | jq, I actually already have this working on this branch, so we're not far away. #741

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

No branches or pull requests

3 participants