migrate.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Code generated by ent, DO NOT EDIT.
  2. package migrate
  3. import (
  4. "context"
  5. "fmt"
  6. "io"
  7. "entgo.io/ent/dialect"
  8. "entgo.io/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. // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
  28. WithForeignKeys = schema.WithForeignKeys
  29. )
  30. // Schema is the API for creating, migrating and dropping a schema.
  31. type Schema struct {
  32. drv dialect.Driver
  33. }
  34. // NewSchema creates a new schema client.
  35. func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
  36. // Create creates all schema resources.
  37. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
  38. return Create(ctx, s, Tables, opts...)
  39. }
  40. // Create creates all table resources using the given schema driver.
  41. func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error {
  42. migrate, err := schema.NewMigrate(s.drv, opts...)
  43. if err != nil {
  44. return fmt.Errorf("ent/migrate: %w", err)
  45. }
  46. return migrate.Create(ctx, tables...)
  47. }
  48. // WriteTo writes the schema changes to w instead of running them against the database.
  49. //
  50. // if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {
  51. // log.Fatal(err)
  52. // }
  53. func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error {
  54. return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...)
  55. }