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

internal/provider: add deployment context to migrate cmd #95

Merged
merged 1 commit into from
Nov 8, 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 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