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

Add atlas migrate diff #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ type (
LoginParams struct {
Token string
}
// MigrateDiffParams are the parameters for the `migrate diff` command.
MigrateDiffParams struct {
ToURL string
DevURL string
DirURL string
DirFormat string
Schema string
LockTimeout string
Format string
Qualifier string
Context *RunContext
ConfigURL string
Env string
Vars Vars
}
// MigratePushParams are the parameters for the `migrate push` command.
MigratePushParams struct {
Name string
Expand Down Expand Up @@ -248,6 +263,48 @@ func (c *Client) MigratePush(ctx context.Context, params *MigratePushParams) (st
return strings.TrimSpace(resp), err
}

// MigrateDiff runs the 'migrate diff' command.
func (c *Client) MigrateDiff(ctx context.Context, params *MigrateDiffParams) (string, error) {
args := []string{"migrate", "diff", "--format", "{{ sql }}"}
if params.ToURL != "" {
args = append(args, "--to", params.ToURL)
}
if params.DevURL != "" {
args = append(args, "--dev-url", params.DevURL)
}
if params.DirURL != "" {
args = append(args, "--dir", params.DirURL)
}
if params.DirFormat != "" {
args = append(args, "--dir-format", params.DirFormat)
}
if params.Schema != "" {
args = append(args, "--schema", params.Schema)
}
if params.LockTimeout != "" {
args = append(args, "--lock-timeout", params.LockTimeout)
}
if params.Qualifier != "" {
args = append(args, "--qualifier", params.Qualifier)
}
if params.Context != nil {
buf, err := json.Marshal(params.Context)
if err != nil {
return "", err
}
args = append(args, "--context", string(buf))
}
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.Env != "" {
args = append(args, "--env", params.Env)
}
args = append(args, params.Vars.AsArgs()...)
resp, err := stringVal(c.runCommand(ctx, args))
return strings.TrimSpace(resp), err
}

// MigrateApply runs the 'migrate apply' command.
func (c *Client) MigrateApply(ctx context.Context, params *MigrateApplyParams) (*MigrateApply, error) {
return firstResult(c.MigrateApplySlice(ctx, params))
Expand Down
51 changes: 51 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,57 @@ func TestMigrateLintWithLogin(t *testing.T) {
require.True(t, found)
})
}
func TestMigrateDiff(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
// Mock the client with a script that just prints the arguments to stderr and
// exit with an error code.
c, err := atlasexec.NewClient(t.TempDir(), filepath.Join(wd, "./mock-args.sh"))
require.NoError(t, err)

for _, tt := range []struct {
name string
params *atlasexec.MigrateDiffParams
expect string
}{
{
name: "no params",
params: &atlasexec.MigrateDiffParams{},
expect: "migrate diff --format {{ sql }}",
},
{
name: "with env",
params: &atlasexec.MigrateDiffParams{
Env: "test",
},
expect: "migrate diff --format {{ sql }} --env test",
},
{
name: "with url",
params: &atlasexec.MigrateDiffParams{
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
},
expect: "migrate diff --format {{ sql }} --dev-url sqlite://file?_fk=1&cache=shared&mode=memory",
},
{
name: "with exec order",
params: &atlasexec.MigrateDiffParams{
Env: "gorm",
},
expect: "migrate diff --format {{ sql }} --env gorm",
},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := c.MigrateDiff(context.Background(), tt.params)
require.Error(t, err)
// The script mock-args.sh exit with an error code.
// So, our atlasexec.MigrateApply should return a cliError.
// Which contains all output from the script (both stdout and stderr).
require.Equal(t, tt.expect, err.Error())
})
}
}


func TestMigratePush(t *testing.T) {
type (
Expand Down