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

atlas fmt check only #2785

Open
wyardley opened this issue May 15, 2024 · 3 comments
Open

atlas fmt check only #2785

wyardley opened this issue May 15, 2024 · 3 comments

Comments

@wyardley
Copy link

wyardley commented May 15, 2024

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
Copy link
Author

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

@wyardley
Copy link
Author

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
Copy link
Author

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

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

1 participant