Skip to content

Commit

Permalink
atlasexec: add migrate hash command
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Mar 4, 2024
1 parent 2a44544 commit b5697bc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
28 changes: 28 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ type (
URL string
Vars Vars
}
// MigrateHashParams are the parameters for the `migrate hash` command.
MigrateHashParams struct {
Env string
ConfigURL string
DirURL string
DirFormat string
Vars Vars
}
Vars map[string]string
)

Expand Down Expand Up @@ -230,6 +238,26 @@ func (c *Client) MigratePush(ctx context.Context, params *MigratePushParams) (st
return strings.TrimSpace(resp), err
}

// MigrateHash runs the 'migrate hash' command.
func (c *Client) MigrateHash(ctx context.Context, params *MigrateHashParams) error {
args := []string{"migrate", "hash"}
if params.Env != "" {
args = append(args, "--env", params.Env)
}
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.DirURL != "" {
args = append(args, "--dir", params.DirURL)
}
if params.DirFormat != "" {
args = append(args, "--dir-format", params.DirFormat)
}
args = append(args, params.Vars.AsArgs()...)
_, err := c.runCommand(ctx, args)
return 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
29 changes: 29 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,35 @@ func TestMigratePush(t *testing.T) {
})
}

func TestMigrateHash(t *testing.T) {
ce, err := atlasexec.NewWorkingDir(
atlasexec.WithMigrations(os.DirFS(filepath.Join("testdata", "migrations"))),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, ce.Close())
})
c, err := atlasexec.NewClient(ce.Path(), "atlas")
require.NoError(t, err)
_, err = ce.WriteFile("migrations/new.sql", []byte(`
-- create table "users"
CREATE TABLE users(id int NOT NULL);
`))
require.NoError(t, err)
err = c.MigrateHash(context.Background(), &atlasexec.MigrateHashParams{
DirURL: "file://migrations",
})
require.NoError(t, err)
sum, err := os.ReadFile(ce.Path("migrations/atlas.sum"))
require.NoError(t, err)
require.Equal(t, `h1:kox3n1k+sm1yUsqcdJLcmU5rL8es0m+70rQJAjzsPZ4=
20230727105553_init.sql h1:jxgvnWO6tZD3lSPpH1ao5E/6VjapP7iwvBCUJ6aez58=
20230727105615_t2.sql h1:UvzeoFxe90Y/7b21ziwg6pPzWJQSV7LeYwJl8J63lMU=
20230926085734_destructive-change.sql h1:Gf/bSvUkfqHr/MEXKCxdGu2YvG8zwe4ER5TW8T/laA0=
new.sql h1:D0A4nIljowwFpTvvTJ2kdJcuozZHJo+mKsHZ7vZd0e8=
`, string(sum))
}

func generateHCL(t *testing.T, token string, srv *httptest.Server) string {
st := fmt.Sprintf(
`atlas {
Expand Down

0 comments on commit b5697bc

Please sign in to comment.