From e3fdc275b4f498a4a1c4fa3dd670136e4577f2e6 Mon Sep 17 00:00:00 2001 From: "Dat. Ba Dao" Date: Fri, 31 May 2024 11:33:05 +0700 Subject: [PATCH] src/commands: add migrate apply (#17) Need rebase before merge --- .circleci/test-deploy.yml | 47 ++++++++++++++++++++++++++++++ src/commands/migrate_apply.yml | 53 ++++++++++++++++++++++++++++++++++ src/commands/migrate_lint.yml | 2 +- 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/commands/migrate_apply.yml diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 2a2a197..c60aa67 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -107,6 +107,39 @@ 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-apply: + 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_apply: + working_directory: testdata + dir: "file://migrations" + env: "circleci-test" + config: "file://atlas.hcl" + url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + dry_run: true + vars: | + foo=bar baz=qux quux=corge + - run: + name: Check echo.out for expected output + command: | + # 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_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 dry_run should be set in the environment + grep -qe "INPUT_DRY_RUN=1" /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 @@ -128,6 +161,10 @@ jobs: working_directory: testdata dir_name: my-cool-project dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + - atlas-orb/migrate_apply: + working_directory: testdata + dir: atlas://my-cool-project + url: postgres://postgres:pass@localhost:5432/test?sslmode=disable integration-test-gh: docker: - image: cimg/base:current @@ -151,6 +188,10 @@ jobs: working_directory: testdata dir_name: my-cool-project dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable + - atlas-orb/migrate_apply: + working_directory: testdata + dir: atlas://my-cool-project + url: postgres://postgres:pass@localhost:5432/test?sslmode=disable integration-test-gh-lint: docker: - image: cimg/base:current @@ -191,6 +232,8 @@ workflows: filters: *filters - command-test-migrate-lint: filters: *filters + - command-test-migrate-apply: + filters: *filters - integration-test: context: ariga-atlas filters: *filters @@ -198,6 +241,7 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint + - command-test-migrate-apply - integration-test-gh: context: ariga-atlas-gh filters: *filters @@ -205,6 +249,7 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint + - command-test-migrate-apply - integration-test-gh-lint: context: ariga-atlas-gh filters: *filters @@ -212,6 +257,7 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint + - command-test-migrate-apply # The orb must be re-packed for publishing, and saved to the workspace. - orb-tools/pack: filters: *release-filters @@ -225,5 +271,6 @@ workflows: - command-test-setup - command-test-migrate-push - command-test-migrate-lint + - command-test-migrate-apply context: orb-publisher filters: *release-filters diff --git a/src/commands/migrate_apply.yml b/src/commands/migrate_apply.yml new file mode 100644 index 0000000..0f8e101 --- /dev/null +++ b/src/commands/migrate_apply.yml @@ -0,0 +1,53 @@ +description: > + This command apply 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 lint. 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). + 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`. + dry_run: + type: boolean + default: false + description: | + Print SQL without executing it. Defaults to `false` +steps: + - run: + name: Apply migrations to a database + working_directory: <> + command: atlasaction --action migrate/apply + environment: + INPUT_DIR: <> + INPUT_CONFIG: <> + INPUT_ENV: <> + INPUT_DRY_RUN: <> + INPUT_VARS: <> + INPUT_URL: <> diff --git a/src/commands/migrate_lint.yml b/src/commands/migrate_lint.yml index 874e802..5c47858 100644 --- a/src/commands/migrate_lint.yml +++ b/src/commands/migrate_lint.yml @@ -53,7 +53,7 @@ parameters: (e.g. `GITHUB_TOKEN`) steps: - run: - name: Lint migrations to Atlas Cloud + name: Lint migrations and report results to Atlas Cloud working_directory: <> environment: INPUT_DIR_NAME: <>