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

Option to output JSON #350

Open
brandonchinn178 opened this issue May 12, 2022 · 3 comments
Open

Option to output JSON #350

brandonchinn178 opened this issue May 12, 2022 · 3 comments
Labels

Comments

@brandonchinn178
Copy link

brandonchinn178 commented May 12, 2022

It would be great to have a --json or --format json flag to force all output to be json lines

Use-case: we're running goose in a service, and we expect any output from our service to be JSON. We could parse the output + reoutput as JSON or just collect all output and output as a single string, but it'd be nicer to have that output out of the box

Related: #225

@mfridman
Copy link
Collaborator

Ye I'd like to see this as well. Should be a fairly straightforward add, just need to define the returned structs.

I might give this a go in the next few days.

@mfridman
Copy link
Collaborator

mfridman commented Apr 24, 2023

I might give this a go in the next few days year 😅 .

The plan is to make JSON output, for all commands, natively supported in #379.

One thing I'm currently thinking through is the user experience when relying on --json, what should the unhappy path be when there is a partial migration error running goose up (as an example).

happy path:

{
  "migrations": [
    {
      "migration_type": "sql",
      "version": 1,
      "filename": "00001_add_table.sql",
      "duration_ms": 17,
      "direction": "up",
      "empty": false
    },
    {
      "migration_type": "sql",
      "version": 2,
      "filename": "00002_fill_lots_of_data.sql",
      "duration_ms": 889,
      "direction": "up",
      "empty": false
    },
    {
      "migration_type": "sql",
      "version": 3,
      "filename": "00003_create_concurrent_index.sql",
      "duration_ms": 1153,
      "direction": "up",
      "empty": false
    }
  ],
  "total_duration_ms": 2090,
  "message": "successfully ran 3 migrations in 2.09s"
}

unhappy path:

In this particular case, we have 2 valid migrations files and 1 invalid migration file. Running goose up --json will apply the first two, but fail on the third one.

The best we can do (if the goal is to retain JSON output) is to surface a new error field.

{
  "migrations": [
    {
      "migration_type": "sql",
      "version": 1,
      "filename": "00001_add_table.sql",
      "duration_ms": 9,
      "direction": "up",
      "empty": false
    },
    {
      "migration_type": "sql",
      "version": 2,
      "filename": "00002_fill_lots_of_data.sql",
      "duration_ms": 947,
      "direction": "up",
      "empty": false
    },
    {
      "migration_type": "sql",
      "version": 3,
      "filename": "00003_create_concurrent_index.sql",
      "duration_ms": 1,
      "direction": "up",
      "empty": false,
      "error": "ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block (SQLSTATE 25001)"
    }
  ],
  "total_duration_ms": 987,
  "message": "partial migration error: ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block (SQLSTATE 25001)"
}

@MicrowaveTank
Copy link

MicrowaveTank commented Jul 20, 2024

Has there been any recent movement on this? Goose is a nice migration tool and I'm working on writing a Java wrapper for its CLI. The stdout isn't too hard to parse, but JSON output would make the parsing feel significantly less brittle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants