Skip to content

Commit

Permalink
src/commands: add migrate down (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Jun 4, 2024
1 parent e3fdc27 commit e3c5d9a
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,53 @@ jobs:
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
command-test-migrate-down:
executor: atlas-orb/default
steps:
- checkout
- run:
name: Mock atlas with echo.sh
command: |
sudo cp ./src/scripts/echo.sh /bin/atlas
sudo cp ./src/scripts/echo.sh /bin/atlasaction
- atlas-orb/migrate_down:
working_directory: testdata
url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
dir: "file://migrations"
config: "file://atlas.hcl"
dev_url: postgres://postgres:pass@localhost:5432/dev?sslmode=disable
env: "circleci-test"
amount: 1
to_tag: "tag"
to_version: "version"
wait_interval: "2s"
wait_timeout: "2m"
vars: |
foo=bar baz=qux quux=corge
- run:
name: Check echo.out for expected output
command: |
grep -qe "--action migrate/down" /tmp/echo.out
# check url should be set in the environment
grep -qe "INPUT_URL=postgres://postgres:pass@localhost:5432/test?sslmode=disable" /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/dev?sslmode=disable" /tmp/env.out
# check env should be set in the environment
grep -qe "INPUT_ENV=circleci-test" /tmp/env.out
# check amount should be set in the environment
grep -qe "INPUT_AMOUNT=1" /tmp/env.out
# check to_tag should be set in the environment
grep -qe "INPUT_TO_TAG=tag" /tmp/env.out
# check to_version should be set in the environment
grep -qe "INPUT_TO_VERSION=version" /tmp/env.out
# check wait_interval should be set in the environment
grep -qe "INPUT_WAIT_INTERVAL=2s" /tmp/env.out
# check wait_timeout should be set in the environment
grep -qe "INPUT_WAIT_TIMEOUT=2m" /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 @@ -150,6 +197,13 @@ jobs:
POSTGRES_PASSWORD: pass
steps:
- checkout
- run:
name: Wait for Postgres
command: dockerize -wait tcp://localhost:5432 -timeout 60s
- run:
name: Create "dev" database
command: |
psql postgres://postgres:pass@localhost:5432 -c "CREATE DATABASE dev;"
- atlas-orb/setup:
version: "latest"
cloud_token_env: "ATLAS_TOKEN"
Expand All @@ -165,6 +219,12 @@ jobs:
working_directory: testdata
dir: atlas://my-cool-project
url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
- atlas-orb/migrate_down:
working_directory: testdata
url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
dir: "file://migrations"
dev_url: postgres://postgres:pass@localhost:5432/dev?sslmode=disable
amount: 1
integration-test-gh:
docker:
- image: cimg/base:current
Expand All @@ -177,6 +237,13 @@ jobs:
GITHUB_REPOSITORY: "ariga/atlas-orb"
steps:
- checkout
- run:
name: Wait for Postgres
command: dockerize -wait tcp://localhost:5432 -timeout 60s
- run:
name: Create "dev" database
command: |
psql postgres://postgres:pass@localhost:5432 -c "CREATE DATABASE dev;"
- atlas-orb/setup:
version: "latest"
cloud_token_env: "ATLAS_TOKEN"
Expand All @@ -192,6 +259,12 @@ jobs:
working_directory: testdata
dir: atlas://my-cool-project
url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
- atlas-orb/migrate_down:
working_directory: testdata
url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
dir: "file://migrations"
dev_url: postgres://postgres:pass@localhost:5432/dev?sslmode=disable
amount: 1
integration-test-gh-lint:
docker:
- image: cimg/base:current
Expand Down Expand Up @@ -234,6 +307,8 @@ workflows:
filters: *filters
- command-test-migrate-apply:
filters: *filters
- command-test-migrate-down:
filters: *filters
- integration-test:
context: ariga-atlas
filters: *filters
Expand All @@ -242,6 +317,7 @@ workflows:
- command-test-migrate-push
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
- integration-test-gh:
context: ariga-atlas-gh
filters: *filters
Expand All @@ -250,6 +326,7 @@ workflows:
- command-test-migrate-push
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
- integration-test-gh-lint:
context: ariga-atlas-gh
filters: *filters
Expand All @@ -258,6 +335,7 @@ workflows:
- command-test-migrate-push
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
# The orb must be re-packed for publishing, and saved to the workspace.
- orb-tools/pack:
filters: *release-filters
Expand All @@ -272,5 +350,6 @@ workflows:
- command-test-migrate-push
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
context: orb-publisher
filters: *release-filters
105 changes: 105 additions & 0 deletions src/commands/migrate_down.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
description: >
Revert migrations to a database.
parameters:
url:
type: string
default: ''
description: |
The URL of the target database. For example: `mysql://root:pass@localhost:3306/dev`.
dir:
type: string
default: ""
description: |
The URL of the migration directory to push. For example: file://migrations.
Read more about [Atlas URLs](https://atlasgo.io/concepts/url)
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).
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).
env:
type: string
default: ''
description: |
The environment to use from the Atlas configuration file. For example, `dev`.
amount:
type: integer
default: 1
description: |
The number of migrations to revert. Defaults to 1.
to_version:
type: string
default: ''
description: |
To which version to revert.
to_tag:
type: string
default: ''
description: |
To which tag to revert.
working_directory:
type: string
default: "."
description: |
The working directory to run from. Defaults to project root.
wait_interval:
type: string
default: "1s"
description: |
Time in seconds between different migrate down attempts, useful when waiting for plan approval, defaults to 1s.
wait_timeout:
type: string
default: ""
description: |
Time after which no other retry attempt is made and the action exits. If not set, only one attempt is made.
vars:
type: string
default: ''
description: |
Extra variables to pass to the Atlas configuration file. For example, `key=value`.
github_repo_env:
type: env_var_name
default: GITHUB_REPOSITORY
description: |
The repository name that linting results will be posted to.
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: Revert migrations to a database
command: |
# replace GITHUB_REPOSITORY with the github_repo_env if provided
if [ -n "${<<parameters.github_repo_env>>}" ]; then
GITHUB_REPOSITORY=${<<parameters.github_repo_env>>}
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/down
working_directory: <<parameters.working_directory>>
environment:
INPUT_URL: <<parameters.url>>
INPUT_DIR: <<parameters.dir>>
INPUT_CONFIG: <<parameters.config>>
INPUT_DEV_URL: <<parameters.dev_url>>
INPUT_ENV: <<parameters.env>>
INPUT_AMOUNT: <<parameters.amount>>
INPUT_TO_VERSION: <<parameters.to_version>>
INPUT_TO_TAG: <<parameters.to_tag>>
INPUT_WAIT_INTERVAL: <<parameters.wait_interval>>
INPUT_WAIT_TIMEOUT: <<parameters.wait_timeout>>
INPUT_VARS: <<parameters.vars>>

0 comments on commit e3c5d9a

Please sign in to comment.