Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialect/sql/schema: add method to create a named versioned migration … #2385

Merged
merged 5 commits into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions dialect/sql/schema/atlas.go
Expand Up @@ -308,7 +308,7 @@ func (m *Migrate) atCreate(ctx context.Context, tables ...*Table) error {
return err
}
}
plan, err := m.atDiff(ctx, tx, tables...)
plan, err := m.atDiff(ctx, tx, "", tables...)
if err != nil {
return err
}
Expand All @@ -334,7 +334,7 @@ func (m *Migrate) atCreate(ctx context.Context, tables ...*Table) error {
return tx.Commit()
}

func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, tables ...*Table) (*migrate.Plan, error) {
func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, name string, tables ...*Table) (*migrate.Plan, error) {
drv, err := m.atOpen(conn)
if err != nil {
return nil, err
Expand Down Expand Up @@ -364,7 +364,7 @@ func (m *Migrate) atDiff(ctx context.Context, conn dialect.ExecQuerier, tables .
return nil, err
}
// Plan changes.
return drv.PlanChanges(ctx, "changes", changes)
return drv.PlanChanges(ctx, name, changes)
}

type db struct{ dialect.ExecQuerier }
Expand Down
8 changes: 7 additions & 1 deletion dialect/sql/schema/migrate.go
Expand Up @@ -164,10 +164,16 @@ func (m *Migrate) Create(ctx context.Context, tables ...*Table) error {
// Diff compares the state read from the StateReader with the state defined by Ent.
// Changes will be written to migration files by the configures Planner.
func (m *Migrate) Diff(ctx context.Context, tables ...*Table) error {
return m.NamedDiff(ctx, "changes", tables...)
}

// NamedDiff compares the state read from the StateReader with the state defined by Ent.
// Changes will be written to migration files by the configures Planner.
func (m *Migrate) NamedDiff(ctx context.Context, name string, tables ...*Table) error {
if m.atlas.dir == nil {
return errors.New("no migration directory given")
}
plan, err := m.atDiff(ctx, m, tables...)
plan, err := m.atDiff(ctx, m, name, tables...)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions doc/md/versioned-migrations.md
Expand Up @@ -82,6 +82,8 @@ func main() {
}
// Write migration diff.
err = client.Schema.Diff(ctx, schema.WithDir(dir))
// You can use the following method to give the migration files a name.
// err = client.Schema.NamedDiff(ctx, "migration_name", schema.WithDir(dir))
if err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
Expand Down Expand Up @@ -137,6 +139,10 @@ func main() {
if err := m.Diff(context.Background(), tbls...); err != nil {
log.Fatalln(err)
}
// You can use the following method to give the migration files a name.
// if err := m.NamedDiff(context.Background(), "migration_name", tbls...); err != nil {
// log.Fatalln(err)
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix weird indentation

}
```

Expand Down
10 changes: 10 additions & 0 deletions entc/gen/template/dialect/sql/feature/migrate_diff.tmpl
Expand Up @@ -16,4 +16,14 @@ func (s *Schema) Diff(ctx context.Context, opts ...schema.MigrateOption) error {
}
return migrate.Diff(ctx, Tables...)
}

// NamedDiff creates a named migration file containing the statements to resolve the diff
// between the Ent schema and the connected database.
func (s *Schema) NamedDiff(ctx context.Context, name string, opts ...schema.MigrateOption) error {
migrate, err := schema.NewMigrate(s.drv, opts...)
if err != nil {
return fmt.Errorf("ent/migrate: %w", err)
}
return migrate.NamedDiff(ctx, name, Tables...)
}
{{ end }}
10 changes: 10 additions & 0 deletions entc/integration/migrate/versioned/migrate/migrate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.