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

atlasexec: added context flag to migrate-apply #29

Merged
merged 2 commits into from
Nov 1, 2023
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
4 changes: 4 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
MigrateApplyParams struct {
Env string
ConfigURL string
Context string
DirURL string
URL string
RevisionsSchema string
Expand Down Expand Up @@ -204,6 +205,9 @@ func (c *Client) MigrateApply(ctx context.Context, params *MigrateApplyParams) (
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.Context != "" {
args = append(args, "--context", params.Context)
}
if params.URL != "" {
args = append(args, "--url", params.URL)
}
Expand Down
81 changes: 81 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,87 @@ func Test_MigrateApply(t *testing.T) {
require.EqualValues(t, "20230926085734", got.Target)
}

func Test_MigrateApplyWithRemote(t *testing.T) {
type (
ContextInput struct {
TriggerType string `json:"triggerType,omitempty"`
TriggerVersion string `json:"triggerVersion,omitempty"`
}
graphQLQuery struct {
Query string `json:"query"`
Variables json.RawMessage `json:"variables"`
MigrateApplyReport struct {
Input struct {
Context *ContextInput `json:"context,omitempty"`
} `json:"input"`
}
}
)
token := "123456789"
handler := func(payloads *[]graphQLQuery) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer "+token, r.Header.Get("Authorization"))
var query graphQLQuery
require.NoError(t, json.NewDecoder(r.Body).Decode(&query))
*payloads = append(*payloads, query)
}
}
var payloads []graphQLQuery
srv := httptest.NewServer(handler(&payloads))
t.Cleanup(srv.Close)
ec, err := atlasexec.NewWorkingDir(
atlasexec.WithMigrations(os.DirFS(filepath.Join("testdata", "migrations"))),
atlasexec.WithAtlasHCL(func(w io.Writer) error {
_, err := fmt.Fprintf(w, `
env {
name = atlas.env
url = "sqlite://file?_fk=1&cache=shared&mode=memory"
migration {
dir = "atlas://test_dir"
}
}
atlas {
cloud {
token = %q
url = %q
}
}`, token, srv.URL)
return err
}),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, ec.Close())
})
c, err := atlasexec.NewClient(ec.Path(), "atlas")
require.NoError(t, err)
got, err := c.MigrateApply(context.Background(), &atlasexec.MigrateApplyParams{
Env: "test",
})
require.NoError(t, err)
require.NotNil(t, got)
require.Len(t, payloads, 3)
reportPayload := payloads[2]
require.Regexp(t, "mutation ReportMigration", reportPayload.Query)
err = json.Unmarshal(reportPayload.Variables, &reportPayload.MigrateApplyReport)
require.NoError(t, err)
require.Nil(t, reportPayload.MigrateApplyReport.Input.Context)
got, err = c.MigrateApply(context.Background(), &atlasexec.MigrateApplyParams{
Env: "test",
Context: `{ "triggerVersion": "1.2.3", "triggerType": "GITHUB_ACTION" }`,
})
require.NoError(t, err)
require.NotNil(t, got)
require.Len(t, payloads, 6)
reportPayload = payloads[5]
require.Regexp(t, "mutation ReportMigration", reportPayload.Query)
err = json.Unmarshal(reportPayload.Variables, &reportPayload.MigrateApplyReport)
require.NoError(t, err)
require.NotNil(t, reportPayload.MigrateApplyReport.Input.Context)
require.Equal(t, "GITHUB_ACTION", reportPayload.MigrateApplyReport.Input.Context.TriggerType)
require.Equal(t, "1.2.3", reportPayload.MigrateApplyReport.Input.Context.TriggerVersion)
}

func TestBrokenApply(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
Expand Down