migrate

package
v0.0.0-...-aa3ac78 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 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 (
	// CommentsColumns holds the columns for the "comments" table.
	CommentsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "text", Type: field.TypeString},
	}
	// CommentsTable holds the schema information for the "comments" table.
	CommentsTable = &schema.Table{
		Name:       "comments",
		Columns:    CommentsColumns,
		PrimaryKey: []*schema.Column{CommentsColumns[0]},
	}
	// EpicsColumns holds the columns for the "epics" table.
	EpicsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true},
		{Name: "description", Type: field.TypeString},
		{Name: "epic_project", Type: field.TypeInt, Nullable: true},
		{Name: "user_reporter", Type: field.TypeInt, Nullable: true},
		{Name: "user_assignee", Type: field.TypeInt, Nullable: true},
	}
	// EpicsTable holds the schema information for the "epics" table.
	EpicsTable = &schema.Table{
		Name:       "epics",
		Columns:    EpicsColumns,
		PrimaryKey: []*schema.Column{EpicsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "epics_projects_project",
				Columns:    []*schema.Column{EpicsColumns[3]},
				RefColumns: []*schema.Column{ProjectsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "epics_users_reporter",
				Columns:    []*schema.Column{EpicsColumns[4]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "epics_users_assignee",
				Columns:    []*schema.Column{EpicsColumns[5]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// ProjectsColumns holds the columns for the "projects" table.
	ProjectsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true},
		{Name: "description", Type: field.TypeString, Nullable: true},
		{Name: "user_owns", Type: field.TypeInt, Nullable: true},
	}
	// ProjectsTable holds the schema information for the "projects" table.
	ProjectsTable = &schema.Table{
		Name:       "projects",
		Columns:    ProjectsColumns,
		PrimaryKey: []*schema.Column{ProjectsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "projects_users_owns",
				Columns:    []*schema.Column{ProjectsColumns[3]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString},
		{Name: "surname", Type: field.TypeString},
		{Name: "username", Type: field.TypeString, Unique: true},
		{Name: "email", Type: field.TypeString, Unique: true},
	}
	// UsersTable holds the schema information for the "users" table.
	UsersTable = &schema.Table{
		Name:       "users",
		Columns:    UsersColumns,
		PrimaryKey: []*schema.Column{UsersColumns[0]},
	}
	// EpicCommentsColumns holds the columns for the "epic_comments" table.
	EpicCommentsColumns = []*schema.Column{
		{Name: "epic_id", Type: field.TypeInt},
		{Name: "comment_id", Type: field.TypeInt},
	}
	// EpicCommentsTable holds the schema information for the "epic_comments" table.
	EpicCommentsTable = &schema.Table{
		Name:       "epic_comments",
		Columns:    EpicCommentsColumns,
		PrimaryKey: []*schema.Column{EpicCommentsColumns[0], EpicCommentsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "epic_comments_epic_id",
				Columns:    []*schema.Column{EpicCommentsColumns[0]},
				RefColumns: []*schema.Column{EpicsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "epic_comments_comment_id",
				Columns:    []*schema.Column{EpicCommentsColumns[1]},
				RefColumns: []*schema.Column{CommentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// ProjectCommentsColumns holds the columns for the "project_comments" table.
	ProjectCommentsColumns = []*schema.Column{
		{Name: "project_id", Type: field.TypeInt},
		{Name: "comment_id", Type: field.TypeInt},
	}
	// ProjectCommentsTable holds the schema information for the "project_comments" table.
	ProjectCommentsTable = &schema.Table{
		Name:       "project_comments",
		Columns:    ProjectCommentsColumns,
		PrimaryKey: []*schema.Column{ProjectCommentsColumns[0], ProjectCommentsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "project_comments_project_id",
				Columns:    []*schema.Column{ProjectCommentsColumns[0]},
				RefColumns: []*schema.Column{ProjectsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "project_comments_comment_id",
				Columns:    []*schema.Column{ProjectCommentsColumns[1]},
				RefColumns: []*schema.Column{CommentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// UserCommentsColumns holds the columns for the "user_comments" table.
	UserCommentsColumns = []*schema.Column{
		{Name: "user_id", Type: field.TypeInt},
		{Name: "comment_id", Type: field.TypeInt},
	}
	// UserCommentsTable holds the schema information for the "user_comments" table.
	UserCommentsTable = &schema.Table{
		Name:       "user_comments",
		Columns:    UserCommentsColumns,
		PrimaryKey: []*schema.Column{UserCommentsColumns[0], UserCommentsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "user_comments_user_id",
				Columns:    []*schema.Column{UserCommentsColumns[0]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "user_comments_comment_id",
				Columns:    []*schema.Column{UserCommentsColumns[1]},
				RefColumns: []*schema.Column{CommentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		CommentsTable,
		EpicsTable,
		ProjectsTable,
		UsersTable,
		EpicCommentsTable,
		ProjectCommentsTable,
		UserCommentsTable,
	}
)

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