migrate

package
v1.0.1-a Latest Latest
Warning

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

Go to latest
Published: Jan 11, 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 (
	// BillingAddressesColumns holds the columns for the "billing_addresses" table.
	BillingAddressesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "city", Type: field.TypeString},
		{Name: "street", Type: field.TypeString},
		{Name: "zip", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "customer_billing_addresses", Type: field.TypeInt, Nullable: true},
	}
	// BillingAddressesTable holds the schema information for the "billing_addresses" table.
	BillingAddressesTable = &schema.Table{
		Name:       "billing_addresses",
		Columns:    BillingAddressesColumns,
		PrimaryKey: []*schema.Column{BillingAddressesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "billing_addresses_customers_billing_addresses",
				Columns:    []*schema.Column{BillingAddressesColumns[6]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// CustomersColumns holds the columns for the "customers" table.
	CustomersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true, Size: 20},
		{Name: "email", Type: field.TypeString, Unique: true},
		{Name: "password", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "customer_created_by", Type: field.TypeInt, Nullable: true},
		{Name: "customer_login", Type: field.TypeInt, Nullable: true},
	}
	// CustomersTable holds the schema information for the "customers" table.
	CustomersTable = &schema.Table{
		Name:       "customers",
		Columns:    CustomersColumns,
		PrimaryKey: []*schema.Column{CustomersColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "customers_users_created_by",
				Columns:    []*schema.Column{CustomersColumns[6]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "customers_logins_login",
				Columns:    []*schema.Column{CustomersColumns[7]},
				RefColumns: []*schema.Column{LoginsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// DeliveryAddressesColumns holds the columns for the "delivery_addresses" table.
	DeliveryAddressesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "city", Type: field.TypeString},
		{Name: "street", Type: field.TypeString},
		{Name: "zip", Type: field.TypeString},
		{Name: "number", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "customer_delivery_addresses", Type: field.TypeInt, Nullable: true},
		{Name: "delivery_address_telephone", Type: field.TypeInt, Nullable: true},
	}
	// DeliveryAddressesTable holds the schema information for the "delivery_addresses" table.
	DeliveryAddressesTable = &schema.Table{
		Name:       "delivery_addresses",
		Columns:    DeliveryAddressesColumns,
		PrimaryKey: []*schema.Column{DeliveryAddressesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "delivery_addresses_customers_delivery_addresses",
				Columns:    []*schema.Column{DeliveryAddressesColumns[7]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "delivery_addresses_tels_telephone",
				Columns:    []*schema.Column{DeliveryAddressesColumns[8]},
				RefColumns: []*schema.Column{TelsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// LoginsColumns holds the columns for the "logins" table.
	LoginsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "username", Type: field.TypeString, Unique: true},
		{Name: "password", Type: field.TypeString},
		{Name: "last_login", Type: field.TypeTime},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
	}
	// LoginsTable holds the schema information for the "logins" table.
	LoginsTable = &schema.Table{
		Name:       "logins",
		Columns:    LoginsColumns,
		PrimaryKey: []*schema.Column{LoginsColumns[0]},
	}
	// NotesColumns holds the columns for the "notes" table.
	NotesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "content", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "billing_address_notes", Type: field.TypeInt, Nullable: true},
		{Name: "customer_notes", Type: field.TypeInt, Nullable: true},
		{Name: "delivery_address_notes", Type: field.TypeInt, Nullable: true},
		{Name: "order_notes", Type: field.TypeInt, Nullable: true},
		{Name: "tel_note", Type: field.TypeInt, Unique: true, Nullable: true},
	}
	// NotesTable holds the schema information for the "notes" table.
	NotesTable = &schema.Table{
		Name:       "notes",
		Columns:    NotesColumns,
		PrimaryKey: []*schema.Column{NotesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "notes_billing_addresses_notes",
				Columns:    []*schema.Column{NotesColumns[4]},
				RefColumns: []*schema.Column{BillingAddressesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "notes_customers_notes",
				Columns:    []*schema.Column{NotesColumns[5]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "notes_delivery_addresses_notes",
				Columns:    []*schema.Column{NotesColumns[6]},
				RefColumns: []*schema.Column{DeliveryAddressesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "notes_orders_notes",
				Columns:    []*schema.Column{NotesColumns[7]},
				RefColumns: []*schema.Column{OrdersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "notes_tels_note",
				Columns:    []*schema.Column{NotesColumns[8]},
				RefColumns: []*schema.Column{TelsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// OrdersColumns holds the columns for the "orders" table.
	OrdersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "status", Type: field.TypeString, Default: "pending"},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "customer_orders", Type: field.TypeInt, Nullable: true},
		{Name: "order_customer", Type: field.TypeInt, Nullable: true},
		{Name: "order_address", Type: field.TypeInt, Nullable: true},
	}
	// OrdersTable holds the schema information for the "orders" table.
	OrdersTable = &schema.Table{
		Name:       "orders",
		Columns:    OrdersColumns,
		PrimaryKey: []*schema.Column{OrdersColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "orders_customers_orders",
				Columns:    []*schema.Column{OrdersColumns[4]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "orders_customers_customer",
				Columns:    []*schema.Column{OrdersColumns[5]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "orders_billing_addresses_address",
				Columns:    []*schema.Column{OrdersColumns[6]},
				RefColumns: []*schema.Column{BillingAddressesColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// PositionsColumns holds the columns for the "positions" table.
	PositionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
	}
	// PositionsTable holds the schema information for the "positions" table.
	PositionsTable = &schema.Table{
		Name:       "positions",
		Columns:    PositionsColumns,
		PrimaryKey: []*schema.Column{PositionsColumns[0]},
	}
	// RolesColumns holds the columns for the "roles" table.
	RolesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
	}
	// RolesTable holds the schema information for the "roles" table.
	RolesTable = &schema.Table{
		Name:       "roles",
		Columns:    RolesColumns,
		PrimaryKey: []*schema.Column{RolesColumns[0]},
	}
	// TelsColumns holds the columns for the "tels" table.
	TelsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "tel", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
	}
	// TelsTable holds the schema information for the "tels" table.
	TelsTable = &schema.Table{
		Name:       "tels",
		Columns:    TelsColumns,
		PrimaryKey: []*schema.Column{TelsColumns[0]},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true, Size: 20},
		{Name: "username", Type: field.TypeString, Unique: true},
		{Name: "password", Type: field.TypeString},
		{Name: "last_login", Type: field.TypeTime},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_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]},
	}
	// CustomerTelsColumns holds the columns for the "customer_tels" table.
	CustomerTelsColumns = []*schema.Column{
		{Name: "customer_id", Type: field.TypeInt},
		{Name: "tel_id", Type: field.TypeInt},
	}
	// CustomerTelsTable holds the schema information for the "customer_tels" table.
	CustomerTelsTable = &schema.Table{
		Name:       "customer_tels",
		Columns:    CustomerTelsColumns,
		PrimaryKey: []*schema.Column{CustomerTelsColumns[0], CustomerTelsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "customer_tels_customer_id",
				Columns:    []*schema.Column{CustomerTelsColumns[0]},
				RefColumns: []*schema.Column{CustomersColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "customer_tels_tel_id",
				Columns:    []*schema.Column{CustomerTelsColumns[1]},
				RefColumns: []*schema.Column{TelsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		BillingAddressesTable,
		CustomersTable,
		DeliveryAddressesTable,
		LoginsTable,
		NotesTable,
		OrdersTable,
		PositionsTable,
		RolesTable,
		TelsTable,
		UsersTable,
		CustomerTelsTable,
	}
)

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