migrate

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// WithGlobalUniqueID sets the universal ids options to the migration.
	// If this option is enabled, ent 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, ent 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, ent 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
	// WithFixture sets the foreign-key renaming option to the migration when upgrading
	// ent from v0.1.0 (issue-#285). Defaults to false.
	WithFixture = schema.WithFixture
	// WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
	WithForeignKeys = schema.WithForeignKeys
)
View Source
var (
	// BadgesColumns holds the columns for the "badges" table.
	BadgesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "color", Type: field.TypeEnum, Enums: []string{"red", "orange", "yellow", "green", "blue", "indigo", "violet", "purple", "pink", "silver", "gold", "beige", "brown", "grey", "black", "white"}},
		{Name: "material", Type: field.TypeEnum, Enums: []string{"leather", "plastic", "fabric"}},
		{Name: "pet_badge", Type: field.TypeInt, Unique: true, Nullable: true},
	}
	// BadgesTable holds the schema information for the "badges" table.
	BadgesTable = &schema.Table{
		Name:       "badges",
		Columns:    BadgesColumns,
		PrimaryKey: []*schema.Column{BadgesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "badges_pets_badge",
				Columns:    []*schema.Column{BadgesColumns[3]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// PetsColumns holds the columns for the "pets" table.
	PetsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "height", Type: field.TypeInt},
		{Name: "weight", Type: field.TypeFloat64, Nullable: true},
		{Name: "castrated", Type: field.TypeBool},
		{Name: "name", Type: field.TypeString, Unique: true},
		{Name: "birthday", Type: field.TypeTime, Nullable: true},
		{Name: "nicknames", Type: field.TypeJSON, Nullable: true},
		{Name: "sex", Type: field.TypeEnum, Enums: []string{"male", "female"}},
		{Name: "chip", Type: field.TypeUUID},
		{Name: "pet_mentor", Type: field.TypeInt, Unique: true, Nullable: true},
		{Name: "pet_spouse", Type: field.TypeInt, Unique: true, Nullable: true},
		{Name: "pet_children", Type: field.TypeInt, Nullable: true},
	}
	// 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_mentor",
				Columns:    []*schema.Column{PetsColumns[9]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "pets_pets_spouse",
				Columns:    []*schema.Column{PetsColumns[10]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "pets_pets_children",
				Columns:    []*schema.Column{PetsColumns[11]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// PlayGroupsColumns holds the columns for the "play_groups" table.
	PlayGroupsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "title", Type: field.TypeString},
		{Name: "description", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "weekday", Type: field.TypeEnum, Enums: []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}},
	}
	// PlayGroupsTable holds the schema information for the "play_groups" table.
	PlayGroupsTable = &schema.Table{
		Name:       "play_groups",
		Columns:    PlayGroupsColumns,
		PrimaryKey: []*schema.Column{PlayGroupsColumns[0]},
	}
	// ToysColumns holds the columns for the "toys" table.
	ToysColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "color", Type: field.TypeEnum, Enums: []string{"red", "orange", "yellow", "green", "blue", "indigo", "violet", "purple", "pink", "silver", "gold", "beige", "brown", "grey", "black", "white"}},
		{Name: "material", Type: field.TypeEnum, Enums: []string{"leather", "plastic", "fabric"}},
		{Name: "title", Type: field.TypeString},
		{Name: "pet_toys", Type: field.TypeInt, Nullable: true},
	}
	// ToysTable holds the schema information for the "toys" table.
	ToysTable = &schema.Table{
		Name:       "toys",
		Columns:    ToysColumns,
		PrimaryKey: []*schema.Column{ToysColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "toys_pets_toys",
				Columns:    []*schema.Column{ToysColumns[4]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// PetPlayGroupsColumns holds the columns for the "pet_play_groups" table.
	PetPlayGroupsColumns = []*schema.Column{
		{Name: "pet_id", Type: field.TypeInt},
		{Name: "play_group_id", Type: field.TypeInt},
	}
	// PetPlayGroupsTable holds the schema information for the "pet_play_groups" table.
	PetPlayGroupsTable = &schema.Table{
		Name:       "pet_play_groups",
		Columns:    PetPlayGroupsColumns,
		PrimaryKey: []*schema.Column{PetPlayGroupsColumns[0], PetPlayGroupsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "pet_play_groups_pet_id",
				Columns:    []*schema.Column{PetPlayGroupsColumns[0]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "pet_play_groups_play_group_id",
				Columns:    []*schema.Column{PetPlayGroupsColumns[1]},
				RefColumns: []*schema.Column{PlayGroupsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// PetFriendsColumns holds the columns for the "pet_friends" table.
	PetFriendsColumns = []*schema.Column{
		{Name: "pet_id", Type: field.TypeInt},
		{Name: "friend_id", Type: field.TypeInt},
	}
	// PetFriendsTable holds the schema information for the "pet_friends" table.
	PetFriendsTable = &schema.Table{
		Name:       "pet_friends",
		Columns:    PetFriendsColumns,
		PrimaryKey: []*schema.Column{PetFriendsColumns[0], PetFriendsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "pet_friends_pet_id",
				Columns:    []*schema.Column{PetFriendsColumns[0]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "pet_friends_friend_id",
				Columns:    []*schema.Column{PetFriendsColumns[1]},
				RefColumns: []*schema.Column{PetsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		BadgesTable,
		PetsTable,
		PlayGroupsTable,
		ToysTable,
		PetPlayGroupsTable,
		PetFriendsTable,
	}
)

Functions

This section is empty.

Types

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is the API for creating, migrating and dropping a schema.

func NewSchema

func NewSchema(drv dialect.Driver) *Schema

NewSchema creates a new schema client.

func (*Schema) Create

func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error

Create creates all schema resources.

func (*Schema) WriteTo

func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error

WriteTo writes the schema changes to w instead of running them against the database.

if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {
	log.Fatal(err)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL