Skip to content

Commit

Permalink
atlasexec/migrate-lint: added --base param (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: dorav <dorav@ariga.io>
  • Loading branch information
dorav and dorav authored Sep 27, 2023
1 parent e37b297 commit 9a5834e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 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
56 changes: 54 additions & 2 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand All @@ -16,6 +17,7 @@ import (

"ariga.io/atlas-go-sdk/atlasexec"
"ariga.io/atlas/cmd/atlas/x"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqlcheck"

_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -133,20 +135,58 @@ 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)
})
}

func TestMigrateLintWithLogin(t *testing.T) {
type graphQLQuery struct {
Query string `json:"query"`
Variables json.RawMessage `json:"variables"`
}
type Dir struct {
Name string `json:"name"`
Content string `json:"content"`
Slug string `json:"slug"`
}
type dirsQueryResponse struct {
Data struct {
Dirs []Dir `json:"dirs"`
} `json:"data"`
}
token := "123456789"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer "+token, r.Header.Get("Authorization"))
var query graphQLQuery
require.NoError(t, json.NewDecoder(r.Body).Decode(&query))
if strings.Contains(query.Query, "mutation reportMigrationLint") {
_, err := fmt.Fprint(w, `{ "data": { "reportMigrationLint": { "url": "https://migration-lint-report-url" } } }`)
switch {
case strings.Contains(query.Query, "mutation reportMigrationLint"):
_, err := fmt.Fprintf(w, `{ "data": { "reportMigrationLint": { "url": "https://migration-lint-report-url" } } }`)
require.NoError(t, err)
case strings.Contains(query.Query, "query dirs"):
dir, err := migrate.NewLocalDir("./testdata/migrations")
require.NoError(t, err)
ad, err := migrate.ArchiveDir(dir)
require.NoError(t, err)
var 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)
_, err = fmt.Fprint(w, string(st2bytes))
require.NoError(t, err)
}
}))
Expand Down Expand Up @@ -198,6 +238,18 @@ func TestMigrateLintWithLogin(t *testing.T) {
}))
require.Equal(t, strings.TrimSpace(buf.String()), "https://migration-lint-report-url")
})
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 Test_MigrateStatus(t *testing.T) {
Expand Down

0 comments on commit 9a5834e

Please sign in to comment.