Skip to content

Commit

Permalink
src/commands: support migrate lint (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored May 7, 2024
1 parent d4a71b2 commit 14b2048
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ jobs:
grep -qe "--config file://atlas.hcl" /tmp/echo.out
# Should include the vars.
grep -qe "--var foo=bar --var baz=qux --var quux=corge" /tmp/echo.out
command-test-migrate-lint:
executor: atlas-orb/default
steps:
- checkout
- run:
name: Mock atlas with echo.sh
command:
sudo cp ./src/scripts/echo.sh /bin/atlas
- atlas-orb/migrate_lint:
working_directory: testdata
dir_name: my-cool-project
dir: "file://migrations"
env: "circleci-test"
config: "file://atlas.hcl"
dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
vars: |
foo=bar baz=qux quux=corge
- run:
name: Check echo.out for expected output
command: |
# Should include push with the git revision and latest tag.
grep -qe "--base atlas://my-cool-project" /tmp/echo.out
# Should include the dev URL.
grep -qe "--dev-url postgres://postgres:pass@localhost:5432/test?sslmode=disable" /tmp/echo.out
# Should include the environment.
grep -qe "--env circleci-test" /tmp/echo.out
# Should include the config.
grep -qe "--config file://atlas.hcl" /tmp/echo.out
# Should include the vars.
grep -qe "--var foo=bar --var baz=qux --var quux=corge" /tmp/echo.out
integration-test:
docker:
- image: cimg/base:current
Expand All @@ -87,6 +117,10 @@ jobs:
- atlas-orb/setup:
version: "latest"
cloud_token_env: "ATLAS_TOKEN"
- atlas-orb/migrate_lint:
working_directory: testdata
dir_name: my-cool-project
dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
- atlas-orb/migrate_push:
working_directory: testdata
dir_name: my-cool-project
Expand All @@ -99,12 +133,15 @@ workflows:
filters: *filters
- command-test-migrate-push:
filters: *filters
- command-test-migrate-lint:
filters: *filters
- integration-test:
context: ariga-atlas
filters: *filters
requires:
- command-test-setup
- command-test-migrate-push
- command-test-migrate-lint
# The orb must be re-packed for publishing, and saved to the workspace.
- orb-tools/pack:
filters: *release-filters
Expand All @@ -117,5 +154,6 @@ workflows:
- orb-tools/pack
- command-test-setup
- command-test-migrate-push
- command-test-migrate-lint
context: orb-publisher
filters: *release-filters
53 changes: 53 additions & 0 deletions src/commands/migrate_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
description: >
This command lints the migration directory before pushing to Atlas Cloud.
parameters:
dir:
type: string
default: ""
description: |
The URL of the migration directory to lint. For example: file://migrations.
Read more about [Atlas URLs](https://atlasgo.io/concepts/url)
dir_name:
type: string
description: |
The name (slug) of the project in Atlas Cloud.
dev_url:
type: string
default: ''
description: |
The URL of the dev-database to use for analysis. For example: mysql://root:pass@localhost:3306/dev.
Read more about [dev-databases](https://atlasgo.io/concepts/dev-database).
config:
type: string
default: ''
description: |
The path to the Atlas configuration file. By default, Atlas will look for a file named `atlas.hcl` in the current directory.
For example, `file://config/atlas.hcl`. Learn more about [Atlas configuration files](https://atlasgo.io/atlas-schema/projects).
vars:
type: string
default: ''
description: |
Extra variables to pass to the Atlas configuration file. For example, `key=value`.
working_directory:
type: string
default: "."
description: |
The working directory to run from. Defaults to project root.
env:
type: string
default: ''
description: |
The environment to use from the Atlas configuration file. For example, `dev`.
steps:
- run:
name: Lint migrations to Atlas Cloud
command: <<include(scripts/migrate-lint.sh)>>
working_directory: <<parameters.working_directory>>
environment:
PARAM_DIR_NAME: <<parameters.dir_name>>
PARAM_DIR: <<parameters.dir>>
PARAM_CONFIG: <<parameters.config>>
PARAM_ENV: <<parameters.env>>
PARAM_VARS: <<parameters.vars>>
PARAM_DEV_URL: <<parameters.dev_url>>
31 changes: 31 additions & 0 deletions src/scripts/migrate-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

DIR_NAME=$(circleci env subst "${PARAM_DIR_NAME}")
DIR=$(circleci env subst "${PARAM_DIR}")
CONFIG=$(circleci env subst "${PARAM_CONFIG}")
ENV=$(circleci env subst "${PARAM_ENV}")
VARS=$(circleci env subst "${PARAM_VARS}")
DEV_URL=$(circleci env subst "${PARAM_DEV_URL}")

ARGS="$ARGS --dev-url $DEV_URL"
if [ -n "$DIR" ]; then
ARGS="$ARGS --dir $DIR"
fi
if [ -n "$DIR_NAME" ]; then
ARGS="$ARGS --base atlas://$DIR_NAME"
fi
if [ -n "$CONFIG" ]; then
ARGS="$ARGS --config $CONFIG"
fi
if [ -n "$ENV" ]; then
ARGS="$ARGS --env $ENV"
fi
if [ -n "$VARS" ]; then
for _var in $VARS
do
ARGS="$ARGS --var $_var"
done
fi

# shellcheck disable=SC2086
atlas migrate lint $ARGS

0 comments on commit 14b2048

Please sign in to comment.