migrate

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 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 (
	// LoadBalancersColumns holds the columns for the "load_balancers" table.
	LoadBalancersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "created_by", Type: field.TypeString, Nullable: true},
		{Name: "updated_by", Type: field.TypeString, Nullable: true},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "deleted_by", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString, Size: 2147483647},
		{Name: "owner_id", Type: field.TypeString},
		{Name: "location_id", Type: field.TypeString},
		{Name: "provider_id", Type: field.TypeString},
	}
	// LoadBalancersTable holds the schema information for the "load_balancers" table.
	LoadBalancersTable = &schema.Table{
		Name:       "load_balancers",
		Columns:    LoadBalancersColumns,
		PrimaryKey: []*schema.Column{LoadBalancersColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "load_balancers_providers_provider",
				Columns:    []*schema.Column{LoadBalancersColumns[10]},
				RefColumns: []*schema.Column{ProvidersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "loadbalancer_created_at",
				Unique:  false,
				Columns: []*schema.Column{LoadBalancersColumns[1]},
			},
			{
				Name:    "loadbalancer_updated_at",
				Unique:  false,
				Columns: []*schema.Column{LoadBalancersColumns[2]},
			},
			{
				Name:    "loadbalancer_provider_id",
				Unique:  false,
				Columns: []*schema.Column{LoadBalancersColumns[10]},
			},
			{
				Name:    "loadbalancer_location_id",
				Unique:  false,
				Columns: []*schema.Column{LoadBalancersColumns[9]},
			},
			{
				Name:    "loadbalancer_owner_id",
				Unique:  false,
				Columns: []*schema.Column{LoadBalancersColumns[8]},
			},
		},
	}
	// OriginsColumns holds the columns for the "origins" table.
	OriginsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "deleted_by", Type: field.TypeString, Nullable: true},
		{Name: "created_by", Type: field.TypeString, Nullable: true},
		{Name: "updated_by", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString},
		{Name: "weight", Type: field.TypeInt32, Default: 100},
		{Name: "target", Type: field.TypeString},
		{Name: "port_number", Type: field.TypeInt},
		{Name: "active", Type: field.TypeBool, Default: true},
		{Name: "pool_id", Type: field.TypeString},
	}
	// OriginsTable holds the schema information for the "origins" table.
	OriginsTable = &schema.Table{
		Name:       "origins",
		Columns:    OriginsColumns,
		PrimaryKey: []*schema.Column{OriginsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "origins_pools_pool",
				Columns:    []*schema.Column{OriginsColumns[12]},
				RefColumns: []*schema.Column{PoolsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "origin_created_at",
				Unique:  false,
				Columns: []*schema.Column{OriginsColumns[1]},
			},
			{
				Name:    "origin_updated_at",
				Unique:  false,
				Columns: []*schema.Column{OriginsColumns[2]},
			},
			{
				Name:    "origin_pool_id",
				Unique:  false,
				Columns: []*schema.Column{OriginsColumns[12]},
			},
		},
	}
	// PoolsColumns holds the columns for the "pools" table.
	PoolsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "created_by", Type: field.TypeString, Nullable: true},
		{Name: "updated_by", Type: field.TypeString, Nullable: true},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "deleted_by", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString},
		{Name: "protocol", Type: field.TypeEnum, Enums: []string{"tcp", "udp"}},
		{Name: "owner_id", Type: field.TypeString},
	}
	// PoolsTable holds the schema information for the "pools" table.
	PoolsTable = &schema.Table{
		Name:       "pools",
		Columns:    PoolsColumns,
		PrimaryKey: []*schema.Column{PoolsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "pool_created_at",
				Unique:  false,
				Columns: []*schema.Column{PoolsColumns[1]},
			},
			{
				Name:    "pool_updated_at",
				Unique:  false,
				Columns: []*schema.Column{PoolsColumns[2]},
			},
			{
				Name:    "pool_owner_id",
				Unique:  false,
				Columns: []*schema.Column{PoolsColumns[9]},
			},
		},
	}
	// PortsColumns holds the columns for the "ports" table.
	PortsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "deleted_by", Type: field.TypeString, Nullable: true},
		{Name: "created_by", Type: field.TypeString, Nullable: true},
		{Name: "updated_by", Type: field.TypeString, Nullable: true},
		{Name: "number", Type: field.TypeInt},
		{Name: "name", Type: field.TypeString},
		{Name: "load_balancer_id", Type: field.TypeString},
	}
	// PortsTable holds the schema information for the "ports" table.
	PortsTable = &schema.Table{
		Name:       "ports",
		Columns:    PortsColumns,
		PrimaryKey: []*schema.Column{PortsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "ports_load_balancers_load_balancer",
				Columns:    []*schema.Column{PortsColumns[9]},
				RefColumns: []*schema.Column{LoadBalancersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "port_created_at",
				Unique:  false,
				Columns: []*schema.Column{PortsColumns[1]},
			},
			{
				Name:    "port_updated_at",
				Unique:  false,
				Columns: []*schema.Column{PortsColumns[2]},
			},
			{
				Name:    "port_load_balancer_id",
				Unique:  false,
				Columns: []*schema.Column{PortsColumns[9]},
			},
			{
				Name:    "port_load_balancer_id_number",
				Unique:  true,
				Columns: []*schema.Column{PortsColumns[9], PortsColumns[7]},
			},
		},
	}
	// ProvidersColumns holds the columns for the "providers" table.
	ProvidersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "deleted_at", Type: field.TypeTime, Nullable: true},
		{Name: "deleted_by", Type: field.TypeString, Nullable: true},
		{Name: "created_by", Type: field.TypeString, Nullable: true},
		{Name: "updated_by", Type: field.TypeString, Nullable: true},
		{Name: "name", Type: field.TypeString},
		{Name: "owner_id", Type: field.TypeString},
	}
	// ProvidersTable holds the schema information for the "providers" table.
	ProvidersTable = &schema.Table{
		Name:       "providers",
		Columns:    ProvidersColumns,
		PrimaryKey: []*schema.Column{ProvidersColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "provider_created_at",
				Unique:  false,
				Columns: []*schema.Column{ProvidersColumns[1]},
			},
			{
				Name:    "provider_updated_at",
				Unique:  false,
				Columns: []*schema.Column{ProvidersColumns[2]},
			},
			{
				Name:    "provider_owner_id",
				Unique:  false,
				Columns: []*schema.Column{ProvidersColumns[8]},
			},
		},
	}
	// PoolPortsColumns holds the columns for the "pool_ports" table.
	PoolPortsColumns = []*schema.Column{
		{Name: "pool_id", Type: field.TypeString},
		{Name: "port_id", Type: field.TypeString},
	}
	// PoolPortsTable holds the schema information for the "pool_ports" table.
	PoolPortsTable = &schema.Table{
		Name:       "pool_ports",
		Columns:    PoolPortsColumns,
		PrimaryKey: []*schema.Column{PoolPortsColumns[0], PoolPortsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "pool_ports_pool_id",
				Columns:    []*schema.Column{PoolPortsColumns[0]},
				RefColumns: []*schema.Column{PoolsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "pool_ports_port_id",
				Columns:    []*schema.Column{PoolPortsColumns[1]},
				RefColumns: []*schema.Column{PortsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		LoadBalancersTable,
		OriginsTable,
		PoolsTable,
		PortsTable,
		ProvidersTable,
		PoolPortsTable,
	}
)

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.

func Diff

func Diff(ctx context.Context, url string, opts ...schema.MigrateOption) error

Diff compares the state read from a database connection or migration directory with the state defined by the Ent schema. Changes will be written to new migration files.

func NamedDiff

func NamedDiff(ctx context.Context, url, name string, opts ...schema.MigrateOption) error

NamedDiff compares the state read from a database connection or migration directory with the state defined by the Ent schema. Changes will be written to new named migration files.

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) Diff

func (s *Schema) Diff(ctx context.Context, opts ...schema.MigrateOption) error

Diff creates a migration file containing the statements to resolve the diff between the Ent schema and the connected database.

func (*Schema) NamedDiff

func (s *Schema) NamedDiff(ctx context.Context, name string, opts ...schema.MigrateOption) error

NamedDiff creates a named migration file containing the statements to resolve the diff between the Ent schema and the connected database.

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