mirror of
https://github.com/ent/ent.git
synced 2026-03-05 19:35:23 +03:00
dialect/sql/schema: support passing schema diff options (#3516)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -5,7 +5,7 @@ go 1.20
|
||||
replace entgo.io/ent => ../../
|
||||
|
||||
require (
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2
|
||||
entgo.io/ent v0.12.2-0.20230420123650-f6de6bb2e04c
|
||||
github.com/go-sql-driver/mysql v1.7.0
|
||||
github.com/google/uuid v1.3.0
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf h1:Tq2DRB39ZHScIwWACjPKLv5oEErv7zv6PBb5RTz5CKA=
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2 h1:IJmbXmfWIPECxMymEuWauNi6tunoG5jULGQAy/PVbQM=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
|
||||
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
|
||||
|
||||
@@ -5,7 +5,7 @@ go 1.20
|
||||
replace entgo.io/ent => ../
|
||||
|
||||
require (
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2
|
||||
entgo.io/ent v0.0.0-00010101000000-000000000000
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/mattn/go-sqlite3 v1.14.16
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf h1:Tq2DRB39ZHScIwWACjPKLv5oEErv7zv6PBb5RTz5CKA=
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2 h1:IJmbXmfWIPECxMymEuWauNi6tunoG5jULGQAy/PVbQM=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8=
|
||||
bazil.org/fuse v0.0.0-20200407214033-5883e5a4b512/go.mod h1:FbcW6z/2VytnFDhZfumh8Ss8zxHE6qpMP5sHTRe0EaM=
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module entgo.io/ent
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/go-openapi/inflect v0.19.0
|
||||
github.com/google/uuid v1.3.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf h1:Tq2DRB39ZHScIwWACjPKLv5oEErv7zv6PBb5RTz5CKA=
|
||||
ariga.io/atlas v0.10.2-0.20230427182402-87a07dfb83bf/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2 h1:IJmbXmfWIPECxMymEuWauNi6tunoG5jULGQAy/PVbQM=
|
||||
ariga.io/atlas v0.10.2-0.20230502203727-939293626ab2/go.mod h1:+TR129FJZ5Lvzms6dvCeGWh1yR6hMvmXBhug4hrNIGk=
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
|
||||
Reference in New Issue
Block a user