doc/migrate: add info and examples for hooks (#1156)

This commit is contained in:
Ariel Mashraki
2021-01-08 22:45:27 +02:00
committed by GitHub
parent 8eda87dbc3
commit ab651255ee

View File

@@ -213,4 +213,46 @@ func main() {
log.Fatalf("failed creating schema resources: %v", err)
}
}
```
```
## Migration Hooks
The framework provides an option to add hooks (middlewares) to the migration phase.
This option is ideal for modifying or filtering the tables that the migration is working on,
or for creating custom resources in the database.
```go
package main
import (
"context"
"log"
"<project>/ent"
"<project>/ent/migrate"
"github.com/facebook/ent/dialect/sql/schema"
)
func main() {
client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
if err != nil {
log.Fatalf("failed connecting to mysql: %v", err)
}
defer client.Close()
ctx := context.Background()
// Run migration.
err = client.Schema.Create(
ctx,
schema.WithHooks(func(next schema.Creator) schema.Creator {
return schema.CreateFunc(func(ctx context.Context, tables ...*schema.Table) error {
// Run custom code here.
return next.Create(ctx, tables...)
})
}),
)
if err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
}
```