Skip to content

Commit

Permalink
if enabled, check directory integrity early
Browse files Browse the repository at this point in the history
  • Loading branch information
masseelch committed Jun 14, 2022
1 parent 908a2e9 commit 8eeb23c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 11 additions & 11 deletions dialect/sql/schema/migrate.go
Expand Up @@ -176,6 +176,17 @@ func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table)
if m.atlas.dir == nil {
return errors.New("no migration directory given")
}
opts := []migrate.PlannerOption{
migrate.WithFormatter(m.atlas.fmt),
}
if m.atlas.genSum {
// Validate the migration directory before proceeding.
if err := migrate.Validate(m.atlas.dir); err != nil {
return fmt.Errorf("validating migration directory: %w", err)
}
} else {
opts = append(opts, migrate.DisableChecksum())
}
if err := m.init(ctx, m); err != nil {
return err
}
Expand Down Expand Up @@ -226,17 +237,6 @@ func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table)
if len(plan.Changes) == 0 {
return nil
}
opts := []migrate.PlannerOption{
migrate.WithFormatter(m.atlas.fmt),
}
if m.atlas.genSum {
// Validate the migration directory before proceeding.
if err := migrate.Validate(m.atlas.dir); err != nil {
return fmt.Errorf("validating migration directory: %w", err)
}
} else {
opts = append(opts, migrate.DisableChecksum())
}
return migrate.NewPlanner(nil, m.atlas.dir, opts...).WritePlan(plan)
}

Expand Down
5 changes: 4 additions & 1 deletion dialect/sql/schema/migrate_test.go
Expand Up @@ -123,7 +123,7 @@ func TestMigrate_Diff(t *testing.T) {
require.Contains(t, err.Error(), "WithGlobalUniqueID")
require.Contains(t, err.Error(), "WithDir")

m, err = NewMigrate(db, WithFormatter(f), WithDir(d), WithDeterministicGlobalUniqueID())
m, err = NewMigrate(db, WithFormatter(f), WithDir(d), WithDeterministicGlobalUniqueID(), WithSumFile())
require.NoError(t, err)
require.IsType(t, &dirTypeStore{}, m.typeStore)
require.NoError(t, m.Diff(context.Background(),
Expand Down Expand Up @@ -153,6 +153,9 @@ func TestMigrate_Diff(t *testing.T) {
fmt.Sprintf("INSERT INTO sqlite_sequence (name, seq) VALUES (\"pets\", %d);", 2<<32),
"INSERT INTO `ent_types` (`type`) VALUES ('pets');", "",
}, "\n"))

// Checksum will be updated as well.
require.NoError(t, migrate.Validate(d))
}

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

0 comments on commit 8eeb23c

Please sign in to comment.