ariga / atlas

Manage your database schema as code
https://atlasgo.io
Apache License 2.0
5.75k stars 259 forks source link

atlas fmt check only #2785

Open wyardley opened 4 months ago

wyardley commented 4 months ago

For, e.g., atlas schema fmt, currently, it seems to exit 0 whether files are modified or not:

% atlas schema fmt       
maindb.hcl
% echo $?
0
% atlas schema  fmt 
% echo $?
0

Would it be possible to either exit non-0 when files are modified, or have a --check flag, similar to Terraform's -check flag, that just checks whether files would be modified and exits non-0 if they would be?

While it's certainly possible to see if there is output or not, it would be nice to be able to have a simple way to check if formatting is needed (for example, in CI checks)

wyardley commented 4 months ago

Also, is there an easy way to check formatting (and validity) on the Atlas config (e.g., atlas.hcl) itself?

wyardley commented 4 months ago

One thing that does work is

% atlas schema apply --dry-run --to file://foo.hcl --url "docker://postgres/15/dev?search_path=public"

If that succeeds the config should be valid, and if not, it will fail with an error.

However, it would nice to be able to catch completely invalid HCL further left

schema "public" {
  comment = "standard public schema"
}

table "foo" {
  schema = schema.doesnotexist

  column "bar" {
    null = asdfasdf
    type = character_varying(60)
  }

It seems it should be possible for atlas to tell us that asdfasdf is not a valid value (vs. a bool) for null without actually trying to run it. Happy to make a separate issue for this if it's valid and there isn't another way to do this already. Of course, in practice, the above approach is generally very fast and safe, but it would still be nice.

wyardley commented 3 months ago

Side note, in case it's helpful to anyone; here's how I implemented this in GitHub actions for now, since there is no such command:

jobs:
  atlas:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ariga/setup-atlas@v0
      # Work around lack of a -check flag or similar for "atlas schema fmt"
      # https://dev.to/mbjelac/github-action-output-to-if-expression-and-how-to-test-it-531i
      - name: Check that schemas are formatted correctly via atlas schema fmt
        run: echo "modified=$(atlas schema fmt)" >> $GITHUB_OUTPUT
        id: fmt
      - name: Fail if schema is not formatted via atlas schema fmt
        if: steps.fmt.outputs.modified != null
        run: |
          echo "Unformatted files modified: ${{ steps.fmt.outputs.modified }}" >&2
          echo "Use atlas schema fmt to fix this." >&2
          exit 1