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

atlasaction: support migrate/apply #71

Merged
merged 9 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
_ "embed"
"errors"
"strconv"
"strings"
"text/template"

"ariga.io/atlas-go-sdk/atlasexec"
"github.com/sethvargo/go-githubactions"
)

var (
//go:embed atlas.hcl.tmpl
tmpl string

Check failure on line 21 in atlasaction/action.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var `tmpl` is unused (unused)
config = template.Must(template.New("atlashcl").Parse(tmpl))

Check failure on line 22 in atlasaction/action.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var `config` is unused (unused)
)

// MigrateApply runs the GitHub Action for "ariga/atlas-action/migrate/apply".
func MigrateApply(ctx context.Context, client *atlasexec.Client, act *githubactions.Action) error {
params := &atlasexec.MigrateApplyParams{
Expand All @@ -24,6 +32,19 @@
TxMode: act.GetInput("tx-mode"), // Hidden param.
BaselineVersion: act.GetInput("baseline"), // Hidden param.
}
// If dir begins with atlas://, this is a cloud-based migration directory.
// If the user didn't provide a config URL and provided an env name, we'll
// generate a temporary config file containing a naked env block. This is
// done so Atlas can report the run to the cloud.
if strings.HasPrefix(params.DirURL, "atlas://") && params.Env != "" && params.ConfigURL == "" {
cfg, clean, err := atlasexec.TempFile(`env { name = atlas.env }`, "hcl")
Copy link
Contributor

@dorav dorav Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If there is an env name but no URL, there might be a missing parameter (a human error) on the side of the user, in which case we might perform the wrong operation (apply a migration with a wrong parameter for example)?

If that's the case, I think throwing an error is safer and more predictable (and should probably be done by Atlas / Nebula, since we agreed the actions should do minimal logic)

  1. Why is that needed for reporting the run to the cloud? the user provided the migration directory
  2. What happens if the user didn't provide an 'env' name? will we get a report to the cloud?

if err != nil {
return err
}
// nolint:errcheck
defer clean()
params.ConfigURL = cfg
}
run, err := client.MigrateApply(ctx, params)
if err != nil {
act.SetOutput("error", err.Error())
Expand All @@ -40,3 +61,15 @@
act.Infof("Run complete: +%v", run)
return nil
}

type (
cloud struct {

Check failure on line 66 in atlasaction/action.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type `cloud` is unused (unused)
Dir string
Tag string
Token string
URL string
}
tmplParams struct {

Check failure on line 72 in atlasaction/action.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type `tmplParams` is unused (unused)
Cloud cloud
}
)
160 changes: 160 additions & 0 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"bytes"
"context"
"database/sql"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"text/template"

"ariga.io/atlas-go-sdk/atlasexec"
"ariga.io/atlas/sql/migrate"
_ "github.com/mattn/go-sqlite3"
"github.com/sethvargo/go-githubactions"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -87,6 +93,114 @@ func TestMigrateApply(t *testing.T) {
})
}

