migrate

package
v0.0.0-...-3da28a4 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 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
	// WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
	WithForeignKeys = schema.WithForeignKeys
)
View Source
var (
	// BiographiesColumns holds the columns for the "biographies" table.
	BiographiesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "user_id", Type: field.TypeUint32},
		{Name: "is_public", Type: field.TypeBool, Default: false},
		{Name: "location_id", Type: field.TypeUint32},
		{Name: "position", Type: field.TypeString, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "position_ja", Type: field.TypeString, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "join", Type: field.TypeTime, SchemaType: map[string]string{"mysql": "date"}},
		{Name: "leave", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"mysql": "date"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// BiographiesTable holds the schema information for the "biographies" table.
	BiographiesTable = &schema.Table{
		Name:       "biographies",
		Columns:    BiographiesColumns,
		PrimaryKey: []*schema.Column{BiographiesColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "biography_user_id",
				Unique:  false,
				Columns: []*schema.Column{BiographiesColumns[1]},
			},
			{
				Name:    "biography_user_id_join",
				Unique:  false,
				Columns: []*schema.Column{BiographiesColumns[1], BiographiesColumns[6]},
			},
		},
	}
	// CategoriesColumns holds the columns for the "categories" table.
	CategoriesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "name", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "name_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "emoji", Type: field.TypeString, SchemaType: map[string]string{"mysql": "varchar(15)"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// CategoriesTable holds the schema information for the "categories" table.
	CategoriesTable = &schema.Table{
		Name:       "categories",
		Columns:    CategoriesColumns,
		PrimaryKey: []*schema.Column{CategoriesColumns[0]},
	}
	// ContactsColumns holds the columns for the "contacts" table.
	ContactsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "to_user_id", Type: field.TypeUint32},
		{Name: "name", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "title", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "detail", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "mail", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "ip", Type: field.TypeString, Size: 63},
		{Name: "lang", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "url", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "category", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "custom_title", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "custom_value", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "device_name", Type: field.TypeString, Nullable: true, Size: 31},
		{Name: "os", Type: field.TypeString, Nullable: true, Size: 15},
		{Name: "browser_name", Type: field.TypeString, Nullable: true, Size: 15},
		{Name: "is_mobile", Type: field.TypeBool, Nullable: true},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// ContactsTable holds the schema information for the "contacts" table.
	ContactsTable = &schema.Table{
		Name:       "contacts",
		Columns:    ContactsColumns,
		PrimaryKey: []*schema.Column{ContactsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "contact_to_user_id",
				Unique:  false,
				Columns: []*schema.Column{ContactsColumns[1]},
			},
		},
	}
	// ContactDefaultsColumns holds the columns for the "contact_defaults" table.
	ContactDefaultsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "name", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "email", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "url", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "category", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "custom_title", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "description", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// ContactDefaultsTable holds the schema information for the "contact_defaults" table.
	ContactDefaultsTable = &schema.Table{
		Name:       "contact_defaults",
		Columns:    ContactDefaultsColumns,
		PrimaryKey: []*schema.Column{ContactDefaultsColumns[0]},
	}
	// LinksColumns holds the columns for the "links" table.
	LinksColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "user_id", Type: field.TypeUint32},
		{Name: "name", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "name_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "site_url", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "favicon_url", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "category_id", Type: field.TypeUint32},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// LinksTable holds the schema information for the "links" table.
	LinksTable = &schema.Table{
		Name:       "links",
		Columns:    LinksColumns,
		PrimaryKey: []*schema.Column{LinksColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "link_user_id",
				Unique:  false,
				Columns: []*schema.Column{LinksColumns[1]},
			},
		},
	}
	// LocationsColumns holds the columns for the "locations" table.
	LocationsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "type", Type: field.TypeEnum, Enums: []string{"univ", "corp"}},
		{Name: "name", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "name_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "address", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "address_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// LocationsTable holds the schema information for the "locations" table.
	LocationsTable = &schema.Table{
		Name:       "locations",
		Columns:    LocationsColumns,
		PrimaryKey: []*schema.Column{LocationsColumns[0]},
	}
	// NoticesColumns holds the columns for the "notices" table.
	NoticesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "user_id", Type: field.TypeUint32, Unique: true},
		{Name: "discord_webhook", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "slack_webhook", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "mail", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// NoticesTable holds the schema information for the "notices" table.
	NoticesTable = &schema.Table{
		Name:       "notices",
		Columns:    NoticesColumns,
		PrimaryKey: []*schema.Column{NoticesColumns[0]},
	}
	// ProductsColumns holds the columns for the "products" table.
	ProductsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "user_id", Type: field.TypeUint32},
		{Name: "name", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "name_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "detail", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "detail_ja", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "site_url", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "github_url", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "dev_time", Type: field.TypeTime, SchemaType: map[string]string{"mysql": "date"}},
		{Name: "thumbnail", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// ProductsTable holds the schema information for the "products" table.
	ProductsTable = &schema.Table{
		Name:       "products",
		Columns:    ProductsColumns,
		PrimaryKey: []*schema.Column{ProductsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "product_user_id",
				Unique:  false,
				Columns: []*schema.Column{ProductsColumns[1]},
			},
			{
				Name:    "product_user_id_dev_time",
				Unique:  false,
				Columns: []*schema.Column{ProductsColumns[1], ProductsColumns[8]},
			},
		},
	}
	// SessionsColumns holds the columns for the "sessions" table.
	SessionsColumns = []*schema.Column{
		{Name: "session_token", Type: field.TypeUUID},
		{Name: "user_id", Type: field.TypeUint32},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "period", Type: field.TypeTime, SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// SessionsTable holds the schema information for the "sessions" table.
	SessionsTable = &schema.Table{
		Name:       "sessions",
		Columns:    SessionsColumns,
		PrimaryKey: []*schema.Column{SessionsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "session_user_id",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[1]},
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint32, Increment: true},
		{Name: "given_name", Type: field.TypeString, Size: 10},
		{Name: "family_name", Type: field.TypeString, Size: 10},
		{Name: "given_name_ja", Type: field.TypeString, Size: 10},
		{Name: "family_name_ja", Type: field.TypeString, Size: 10},
		{Name: "user_id", Type: field.TypeString, Size: 10},
		{Name: "mail", Type: field.TypeString, Size: 254},
		{Name: "birth_date", Type: field.TypeTime, SchemaType: map[string]string{"mysql": "date"}},
		{Name: "location", Type: field.TypeString, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "location_ja", Type: field.TypeString, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "sso_token", Type: field.TypeString, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "avatar_url", Type: field.TypeString, Nullable: true, Size: 2147483647, SchemaType: map[string]string{"mysql": "text"}},
		{Name: "selected", Type: field.TypeBool, Default: false},
		{Name: "created", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
		{Name: "modified", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", SchemaType: map[string]string{"mysql": "datetime"}},
	}
	// 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{
		BiographiesTable,
		CategoriesTable,
		ContactsTable,
		ContactDefaultsTable,
		LinksTable,
		LocationsTable,
		NoticesTable,
		ProductsTable,
		SessionsTable,
		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 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