diff --git a/.circleci/config.yml b/.circleci/config.yml index 394244d..3cff610 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,7 +16,7 @@ workflows: - orb-tools/pack: filters: *filters - orb-tools/review: - exclude: RC005,RC006 # Our repository is not a public at the moment. + exclude: RC005,RC006,RC009 # Our repository is not a public at the moment. filters: *filters - shellcheck/check: filters: *filters diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index f1742f1..bd67b7a 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -41,8 +41,7 @@ jobs: - checkout - run: name: Mock atlas with echo.sh - command: - sudo cp ./src/scripts/echo.sh /bin/atlas + command: sudo cp ./src/scripts/echo.sh /bin/atlas - atlas-orb/migrate_push: working_directory: testdata dir_name: my-cool-project @@ -80,8 +79,9 @@ jobs: - checkout - run: name: Mock atlas with echo.sh - command: + command: | sudo cp ./src/scripts/echo.sh /bin/atlas + sudo cp ./src/scripts/echo.sh /bin/atlasaction - atlas-orb/migrate_lint: working_directory: testdata dir_name: my-cool-project @@ -94,16 +94,19 @@ jobs: - 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 + grep -qe "--action migrate/lint" /tmp/echo.out + # check dir_name should be set in the environment + grep -qe "INPUT_DIR_NAME=my-cool-project" /tmp/env.out + # check dir should be set in the environment + grep -qe "INPUT_DIR=file://migrations" /tmp/env.out + # check dev_url should be set in the environment + grep -qe "INPUT_DEV_URL=postgres://postgres:pass@localhost:5432/test?sslmode=disable" /tmp/env.out + # check env should be set in the environment + grep -qe "INPUT_ENV=circleci-test" /tmp/env.out + # check config should be set in the environment + grep -qe "INPUT_CONFIG=file://atlas.hcl" /tmp/env.out + # check vars should be set in the environment + grep -qe "INPUT_VARS=foo=bar baz=qux quux=corge" /tmp/env.out integration-test: docker: - image: cimg/base:current @@ -125,6 +128,34 @@ jobs: working_directory: testdata dir_name: my-cool-project dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + integration-test-lint: + docker: + - image: cimg/base:current + steps: + - checkout + - atlas-orb/setup: + version: "latest" + cloud_token_env: "ATLAS_TOKEN" + - run: + name: Run atlasaction with migrate-lint-fail.sh + command: | + sudo cp /bin/atlasaction /bin/atlasaction-origin + sudo cp ./src/scripts/migrate-lint-fail.sh /bin/atlasaction + - atlas-orb/migrate_lint: + working_directory: testdata + dir_name: test-dir-sqlite + dir: "file://sqlite-broken-file" + dev_url: "sqlite://file.db?mode=memory" + - atlas-orb/migrate_lint: + working_directory: testdata + dir_name: test-dir-sqlite + dir: "file://sqlite-destructive" + dev_url: "sqlite://file.db?mode=memory" + - atlas-orb/migrate_lint: + working_directory: testdata + dir_name: test-dir-sqlite + dir: "file://sqlite-wrong-sum" + dev_url: "sqlite://file.db?mode=memory" workflows: test-deploy: jobs: @@ -142,6 +173,13 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint + - integration-test-lint: + 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 diff --git a/src/commands/migrate_lint.yml b/src/commands/migrate_lint.yml index 30738c1..ccce537 100644 --- a/src/commands/migrate_lint.yml +++ b/src/commands/migrate_lint.yml @@ -39,15 +39,37 @@ parameters: default: '' description: | The environment to use from the Atlas configuration file. For example, `dev`. + repo_url: + type: string + default: '' + description: | + The URL of the repository. If provided, linting results will be posted to the repository. + Otherwise, the repo url will based on your token type. + github_token_env: + type: env_var_name + default: GITHUB_TOKEN + description: | + Environment variable containing the GitHub token. + If provided, the command will authenticate to GitHub. + (e.g. `GITHUB_TOKEN`) 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: <> + INPUT_DIR_NAME: <> + INPUT_DIR: <> + INPUT_CONFIG: <> + INPUT_ENV: <> + INPUT_VARS: <> + INPUT_DEV_URL: <> + command: | + # replace CIRCLE_REPOSITORY_URL with the repo_url if provided + if [ -n "<>" ]; then + CIRCLE_REPOSITORY_URL=<> + fi + # replace GITHUB_TOKEN with the github_token_env if provided + if [ -n "${<>}" ]; then + GITHUB_TOKEN=${<>} + fi + atlasaction --action migrate/lint diff --git a/src/commands/setup.yaml b/src/commands/setup.yaml index 61cd39c..63b4bfc 100644 --- a/src/commands/setup.yaml +++ b/src/commands/setup.yaml @@ -21,6 +21,13 @@ steps: curl -sSf https://atlasgo.sh | sh environment: ATLAS_VERSION: <> + - run: + name: Download Atlas Action + command: | + # Download the binary + curl -o atlasaction https://release.ariga.io/atlas-action/atlas-action-v1 + # Install the binary in path + sudo install -o root -g root -m 0755 ./atlasaction /bin/atlasaction - run: name: Authenticate to Atlas Cloud command: | diff --git a/src/examples/example.yml b/src/examples/example.yml index 74b30a0..e75288b 100755 --- a/src/examples/example.yml +++ b/src/examples/example.yml @@ -23,6 +23,10 @@ usage: - atlas-orb/setup: version: "latest" cloud_token_env: "ATLAS_TOKEN" + - atlas-orb/migrate_lint: + dir_name: my-cool-project + dev_url: "postgres://postgres:pass@localhost:5432/postgres?sslmode=disable" + github_token_env: "GITHUB_TOKEN" - atlas-orb/migrate_push: dir_name: my-cool-project dev_url: postgres://postgres:pass@localhost:5432/postgres?sslmode=disable diff --git a/src/scripts/echo.sh b/src/scripts/echo.sh index ccf8488..2d54326 100755 --- a/src/scripts/echo.sh +++ b/src/scripts/echo.sh @@ -1,3 +1,4 @@ #!/bin/bash echo "$@" >> /tmp/echo.out +printenv >> /tmp/env.out diff --git a/src/scripts/migrate-lint-fail.sh b/src/scripts/migrate-lint-fail.sh new file mode 100644 index 0000000..32b31e8 --- /dev/null +++ b/src/scripts/migrate-lint-fail.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# execute the command and check the exit code is 1 +# testing purpose only, use for integration tests +atlasaction-origin --action migrate/lint +if [ $? -ne 1 ]; then + echo "Expected exit code 1, got $?" + exit 1 +else + echo "Exit code 1 as expected" + exit 0 +fi \ No newline at end of file diff --git a/src/scripts/migrate-lint.sh b/src/scripts/migrate-lint.sh deleted file mode 100755 index 32b8dfc..0000000 --- a/src/scripts/migrate-lint.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/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 diff --git a/testdata/sqlite-broken-file/20220318104614_initial.sql b/testdata/sqlite-broken-file/20220318104614_initial.sql new file mode 100644 index 0000000..962caa8 --- /dev/null +++ b/testdata/sqlite-broken-file/20220318104614_initial.sql @@ -0,0 +1 @@ +CREATE BAD SQL STATEMENT; \ No newline at end of file diff --git a/testdata/sqlite-destructive/20230922132634_init.sql b/testdata/sqlite-destructive/20230922132634_init.sql new file mode 100644 index 0000000..63ab153 --- /dev/null +++ b/testdata/sqlite-destructive/20230922132634_init.sql @@ -0,0 +1 @@ +create table t1 ( c int ); diff --git a/testdata/sqlite-destructive/20230925192914.sql b/testdata/sqlite-destructive/20230925192914.sql new file mode 100644 index 0000000..853c1ad --- /dev/null +++ b/testdata/sqlite-destructive/20230925192914.sql @@ -0,0 +1 @@ +drop table t1; diff --git a/testdata/sqlite-destructive/atlas.sum b/testdata/sqlite-destructive/atlas.sum new file mode 100644 index 0000000..94a0680 --- /dev/null +++ b/testdata/sqlite-destructive/atlas.sum @@ -0,0 +1,3 @@ +h1:jw+3IjeTY+1ngJYKRc/Ddq8hJkBWpVRnF8qS61zspJg= +20230922132634_init.sql h1:Q+dJaaJDja1u1qEni6E0SfC4dMXhHgW2F1ybAtgcgeE= +20230925192914.sql h1:2YTCgLJQ5t3t71z3zGGRNSaWBItjTXF9zEcQICGIZkc= diff --git a/testdata/sqlite-wrong-sum/20220318104614_initial.sql b/testdata/sqlite-wrong-sum/20220318104614_initial.sql new file mode 100644 index 0000000..855b2d2 --- /dev/null +++ b/testdata/sqlite-wrong-sum/20220318104614_initial.sql @@ -0,0 +1,3 @@ +-- create "tbl" table +CREATE TABLE tbl (`col` int NOT NULL); +CREATE TABLE tbl2 (`col` int NOT NULL); \ No newline at end of file diff --git a/testdata/sqlite-wrong-sum/atlas.sum b/testdata/sqlite-wrong-sum/atlas.sum new file mode 100644 index 0000000..20b98d8 --- /dev/null +++ b/testdata/sqlite-wrong-sum/atlas.sum @@ -0,0 +1,2 @@ +h1:hR7g3H9ehihtbM7U27NiN6apGicRoGL/Lu2IaIarrEk= +20220318104614_initial.sql h1:tnZ9Ye2s3YQVXnXKjHqGaxIxL0/asd \ No newline at end of file