migrate.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Code generated by entc, DO NOT EDIT.
  2. package migrate
  3. import (
  4. "context"
  5. "fmt"
  6. "io"
  7. "github.com/facebookincubator/ent/dialect"
  8. "github.com/facebookincubator/ent/dialect/sql/schema"
  9. )
  10. var (
  11. // WithGlobalUniqueID sets the universal ids options to the migration.
  12. // If this option is enabled, ent migration will allocate a 1<<32 range
  13. // for the ids of each entity (table).
  14. // Note that this option cannot be applied on tables that already exist.
  15. WithGlobalUniqueID = schema.WithGlobalUniqueID
  16. // WithDropColumn sets the drop column option to the migration.
  17. // If this option is enabled, ent migration will drop old columns
  18. // that were used for both fields and edges. This defaults to false.
  19. WithDropColumn = schema.WithDropColumn
  20. // WithDropIndex sets the drop index option to the migration.
  21. // If this option is enabled, ent migration will drop old indexes
  22. // that were defined in the schema. This defaults to false.
  23. // Note that unique constraints are defined using `UNIQUE INDEX`,
  24. // and therefore, it's recommended to enable this option to get more
  25. // flexibility in the schema changes.
  26. WithDropIndex = schema.WithDropIndex
  27. // WithFixture sets the foreign-key renaming option to the migration when upgrading
  28. // ent from v0.1.0 (issue-#285). Defaults to true.
  29. WithFixture = schema.WithFixture
  30. )
  31. // Schema is the API for creating, migrating and dropping a schema.
  32. type Schema struct {
  33. drv dialect.Driver
  34. universalID bool
  35. }
  36. // NewSchema creates a new schema client.
  37. func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
  38. // Create creates all schema resources.
  39. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
  40. migrate, err := schema.NewMigrate(s.drv, opts...)
  41. if err != nil {
  42. return fmt.Errorf("ent/migrate: %v", err)
  43. }
  44. return migrate.Create(ctx, Tables...)
  45. }
  46. // WriteTo writes the schema changes to w instead of running them against the database.
  47. //
  48. // if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {
  49. // log.Fatal(err)
  50. // }
  51. //
  52. func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error {
  53. drv := &schema.WriteDriver{
  54. Writer: w,
  55. Driver: s.drv,
  56. }
  57. migrate, err := schema.NewMigrate(drv, opts...)
  58. if err != nil {
  59. return fmt.Errorf("ent/migrate: %v", err)
  60. }
  61. return migrate.Create(ctx, Tables...)
  62. }