Skip to content

Commit

Permalink
src/commands: run migrate lint by atlasaction binary (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored May 21, 2024
1 parent 14b2048 commit ca0d31f
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 51 additions & 13 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
36 changes: 29 additions & 7 deletions src/commands/migrate_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: <<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>>
INPUT_DIR_NAME: <<parameters.dir_name>>
INPUT_DIR: <<parameters.dir>>
INPUT_CONFIG: <<parameters.config>>
INPUT_ENV: <<parameters.env>>
INPUT_VARS: <<parameters.vars>>
INPUT_DEV_URL: <<parameters.dev_url>>
command: |
# replace CIRCLE_REPOSITORY_URL with the repo_url if provided
if [ -n "<<parameters.repo_url>>" ]; then
CIRCLE_REPOSITORY_URL=<<parameters.repo_url>>
fi
# replace GITHUB_TOKEN with the github_token_env if provided
if [ -n "${<<parameters.github_token_env>>}" ]; then
GITHUB_TOKEN=${<<parameters.github_token_env>>}
fi
atlasaction --action migrate/lint
7 changes: 7 additions & 0 deletions src/commands/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ steps:
curl -sSf https://atlasgo.sh | sh
environment:
ATLAS_VERSION: <<parameters.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: |
Expand Down
4 changes: 4 additions & 0 deletions src/examples/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/scripts/echo.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash

echo "$@" >> /tmp/echo.out
printenv >> /tmp/env.out
12 changes: 12 additions & 0 deletions src/scripts/migrate-lint-fail.sh
Original file line number Diff line number Diff line change
@@ -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
31 changes: 0 additions & 31 deletions src/scripts/migrate-lint.sh

This file was deleted.

1 change: 1 addition & 0 deletions testdata/sqlite-broken-file/20220318104614_initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE BAD SQL STATEMENT;
1 change: 1 addition & 0 deletions testdata/sqlite-destructive/20230922132634_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table t1 ( c int );
1 change: 1 addition & 0 deletions testdata/sqlite-destructive/20230925192914.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table t1;
3 changes: 3 additions & 0 deletions testdata/sqlite-destructive/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1:jw+3IjeTY+1ngJYKRc/Ddq8hJkBWpVRnF8qS61zspJg=
20230922132634_init.sql h1:Q+dJaaJDja1u1qEni6E0SfC4dMXhHgW2F1ybAtgcgeE=
20230925192914.sql h1:2YTCgLJQ5t3t71z3zGGRNSaWBItjTXF9zEcQICGIZkc=
3 changes: 3 additions & 0 deletions testdata/sqlite-wrong-sum/20220318104614_initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- create "tbl" table
CREATE TABLE tbl (`col` int NOT NULL);
CREATE TABLE tbl2 (`col` int NOT NULL);
2 changes: 2 additions & 0 deletions testdata/sqlite-wrong-sum/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:hR7g3H9ehihtbM7U27NiN6apGicRoGL/Lu2IaIarrEk=
20220318104614_initial.sql h1:tnZ9Ye2s3YQVXnXKjHqGaxIxL0/asd

0 comments on commit ca0d31f

Please sign in to comment.