Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/commands: add migrate apply #17

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -191,27 +232,32 @@ workflows:
filters: *filters
- command-test-migrate-lint:
filters: *filters
- command-test-migrate-apply:
filters: *filters
- integration-test:
context: ariga-atlas
filters: *filters
requires:
- command-test-setup
- command-test-migrate-push
- command-test-migrate-lint
- command-test-migrate-apply
- integration-test-gh:
context: ariga-atlas-gh
filters: *filters
requires:
- 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
requires:
- 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
Expand All @@ -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
53 changes: 53 additions & 0 deletions src/commands/migrate_apply.yml
Original file line number Diff line number Diff line change
@@ -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: <<parameters.working_directory>>
command: atlasaction --action migrate/apply
environment:
INPUT_DIR: <<parameters.dir>>
INPUT_CONFIG: <<parameters.config>>
INPUT_ENV: <<parameters.env>>
INPUT_DRY_RUN: <<parameters.dry_run>>
INPUT_VARS: <<parameters.vars>>
INPUT_URL: <<parameters.url>>
2 changes: 1 addition & 1 deletion src/commands/migrate_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: <<parameters.working_directory>>
environment:
INPUT_DIR_NAME: <<parameters.dir_name>>
Expand Down