Skip to content

Commit

Permalink
dialect/sql/schema: respect sumfile when present and do not operate o…
Browse files Browse the repository at this point in the history
…n checksum mismatch (#2522)
  • Loading branch information
masseelch committed May 5, 2022
1 parent 2fe8e05 commit 12b6659
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion dialect/sql/schema/migrate.go
Expand Up @@ -187,7 +187,12 @@ func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table)
opts := []migrate.PlannerOption{
migrate.WithFormatter(m.atlas.fmt),
}
if !m.atlas.genSum {
if m.atlas.genSum {
// Validate the migration directory before proceeding.
if err := migrate.Validate(m.atlas.dir); err != nil {
return err
}
} else {
opts = append(opts, migrate.DisableChecksum())
}
return migrate.NewPlanner(nil, m.atlas.dir, opts...).WritePlan(plan)
Expand Down
13 changes: 13 additions & 0 deletions dialect/sql/schema/migrate_test.go
Expand Up @@ -84,6 +84,19 @@ func TestMigrate_Diff(t *testing.T) {
requireFileEqual(t, filepath.Join(p, v+"_changes.up.sql"), "-- create \"users\" table\nCREATE TABLE `users` (, PRIMARY KEY ());\n")
requireFileEqual(t, filepath.Join(p, v+"_changes.down.sql"), "-- reverse: create \"users\" table\nDROP TABLE `users`;\n")
require.NoFileExists(t, filepath.Join(p, "atlas.sum"))

// Test integrity file.
p = t.TempDir()
d, err = migrate.NewLocalDir(p)
require.NoError(t, err)
m, err = NewMigrate(db, WithDir(d), WithSumFile())
require.NoError(t, err)
require.NoError(t, m.Diff(context.Background(), &Table{Name: "users"}))
requireFileEqual(t, filepath.Join(p, v+"_changes.up.sql"), "-- create \"users\" table\nCREATE TABLE `users` (, PRIMARY KEY ());\n")
requireFileEqual(t, filepath.Join(p, v+"_changes.down.sql"), "-- reverse: create \"users\" table\nDROP TABLE `users`;\n")
require.FileExists(t, filepath.Join(p, "atlas.sum"))
require.NoError(t, d.WriteFile("tmp.sql", nil))
require.ErrorIs(t, m.Diff(context.Background(), &Table{Name: "users"}), migrate.ErrChecksumMismatch)
}

func requireFileEqual(t *testing.T, name, contents string) {
Expand Down

0 comments on commit 12b6659

Please sign in to comment.