migrate

package
v0.0.0-...-2db6053 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 7 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 (
	// AccessControlsColumns holds the columns for the "access_controls" table.
	AccessControlsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "ptype", Type: field.TypeString, Default: ""},
		{Name: "v0", Type: field.TypeString, Default: ""},
		{Name: "v1", Type: field.TypeString, Default: ""},
		{Name: "v2", Type: field.TypeString, Default: ""},
		{Name: "v3", Type: field.TypeString, Default: ""},
		{Name: "v4", Type: field.TypeString, Default: ""},
		{Name: "v5", Type: field.TypeString, Default: ""},
	}
	// AccessControlsTable holds the schema information for the "access_controls" table.
	AccessControlsTable = &schema.Table{
		Name:       "access_controls",
		Columns:    AccessControlsColumns,
		PrimaryKey: []*schema.Column{AccessControlsColumns[0]},
	}
	// AuditsColumns holds the columns for the "audits" table.
	AuditsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "ent_name", Type: field.TypeString, Size: 50},
		{Name: "ent_id", Type: field.TypeInt64},
		{Name: "operation", Type: field.TypeEnum, Enums: []string{"Create", "Update", "UpdateOne", "Delete", "DeleteOne"}},
		{Name: "description", Type: field.TypeString, Size: 1000},
		{Name: "ip", Type: field.TypeString, Nullable: true, Size: 40},
		{Name: "user_name", Type: field.TypeString, Nullable: true, Size: 150},
		{Name: "user_id", Type: field.TypeInt64, Nullable: true},
	}
	// AuditsTable holds the schema information for the "audits" table.
	AuditsTable = &schema.Table{
		Name:       "audits",
		Columns:    AuditsColumns,
		PrimaryKey: []*schema.Column{AuditsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "audits_users_audit_logs",
				Columns:    []*schema.Column{AuditsColumns[8]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "audit_ent_name_ent_id",
				Unique:  false,
				Columns: []*schema.Column{AuditsColumns[2], AuditsColumns[3]},
			},
			{
				Name:    "audit_operation",
				Unique:  false,
				Columns: []*schema.Column{AuditsColumns[4]},
			},
			{
				Name:    "audit_ip",
				Unique:  false,
				Columns: []*schema.Column{AuditsColumns[6]},
			},
		},
	}
	// RolesColumns holds the columns for the "roles" table.
	RolesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "name", Type: field.TypeString},
	}
	// RolesTable holds the schema information for the "roles" table.
	RolesTable = &schema.Table{
		Name:       "roles",
		Columns:    RolesColumns,
		PrimaryKey: []*schema.Column{RolesColumns[0]},
	}
	// TodosColumns holds the columns for the "todos" table.
	TodosColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
	}
	// TodosTable holds the schema information for the "todos" table.
	TodosTable = &schema.Table{
		Name:       "todos",
		Columns:    TodosColumns,
		PrimaryKey: []*schema.Column{TodosColumns[0]},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "email", Type: field.TypeString, Unique: true},
		{Name: "email_verified", Type: field.TypeBool, Default: false},
		{Name: "phone", Type: field.TypeString, Nullable: true, Size: 20},
		{Name: "phone_verified", Type: field.TypeBool, Default: false},
		{Name: "pwd_salt", Type: field.TypeString, Size: 100},
		{Name: "pwd_hash", Type: field.TypeString, Size: 100},
		{Name: "login_failed_count", Type: field.TypeUint8, Nullable: true, Default: 0},
		{Name: "login_attempt_on", Type: field.TypeTime, Nullable: true},
		{Name: "login_locked_until", Type: field.TypeTime, Nullable: true},
		{Name: "first_name", Type: field.TypeString, Size: 30},
		{Name: "middle_name", Type: field.TypeString, Size: 30},
		{Name: "last_name", Type: field.TypeString, Size: 30},
		{Name: "status", Type: field.TypeEnum, Enums: []string{"Pending", "Active", "InActive"}, Default: "Pending"},
	}
	// UsersTable holds the schema information for the "users" table.
	UsersTable = &schema.Table{
		Name:       "users",
		Columns:    UsersColumns,
		PrimaryKey: []*schema.Column{UsersColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "user_created_at",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[1]},
				Annotation: &entsql.IndexAnnotation{
					Desc: true,
				},
			},
			{
				Name:    "user_updated_at",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[2]},
				Annotation: &entsql.IndexAnnotation{
					Desc: true,
				},
			},
			{
				Name:    "user_phone",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[5]},
			},
			{
				Name:    "user_status",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[15]},
			},
		},
	}
	// UserSessionsColumns holds the columns for the "user_sessions" table.
	UserSessionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "issued_at", Type: field.TypeTime},
		{Name: "expires_at", Type: field.TypeTime},
		{Name: "invalidated", Type: field.TypeBool, Nullable: true, Default: false},
		{Name: "user_agent", Type: field.TypeString, Size: 50},
		{Name: "ip", Type: field.TypeString, Size: 40},
		{Name: "user_id", Type: field.TypeInt64},
	}
	// UserSessionsTable holds the schema information for the "user_sessions" table.
	UserSessionsTable = &schema.Table{
		Name:       "user_sessions",
		Columns:    UserSessionsColumns,
		PrimaryKey: []*schema.Column{UserSessionsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "user_sessions_users_sessions",
				Columns:    []*schema.Column{UserSessionsColumns[6]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "usersession_expires_at",
				Unique:  false,
				Columns: []*schema.Column{UserSessionsColumns[2]},
			},
			{
				Name:    "usersession_invalidated",
				Unique:  false,
				Columns: []*schema.Column{UserSessionsColumns[3]},
			},
			{
				Name:    "usersession_ip",
				Unique:  false,
				Columns: []*schema.Column{UserSessionsColumns[5]},
			},
		},
	}
	// VerifyTokensColumns holds the columns for the "verify_tokens" table.
	VerifyTokensColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "expires_at", Type: field.TypeTime},
		{Name: "token", Type: field.TypeString, Unique: true, Size: 50},
		{Name: "purpose", Type: field.TypeString, Nullable: true, Size: 50},
		{Name: "user_id", Type: field.TypeInt64, Nullable: true},
	}
	// VerifyTokensTable holds the schema information for the "verify_tokens" table.
	VerifyTokensTable = &schema.Table{
		Name:       "verify_tokens",
		Columns:    VerifyTokensColumns,
		PrimaryKey: []*schema.Column{VerifyTokensColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "verify_tokens_users_verify_tokens",
				Columns:    []*schema.Column{VerifyTokensColumns[5]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		AccessControlsTable,
		AuditsTable,
		RolesTable,
		TodosTable,
		UsersTable,
		UserSessionsTable,
		VerifyTokensTable,
	}
)

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