Skip to content

Commit

Permalink
support allow-dirty flag in migrate apply cmd (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored Nov 21, 2024
1 parent 09aab86 commit b50c5b8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ All inputs are optional as they may be specified in the Atlas configuration file
* `vars` - Stringify JSON object containing variables to be used inside the Atlas configuration file.
For example: `'{"var1": "value1", "var2": "value2"}'`.
* `working-directory` - The working directory to run from. Defaults to project root.
* `allow-dirty` - Allow applying migration on a non-clean database. Defaults to false.

#### Outputs

Expand Down
1 change: 1 addition & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (a *Actions) MigrateApply(ctx context.Context) error {
URL: a.GetInput("url"),
DryRun: a.GetBoolInput("dry-run"),
RevisionsSchema: a.GetInput("revisions-schema"),
AllowDirty: a.GetBoolInput("allow-dirty"),
TxMode: a.GetInput("tx-mode"), // Hidden param.
BaselineVersion: a.GetInput("baseline"), // Hidden param.
}
Expand Down
15 changes: 15 additions & 0 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ func TestMigrateApply(t *testing.T) {
err := (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateApply(context.Background())
require.NoError(t, err)
})
t.Run("allow-dirty", func(t *testing.T) {
tt := newT(t, nil)
db, err := sql.Open("sqlite3", tt.db)
require.NoError(t, err)
_, err = db.Exec("CREATE TABLE dirty_table (id INTEGER PRIMARY KEY)")
require.NoError(t, err)
tt.setInput("url", "sqlite://"+tt.db)
tt.setInput("dir", "file://testdata/migrations/")
err = (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateApply(context.Background())
require.EqualError(t, err, "Error: sql/migrate: connected database is not clean: found multiple tables: 2. baseline version or allow-dirty is required")

tt.setInput("allow-dirty", "true")
err = (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateApply(context.Background())
require.NoError(t, err)
})
}

func TestMigrateDown(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions migrate/apply/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ inputs:
revisions-schema:
description: The name of the schema containing the revisions table.
required: false
allow-dirty:
description: Allow start working on a non-clean database.
required: false
outputs:
current:
description: The current version of the database. (before applying migrations)
Expand Down

0 comments on commit b50c5b8

Please sign in to comment.