migrate

package
v0.0.0-...-81e723e Latest Latest
Warning

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

Go to latest
Published: Dec 30, 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 (
	// AdminsColumns holds the columns for the "admins" table.
	AdminsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint64, Increment: true},
		{Name: "username", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString, Nullable: true},
		{Name: "email", Type: field.TypeString, Nullable: true},
		{Name: "email_verified_at", Type: field.TypeTime, Nullable: true},
		{Name: "password", Type: field.TypeString},
		{Name: "password2", Type: field.TypeString},
		{Name: "lang", Type: field.TypeString, Default: "en"},
		{Name: "avatar", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "type", Type: field.TypeUint8, Default: 0},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
	}
	// AdminsTable holds the schema information for the "admins" table.
	AdminsTable = &schema.Table{
		Name:       "admins",
		Columns:    AdminsColumns,
		PrimaryKey: []*schema.Column{AdminsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "admin_username",
				Unique:  false,
				Columns: []*schema.Column{AdminsColumns[1]},
			},
			{
				Name:    "admin_email",
				Unique:  false,
				Columns: []*schema.Column{AdminsColumns[3]},
			},
		},
	}
	// BanksColumns holds the columns for the "banks" table.
	BanksColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint64, Increment: true},
		{Name: "name_en", Type: field.TypeString},
		{Name: "name_zh", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "country_id", Type: field.TypeUint64, Nullable: true},
	}
	// BanksTable holds the schema information for the "banks" table.
	BanksTable = &schema.Table{
		Name:       "banks",
		Columns:    BanksColumns,
		PrimaryKey: []*schema.Column{BanksColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "banks_countries_banks",
				Columns:    []*schema.Column{BanksColumns[6]},
				RefColumns: []*schema.Column{CountriesColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "bank_country_id",
				Unique:  false,
				Columns: []*schema.Column{BanksColumns[6]},
			},
		},
	}
	// CountriesColumns holds the columns for the "countries" table.
	CountriesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint64, Increment: true},
		{Name: "iso2", Type: field.TypeString},
		{Name: "iso3", Type: field.TypeString},
		{Name: "name", Type: field.TypeString},
		{Name: "official_name", Type: field.TypeString, Nullable: true},
		{Name: "numeric_code", Type: field.TypeString, Nullable: true},
		{Name: "phone_code", Type: field.TypeString, Nullable: true},
		{Name: "capital", Type: field.TypeString, Nullable: true},
		{Name: "currency_name", Type: field.TypeString, Nullable: true},
		{Name: "currency_code", Type: field.TypeString, Nullable: true},
		{Name: "currency_symbol", Type: field.TypeString, Nullable: true},
		{Name: "conversion_rate", Type: field.TypeFloat64, Default: 0},
		{Name: "status", Type: field.TypeUint8, Default: 0},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
	}
	// CountriesTable holds the schema information for the "countries" table.
	CountriesTable = &schema.Table{
		Name:       "countries",
		Columns:    CountriesColumns,
		PrimaryKey: []*schema.Column{CountriesColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "country_iso2",
				Unique:  true,
				Columns: []*schema.Column{CountriesColumns[1]},
			},
			{
				Name:    "country_iso3",
				Unique:  true,
				Columns: []*schema.Column{CountriesColumns[2]},
			},
			{
				Name:    "country_status",
				Unique:  false,
				Columns: []*schema.Column{CountriesColumns[12]},
			},
		},
	}
	// CountryLocationsColumns holds the columns for the "country_locations" table.
	CountryLocationsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint64, Increment: true},
		{Name: "sorting", Type: field.TypeUint64, Default: 0},
		{Name: "name_en", Type: field.TypeString},
		{Name: "name_zh", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "country_id", Type: field.TypeUint64, Nullable: true},
		{Name: "parent_id", Type: field.TypeUint64, Nullable: true},
	}
	// CountryLocationsTable holds the schema information for the "country_locations" table.
	CountryLocationsTable = &schema.Table{
		Name:       "country_locations",
		Columns:    CountryLocationsColumns,
		PrimaryKey: []*schema.Column{CountryLocationsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "country_locations_countries_locations",
				Columns:    []*schema.Column{CountryLocationsColumns[7]},
				RefColumns: []*schema.Column{CountriesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "country_locations_country_locations_child_locations",
				Columns:    []*schema.Column{CountryLocationsColumns[8]},
				RefColumns: []*schema.Column{CountryLocationsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "countrylocation_country_id",
				Unique:  false,
				Columns: []*schema.Column{CountryLocationsColumns[7]},
			},
			{
				Name:    "countrylocation_parent_id",
				Unique:  false,
				Columns: []*schema.Column{CountryLocationsColumns[8]},
			},
			{
				Name:    "countrylocation_sorting",
				Unique:  false,
				Columns: []*schema.Column{CountryLocationsColumns[1]},
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUint64, Increment: true},
		{Name: "username", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString, Nullable: true},
		{Name: "email", Type: field.TypeString, Nullable: true},
		{Name: "email_verified_at", Type: field.TypeTime, Nullable: true},
		{Name: "password", Type: field.TypeString},
		{Name: "password2", Type: field.TypeString},
		{Name: "contact_number", Type: field.TypeString, Nullable: true},
		{Name: "full_contact_number", Type: field.TypeString, Nullable: true},
		{Name: "lang", Type: field.TypeString, Default: "en"},
		{Name: "avatar", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "credit_1", Type: field.TypeFloat64, Default: 0},
		{Name: "credit_2", Type: field.TypeFloat64, Default: 0},
		{Name: "credit_3", Type: field.TypeFloat64, Default: 0},
		{Name: "credit_4", Type: field.TypeFloat64, Default: 0},
		{Name: "credit_5", Type: field.TypeFloat64, Default: 0},
		{Name: "bank_account_name", Type: field.TypeString, Nullable: true},
		{Name: "bank_account_number", Type: field.TypeString, Nullable: true},
		{Name: "national_id", Type: field.TypeString, Nullable: true},
		{Name: "first_login", Type: field.TypeBool, Default: false},
		{Name: "ban_until", Type: field.TypeTime, Nullable: true},
		{Name: "new_login_at", Type: field.TypeTime, Nullable: true},
		{Name: "last_login_at", Type: field.TypeTime, Nullable: true},
		{Name: "unilevel", Type: field.TypeUint64, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "bank_id", Type: field.TypeUint64, Nullable: true},
		{Name: "country_id", Type: field.TypeUint64, Nullable: true},
		{Name: "contact_country_id", Type: field.TypeUint64, Nullable: true},
		{Name: "introducer_user_id", Type: field.TypeUint64, Nullable: true},
	}
	// UsersTable holds the schema information for the "users" table.
	UsersTable = &schema.Table{
		Name:       "users",
		Columns:    UsersColumns,
		PrimaryKey: []*schema.Column{UsersColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "users_banks_users",
				Columns:    []*schema.Column{UsersColumns[27]},
				RefColumns: []*schema.Column{BanksColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "users_countries_users",
				Columns:    []*schema.Column{UsersColumns[28]},
				RefColumns: []*schema.Column{CountriesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "users_countries_contact_users",
				Columns:    []*schema.Column{UsersColumns[29]},
				RefColumns: []*schema.Column{CountriesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "users_users_introduced_users",
				Columns:    []*schema.Column{UsersColumns[30]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "user_username",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[1]},
			},
			{
				Name:    "user_email",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[3]},
			},
			{
				Name:    "user_country_id",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[28]},
			},
			{
				Name:    "user_contact_country_id",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[29]},
			},
			{
				Name:    "user_introducer_user_id",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[30]},
			},
			{
				Name:    "user_bank_id",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[27]},
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		AdminsTable,
		BanksTable,
		CountriesTable,
		CountryLocationsTable,
		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