From 14b204896cfd2a6f28de4cf86c65720b14a653c9 Mon Sep 17 00:00:00 2001 From: "Dat. Ba Dao" Date: Tue, 7 May 2024 10:50:04 +0700 Subject: [PATCH] src/commands: support migrate lint (#8) --- .circleci/test-deploy.yml | 38 +++++++++++++++++++++++++ src/commands/migrate_lint.yml | 53 +++++++++++++++++++++++++++++++++++ src/scripts/migrate-lint.sh | 31 ++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/commands/migrate_lint.yml create mode 100755 src/scripts/migrate-lint.sh diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 458605c..f1742f1 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/commands/migrate_lint.yml b/src/commands/migrate_lint.yml new file mode 100644 index 0000000..30738c1 --- /dev/null +++ b/src/commands/migrate_lint.yml @@ -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: <> + working_directory: <> + environment: + PARAM_DIR_NAME: <> + PARAM_DIR: <> + PARAM_CONFIG: <> + PARAM_ENV: <> + PARAM_VARS: <> + PARAM_DEV_URL: <> diff --git a/src/scripts/migrate-lint.sh b/src/scripts/migrate-lint.sh new file mode 100755 index 0000000..32b8dfc --- /dev/null +++ b/src/scripts/migrate-lint.sh @@ -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