migrate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 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 (
	// OrgsColumns holds the columns for the "orgs" table.
	OrgsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString},
		{Name: "slug", Type: field.TypeString, Unique: true},
		{Name: "plan", Type: field.TypeEnum, Enums: []string{"free", "team", "enterprise"}, Default: "free"},
		{Name: "traffic_retention_days", Type: field.TypeInt, Default: 30},
		{Name: "max_proxies", Type: field.TypeInt, Default: 5},
		{Name: "active", Type: field.TypeBool, Default: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
	}
	// OrgsTable holds the schema information for the "orgs" table.
	OrgsTable = &schema.Table{
		Name:       "orgs",
		Columns:    OrgsColumns,
		PrimaryKey: []*schema.Column{OrgsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "org_slug",
				Unique:  false,
				Columns: []*schema.Column{OrgsColumns[2]},
			},
			{
				Name:    "org_active",
				Unique:  false,
				Columns: []*schema.Column{OrgsColumns[6]},
			},
		},
	}
	// ProxiesColumns holds the columns for the "proxies" table.
	ProxiesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString},
		{Name: "slug", Type: field.TypeString},
		{Name: "mode", Type: field.TypeEnum, Enums: []string{"forward", "reverse", "transparent"}, Default: "forward"},
		{Name: "port", Type: field.TypeInt, Default: 8080},
		{Name: "host", Type: field.TypeString, Default: "127.0.0.1"},
		{Name: "mitm_enabled", Type: field.TypeBool, Default: true},
		{Name: "skip_hosts", Type: field.TypeJSON, Nullable: true},
		{Name: "include_hosts", Type: field.TypeJSON, Nullable: true},
		{Name: "exclude_hosts", Type: field.TypeJSON, Nullable: true},
		{Name: "include_paths", Type: field.TypeJSON, Nullable: true},
		{Name: "exclude_paths", Type: field.TypeJSON, Nullable: true},
		{Name: "upstream", Type: field.TypeString, Nullable: true},
		{Name: "skip_binary", Type: field.TypeBool, Default: true},
		{Name: "active", Type: field.TypeBool, Default: true},
		{Name: "last_started_at", Type: field.TypeTime, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "org_proxies", Type: field.TypeInt},
	}
	// ProxiesTable holds the schema information for the "proxies" table.
	ProxiesTable = &schema.Table{
		Name:       "proxies",
		Columns:    ProxiesColumns,
		PrimaryKey: []*schema.Column{ProxiesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "proxies_orgs_proxies",
				Columns:    []*schema.Column{ProxiesColumns[18]},
				RefColumns: []*schema.Column{OrgsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "proxy_slug_org_proxies",
				Unique:  true,
				Columns: []*schema.Column{ProxiesColumns[2], ProxiesColumns[18]},
			},
			{
				Name:    "proxy_active",
				Unique:  false,
				Columns: []*schema.Column{ProxiesColumns[14]},
			},
			{
				Name:    "proxy_mode",
				Unique:  false,
				Columns: []*schema.Column{ProxiesColumns[3]},
			},
		},
	}
	// SessionsColumns holds the columns for the "sessions" table.
	SessionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "token", Type: field.TypeString, Unique: true},
		{Name: "ip_address", Type: field.TypeString, Nullable: true},
		{Name: "user_agent", Type: field.TypeString, Nullable: true},
		{Name: "expires_at", Type: field.TypeTime},
		{Name: "last_active_at", Type: field.TypeTime},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "user_sessions", Type: field.TypeInt},
	}
	// SessionsTable holds the schema information for the "sessions" table.
	SessionsTable = &schema.Table{
		Name:       "sessions",
		Columns:    SessionsColumns,
		PrimaryKey: []*schema.Column{SessionsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "sessions_users_sessions",
				Columns:    []*schema.Column{SessionsColumns[7]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "session_token",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[1]},
			},
			{
				Name:    "session_expires_at",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[4]},
			},
		},
	}
	// TrafficsColumns holds the columns for the "traffics" table.
	TrafficsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "method", Type: field.TypeString},
		{Name: "url", Type: field.TypeString},
		{Name: "scheme", Type: field.TypeString},
		{Name: "host", Type: field.TypeString},
		{Name: "path", Type: field.TypeString},
		{Name: "query", Type: field.TypeString, Nullable: true},
		{Name: "request_headers", Type: field.TypeJSON, Nullable: true},
		{Name: "request_body", Type: field.TypeBytes, Nullable: true},
		{Name: "request_body_size", Type: field.TypeInt64, Default: 0},
		{Name: "request_is_binary", Type: field.TypeBool, Default: false},
		{Name: "content_type", Type: field.TypeString, Nullable: true},
		{Name: "status_code", Type: field.TypeInt},
		{Name: "status_text", Type: field.TypeString, Nullable: true},
		{Name: "response_headers", Type: field.TypeJSON, Nullable: true},
		{Name: "response_body", Type: field.TypeBytes, Nullable: true},
		{Name: "response_body_size", Type: field.TypeInt64, Default: 0},
		{Name: "response_is_binary", Type: field.TypeBool, Default: false},
		{Name: "response_content_type", Type: field.TypeString, Nullable: true},
		{Name: "started_at", Type: field.TypeTime},
		{Name: "duration_ms", Type: field.TypeFloat64},
		{Name: "ttfb_ms", Type: field.TypeFloat64, Nullable: true},
		{Name: "client_ip", Type: field.TypeString, Nullable: true},
		{Name: "error", Type: field.TypeString, Nullable: true},
		{Name: "tags", Type: field.TypeJSON, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "proxy_traffic", Type: field.TypeInt},
	}
	// TrafficsTable holds the schema information for the "traffics" table.
	TrafficsTable = &schema.Table{
		Name:       "traffics",
		Columns:    TrafficsColumns,
		PrimaryKey: []*schema.Column{TrafficsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "traffics_proxies_traffic",
				Columns:    []*schema.Column{TrafficsColumns[26]},
				RefColumns: []*schema.Column{ProxiesColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "traffic_host",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[4]},
			},
			{
				Name:    "traffic_path",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[5]},
			},
			{
				Name:    "traffic_method",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[1]},
			},
			{
				Name:    "traffic_status_code",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[12]},
			},
			{
				Name:    "traffic_started_at",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[19]},
			},
			{
				Name:    "traffic_host_path",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[4], TrafficsColumns[5]},
			},
			{
				Name:    "traffic_method_host_path",
				Unique:  false,
				Columns: []*schema.Column{TrafficsColumns[1], TrafficsColumns[4], TrafficsColumns[5]},
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "email", Type: field.TypeString},
		{Name: "name", Type: field.TypeString, Nullable: true},
		{Name: "password_hash", Type: field.TypeString, Nullable: true},
		{Name: "role", Type: field.TypeEnum, Enums: []string{"owner", "admin", "member", "viewer"}, Default: "member"},
		{Name: "auth_provider", Type: field.TypeEnum, Enums: []string{"local", "google", "github", "oidc"}, Default: "local"},
		{Name: "auth_provider_id", Type: field.TypeString, Nullable: true},
		{Name: "avatar_url", Type: field.TypeString, Nullable: true},
		{Name: "active", Type: field.TypeBool, Default: true},
		{Name: "last_login_at", Type: field.TypeTime, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "org_users", Type: field.TypeInt, 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_orgs_users",
				Columns:    []*schema.Column{UsersColumns[12]},
				RefColumns: []*schema.Column{OrgsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "user_email_org_users",
				Unique:  true,
				Columns: []*schema.Column{UsersColumns[1], UsersColumns[12]},
			},
			{
				Name:    "user_auth_provider_auth_provider_id",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[5], UsersColumns[6]},
			},
			{
				Name:    "user_active",
				Unique:  false,
				Columns: []*schema.Column{UsersColumns[8]},
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		OrgsTable,
		ProxiesTable,
		SessionsTable,
		TrafficsTable,
		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