From 4afdfe17b240cd248bdd3e781fcfe4b863f91449 Mon Sep 17 00:00:00 2001 From: noamcattan Date: Tue, 13 Feb 2024 11:31:18 +0200 Subject: [PATCH 1/2] atlasaction: support dry-run flag --- atlasaction/action.go | 11 +++++++++++ migrate/apply/action.yml | 3 +++ 2 files changed, 14 insertions(+) diff --git a/atlasaction/action.go b/atlasaction/action.go index 00ad4f52..2bb455b8 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -29,11 +29,22 @@ var Version string // MigrateApply runs the GitHub Action for "ariga/atlas-action/migrate/apply". func MigrateApply(ctx context.Context, client *atlasexec.Client, act *githubactions.Action) error { + dryRun, err := func() (bool, error) { + inp := act.GetInput("dry-run") + if inp == "" { + return false, nil + } + return strconv.ParseBool(inp) + }() + if err != nil { + return fmt.Errorf(`invlid value for the "dry-run" input: %w`, err) + } params := &atlasexec.MigrateApplyParams{ URL: act.GetInput("url"), DirURL: act.GetInput("dir"), ConfigURL: act.GetInput("config"), Env: act.GetInput("env"), + DryRun: dryRun, TxMode: act.GetInput("tx-mode"), // Hidden param. BaselineVersion: act.GetInput("baseline"), // Hidden param. Context: &atlasexec.DeployRunContext{ diff --git a/migrate/apply/action.yml b/migrate/apply/action.yml index 407b9f6a..10c5c1db 100644 --- a/migrate/apply/action.yml +++ b/migrate/apply/action.yml @@ -21,6 +21,9 @@ inputs: env: description: The environment to use from the Atlas configuration file. For example, `dev`. required: false + dry-run: + description: Print SQL without executing it. Either "true" or "false". + required: false outputs: current: description: The current version of the database. (before applying migrations) From 844fd465e7ea38b55dd3ef49f6d59b4818ec6de8 Mon Sep 17 00:00:00 2001 From: noamcattan Date: Tue, 13 Feb 2024 11:45:50 +0200 Subject: [PATCH 2/2] add tests --- atlasaction/action_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/atlasaction/action_test.go b/atlasaction/action_test.go index 557123fa..cd1b095c 100644 --- a/atlasaction/action_test.go +++ b/atlasaction/action_test.go @@ -44,6 +44,36 @@ func TestMigrateApply(t *testing.T) { "target": "20230922132634", }, m) }) + t.Run("dry-run", func(t *testing.T) { + tt := newT(t) + tt.setInput("url", "sqlite://"+tt.db) + tt.setInput("dir", "file://testdata/migrations/") + tt.setInput("dry-run", "true") + err := MigrateApply(context.Background(), tt.cli, tt.act) + require.NoError(t, err) + out, err := tt.cli.SchemaInspect(context.Background(), &atlasexec.SchemaInspectParams{ + URL: "sqlite://" + tt.db, + Exclude: []string{"atlas_schema_revisions"}, + Format: "sql", + }) + require.NoError(t, err) + require.Empty(t, out) + }) + t.Run("dry-run false", func(t *testing.T) { + tt := newT(t) + tt.setInput("url", "sqlite://"+tt.db) + tt.setInput("dir", "file://testdata/migrations/") + tt.setInput("dry-run", "false") + err := MigrateApply(context.Background(), tt.cli, tt.act) + require.NoError(t, err) + out, err := tt.cli.SchemaInspect(context.Background(), &atlasexec.SchemaInspectParams{ + URL: "sqlite://" + tt.db, + Exclude: []string{"atlas_schema_revisions"}, + Format: "sql", + }) + require.NoError(t, err) + require.NotEmpty(t, out) + }) t.Run("tx-mode", func(t *testing.T) { tt := newT(t) tt.setInput("url", "sqlite://"+tt.db)