migrate

package
v0.0.0-...-a746138 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 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 (
	// CommentsColumns holds the columns for the "comments" table.
	CommentsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "parent_id", Type: field.TypeInt, Default: 0},
		{Name: "content", Type: field.TypeString, Size: 4096},
		{Name: "modified_at", Type: field.TypeTime, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "thread_thread_comments", Type: field.TypeInt},
		{Name: "created_by", Type: field.TypeUUID},
	}
	// CommentsTable holds the schema information for the "comments" table.
	CommentsTable = &schema.Table{
		Name:       "comments",
		Columns:    CommentsColumns,
		PrimaryKey: []*schema.Column{CommentsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "comments_threads_thread_comments",
				Columns:    []*schema.Column{CommentsColumns[5]},
				RefColumns: []*schema.Column{ThreadsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "comments_users_user_comments",
				Columns:    []*schema.Column{CommentsColumns[6]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// CommentKudosColumns holds the columns for the "comment_kudos" table.
	CommentKudosColumns = []*schema.Column{
		{Name: "user_id", Type: field.TypeUUID},
		{Name: "comment_id", Type: field.TypeInt},
	}
	// CommentKudosTable holds the schema information for the "comment_kudos" table.
	CommentKudosTable = &schema.Table{
		Name:       "comment_kudos",
		Columns:    CommentKudosColumns,
		PrimaryKey: []*schema.Column{CommentKudosColumns[0], CommentKudosColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "comment_kudos_users_user",
				Columns:    []*schema.Column{CommentKudosColumns[0]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "comment_kudos_comments_comment",
				Columns:    []*schema.Column{CommentKudosColumns[1]},
				RefColumns: []*schema.Column{CommentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// ModeratorsColumns holds the columns for the "moderators" table.
	ModeratorsColumns = []*schema.Column{
		{Name: "moderator_id", Type: field.TypeUUID},
		{Name: "topic_id", Type: field.TypeInt},
	}
	// ModeratorsTable holds the schema information for the "moderators" table.
	ModeratorsTable = &schema.Table{
		Name:       "moderators",
		Columns:    ModeratorsColumns,
		PrimaryKey: []*schema.Column{ModeratorsColumns[0], ModeratorsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "moderators_users_moderator",
				Columns:    []*schema.Column{ModeratorsColumns[0]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "moderators_topics_topic",
				Columns:    []*schema.Column{ModeratorsColumns[1]},
				RefColumns: []*schema.Column{TopicsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// TagsColumns holds the columns for the "tags" table.
	TagsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "tag_name", Type: field.TypeString, Size: 255},
	}
	// TagsTable holds the schema information for the "tags" table.
	TagsTable = &schema.Table{
		Name:       "tags",
		Columns:    TagsColumns,
		PrimaryKey: []*schema.Column{TagsColumns[0]},
	}
	// ThreadsColumns holds the columns for the "threads" table.
	ThreadsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "title", Type: field.TypeString, Size: 255},
		{Name: "slug", Type: field.TypeString, Size: 255},
		{Name: "description", Type: field.TypeString, Nullable: true, Size: 4096},
		{Name: "modified_at", Type: field.TypeTime, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "topic_topic_threads", Type: field.TypeInt},
		{Name: "created_by", Type: field.TypeUUID},
	}
	// ThreadsTable holds the schema information for the "threads" table.
	ThreadsTable = &schema.Table{
		Name:       "threads",
		Columns:    ThreadsColumns,
		PrimaryKey: []*schema.Column{ThreadsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "threads_topics_topic_threads",
				Columns:    []*schema.Column{ThreadsColumns[6]},
				RefColumns: []*schema.Column{TopicsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "threads_users_user_threads",
				Columns:    []*schema.Column{ThreadsColumns[7]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// ThreadKudosColumns holds the columns for the "thread_kudos" table.
	ThreadKudosColumns = []*schema.Column{
		{Name: "user_id", Type: field.TypeUUID},
		{Name: "thread_id", Type: field.TypeInt},
	}
	// ThreadKudosTable holds the schema information for the "thread_kudos" table.
	ThreadKudosTable = &schema.Table{
		Name:       "thread_kudos",
		Columns:    ThreadKudosColumns,
		PrimaryKey: []*schema.Column{ThreadKudosColumns[0], ThreadKudosColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "thread_kudos_users_user",
				Columns:    []*schema.Column{ThreadKudosColumns[0]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "thread_kudos_threads_thread",
				Columns:    []*schema.Column{ThreadKudosColumns[1]},
				RefColumns: []*schema.Column{ThreadsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// TopicsColumns holds the columns for the "topics" table.
	TopicsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "title", Type: field.TypeString, Unique: true, Size: 255},
		{Name: "slug", Type: field.TypeString, Unique: true, Size: 255},
		{Name: "short_description", Type: field.TypeString, Size: 255},
		{Name: "description", Type: field.TypeString, Nullable: true, Size: 4096},
		{Name: "profile_pic_url", Type: field.TypeString, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
	}
	// TopicsTable holds the schema information for the "topics" table.
	TopicsTable = &schema.Table{
		Name:       "topics",
		Columns:    TopicsColumns,
		PrimaryKey: []*schema.Column{TopicsColumns[0]},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "email", Type: field.TypeString, Size: 255},
		{Name: "username", Type: field.TypeString, Unique: true, Size: 255},
		{Name: "first_name", Type: field.TypeString, Nullable: true, Size: 255},
		{Name: "last_name", Type: field.TypeString, Nullable: true, Size: 255},
		{Name: "hash", Type: field.TypeBytes, Unique: true, Size: 255},
		{Name: "email_verified", Type: field.TypeBool, Default: false},
		{Name: "created_at", Type: field.TypeTime},
	}
	// UsersTable holds the schema information for the "users" table.
	UsersTable = &schema.Table{
		Name:       "users",
		Columns:    UsersColumns,
		PrimaryKey: []*schema.Column{UsersColumns[0]},
	}
	// ThreadTagsColumns holds the columns for the "thread_tags" table.
	ThreadTagsColumns = []*schema.Column{
		{Name: "thread_id", Type: field.TypeInt},
		{Name: "tag_id", Type: field.TypeInt},
	}
	// ThreadTagsTable holds the schema information for the "thread_tags" table.
	ThreadTagsTable = &schema.Table{
		Name:       "thread_tags",
		Columns:    ThreadTagsColumns,
		PrimaryKey: []*schema.Column{ThreadTagsColumns[0], ThreadTagsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "thread_tags_thread_id",
				Columns:    []*schema.Column{ThreadTagsColumns[0]},
				RefColumns: []*schema.Column{ThreadsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "thread_tags_tag_id",
				Columns:    []*schema.Column{ThreadTagsColumns[1]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		CommentsTable,
		CommentKudosTable,
		ModeratorsTable,
		TagsTable,
		ThreadsTable,
		ThreadKudosTable,
		TopicsTable,
		UsersTable,
		ThreadTagsTable,
	}
)

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