Skip to content

Commit

Permalink
atlasexec/migrate-lint: added --base param
Browse files Browse the repository at this point in the history
  • Loading branch information
dorav committed Sep 26, 2023
1 parent ff89d11 commit c6242ab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type (
Latest uint64
Vars Vars
Writer io.Writer
Base string
}
// SchemaApplyParams are the parameters for the `schema apply` command.
SchemaApplyParams struct {
Expand Down Expand Up @@ -302,6 +303,9 @@ func lintArgs(params *MigrateLintParams) []string {
if params.DirURL != "" {
args = append(args, "--dir", params.DirURL)
}
if params.Base != "" {
args = append(args, "--base", params.Base)
}
if params.Latest > 0 {
args = append(args, "--latest", strconv.FormatUint(params.Latest, 10))
}
Expand Down
54 changes: 54 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package atlasexec_test

import (
"ariga.io/atlas/cmd/atlas/x"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqlcheck"
"bytes"
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
Expand Down Expand Up @@ -134,6 +136,19 @@ func TestMigrateLint(t *testing.T) {
var raw json.RawMessage
require.NoError(t, json.NewDecoder(&buf).Decode(&raw))
})
t.Run("lint uses --base and --latest", func(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
summary, err := c.MigrateLint(context.Background(), &atlasexec.MigrateLintParams{
DevURL: "sqlite://file?mode=memory",
DirURL: "file://testdata/migrations",

Latest: 1,
Base: "atlas://test-dir",
})
require.ErrorContains(t, err, "--latest, --git-base, and --base are mutually exclusive")
require.Nil(t, summary)
})
}

type graphQLQuery struct {
Expand All @@ -144,6 +159,7 @@ type graphQLQuery struct {
type Dir struct {
Name string `json:"name"`
Content string `json:"content"`
Slug string `json:"slug"`
}
type dirsQueryResponse struct {
Data struct {
Expand All @@ -161,6 +177,19 @@ func TestMigrateLintWithLogin(t *testing.T) {
switch {
case strings.Contains(query.Query, "mutation reportMigrationLint"):
fmt.Fprintf(w, `{ "data": { "reportMigrationLint": { "url": "https://migration-lint-report-url" } } }`)
case strings.Contains(query.Query, "query dirs"):
dir := testDir(t, "./testdata/migrations")
ad, err := migrate.ArchiveDir(&dir)
require.NoError(t, err)
resp := &dirsQueryResponse{}
resp.Data.Dirs = []Dir{{
Name: "test-dir-name",
Slug: "test-dir-slug",
Content: base64.StdEncoding.EncodeToString(ad),
}}
st2bytes, err := json.Marshal(resp)
require.NoError(t, err)
fmt.Fprint(w, string(st2bytes))
}
}))
t.Cleanup(srv.Close)
Expand Down Expand Up @@ -201,6 +230,19 @@ func TestMigrateLintWithLogin(t *testing.T) {
require.NoError(t, err)
require.Equal(t, strings.TrimSpace(string(buf.Bytes())), "https://migration-lint-report-url")

Check failure on line 231 in atlasexec/atlas_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1030: should use buf.String() instead of string(buf.Bytes()) (gosimple)
})
t.Run("lint uses --base", func(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
summary, err := c.MigrateLint(context.Background(), &atlasexec.MigrateLintParams{
DevURL: "sqlite://file?mode=memory",
DirURL: "file://testdata/migrations",

ConfigURL: atlasConfigURL,
Base: "atlas://test-dir-slug",
})
require.NoError(t, err)
require.NotNil(t, summary)
})
}

func generateHCL(t *testing.T, url, token string) string {
Expand Down Expand Up @@ -235,6 +277,18 @@ func generateHCL(t *testing.T, url, token string) string {
return atlasConfigURL
}

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 Test_MigrateStatus(t *testing.T) {
type args struct {
ctx context.Context
Expand Down

0 comments on commit c6242ab

Please sign in to comment.