dialect/sql/schema: support passing schema diff options (#3516)

This commit is contained in:
Ariel Mashraki
2023-05-03 11:28:10 +03:00
committed by GitHub
parent a792f429a6
commit f251400818
8 changed files with 33 additions and 19 deletions

View File

@@ -40,12 +40,13 @@ type Atlas struct {
dropIndexes bool // drop deleted indexes
withForeignKeys bool // with foreign keys
mode Mode
hooks []Hook // hooks to apply before creation
diffHooks []DiffHook // diff hooks to run when diffing current and desired
applyHook []ApplyHook // apply hooks to run when applying the plan
skip ChangeKind // what changes to skip and not apply
dir migrate.Dir // the migration directory to read from
fmt migrate.Formatter // how to format the plan into migration files
hooks []Hook // hooks to apply before creation
diffHooks []DiffHook // diff hooks to run when diffing current and desired
diffOptions []schema.DiffOption // diff options to pass to the diff engine
applyHook []ApplyHook // apply hooks to run when applying the plan
skip ChangeKind // what changes to skip and not apply
dir migrate.Dir // the migration directory to read from
fmt migrate.Formatter // how to format the plan into migration files
driver dialect.Driver // driver passed in when not using an atlas URL
url *url.URL // url of database connection
@@ -304,6 +305,13 @@ func WithDiffHook(hooks ...DiffHook) MigrateOption {
}
}
// WithDiffOptions adds a list of options to pass to the diff engine.
func WithDiffOptions(opts ...schema.DiffOption) MigrateOption {
return func(a *Atlas) {
a.diffOptions = append(a.diffOptions, opts...)
}
}
// WithSkipChanges allows skipping/filtering list of changes
// returned by the Differ before executing migration planning.
//
@@ -791,7 +799,7 @@ func (a *Atlas) planReplay(ctx context.Context, name string, tables []*Table) (*
}
func (a *Atlas) diff(ctx context.Context, name string, current, desired *schema.Schema, newTypes []string, opts ...migrate.PlanOption) (*migrate.Plan, error) {
changes, err := (&diffDriver{a.atDriver, a.diffHooks}).SchemaDiff(current, desired)
changes, err := (&diffDriver{a.atDriver, a.diffHooks}).SchemaDiff(current, desired, a.diffOptions...)
if err != nil {
return nil, err
}
@@ -1147,14 +1155,14 @@ type diffDriver struct {
// RealmDiff creates the diff between two realms. Since Ent does not care about Realms,
// not even schema changes, calling this method raises an error.
func (r *diffDriver) RealmDiff(_, _ *schema.Realm) ([]schema.Change, error) {
func (r *diffDriver) RealmDiff(_, _ *schema.Realm, _ ...schema.DiffOption) ([]schema.Change, error) {
return nil, errors.New("sqlDialect does not support working with realms")
}
// SchemaDiff creates the diff between two schemas, but includes "diff hooks".
func (r *diffDriver) SchemaDiff(from, to *schema.Schema) ([]schema.Change, error) {
func (r *diffDriver) SchemaDiff(from, to *schema.Schema, opts ...schema.DiffOption) ([]schema.Change, error) {
var d Differ = DiffFunc(func(current, desired *schema.Schema) ([]schema.Change, error) {
return r.Driver.SchemaDiff(current, desired)
return r.Driver.SchemaDiff(current, desired, opts...)
})
for i := len(r.hooks) - 1; i >= 0; i-- {
d = r.hooks[i](d)

View File

@@ -292,6 +292,12 @@ func TestMigrate_Diff(t *testing.T) {
}, "\n")
requireFileEqual(t, filepath.Join(p, "changes.sql"), changesSQL)
// Skipping table creation should write only the ent_type insertion.
m, err = NewMigrate(db, WithFormatter(f), WithDir(d), WithGlobalUniqueID(true), WithDiffOptions(schema.DiffSkipChanges(&schema.AddTable{})))
require.NoError(t, err)
require.NoError(t, m.Diff(ctx, tables...))
requireFileEqual(t, filepath.Join(p, "changes.sql"), "INSERT INTO `ent_types` (`type`) VALUES ('groups'), ('users');\n")
// Enable indentations.
m, err = NewMigrate(db, WithFormatter(f), WithDir(d), WithGlobalUniqueID(true), WithIndent(" "))
require.NoError(t, err)