Skip to content

Commit

Permalink
internal/provider: add deployment context to migrate cmd (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Nov 8, 2023
1 parent 890784f commit ead412f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ func (r *MigrationResource) migrate(ctx context.Context, data *MigrationResource
ConfigURL: fmt.Sprintf("file://%s", cfgPath),
Env: defaultString(data.EnvName, "tf"),
Amount: amount,
Context: &atlas.DeployRunContext{
TriggerType: atlas.TriggerTypeTerraform,
TriggerVersion: r.version,
},
})
if err != nil {
diags.Append(errorDiagnostic(err, "Failed to apply migrations"))
Expand Down
24 changes: 18 additions & 6 deletions internal/provider/atlas_migration_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"ariga.io/atlas-go-sdk/atlasexec"
"ariga.io/atlas/sql/migrate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -319,17 +320,28 @@ func TestAccMigrationResource_RemoteDir(t *testing.T) {
dir = migrate.MemDir{}
dbURL = fmt.Sprintf("sqlite://%s?_fk=true", filepath.Join(t.TempDir(), "sqlite.db"))
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var m struct {
Query string `json:"query"`
Variables struct {
Input json.RawMessage `json:"input"`
} `json:"variables"`
}
type (
Input struct {
Context atlasexec.DeployRunContext `json:"context,omitempty"`
}
GraphQLQuery struct {
Query string `json:"query"`
Variables struct {
Input json.RawMessage `json:"input"`
} `json:"variables"`
}
)
var m GraphQLQuery
require.NoError(t, json.NewDecoder(r.Body).Decode(&m))
switch {
case strings.Contains(m.Query, "query"):
writeDir(t, &dir, w)
case strings.Contains(m.Query, "reportMigration"):
var i Input
err := json.Unmarshal(m.Variables.Input, &i)
require.NoError(t, err)
require.Equal(t, "test", i.Context.TriggerVersion)
require.Equal(t, atlasexec.TriggerTypeTerraform, i.Context.TriggerType)
fmt.Fprint(w, `{"data":{"reportMigration":{"success":true}}}`)
default:
t.Fatalf("unexpected query: %s", m.Query)
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type (
devURL string
// cloud is the Atlas Cloud configuration.
cloud *AtlasCloudBlock
// version is set to the provider version on release
version string
}
)

Expand Down Expand Up @@ -150,7 +152,7 @@ func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureReq
if resp.Diagnostics.HasError() {
return
}
p.data = providerData{client: c, cloud: model.Cloud}
p.data = providerData{client: c, cloud: model.Cloud, version: p.version}
if model != nil {
p.data.devURL = model.DevURL.ValueString()
}
Expand Down

0 comments on commit ead412f

Please sign in to comment.