Documentation ¶
Index ¶
- Variables
- func Create(ctx context.Context, s *Schema, tables []*schema.Table, ...) error
- func Diff(ctx context.Context, url string, opts ...schema.MigrateOption) error
- func NamedDiff(ctx context.Context, url, name string, opts ...schema.MigrateOption) error
- type Schema
- func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error
- func (s *Schema) Diff(ctx context.Context, opts ...schema.MigrateOption) error
- func (s *Schema) NamedDiff(ctx context.Context, name string, opts ...schema.MigrateOption) error
- func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // WithGlobalUniqueID sets the universal ids options to the migration. // If this option is enabled, fluent migration will allocate a 1<<32 range // for the ids of each entity (table). // Note that this option cannot be applied on tables that already exist. WithGlobalUniqueID = schema.WithGlobalUniqueID // WithDropColumn sets the drop column option to the migration. // If this option is enabled, fluent migration will drop old columns // that were used for both fields and edges. This defaults to false. WithDropColumn = schema.WithDropColumn // WithDropIndex sets the drop index option to the migration. // If this option is enabled, fluent migration will drop old indexes // that were defined in the schema. This defaults to false. // Note that unique constraints are defined using `UNIQUE INDEX`, // and therefore, it's recommended to enable this option to get more // flexibility in the schema changes. WithDropIndex = schema.WithDropIndex // WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true. WithForeignKeys = schema.WithForeignKeys )
View Source
var ( // CardsColumns holds the columns for the "cards" table. CardsColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "type", Type: field.TypeString, Default: "unknown"}, {Name: "number_hash", Type: field.TypeString}, {Name: "cvv_hash", Type: field.TypeString}, {Name: "expires_at", Type: field.TypeTime, Nullable: true}, {Name: "owner_id", Type: field.TypeInt, Default: 0}, } // CardsTable holds the schema information for the "cards" table. CardsTable = &schema.Table{ Name: "cards", Columns: CardsColumns, PrimaryKey: []*schema.Column{CardsColumns[0]}, ForeignKeys: []*schema.ForeignKey{ { Symbol: "cards_users_cards", Columns: []*schema.Column{CardsColumns[5]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.NoAction, }, }, } // PaymentsColumns holds the columns for the "payments" table. PaymentsColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "amount", Type: field.TypeFloat64}, {Name: "currency", Type: field.TypeEnum, Enums: []string{"USD", "EUR", "VND", "ILS"}}, {Name: "time", Type: field.TypeTime}, {Name: "description", Type: field.TypeString}, {Name: "status", Type: field.TypeEnum, Enums: []string{"pending", "completed", "failed"}}, {Name: "card_id", Type: field.TypeInt}, } // PaymentsTable holds the schema information for the "payments" table. PaymentsTable = &schema.Table{ Name: "payments", Columns: PaymentsColumns, PrimaryKey: []*schema.Column{PaymentsColumns[0]}, ForeignKeys: []*schema.ForeignKey{ { Symbol: "payments_cards_payments", Columns: []*schema.Column{PaymentsColumns[6]}, RefColumns: []*schema.Column{CardsColumns[0]}, OnDelete: schema.NoAction, }, }, Indexes: []*schema.Index{ { Name: "payment_status_time", Unique: false, Columns: []*schema.Column{PaymentsColumns[5], PaymentsColumns[3]}, }, }, } // PetsColumns holds the columns for the "pets" table. PetsColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, {Name: "name", Type: field.TypeString}, {Name: "age", Type: field.TypeFloat64}, {Name: "weight", Type: field.TypeFloat64}, {Name: "best_friend_id", Type: field.TypeUUID, Unique: true, Nullable: true, Default: "00000000-0000-0000-0000-000000000000"}, {Name: "owner_id", Type: field.TypeInt, Default: 0}, } // PetsTable holds the schema information for the "pets" table. PetsTable = &schema.Table{ Name: "pets", Columns: PetsColumns, PrimaryKey: []*schema.Column{PetsColumns[0]}, ForeignKeys: []*schema.ForeignKey{ { Symbol: "pets_pets_best_friend", Columns: []*schema.Column{PetsColumns[4]}, RefColumns: []*schema.Column{PetsColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "pets_users_owner", Columns: []*schema.Column{PetsColumns[5]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.NoAction, }, }, Indexes: []*schema.Index{ { Name: "pet_name_owner_id", Unique: true, Columns: []*schema.Column{PetsColumns[1], PetsColumns[5]}, }, }, } // SessionsColumns holds the columns for the "sessions" table. SessionsColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, {Name: "active", Type: field.TypeBool, Default: false}, {Name: "issued_at", Type: field.TypeTime}, {Name: "expires_at", Type: field.TypeTime, Nullable: true}, {Name: "token", Type: field.TypeString, Nullable: true}, {Name: "method", Type: field.TypeJSON, Nullable: true}, {Name: "device_id", Type: field.TypeUUID, Nullable: true}, } // SessionsTable holds the schema information for the "sessions" table. SessionsTable = &schema.Table{ Name: "sessions", Columns: SessionsColumns, PrimaryKey: []*schema.Column{SessionsColumns[0]}, ForeignKeys: []*schema.ForeignKey{ { Symbol: "sessions_session_devices_sessions", Columns: []*schema.Column{SessionsColumns[6]}, RefColumns: []*schema.Column{SessionDevicesColumns[0]}, OnDelete: schema.SetNull, }, }, Indexes: []*schema.Index{ { Name: "session_active_expires_at", Unique: false, Columns: []*schema.Column{SessionsColumns[1], SessionsColumns[3]}, }, }, } // SessionDevicesColumns holds the columns for the "session_devices" table. SessionDevicesColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, {Name: "ip_address", Type: field.TypeString, Size: 50}, {Name: "user_agent", Type: field.TypeString, Size: 512}, {Name: "location", Type: field.TypeString, Size: 512}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime, Nullable: true}, } // SessionDevicesTable holds the schema information for the "session_devices" table. SessionDevicesTable = &schema.Table{ Name: "session_devices", Columns: SessionDevicesColumns, PrimaryKey: []*schema.Column{SessionDevicesColumns[0]}, Indexes: []*schema.Index{ { Name: "sessiondevice_ip_address_user_agent", Unique: false, Columns: []*schema.Column{SessionDevicesColumns[1], SessionDevicesColumns[2]}, }, { Name: "sessiondevice_location_updated_at", Unique: false, Columns: []*schema.Column{SessionDevicesColumns[3], SessionDevicesColumns[5]}, }, }, } // UsersColumns holds the columns for the "users" table. UsersColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "age", Type: field.TypeFloat64}, {Name: "first_name", Type: field.TypeString}, {Name: "last_name", Type: field.TypeString}, {Name: "tags", Type: field.TypeJSON, Nullable: true}, } // UsersTable holds the schema information for the "users" table. UsersTable = &schema.Table{ Name: "users", Columns: UsersColumns, PrimaryKey: []*schema.Column{UsersColumns[0]}, } // Tables holds all the tables in the schema. Tables = []*schema.Table{ CardsTable, PaymentsTable, PetsTable, SessionsTable, SessionDevicesTable, UsersTable, } )
Functions ¶
func Create ¶
func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error
Create creates all table resources using the given schema driver.
Types ¶
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is the API for creating, migrating and dropping a schema.
func (*Schema) Diff ¶
Diff creates a migration file containing the statements to resolve the diff between the Ent schema and the connected database.
func (*Schema) NamedDiff ¶
NamedDiff creates a named migration file containing the statements to resolve the diff between the Ent schema and the connected database.
Click to show internal directories.
Click to hide internal directories.