migrate.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Code generated by entc, 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. // WithFixture sets the foreign-key renaming option to the migration when upgrading
  28. // ent from v0.1.0 (issue-#285). Defaults to false.
  29. WithFixture = schema.WithFixture
  30. // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
  31. WithForeignKeys = schema.WithForeignKeys
  32. )
  33. // Schema is the API for creating, migrating and dropping a schema.
  34. type Schema struct {
  35. drv dialect.Driver
  36. }
  37. // NewSchema creates a new schema client.
  38. func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
  39. // Create creates all schema resources.
  40. func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
  41. migrate, err := schema.NewMigrate(s.drv, opts...)
  42. if err != nil {
  43. return fmt.Errorf("ent/migrate: %w", err)
  44. }
  45. return migrate.Create(ctx, Tables...)
  46. }
  47. // WriteTo writes the schema changes to w instead of running them against the database.
  48. //
  49. // if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {
  50. // log.Fatal(err)
  51. // }
  52. //
  53. func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error {
  54. drv := &schema.WriteDriver{
  55. Writer: w,
  56. Driver: s.drv,
  57. }
  58. migrate, err := schema.NewMigrate(drv, opts...)
  59. if err != nil {
  60. return fmt.Errorf("ent/migrate: %w", err)
  61. }
  62. return migrate.Create(ctx, Tables...)
  63. }