func TestMigrateApplyCloud(t *testing.T) {
handler := func(payloads *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer token", r.Header.Get("Authorization"))
body := readBody(t, r.Body)
*payloads = append(*payloads, body)
switch b := body; {
case strings.Contains(b, "query dirState"):
dir := testDir(t, "./testdata/migrations")
ad, err := migrate.ArchiveDir(&dir)
require.NoError(t, err)
// nolint:errcheck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Fprintf(w, `{"data":{"dirState":{"content":%q}}}`, base64.StdEncoding.EncodeToString(ad))
case strings.Contains(b, "mutation ReportMigration"):
// nolint:errcheck
fmt.Fprintf(w, `{"data":{"reportMigration":{"url":"https://atlas.com"}}}`)
case strings.Contains(b, "query {\\n\\t\\t\\tme"):
default:
t.Log("Unhandled call: ", body)
}
}
}
t.Run("with-dir", func(t *testing.T) {
tt := newT(t)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "fail")
tt.setInput("dir-name", "cloud-project")

err := MigrateApply(context.Background(), tt.cli, tt.act)
require.ErrorContains(t, err, "dir and dir-name are mutually exclusive")
})
t.Run("with-config", func(t *testing.T) {
tt := newT(t)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir-name", "fail")
tt.setInput("config", "file://atlas.hcl")

err := MigrateApply(context.Background(), tt.cli, tt.act)
require.ErrorContains(t, err, "config and dir-name are mutually exclusive")
})
t.Run("basic", func(t *testing.T) {
var payloads []string
srv := httptest.NewServer(handler(&payloads))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big like! 👍

t.Cleanup(srv.Close)

tt := newT(t)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "atlas://cloud-project")
tt.setInput("env", "atlas")

// 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)
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")

m, err := tt.outputs()
require.NoError(t, err)
require.EqualValues(t, map[string]string{
"current": "",
"applied_count": "1",
"pending_count": "1",
"target": "20230922132634",
}, m)
})
t.Run("no-env", func(t *testing.T) {
var payloads []string
srv := httptest.NewServer(handler(&payloads))
t.Cleanup(srv.Close)

tt := newT(t)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "atlas://cloud-project")

// 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)
require.NoError(t, err)

require.Len(t, payloads, 2)
require.Contains(t, payloads[0], "query {\\n\\t\\t\\tme")
require.Contains(t, payloads[1], "query dirState")

m, err := tt.outputs()
require.NoError(t, err)
require.EqualValues(t, map[string]string{
"current": "",
"applied_count": "1",
"pending_count": "1",
"target": "20230922132634",
}, m)
})
}

func readBody(t *testing.T, r io.Reader) string {
b, err := io.ReadAll(r)
require.NoError(t, err)
return string(b)
}

// sqlitedb returns a path to an initialized sqlite database file. The file is
// created in a temporary directory and will be deleted when the test finishes.
func sqlitedb(t *testing.T) string {
Expand Down Expand Up @@ -179,3 +293,49 @@ func TestSetInput(t *testing.T) {
require.Equal(t, "greetings", tt.act.GetInput("hello-world"))
require.Equal(t, "farewell", tt.act.GetInput("goodbye-friends"))
}

// testDir returns a migrate.MemDir from the given path.
func testDir(t *testing.T, path string) (d migrate.MemDir) {
rd, err := os.ReadDir(path)
require.NoError(t, err)
for _, f := range rd {
fp := filepath.Join(path, f.Name())
b, err := os.ReadFile(fp)
require.NoError(t, err)
require.NoError(t, d.WriteFile(f.Name(), b))
}
return d
}

func generateHCL(t *testing.T, url, token string) string {
tmpl := `
atlas {
cloud {
token = "{{ .Token }}"
{{- if .URL }}
url = "{{ .URL }}"
{{- end }}
}
}
env {
name = atlas.env
}
`
config := template.Must(template.New("atlashcl").Parse(tmpl))
templateParams := struct {
URL string
Token string
}{
URL: url,
Token: token,
}
var buf bytes.Buffer
err := config.Execute(&buf, templateParams)
require.NoError(t, err)
atlasConfigURL, clean, err := atlasexec.TempFile(buf.String(), "hcl")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, clean())
})
return atlasConfigURL
}
26 changes: 26 additions & 0 deletions atlasaction/atlas.hcl.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{- with .Cloud }}
atlas {
cloud {
{{- if .Token }}
token = "{{ .Token }}"
{{- end }}
{{- if .URL }}
url = "{{ .URL }}"
{{- end }}
}
}
data "remote_dir" "this" {
name = "{{ .Dir }}"
{{- if .Tag }}
tag = "{{ .Tag }}"
{{- end }}
}
{{- end }}
env {
name = atlas.env
migration {
{{- with .Cloud }}
dir = data.remote_dir.this.url
{{- end }}
}
}
Loading