migrate

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 7 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 (
	// AgentsColumns holds the columns for the "agents" table.
	AgentsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "slug", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "name", Type: field.TypeString},
		{Name: "description", Type: field.TypeString, Nullable: true},
		{Name: "persona", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "model", Type: field.TypeString, Nullable: true},
		{Name: "provider", Type: field.TypeString, Nullable: true},
		{Name: "visibility", Type: field.TypeEnum, Enums: []string{"private", "listed"}, Default: "private"},
		{Name: "featured", Type: field.TypeBool, Default: false},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "created_by", Type: field.TypeUUID},
	}
	// AgentsTable holds the schema information for the "agents" table.
	AgentsTable = &schema.Table{
		Name:       "agents",
		Columns:    AgentsColumns,
		PrimaryKey: []*schema.Column{AgentsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "agents_users_agents_created",
				Columns:    []*schema.Column{AgentsColumns[11]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// AgentRolesColumns holds the columns for the "agent_roles" table.
	AgentRolesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "role", Type: field.TypeEnum, Enums: []string{"owner", "maintainer"}},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "agent_id", Type: field.TypeUUID},
		{Name: "user_id", Type: field.TypeUUID},
	}
	// AgentRolesTable holds the schema information for the "agent_roles" table.
	AgentRolesTable = &schema.Table{
		Name:       "agent_roles",
		Columns:    AgentRolesColumns,
		PrimaryKey: []*schema.Column{AgentRolesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "agent_roles_agents_roles",
				Columns:    []*schema.Column{AgentRolesColumns[3]},
				RefColumns: []*schema.Column{AgentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "agent_roles_users_agent_roles",
				Columns:    []*schema.Column{AgentRolesColumns[4]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "agentrole_agent_id_user_id",
				Unique:  true,
				Columns: []*schema.Column{AgentRolesColumns[3], AgentRolesColumns[4]},
			},
			{
				Name:    "agentrole_user_id",
				Unique:  false,
				Columns: []*schema.Column{AgentRolesColumns[4]},
			},
		},
	}
	// AgentSkillsColumns holds the columns for the "agent_skills" table.
	AgentSkillsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "skill", Type: field.TypeString, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "agent_id", Type: field.TypeUUID},
	}
	// AgentSkillsTable holds the schema information for the "agent_skills" table.
	AgentSkillsTable = &schema.Table{
		Name:       "agent_skills",
		Columns:    AgentSkillsColumns,
		PrimaryKey: []*schema.Column{AgentSkillsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "agent_skills_agents_skills",
				Columns:    []*schema.Column{AgentSkillsColumns[2]},
				RefColumns: []*schema.Column{AgentsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "agentskill_agent_id_skill",
				Unique:  true,
				Columns: []*schema.Column{AgentSkillsColumns[2], AgentSkillsColumns[1]},
			},
		},
	}
	// AllowlistColumns holds the columns for the "allowlist" table.
	AllowlistColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "email", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "note", Type: field.TypeString, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "added_by", Type: field.TypeUUID},
	}
	// AllowlistTable holds the schema information for the "allowlist" table.
	AllowlistTable = &schema.Table{
		Name:       "allowlist",
		Columns:    AllowlistColumns,
		PrimaryKey: []*schema.Column{AllowlistColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "allowlist_users_allowlist_added",
				Columns:    []*schema.Column{AllowlistColumns[4]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// AuthSessionsColumns holds the columns for the "auth_sessions" table.
	AuthSessionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "token_hash", Type: field.TypeString, Unique: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "last_seen_at", Type: field.TypeTime},
		{Name: "expires_at", Type: field.TypeTime},
		{Name: "user_agent", Type: field.TypeString, Nullable: true},
		{Name: "created_ip", Type: field.TypeString, Nullable: true},
		{Name: "user_id", Type: field.TypeUUID},
	}
	// AuthSessionsTable holds the schema information for the "auth_sessions" table.
	AuthSessionsTable = &schema.Table{
		Name:       "auth_sessions",
		Columns:    AuthSessionsColumns,
		PrimaryKey: []*schema.Column{AuthSessionsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "auth_sessions_users_auth_sessions",
				Columns:    []*schema.Column{AuthSessionsColumns[7]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// ChatsColumns holds the columns for the "chats" table.
	ChatsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "type", Type: field.TypeEnum, Enums: []string{"private", "group"}},
		{Name: "name", Type: field.TypeString, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "agent_id", Type: field.TypeUUID, Nullable: true},
		{Name: "created_by", Type: field.TypeUUID},
	}
	// ChatsTable holds the schema information for the "chats" table.
	ChatsTable = &schema.Table{
		Name:       "chats",
		Columns:    ChatsColumns,
		PrimaryKey: []*schema.Column{ChatsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "chats_agents_agent",
				Columns:    []*schema.Column{ChatsColumns[4]},
				RefColumns: []*schema.Column{AgentsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "chats_users_chats_created",
				Columns:    []*schema.Column{ChatsColumns[5]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "chat_created_by_agent_id",
				Unique:  true,
				Columns: []*schema.Column{ChatsColumns[5], ChatsColumns[4]},
				Annotation: &entsql.IndexAnnotation{
					Where: "type = 'private'",
				},
			},
		},
	}
	// ChatMembersColumns holds the columns for the "chat_members" table.
	ChatMembersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "role", Type: field.TypeEnum, Enums: []string{"owner", "member"}, Default: "member"},
		{Name: "joined_at", Type: field.TypeTime},
		{Name: "chat_id", Type: field.TypeUUID},
		{Name: "user_id", Type: field.TypeUUID},
	}
	// ChatMembersTable holds the schema information for the "chat_members" table.
	ChatMembersTable = &schema.Table{
		Name:       "chat_members",
		Columns:    ChatMembersColumns,
		PrimaryKey: []*schema.Column{ChatMembersColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "chat_members_chats_members",
				Columns:    []*schema.Column{ChatMembersColumns[3]},
				RefColumns: []*schema.Column{ChatsColumns[0]},
				OnDelete:   schema.NoAction,
			},
			{
				Symbol:     "chat_members_users_memberships",
				Columns:    []*schema.Column{ChatMembersColumns[4]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "chatmember_chat_id_user_id",
				Unique:  true,
				Columns: []*schema.Column{ChatMembersColumns[3], ChatMembersColumns[4]},
			},
			{
				Name:    "chatmember_user_id",
				Unique:  false,
				Columns: []*schema.Column{ChatMembersColumns[4]},
			},
		},
	}
	// IdentitiesColumns holds the columns for the "identities" table.
	IdentitiesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "provider", Type: field.TypeEnum, Enums: []string{"magic_link", "google", "github"}},
		{Name: "provider_subject", Type: field.TypeString},
		{Name: "verified_email", Type: field.TypeString},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "user_id", Type: field.TypeUUID},
	}
	// IdentitiesTable holds the schema information for the "identities" table.
	IdentitiesTable = &schema.Table{
		Name:       "identities",
		Columns:    IdentitiesColumns,
		PrimaryKey: []*schema.Column{IdentitiesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "identities_users_identities",
				Columns:    []*schema.Column{IdentitiesColumns[5]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "identity_provider_provider_subject",
				Unique:  true,
				Columns: []*schema.Column{IdentitiesColumns[1], IdentitiesColumns[2]},
			},
		},
	}
	// MagicLinkTokensColumns holds the columns for the "magic_link_tokens" table.
	MagicLinkTokensColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "email", Type: field.TypeString, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "token_hash", Type: field.TypeString, Unique: true},
		{Name: "expires_at", Type: field.TypeTime},
		{Name: "consumed_at", Type: field.TypeTime, Nullable: true},
		{Name: "created_ip", Type: field.TypeString, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
	}
	// MagicLinkTokensTable holds the schema information for the "magic_link_tokens" table.
	MagicLinkTokensTable = &schema.Table{
		Name:       "magic_link_tokens",
		Columns:    MagicLinkTokensColumns,
		PrimaryKey: []*schema.Column{MagicLinkTokensColumns[0]},
	}
	// MessagesColumns holds the columns for the "messages" table.
	MessagesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "author_type", Type: field.TypeEnum, Enums: []string{"user", "agent"}},
		{Name: "content", Type: field.TypeString, Size: 2147483647},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "chat_id", Type: field.TypeUUID},
		{Name: "author_user_id", Type: field.TypeUUID, Nullable: true},
	}
	// MessagesTable holds the schema information for the "messages" table.
	MessagesTable = &schema.Table{
		Name:       "messages",
		Columns:    MessagesColumns,
		PrimaryKey: []*schema.Column{MessagesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "messages_chats_messages",
				Columns:    []*schema.Column{MessagesColumns[4]},
				RefColumns: []*schema.Column{ChatsColumns[0]},
				OnDelete:   schema.NoAction,
			},
			{
				Symbol:     "messages_users_messages",
				Columns:    []*schema.Column{MessagesColumns[5]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "message_chat_id_created_at",
				Unique:  false,
				Columns: []*schema.Column{MessagesColumns[4], MessagesColumns[3]},
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeUUID},
		{Name: "email", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "username", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "citext"}},
		{Name: "display_name", Type: field.TypeString, Nullable: true},
		{Name: "role", Type: field.TypeEnum, Enums: []string{"superadmin", "member"}, Default: "member"},
		{Name: "status", Type: field.TypeEnum, Enums: []string{"active", "disabled"}, Default: "active"},
		{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]},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		AgentsTable,
		AgentRolesTable,
		AgentSkillsTable,
		AllowlistTable,
		AuthSessionsTable,
		ChatsTable,
		ChatMembersTable,
		IdentitiesTable,
		MagicLinkTokensTable,
		MessagesTable,
		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