From 5fb8c081f357091732f4d76f0bf95a8809fc397a Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Mon, 9 Sep 2019 01:18:52 -0700 Subject: [PATCH] ent/doc: add documentation and exmples to offline migrate Summary: https://pxl.cl/Knr4 Reviewed By: alexsn Differential Revision: D17253380 fbshipit-source-id: 517985f1b912c68e379da3412863b5d14927aedc --- doc/md/migrate.md | 71 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/doc/md/migrate.md b/doc/md/migrate.md index e7d6f6c01..27adff220 100755 --- a/doc/md/migrate.md +++ b/doc/md/migrate.md @@ -112,4 +112,73 @@ func main() { and store this information in a table named `ent_types`. For example, type `A` will have the range of `[1,4294967296)` for its IDs, and type `B` will have the range of `[4294967296,8589934592)`, etc. -Note that if this option is enabled, the maximum number of possible tables is **65535**. \ No newline at end of file +Note that if this option is enabled, the maximum number of possible tables is **65535**. + +## Offline Mode + +Offline mode allows you to write the schema changes to an `io.Writer` before executing them on the database. +It's useful for verifying the SQL commands before they're executed on the database, or to get an SQL script +to run manually. + +**Print changes** +```go +package main + +import ( + "context" + "log" + "os" + + "/ent" + "/ent/migrate" + + "github.com/facebookincubator/ent/dialect/sql" +) + +func main() { + db, err := sql.Open("mysql", "root:pass@tcp(localhost:3306)/test") + if err != nil { + log.Fatalf("failed connecting to mysql: %v", err) + } + ctx := context.Background() + client := ent.NewClient(ent.Driver(db)) + // Dump migration changes to stdout. + if err := client.Schema.WriteTo(ctx, os.Stdout); err != nil { + log.Fatalf("failed printing schema changes: %v", err) + } +} +``` + +**Write changes to file** +```go +package main + +import ( + "context" + "log" + "os" + + "/ent" + "/ent/migrate" + + "github.com/facebookincubator/ent/dialect/sql" +) + +func main() { + db, err := sql.Open("mysql", "root:pass@tcp(localhost:3306)/test") + if err != nil { + log.Fatalf("failed connecting to mysql: %v", err) + } + ctx := context.Background() + client := ent.NewClient(ent.Driver(db)) + // Dump migration changes to an SQL script. + f, err := os.Create("migrate.sql") + if err != nil { + log.Fatalf("create migrate file: %v", err) + } + defer f.Close() + if err := client.Schema.WriteTo(ctx, f); err != nil { + log.Fatalf("failed printing schema changes: %v", err) + } +} +``` \ No newline at end of file