From e3c5d9aae5129cca05464425cb3c28e58107ab35 Mon Sep 17 00:00:00 2001 From: "Dat. Ba Dao" Date: Tue, 4 Jun 2024 10:46:59 +0700 Subject: [PATCH] src/commands: add migrate down (#18) --- .circleci/test-deploy.yml | 79 +++++++++++++++++++++++++ src/commands/migrate_down.yml | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100755 src/commands/migrate_down.yml diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index c60aa67..25f98fc 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -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 @@ -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" @@ -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 @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/commands/migrate_down.yml b/src/commands/migrate_down.yml new file mode 100755 index 0000000..73787e0 --- /dev/null +++ b/src/commands/migrate_down.yml @@ -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 "${<>}" ]; then + GITHUB_REPOSITORY=${<>} + fi + # replace GITHUB_TOKEN with the github_token_env if provided + if [ -n "${<>}" ]; then + GITHUB_TOKEN=${<>} + fi + atlasaction --action migrate/down + working_directory: <> + environment: + INPUT_URL: <> + INPUT_DIR: <> + INPUT_CONFIG: <> + INPUT_DEV_URL: <> + INPUT_ENV: <> + INPUT_AMOUNT: <> + INPUT_TO_VERSION: <> + INPUT_TO_TAG: <> + INPUT_WAIT_INTERVAL: <> + INPUT_WAIT_TIMEOUT: <> + INPUT_VARS: <>