Skip to content

Commit

Permalink
atlasaction: add context parameter to migrate/apply
Browse files Browse the repository at this point in the history
  • Loading branch information
dorav committed Nov 1, 2023
1 parent ba0e88b commit 7c469b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
echo "BINARY_NAME=atlas-action-${{ github.event.inputs.version || 'v1' }}" >> $GITHUB_ENV
- name: Compile Go Binary
run: |
go build -o $BINARY_NAME ./cmd/atlas-action
go build -o $BINARY_NAME -ldflags "-X main.Version=${{ github.event.inputs.version || 'v1' }}" ./cmd/atlas-action
- name: Configure AWS credentials
run: |
aws configure set aws_access_key_id ${{ secrets.RELEASE_AWS_ACCESS_KEY_ID }}
Expand Down
10 changes: 10 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,25 @@ import (
"github.com/sethvargo/go-githubactions"
)

type VersionContextKey struct{}

// MigrateApply runs the GitHub Action for "ariga/atlas-action/migrate/apply".
func MigrateApply(ctx context.Context, client *atlasexec.Client, act *githubactions.Action) error {
ver, ok := ctx.Value(VersionContextKey{}).(string)
if !ok {
return fmt.Errorf("invalid type of context value for %v, expected string", VersionContextKey{})
}
params := &atlasexec.MigrateApplyParams{
URL: act.GetInput("url"),
DirURL: act.GetInput("dir"),
ConfigURL: act.GetInput("config"),
Env: act.GetInput("env"),
TxMode: act.GetInput("tx-mode"), // Hidden param.
BaselineVersion: act.GetInput("baseline"), // Hidden param.
Context: &atlasexec.DeployRunContext{
TriggerType: "GITHUB_ACTION",
TriggerVersion: ver,
},
}
run, err := client.MigrateApply(ctx, params)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import (
"github.com/stretchr/testify/require"
)

var applyCtx = context.WithValue(context.Background(), VersionContextKey{}, "v1.2.3")

func TestMigrateApply(t *testing.T) {
t.Run("local dir", func(t *testing.T) {
tt := newT(t)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "file://testdata/migrations/")

err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
require.NoError(t, err)

m, err := tt.outputs()
Expand All @@ -50,7 +51,7 @@ func TestMigrateApply(t *testing.T) {
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "file://testdata/migrations/")
tt.setInput("tx-mode", "fake")
err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)

// The error here proves that the tx-mode was passed to atlasexec, which
// is what we want to test.
Expand All @@ -65,7 +66,7 @@ func TestMigrateApply(t *testing.T) {
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "file://testdata/migrations/")
tt.setInput("baseline", "111_fake")
err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
// The error here proves that the baseline was passed to atlasexec, which
// is what we want to test.
exp := `Error: baseline version "111_fake" not found`
Expand All @@ -79,14 +80,14 @@ func TestMigrateApply(t *testing.T) {
t.Run("config-broken", func(t *testing.T) {
tt := newT(t)
tt.setInput("config", "file://testdata/config/broken.hcl")
err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
require.ErrorContains(t, err, `"testdata/config/broken.hcl" was not found`)
})
t.Run("config", func(t *testing.T) {
tt := newT(t)
tt.setInput("config", "file://testdata/config/atlas.hcl")
tt.setInput("env", "test")
err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
require.NoError(t, err)
})
}
Expand Down Expand Up @@ -525,14 +526,14 @@ func TestMigrateApplyCloud(t *testing.T) {
// This isn't simulating a user input but is a workaround for testing Cloud API calls.
cfgURL := generateHCL(t, srv.URL, "token")
tt.setInput("config", cfgURL)

err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
require.NoError(t, err)

require.Len(t, payloads, 3)
require.Contains(t, payloads[0], "query {\\n\\t\\t\\tme")
require.Contains(t, payloads[1], "query dirState")
require.Contains(t, payloads[2], "mutation ReportMigration")
require.Contains(t, payloads[2], `"context":{"triggerType":"GITHUB_ACTION","triggerVersion":"v1.2.3"}`)

m, err := tt.outputs()
require.NoError(t, err)
Expand All @@ -556,7 +557,7 @@ func TestMigrateApplyCloud(t *testing.T) {
cfgURL := generateHCL(t, srv.URL, "token")
tt.setInput("config", cfgURL)

err := MigrateApply(context.Background(), tt.cli, tt.act)
err := MigrateApply(applyCtx, tt.cli, tt.act)
require.NoError(t, err)

require.Len(t, payloads, 2)
Expand Down
4 changes: 3 additions & 1 deletion cmd/atlas-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const (

var cli RunAction

var Version string

func main() {
action := githubactions.New()
c, err := atlasexec.NewClient("", "atlas")
if err != nil {
action.Fatalf("Failed to create client: %s", err)
}
ctx := context.Background()
ctx := context.WithValue(context.Background(), atlasaction.VersionContextKey{}, Version)
cli := kong.Parse(
&cli,
kong.BindTo(ctx, (*context.Context)(nil)),
Expand Down

0 comments on commit 7c469b7

Please sign in to comment.