From 311d76067960a9bb1d66f54d03889fffdefde44a Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Mon, 20 Jun 2022 08:32:30 +0300 Subject: [PATCH] entc/gen: allow opening and testing concurrent enttest clients (#2665) --- dialect/sql/schema/migrate.go | 2 +- dialect/sql/schema/schema.go | 90 +++++++++++++++++++ dialect/sql/schema/schema_test.go | 48 ++++++++++ entc/gen/template/enttest.tmpl | 24 +++-- entc/gen/template/migrate/migrate.tmpl | 20 ++--- .../cascadelete/ent/enttest/enttest.go | 18 ++-- .../cascadelete/ent/migrate/migrate.go | 20 ++--- .../integration/config/ent/enttest/enttest.go | 18 ++-- .../integration/config/ent/migrate/migrate.go | 20 ++--- .../customid/ent/enttest/enttest.go | 18 ++-- .../customid/ent/migrate/migrate.go | 20 ++--- .../edgefield/ent/enttest/enttest.go | 18 ++-- .../edgefield/ent/migrate/migrate.go | 20 ++--- .../edgeschema/ent/enttest/enttest.go | 18 ++-- .../edgeschema/ent/migrate/migrate.go | 20 ++--- entc/integration/ent/client.go | 5 ++ entc/integration/ent/enttest/enttest.go | 18 ++-- entc/integration/ent/migrate/migrate.go | 20 ++--- entc/integration/ent/template/client.tmpl | 5 ++ entc/integration/gremlin/ent/client.go | 5 ++ entc/integration/hooks/ent/enttest/enttest.go | 18 ++-- entc/integration/hooks/ent/migrate/migrate.go | 20 ++--- .../integration/idtype/ent/enttest/enttest.go | 18 ++-- .../integration/idtype/ent/migrate/migrate.go | 20 ++--- entc/integration/integration_test.go | 20 ++++- entc/integration/json/ent/enttest/enttest.go | 18 ++-- entc/integration/json/ent/migrate/migrate.go | 20 ++--- .../migrate/entv1/enttest/enttest.go | 18 ++-- .../migrate/entv1/migrate/migrate.go | 20 ++--- .../migrate/entv2/enttest/enttest.go | 18 ++-- .../migrate/entv2/migrate/migrate.go | 20 ++--- .../migrate/versioned/enttest/enttest.go | 18 ++-- .../migrate/versioned/migrate/migrate.go | 20 ++--- .../multischema/ent/enttest/enttest.go | 18 ++-- .../multischema/ent/migrate/migrate.go | 20 ++--- .../privacy/ent/enttest/enttest.go | 18 ++-- .../privacy/ent/migrate/migrate.go | 20 ++--- .../template/ent/enttest/enttest.go | 18 ++-- .../template/ent/migrate/migrate.go | 20 ++--- examples/edgeindex/ent/enttest/enttest.go | 18 ++-- examples/edgeindex/ent/migrate/migrate.go | 20 ++--- examples/entcpkg/ent/enttest/enttest.go | 18 ++-- examples/entcpkg/ent/migrate/migrate.go | 20 ++--- examples/fs/ent/enttest/enttest.go | 18 ++-- examples/fs/ent/migrate/migrate.go | 20 ++--- examples/m2m2types/ent/enttest/enttest.go | 18 ++-- examples/m2m2types/ent/migrate/migrate.go | 20 ++--- examples/m2mbidi/ent/enttest/enttest.go | 18 ++-- examples/m2mbidi/ent/migrate/migrate.go | 20 ++--- examples/m2mrecur/ent/enttest/enttest.go | 18 ++-- examples/m2mrecur/ent/migrate/migrate.go | 20 ++--- examples/o2m2types/ent/enttest/enttest.go | 18 ++-- examples/o2m2types/ent/migrate/migrate.go | 20 ++--- examples/o2mrecur/ent/enttest/enttest.go | 18 ++-- examples/o2mrecur/ent/migrate/migrate.go | 20 ++--- examples/o2o2types/ent/enttest/enttest.go | 18 ++-- examples/o2o2types/ent/migrate/migrate.go | 20 ++--- examples/o2obidi/ent/enttest/enttest.go | 18 ++-- examples/o2obidi/ent/migrate/migrate.go | 20 ++--- examples/o2orecur/ent/enttest/enttest.go | 18 ++-- examples/o2orecur/ent/migrate/migrate.go | 20 ++--- examples/privacyadmin/ent/enttest/enttest.go | 18 ++-- examples/privacyadmin/ent/migrate/migrate.go | 20 ++--- examples/privacytenant/ent/enttest/enttest.go | 18 ++-- examples/privacytenant/ent/migrate/migrate.go | 20 ++--- examples/start/ent/enttest/enttest.go | 18 ++-- examples/start/ent/migrate/migrate.go | 20 ++--- examples/traversal/ent/enttest/enttest.go | 18 ++-- examples/traversal/ent/migrate/migrate.go | 20 ++--- examples/version/ent/enttest/enttest.go | 18 ++-- examples/version/ent/migrate/migrate.go | 20 ++--- 71 files changed, 783 insertions(+), 614 deletions(-) diff --git a/dialect/sql/schema/migrate.go b/dialect/sql/schema/migrate.go index 20447ea0e..886b652b4 100644 --- a/dialect/sql/schema/migrate.go +++ b/dialect/sql/schema/migrate.go @@ -104,7 +104,7 @@ func (f CreateFunc) Create(ctx context.Context, tables ...*Table) error { return f(ctx, tables...) } -// Migrate runs the migrations logic for the SQL dialects. +// Migrate runs the migration logic for the SQL dialects. type Migrate struct { sqlDialect universalID bool // global unique ids. diff --git a/dialect/sql/schema/schema.go b/dialect/sql/schema/schema.go index 10718b223..c0de6ba8d 100644 --- a/dialect/sql/schema/schema.go +++ b/dialect/sql/schema/schema.go @@ -173,6 +173,96 @@ func (t *Table) fk(symbol string) (*ForeignKey, bool) { return nil, false } +// CopyTables returns a deep-copy of the given tables. This utility function is +// useful for copying the generated schema tables (i.e. migrate.Tables) before +// running schema migration when there is a need for execute multiple migrations +// concurrently. e.g. running parallel unit-tests using the generated enttest package. +func CopyTables(tables []*Table) ([]*Table, error) { + var ( + copyT = make([]*Table, len(tables)) + byName = make(map[string]*Table) + ) + for i, t := range tables { + copyT[i] = &Table{ + Name: t.Name, + Columns: make([]*Column, len(t.Columns)), + Indexes: make([]*Index, len(t.Indexes)), + ForeignKeys: make([]*ForeignKey, len(t.ForeignKeys)), + } + for j, c := range t.Columns { + cc := *c + // SchemaType and Enums are read-only fields. + cc.indexes = nil + cc.foreign = nil + copyT[i].Columns[j] = &cc + } + if at := t.Annotation; at != nil { + cat := *at + copyT[i].Annotation = &cat + } + byName[t.Name] = copyT[i] + } + for i, t := range tables { + ct := copyT[i] + for _, c := range t.PrimaryKey { + cc, ok := ct.column(c.Name) + if !ok { + return nil, fmt.Errorf("sql/schema: missing primary key column %q", c.Name) + } + ct.PrimaryKey = append(ct.PrimaryKey, cc) + } + for j, idx := range t.Indexes { + cidx := &Index{ + Name: idx.Name, + Unique: idx.Unique, + Columns: make([]*Column, len(idx.Columns)), + } + if at := idx.Annotation; at != nil { + cat := *at + cidx.Annotation = &cat + } + for k, c := range idx.Columns { + cc, ok := ct.column(c.Name) + if !ok { + return nil, fmt.Errorf("sql/schema: missing index column %q", c.Name) + } + cidx.Columns[k] = cc + } + ct.Indexes[j] = cidx + } + for j, fk := range t.ForeignKeys { + cfk := &ForeignKey{ + Symbol: fk.Symbol, + OnUpdate: fk.OnUpdate, + OnDelete: fk.OnDelete, + Columns: make([]*Column, len(fk.Columns)), + RefColumns: make([]*Column, len(fk.RefColumns)), + } + for k, c := range fk.Columns { + cc, ok := ct.column(c.Name) + if !ok { + return nil, fmt.Errorf("sql/schema: missing foreign-key column %q", c.Name) + } + cfk.Columns[k] = cc + } + cref, ok := byName[fk.RefTable.Name] + if !ok { + return nil, fmt.Errorf("sql/schema: missing foreign-key ref-table %q", fk.RefTable.Name) + } + cfk.RefTable = cref + for k, c := range fk.RefColumns { + cc, ok := cref.column(c.Name) + if !ok { + return nil, fmt.Errorf("sql/schema: missing foreign-key ref-column %q", c.Name) + } + cfk.RefColumns[k] = cc + } + ct.ForeignKeys[j] = cfk + } + } + return copyT, nil +} + // Column schema definition for SQL dialects. type Column struct { Name string // column name. diff --git a/dialect/sql/schema/schema_test.go b/dialect/sql/schema/schema_test.go index 5527ba583..cec63af6c 100644 --- a/dialect/sql/schema/schema_test.go +++ b/dialect/sql/schema/schema_test.go @@ -7,6 +7,7 @@ package schema import ( "testing" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/field" "github.com/stretchr/testify/require" @@ -101,3 +102,50 @@ func TestColumn_ScanDefault(t *testing.T) { require.NoError(t, c1.ScanDefault("00000000-0000-0000-0000-000000000000")) require.Equal(t, "00000000-0000-0000-0000-000000000000", c1.Default) } + +func TestCopyTables(t *testing.T) { + users := &Table{ + Name: "users", + Columns: []*Column{ + {Name: "id", Type: field.TypeInt}, + {Name: "name", Type: field.TypeString}, + {Name: "spouse_id", Type: field.TypeInt}, + }, + } + users.PrimaryKey = users.Columns[:1] + users.Indexes = append(users.Indexes, &Index{ + Name: "name", + Columns: users.Columns[1:2], + }) + users.AddForeignKey(&ForeignKey{ + Columns: users.Columns[2:], + RefTable: users, + RefColumns: users.Columns[:1], + OnUpdate: SetNull, + }) + users.SetAnnotation(&entsql.Annotation{Table: "Users"}) + pets := &Table{ + Name: "pets", + Columns: []*Column{ + {Name: "id", Type: field.TypeInt}, + {Name: "name", Type: field.TypeString}, + {Name: "owner_id", Type: field.TypeInt}, + }, + } + pets.Indexes = append(pets.Indexes, &Index{ + Name: "name", + Unique: true, + Columns: pets.Columns[1:2], + Annotation: entsql.Desc(), + }) + pets.AddForeignKey(&ForeignKey{ + Columns: pets.Columns[2:], + RefTable: users, + RefColumns: users.Columns[:1], + OnDelete: SetDefault, + }) + tables := []*Table{users, pets} + copyT, err := CopyTables(tables) + require.NoError(t, err) + require.Equal(t, tables, copyT) +} diff --git a/entc/gen/template/enttest.tmpl b/entc/gen/template/enttest.tmpl index 5adb6ef84..f597a5ea9 100644 --- a/entc/gen/template/enttest.tmpl +++ b/entc/gen/template/enttest.tmpl @@ -22,6 +22,7 @@ import ( _ "{{ $.Config.Package }}/runtime" {{ if $.SupportMigrate }} + "{{ $.Config.Package }}/migrate" "entgo.io/ent/dialect/sql/schema" {{ end }} ) @@ -78,10 +79,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *{{ $pk t.FailNow() } {{- if $.SupportMigrate }} - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) {{- end }} return c } @@ -91,12 +89,22 @@ func NewClient(t TestingT, opts ...Option) *{{ $pkg }}.Client { o := newOptions(opts) c := {{ $pkg }}.NewClient(o.opts...) {{- if $.SupportMigrate }} - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) {{- end }} return c } +{{- if $.SupportMigrate }} + func migrateSchema(t TestingT, c *{{ $pkg }}.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { + t.Error(err) + t.FailNow() + } + } +{{- end }} {{ end }} diff --git a/entc/gen/template/migrate/migrate.tmpl b/entc/gen/template/migrate/migrate.tmpl index d608e92f2..226790332 100644 --- a/entc/gen/template/migrate/migrate.tmpl +++ b/entc/gen/template/migrate/migrate.tmpl @@ -37,9 +37,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -54,11 +51,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } {{ if $.Config.FeatureEnabled "sql/versioned-migration" }}{{ template "migrate/diff" $ }}{{ end }} @@ -70,14 +72,6 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv,}}, Tables, opts...) } {{ end }} diff --git a/entc/integration/cascadelete/ent/enttest/enttest.go b/entc/integration/cascadelete/ent/enttest/enttest.go index cc09e6905..c7fde29df 100644 --- a/entc/integration/cascadelete/ent/enttest/enttest.go +++ b/entc/integration/cascadelete/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/cascadelete/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/cascadelete/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/cascadelete/ent/migrate/migrate.go b/entc/integration/cascadelete/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/cascadelete/ent/migrate/migrate.go +++ b/entc/integration/cascadelete/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/config/ent/enttest/enttest.go b/entc/integration/config/ent/enttest/enttest.go index 9443c3a5f..b7359b782 100644 --- a/entc/integration/config/ent/enttest/enttest.go +++ b/entc/integration/config/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/config/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/config/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/config/ent/migrate/migrate.go b/entc/integration/config/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/config/ent/migrate/migrate.go +++ b/entc/integration/config/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/customid/ent/enttest/enttest.go b/entc/integration/customid/ent/enttest/enttest.go index e18a56b39..d61613664 100644 --- a/entc/integration/customid/ent/enttest/enttest.go +++ b/entc/integration/customid/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/customid/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/customid/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/customid/ent/migrate/migrate.go b/entc/integration/customid/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/customid/ent/migrate/migrate.go +++ b/entc/integration/customid/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/edgefield/ent/enttest/enttest.go b/entc/integration/edgefield/ent/enttest/enttest.go index 78e509ba4..5acaa6757 100644 --- a/entc/integration/edgefield/ent/enttest/enttest.go +++ b/entc/integration/edgefield/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/edgefield/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/edgefield/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/edgefield/ent/migrate/migrate.go b/entc/integration/edgefield/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/edgefield/ent/migrate/migrate.go +++ b/entc/integration/edgefield/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/edgeschema/ent/enttest/enttest.go b/entc/integration/edgeschema/ent/enttest/enttest.go index 2a125dbcf..7c922a645 100644 --- a/entc/integration/edgeschema/ent/enttest/enttest.go +++ b/entc/integration/edgeschema/ent/enttest/enttest.go @@ -10,6 +10,7 @@ import ( _ "entgo.io/ent/entc/integration/edgeschema/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/edgeschema/ent/migrate" ) type ( @@ -59,10 +60,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -70,9 +68,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/edgeschema/ent/migrate/migrate.go b/entc/integration/edgeschema/ent/migrate/migrate.go index cadb9bc89..6bccf3918 100644 --- a/entc/integration/edgeschema/ent/migrate/migrate.go +++ b/entc/integration/edgeschema/ent/migrate/migrate.go @@ -28,9 +28,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -45,11 +42,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -59,13 +61,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/ent/client.go b/entc/integration/ent/client.go index e41168b66..a20d1171d 100644 --- a/entc/integration/ent/client.go +++ b/entc/integration/ent/client.go @@ -224,6 +224,11 @@ func (c *Client) Dialect() string { return c.driver.Dialect() } +// Driver returns the underlying driver. +func (c *Client) Driver() dialect.Driver { + return c.driver +} + // CardClient is a client for the Card schema. type CardClient struct { config diff --git a/entc/integration/ent/enttest/enttest.go b/entc/integration/ent/enttest/enttest.go index 6873aa426..c94f3f17d 100644 --- a/entc/integration/ent/enttest/enttest.go +++ b/entc/integration/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/ent/migrate/migrate.go b/entc/integration/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/ent/migrate/migrate.go +++ b/entc/integration/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/ent/template/client.tmpl b/entc/integration/ent/template/client.tmpl index 72d5b200c..7739b2595 100644 --- a/entc/integration/ent/template/client.tmpl +++ b/entc/integration/ent/template/client.tmpl @@ -9,5 +9,10 @@ in the LICENSE file in the root directory of this source tree. func(c *Client) Dialect() string { return c.driver.Dialect() } + + // Driver returns the underlying driver. + func(c *Client) Driver() dialect.Driver { + return c.driver + } {{ end }} diff --git a/entc/integration/gremlin/ent/client.go b/entc/integration/gremlin/ent/client.go index 9654ef1c0..44780be69 100644 --- a/entc/integration/gremlin/ent/client.go +++ b/entc/integration/gremlin/ent/client.go @@ -197,6 +197,11 @@ func (c *Client) Dialect() string { return c.driver.Dialect() } +// Driver returns the underlying driver. +func (c *Client) Driver() dialect.Driver { + return c.driver +} + // CardClient is a client for the Card schema. type CardClient struct { config diff --git a/entc/integration/hooks/ent/enttest/enttest.go b/entc/integration/hooks/ent/enttest/enttest.go index f6c2998f1..554db4c91 100644 --- a/entc/integration/hooks/ent/enttest/enttest.go +++ b/entc/integration/hooks/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/hooks/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/hooks/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/hooks/ent/migrate/migrate.go b/entc/integration/hooks/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/hooks/ent/migrate/migrate.go +++ b/entc/integration/hooks/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/idtype/ent/enttest/enttest.go b/entc/integration/idtype/ent/enttest/enttest.go index 4c3323800..c2ba6a6cc 100644 --- a/entc/integration/idtype/ent/enttest/enttest.go +++ b/entc/integration/idtype/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/idtype/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/idtype/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/idtype/ent/migrate/migrate.go b/entc/integration/idtype/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/idtype/ent/migrate/migrate.go +++ b/entc/integration/idtype/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index 88529fc3c..67cad5f45 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -48,6 +48,7 @@ import ( ) func TestSQLite(t *testing.T) { + t.Parallel() client := enttest.Open(t, dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1", opts) defer client.Close() for _, tt := range tests { @@ -63,6 +64,7 @@ func TestMySQL(t *testing.T) { for version, port := range map[string]int{"56": 3306, "57": 3307, "8": 3308} { addr := net.JoinHostPort("localhost", strconv.Itoa(port)) t.Run(version, func(t *testing.T) { + t.Parallel() client := enttest.Open(t, dialect.MySQL, fmt.Sprintf("root:pass@tcp(%s)/test?parseTime=True", addr), opts) defer client.Close() for _, tt := range tests { @@ -78,8 +80,9 @@ func TestMySQL(t *testing.T) { func TestMaria(t *testing.T) { for version, port := range map[string]int{"10.5": 4306, "10.2": 4307, "10.3": 4308} { + addr := net.JoinHostPort("localhost", strconv.Itoa(port)) t.Run(version, func(t *testing.T) { - addr := net.JoinHostPort("localhost", strconv.Itoa(port)) + t.Parallel() client := enttest.Open(t, dialect.MySQL, fmt.Sprintf("root:pass@tcp(%s)/test?parseTime=True", addr), opts) defer client.Close() for _, tt := range tests { @@ -95,8 +98,10 @@ func TestMaria(t *testing.T) { func TestPostgres(t *testing.T) { for version, port := range map[string]int{"10": 5430, "11": 5431, "12": 5432, "13": 5433, "14": 5434} { + addr := fmt.Sprintf("host=localhost port=%d user=postgres dbname=test password=pass sslmode=disable", port) t.Run(version, func(t *testing.T) { - client := enttest.Open(t, dialect.Postgres, fmt.Sprintf("host=localhost port=%d user=postgres dbname=test password=pass sslmode=disable", port), opts) + t.Parallel() + client := enttest.Open(t, dialect.Postgres, addr, opts) defer client.Close() for _, tt := range tests { name := runtime.FuncForPC(reflect.ValueOf(tt).Pointer()).Name() @@ -1859,7 +1864,16 @@ func NoSchemaChanges(t *testing.T, client *ent.Client) { } return len(p), nil }) - err := client.Schema.WriteTo(context.Background(), w, migrate.WithDropIndex(true), migrate.WithDropColumn(true), sqlschema.WithAtlas(true)) + tables, err := sqlschema.CopyTables(migrate.Tables) + require.NoError(t, err) + err = migrate.Create( + context.Background(), + migrate.NewSchema(&sqlschema.WriteDriver{Writer: w, Driver: client.Driver()}), + tables, + migrate.WithDropIndex(true), + migrate.WithDropColumn(true), + sqlschema.WithAtlas(true), + ) require.NoError(t, err) } diff --git a/entc/integration/json/ent/enttest/enttest.go b/entc/integration/json/ent/enttest/enttest.go index dbc0533b0..52d333fa1 100644 --- a/entc/integration/json/ent/enttest/enttest.go +++ b/entc/integration/json/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/json/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/json/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/json/ent/migrate/migrate.go b/entc/integration/json/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/json/ent/migrate/migrate.go +++ b/entc/integration/json/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/migrate/entv1/enttest/enttest.go b/entc/integration/migrate/entv1/enttest/enttest.go index 83eda729c..141451638 100644 --- a/entc/integration/migrate/entv1/enttest/enttest.go +++ b/entc/integration/migrate/entv1/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/migrate/entv1/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/migrate/entv1/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *entv1. t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *entv1. func NewClient(t TestingT, opts ...Option) *entv1.Client { o := newOptions(opts) c := entv1.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *entv1.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/migrate/entv1/migrate/migrate.go b/entc/integration/migrate/entv1/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/migrate/entv1/migrate/migrate.go +++ b/entc/integration/migrate/entv1/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/migrate/entv2/enttest/enttest.go b/entc/integration/migrate/entv2/enttest/enttest.go index 1760260e9..dfb1cfdd6 100644 --- a/entc/integration/migrate/entv2/enttest/enttest.go +++ b/entc/integration/migrate/entv2/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/migrate/entv2/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/migrate/entv2/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *entv2. t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *entv2. func NewClient(t TestingT, opts ...Option) *entv2.Client { o := newOptions(opts) c := entv2.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *entv2.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/migrate/entv2/migrate/migrate.go b/entc/integration/migrate/entv2/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/migrate/entv2/migrate/migrate.go +++ b/entc/integration/migrate/entv2/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/migrate/versioned/enttest/enttest.go b/entc/integration/migrate/versioned/enttest/enttest.go index 8fac8b91b..eef9cfd01 100644 --- a/entc/integration/migrate/versioned/enttest/enttest.go +++ b/entc/integration/migrate/versioned/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/migrate/versioned/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/migrate/versioned/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *versio t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *versio func NewClient(t TestingT, opts ...Option) *versioned.Client { o := newOptions(opts) c := versioned.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *versioned.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/migrate/versioned/migrate/migrate.go b/entc/integration/migrate/versioned/migrate/migrate.go index 5b57e23f4..811fe1fc2 100644 --- a/entc/integration/migrate/versioned/migrate/migrate.go +++ b/entc/integration/migrate/versioned/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // Diff creates a migration file containing the statements to resolve the diff @@ -83,13 +85,5 @@ func (s *Schema) NamedDiff(ctx context.Context, name string, opts ...schema.Migr // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/multischema/ent/enttest/enttest.go b/entc/integration/multischema/ent/enttest/enttest.go index ec84d97f7..c78feef5c 100644 --- a/entc/integration/multischema/ent/enttest/enttest.go +++ b/entc/integration/multischema/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/multischema/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/multischema/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/multischema/ent/migrate/migrate.go b/entc/integration/multischema/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/multischema/ent/migrate/migrate.go +++ b/entc/integration/multischema/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/privacy/ent/enttest/enttest.go b/entc/integration/privacy/ent/enttest/enttest.go index e647f021b..18324018e 100644 --- a/entc/integration/privacy/ent/enttest/enttest.go +++ b/entc/integration/privacy/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/privacy/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/privacy/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/privacy/ent/migrate/migrate.go b/entc/integration/privacy/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/privacy/ent/migrate/migrate.go +++ b/entc/integration/privacy/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/entc/integration/template/ent/enttest/enttest.go b/entc/integration/template/ent/enttest/enttest.go index 19cc996d7..18c135f63 100644 --- a/entc/integration/template/ent/enttest/enttest.go +++ b/entc/integration/template/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/entc/integration/template/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/entc/integration/template/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/entc/integration/template/ent/migrate/migrate.go b/entc/integration/template/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/entc/integration/template/ent/migrate/migrate.go +++ b/entc/integration/template/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/edgeindex/ent/enttest/enttest.go b/examples/edgeindex/ent/enttest/enttest.go index 5bb6b55d8..fe9e04639 100644 --- a/examples/edgeindex/ent/enttest/enttest.go +++ b/examples/edgeindex/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/edgeindex/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/edgeindex/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/edgeindex/ent/migrate/migrate.go b/examples/edgeindex/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/edgeindex/ent/migrate/migrate.go +++ b/examples/edgeindex/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/entcpkg/ent/enttest/enttest.go b/examples/entcpkg/ent/enttest/enttest.go index d02636c0a..d478628ae 100644 --- a/examples/entcpkg/ent/enttest/enttest.go +++ b/examples/entcpkg/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/entcpkg/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/entcpkg/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/entcpkg/ent/migrate/migrate.go b/examples/entcpkg/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/entcpkg/ent/migrate/migrate.go +++ b/examples/entcpkg/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/fs/ent/enttest/enttest.go b/examples/fs/ent/enttest/enttest.go index fa975a43f..37ccc330b 100644 --- a/examples/fs/ent/enttest/enttest.go +++ b/examples/fs/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/fs/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/fs/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/fs/ent/migrate/migrate.go b/examples/fs/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/fs/ent/migrate/migrate.go +++ b/examples/fs/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/m2m2types/ent/enttest/enttest.go b/examples/m2m2types/ent/enttest/enttest.go index 1131a93be..b16a52bd3 100644 --- a/examples/m2m2types/ent/enttest/enttest.go +++ b/examples/m2m2types/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/m2m2types/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/m2m2types/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/m2m2types/ent/migrate/migrate.go b/examples/m2m2types/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/m2m2types/ent/migrate/migrate.go +++ b/examples/m2m2types/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/m2mbidi/ent/enttest/enttest.go b/examples/m2mbidi/ent/enttest/enttest.go index 7b9251da8..ac8f3177e 100644 --- a/examples/m2mbidi/ent/enttest/enttest.go +++ b/examples/m2mbidi/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/m2mbidi/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/m2mbidi/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/m2mbidi/ent/migrate/migrate.go b/examples/m2mbidi/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/m2mbidi/ent/migrate/migrate.go +++ b/examples/m2mbidi/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/m2mrecur/ent/enttest/enttest.go b/examples/m2mrecur/ent/enttest/enttest.go index 0f01df96a..804864188 100644 --- a/examples/m2mrecur/ent/enttest/enttest.go +++ b/examples/m2mrecur/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/m2mrecur/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/m2mrecur/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/m2mrecur/ent/migrate/migrate.go b/examples/m2mrecur/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/m2mrecur/ent/migrate/migrate.go +++ b/examples/m2mrecur/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/o2m2types/ent/enttest/enttest.go b/examples/o2m2types/ent/enttest/enttest.go index 333da0e7c..0fcf1835b 100644 --- a/examples/o2m2types/ent/enttest/enttest.go +++ b/examples/o2m2types/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/o2m2types/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/o2m2types/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/o2m2types/ent/migrate/migrate.go b/examples/o2m2types/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/o2m2types/ent/migrate/migrate.go +++ b/examples/o2m2types/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/o2mrecur/ent/enttest/enttest.go b/examples/o2mrecur/ent/enttest/enttest.go index 9012d0601..b2fc2f5da 100644 --- a/examples/o2mrecur/ent/enttest/enttest.go +++ b/examples/o2mrecur/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/o2mrecur/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/o2mrecur/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/o2mrecur/ent/migrate/migrate.go b/examples/o2mrecur/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/o2mrecur/ent/migrate/migrate.go +++ b/examples/o2mrecur/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/o2o2types/ent/enttest/enttest.go b/examples/o2o2types/ent/enttest/enttest.go index ab8ba90df..4966d982b 100644 --- a/examples/o2o2types/ent/enttest/enttest.go +++ b/examples/o2o2types/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/o2o2types/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/o2o2types/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/o2o2types/ent/migrate/migrate.go b/examples/o2o2types/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/o2o2types/ent/migrate/migrate.go +++ b/examples/o2o2types/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/o2obidi/ent/enttest/enttest.go b/examples/o2obidi/ent/enttest/enttest.go index 56bbd1225..c9ae98c27 100644 --- a/examples/o2obidi/ent/enttest/enttest.go +++ b/examples/o2obidi/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/o2obidi/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/o2obidi/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/o2obidi/ent/migrate/migrate.go b/examples/o2obidi/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/o2obidi/ent/migrate/migrate.go +++ b/examples/o2obidi/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/o2orecur/ent/enttest/enttest.go b/examples/o2orecur/ent/enttest/enttest.go index c9467d004..ff41e74d1 100644 --- a/examples/o2orecur/ent/enttest/enttest.go +++ b/examples/o2orecur/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/o2orecur/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/o2orecur/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/o2orecur/ent/migrate/migrate.go b/examples/o2orecur/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/o2orecur/ent/migrate/migrate.go +++ b/examples/o2orecur/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/privacyadmin/ent/enttest/enttest.go b/examples/privacyadmin/ent/enttest/enttest.go index e8786c765..2a564f407 100644 --- a/examples/privacyadmin/ent/enttest/enttest.go +++ b/examples/privacyadmin/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/privacyadmin/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/privacyadmin/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/privacyadmin/ent/migrate/migrate.go b/examples/privacyadmin/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/privacyadmin/ent/migrate/migrate.go +++ b/examples/privacyadmin/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/privacytenant/ent/enttest/enttest.go b/examples/privacytenant/ent/enttest/enttest.go index 49e5ad5cf..18b2b93b2 100644 --- a/examples/privacytenant/ent/enttest/enttest.go +++ b/examples/privacytenant/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/privacytenant/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/privacytenant/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/privacytenant/ent/migrate/migrate.go b/examples/privacytenant/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/privacytenant/ent/migrate/migrate.go +++ b/examples/privacytenant/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/start/ent/enttest/enttest.go b/examples/start/ent/enttest/enttest.go index 44ebc49c9..a22fd4c58 100644 --- a/examples/start/ent/enttest/enttest.go +++ b/examples/start/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/start/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/start/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/start/ent/migrate/migrate.go b/examples/start/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/start/ent/migrate/migrate.go +++ b/examples/start/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/traversal/ent/enttest/enttest.go b/examples/traversal/ent/enttest/enttest.go index 4867ddbcc..44fad0ee1 100644 --- a/examples/traversal/ent/enttest/enttest.go +++ b/examples/traversal/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/traversal/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/traversal/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/traversal/ent/migrate/migrate.go b/examples/traversal/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/traversal/ent/migrate/migrate.go +++ b/examples/traversal/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) } diff --git a/examples/version/ent/enttest/enttest.go b/examples/version/ent/enttest/enttest.go index 1df1cfb0a..69881c9a1 100644 --- a/examples/version/ent/enttest/enttest.go +++ b/examples/version/ent/enttest/enttest.go @@ -14,6 +14,7 @@ import ( _ "entgo.io/ent/examples/version/ent/runtime" "entgo.io/ent/dialect/sql/schema" + "entgo.io/ent/examples/version/ent/migrate" ) type ( @@ -63,10 +64,7 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl t.Error(err) t.FailNow() } - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { - t.Error(err) - t.FailNow() - } + migrateSchema(t, c, o) return c } @@ -74,9 +72,17 @@ func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Cl func NewClient(t TestingT, opts ...Option) *ent.Client { o := newOptions(opts) c := ent.NewClient(o.opts...) - if err := c.Schema.Create(context.Background(), o.migrateOpts...); err != nil { + migrateSchema(t, c, o) + return c +} +func migrateSchema(t TestingT, c *ent.Client, o *options) { + tables, err := schema.CopyTables(migrate.Tables) + if err != nil { + t.Error(err) + t.FailNow() + } + if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil { t.Error(err) t.FailNow() } - return c } diff --git a/examples/version/ent/migrate/migrate.go b/examples/version/ent/migrate/migrate.go index 6921c1cc8..2a78c5d78 100644 --- a/examples/version/ent/migrate/migrate.go +++ b/examples/version/ent/migrate/migrate.go @@ -32,9 +32,6 @@ var ( // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex - // WithFixture sets the foreign-key renaming option to the migration when upgrading - // ent from v0.1.0 (issue-#285). Defaults to false. - WithFixture = schema.WithFixture // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys ) @@ -49,11 +46,16 @@ func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} } // Create creates all schema resources. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error { + return Create(ctx, s, Tables, opts...) +} + +// Create creates all table resources using the given schema driver. +func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error { migrate, err := schema.NewMigrate(s.drv, opts...) if err != nil { return fmt.Errorf("ent/migrate: %w", err) } - return migrate.Create(ctx, Tables...) + return migrate.Create(ctx, tables...) } // WriteTo writes the schema changes to w instead of running them against the database. @@ -63,13 +65,5 @@ func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error // } // func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error { - drv := &schema.WriteDriver{ - Writer: w, - Driver: s.drv, - } - migrate, err := schema.NewMigrate(drv, opts...) - if err != nil { - return fmt.Errorf("ent/migrate: %w", err) - } - return migrate.Create(ctx, Tables...) + return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...) }