-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ffd0d95
atlasaction: support migrate/apply
rotemtam e0f461d
fixtest
rotemtam 63d7b28
work
rotemtam 0f5d462
work
rotemtam 2a3306f
merge
rotemtam 42d84f8
fix test
rotemtam 48ae5a9
fix
rotemtam 9753dec
golanglint
rotemtam 79d07cd
fix
rotemtam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)