migrate

package
v0.0.0-...-1ac7f0e Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: GPL-3.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 (
	// AuthenticationsColumns holds the columns for the "authentications" table.
	AuthenticationsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID, Default: "gen_random_ulid()"},
		{Name: "token", Type: field.TypeBytes, Unique: true, SchemaType: map[string]string{"postgres": "bytes"}},
		{Name: "created_ip", Type: field.TypeString, SchemaType: map[string]string{"postgres": "inet"}},
		{Name: "last_used_ip", Type: field.TypeString, SchemaType: map[string]string{"postgres": "inet"}},
		{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamp"}},
		{Name: "last_used_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamp"}},
		{Name: "person_id", Type: field.TypeUUID},
	}
	// AuthenticationsTable holds the schema information for the "authentications" table.
	AuthenticationsTable = &schema.Table{
		Name:       "authentications",
		Columns:    AuthenticationsColumns,
		PrimaryKey: []*schema.Column{AuthenticationsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "authentications_persons_authentications",
				Columns:    []*schema.Column{AuthenticationsColumns[6]},
				RefColumns: []*schema.Column{PersonsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// AuthorizationsColumns holds the columns for the "authorizations" table.
	AuthorizationsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID, Default: "gen_random_ulid()"},
		{Name: "token", Type: field.TypeBytes, Unique: true, SchemaType: map[string]string{"postgres": "bytes"}},
		{Name: "kind", Type: field.TypeEnum, Enums: []string{"email", "password"}, SchemaType: map[string]string{"postgres": "authorization_kind"}},
		{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamp"}},
		{Name: "person_id", Type: field.TypeUUID},
	}
	// AuthorizationsTable holds the schema information for the "authorizations" table.
	AuthorizationsTable = &schema.Table{
		Name:       "authorizations",
		Columns:    AuthorizationsColumns,
		PrimaryKey: []*schema.Column{AuthorizationsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "authorizations_persons_authorizations",
				Columns:    []*schema.Column{AuthorizationsColumns[4]},
				RefColumns: []*schema.Column{PersonsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "authorization_person_id_kind",
				Unique:  true,
				Columns: []*schema.Column{AuthorizationsColumns[4], AuthorizationsColumns[2]},
			},
		},
	}
	// PersonsColumns holds the columns for the "persons" table.
	PersonsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID, Default: "gen_random_ulid()"},
		{Name: "stripe_id", Type: field.TypeString, Unique: true, Nullable: true, SchemaType: map[string]string{"postgres": "string(255)"}},
		{Name: "email", Type: field.TypeString, Unique: true, Size: 254, SchemaType: map[string]string{"postgres": "string(254)"}},
		{Name: "email_verified_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamp"}},
		{Name: "phone", Type: field.TypeString, Unique: true, Nullable: true, Size: 16, SchemaType: map[string]string{"postgres": "string(16)"}},
		{Name: "password", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(97)"}},
		{Name: "tax_id", Type: field.TypeString, Unique: true, Size: 9, SchemaType: map[string]string{"postgres": "char(9)"}},
		{Name: "first_name", Type: field.TypeString, Size: 50, SchemaType: map[string]string{"postgres": "string(50)"}},
		{Name: "last_name", Type: field.TypeString, Nullable: true, Size: 50, SchemaType: map[string]string{"postgres": "string(50)"}},
		{Name: "language", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(2)"}},
		{Name: "birthdate", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "date"}},
		{Name: "gender", Type: field.TypeEnum, Nullable: true, Enums: []string{"woman", "man", "nonbinary"}, SchemaType: map[string]string{"postgres": "gender"}},
		{Name: "address", Type: field.TypeString, Nullable: true, Size: 100, SchemaType: map[string]string{"postgres": "string(100)"}},
		{Name: "postal_code", Type: field.TypeString, Nullable: true, Size: 10, SchemaType: map[string]string{"postgres": "string(10)"}},
		{Name: "city", Type: field.TypeString, Nullable: true, Size: 58, SchemaType: map[string]string{"postgres": "string(58)"}},
		{Name: "country", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "char(2)"}},
		{Name: "subscribed", Type: field.TypeBool, Default: false},
		{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamp"}},
		{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamp"}},
	}
	// PersonsTable holds the schema information for the "persons" table.
	PersonsTable = &schema.Table{
		Name:       "persons",
		Columns:    PersonsColumns,
		PrimaryKey: []*schema.Column{PersonsColumns[0]},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		AuthenticationsTable,
		AuthorizationsTable,
		PersonsTable,
	}
)

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