migrate

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: AGPL-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 (
	// ApprovalRulesColumns holds the columns for the "approval_rules" table.
	ApprovalRulesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "rule_id", Type: field.TypeString, Unique: true},
		{Name: "name", Type: field.TypeString},
		{Name: "tool_name", Type: field.TypeString, Nullable: true},
		{Name: "tool_pattern", Type: field.TypeString, Nullable: true},
		{Name: "tool_category", Type: field.TypeString, Nullable: true},
		{Name: "command_pattern", Type: field.TypeString, Nullable: true},
		{Name: "file_pattern", Type: field.TypeString, Nullable: true},
		{Name: "decision", Type: field.TypeInt},
		{Name: "risk_level", Type: field.TypeInt},
		{Name: "reason", Type: field.TypeString, Nullable: true},
		{Name: "alternative", Type: field.TypeString, Nullable: true},
		{Name: "priority", Type: field.TypeInt, Default: 0},
		{Name: "enabled", Type: field.TypeBool, Default: true},
		{Name: "source", Type: field.TypeString, Default: "user"},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
	}
	// ApprovalRulesTable holds the schema information for the "approval_rules" table.
	ApprovalRulesTable = &schema.Table{
		Name:       "approval_rules",
		Columns:    ApprovalRulesColumns,
		PrimaryKey: []*schema.Column{ApprovalRulesColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "approvalrule_rule_id",
				Unique:  false,
				Columns: []*schema.Column{ApprovalRulesColumns[1]},
			},
			{
				Name:    "approvalrule_priority",
				Unique:  false,
				Columns: []*schema.Column{ApprovalRulesColumns[12]},
			},
			{
				Name:    "approvalrule_enabled",
				Unique:  false,
				Columns: []*schema.Column{ApprovalRulesColumns[13]},
			},
		},
	}
	// ClassificationAnalyticsColumns holds the columns for the "classification_analytics" table.
	ClassificationAnalyticsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "analytics_id", Type: field.TypeString, Unique: true},
		{Name: "session_id", Type: field.TypeString, Nullable: true},
		{Name: "tool_name", Type: field.TypeString},
		{Name: "command_preview", Type: field.TypeString, Nullable: true},
		{Name: "cwd", Type: field.TypeString, Nullable: true},
		{Name: "decision", Type: field.TypeString},
		{Name: "risk_level", Type: field.TypeString},
		{Name: "rule_id", Type: field.TypeString, Nullable: true},
		{Name: "rule_name", Type: field.TypeString, Nullable: true},
		{Name: "reason", Type: field.TypeString, Nullable: true},
		{Name: "alternative", Type: field.TypeString, Nullable: true},
		{Name: "duration_ms", Type: field.TypeInt64, Default: 0},
		{Name: "approval_id", Type: field.TypeString, Nullable: true},
		{Name: "command_program", Type: field.TypeString, Nullable: true},
		{Name: "command_category", Type: field.TypeString, Nullable: true},
		{Name: "command_subcategory", Type: field.TypeString, Nullable: true},
		{Name: "python_imports", Type: field.TypeJSON, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
	}
	// ClassificationAnalyticsTable holds the schema information for the "classification_analytics" table.
	ClassificationAnalyticsTable = &schema.Table{
		Name:       "classification_analytics",
		Columns:    ClassificationAnalyticsColumns,
		PrimaryKey: []*schema.Column{ClassificationAnalyticsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "classificationanalytics_session_id",
				Unique:  false,
				Columns: []*schema.Column{ClassificationAnalyticsColumns[2]},
			},
			{
				Name:    "classificationanalytics_decision",
				Unique:  false,
				Columns: []*schema.Column{ClassificationAnalyticsColumns[6]},
			},
			{
				Name:    "classificationanalytics_risk_level",
				Unique:  false,
				Columns: []*schema.Column{ClassificationAnalyticsColumns[7]},
			},
			{
				Name:    "classificationanalytics_rule_id",
				Unique:  false,
				Columns: []*schema.Column{ClassificationAnalyticsColumns[8]},
			},
			{
				Name:    "classificationanalytics_created_at",
				Unique:  false,
				Columns: []*schema.Column{ClassificationAnalyticsColumns[18]},
			},
		},
	}
	// ClaudeMetadataColumns holds the columns for the "claude_metadata" table.
	ClaudeMetadataColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "key", Type: field.TypeString},
		{Name: "value", Type: field.TypeString},
		{Name: "claude_session_metadata", Type: field.TypeInt},
	}
	// ClaudeMetadataTable holds the schema information for the "claude_metadata" table.
	ClaudeMetadataTable = &schema.Table{
		Name:       "claude_metadata",
		Columns:    ClaudeMetadataColumns,
		PrimaryKey: []*schema.Column{ClaudeMetadataColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "claude_metadata_claude_sessions_metadata",
				Columns:    []*schema.Column{ClaudeMetadataColumns[3]},
				RefColumns: []*schema.Column{ClaudeSessionsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// ClaudeSessionsColumns holds the columns for the "claude_sessions" table.
	ClaudeSessionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "claude_session_id", Type: field.TypeString},
		{Name: "conversation_id", Type: field.TypeString, Nullable: true},
		{Name: "project_name", Type: field.TypeString, Nullable: true},
		{Name: "last_attached", Type: field.TypeTime, Nullable: true},
		{Name: "auto_reattach", Type: field.TypeBool, Default: false},
		{Name: "preferred_session_name", Type: field.TypeString, Nullable: true},
		{Name: "create_new_on_missing", Type: field.TypeBool, Default: false},
		{Name: "show_session_selector", Type: field.TypeBool, Default: false},
		{Name: "session_timeout_minutes", Type: field.TypeInt, Default: 30},
		{Name: "session_claude_session", Type: field.TypeInt, Unique: true},
	}
	// ClaudeSessionsTable holds the schema information for the "claude_sessions" table.
	ClaudeSessionsTable = &schema.Table{
		Name:       "claude_sessions",
		Columns:    ClaudeSessionsColumns,
		PrimaryKey: []*schema.Column{ClaudeSessionsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "claude_sessions_sessions_claude_session",
				Columns:    []*schema.Column{ClaudeSessionsColumns[10]},
				RefColumns: []*schema.Column{SessionsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// DiffStatsColumns holds the columns for the "diff_stats" table.
	DiffStatsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "added", Type: field.TypeInt, Default: 0},
		{Name: "removed", Type: field.TypeInt, Default: 0},
		{Name: "content", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "session_diff_stats", Type: field.TypeInt, Unique: true},
	}
	// DiffStatsTable holds the schema information for the "diff_stats" table.
	DiffStatsTable = &schema.Table{
		Name:       "diff_stats",
		Columns:    DiffStatsColumns,
		PrimaryKey: []*schema.Column{DiffStatsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "diff_stats_sessions_diff_stats",
				Columns:    []*schema.Column{DiffStatsColumns[4]},
				RefColumns: []*schema.Column{SessionsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// SessionsColumns holds the columns for the "sessions" table.
	SessionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "title", Type: field.TypeString, Unique: true},
		{Name: "uuid", Type: field.TypeString, Nullable: true},
		{Name: "path", Type: field.TypeString},
		{Name: "working_dir", Type: field.TypeString, Nullable: true},
		{Name: "branch", Type: field.TypeString, Nullable: true},
		{Name: "status", Type: field.TypeInt},
		{Name: "height", Type: field.TypeInt, Nullable: true},
		{Name: "width", Type: field.TypeInt, Nullable: true},
		{Name: "created_at", Type: field.TypeTime},
		{Name: "updated_at", Type: field.TypeTime},
		{Name: "auto_yes", Type: field.TypeBool, Default: false},
		{Name: "prompt", Type: field.TypeString, Nullable: true},
		{Name: "program", Type: field.TypeString},
		{Name: "existing_worktree", Type: field.TypeString, Nullable: true},
		{Name: "category", Type: field.TypeString, Nullable: true},
		{Name: "is_expanded", Type: field.TypeBool, Default: true},
		{Name: "session_type", Type: field.TypeString, Nullable: true},
		{Name: "tmux_prefix", Type: field.TypeString, Nullable: true},
		{Name: "last_terminal_update", Type: field.TypeTime, Nullable: true},
		{Name: "last_meaningful_output", Type: field.TypeTime, Nullable: true},
		{Name: "last_output_signature", Type: field.TypeString, Nullable: true},
		{Name: "last_added_to_queue", Type: field.TypeTime, Nullable: true},
		{Name: "last_viewed", Type: field.TypeTime, Nullable: true},
		{Name: "last_acknowledged", Type: field.TypeTime, Nullable: true},
	}
	// SessionsTable holds the schema information for the "sessions" table.
	SessionsTable = &schema.Table{
		Name:       "sessions",
		Columns:    SessionsColumns,
		PrimaryKey: []*schema.Column{SessionsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "session_title",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[1]},
			},
			{
				Name:    "session_status",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[6]},
			},
			{
				Name:    "session_category",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[15]},
			},
			{
				Name:    "session_last_meaningful_output",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[20]},
			},
			{
				Name:    "session_last_acknowledged",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[24]},
			},
			{
				Name:    "session_created_at",
				Unique:  false,
				Columns: []*schema.Column{SessionsColumns[9]},
			},
		},
	}
	// TagsColumns holds the columns for the "tags" table.
	TagsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true},
	}
	// TagsTable holds the schema information for the "tags" table.
	TagsTable = &schema.Table{
		Name:       "tags",
		Columns:    TagsColumns,
		PrimaryKey: []*schema.Column{TagsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "tag_name",
				Unique:  false,
				Columns: []*schema.Column{TagsColumns[1]},
			},
		},
	}
	// WorktreesColumns holds the columns for the "worktrees" table.
	WorktreesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "repo_path", Type: field.TypeString},
		{Name: "worktree_path", Type: field.TypeString},
		{Name: "session_name", Type: field.TypeString},
		{Name: "branch_name", Type: field.TypeString},
		{Name: "base_commit_sha", Type: field.TypeString},
		{Name: "session_worktree", Type: field.TypeInt, Unique: true},
	}
	// WorktreesTable holds the schema information for the "worktrees" table.
	WorktreesTable = &schema.Table{
		Name:       "worktrees",
		Columns:    WorktreesColumns,
		PrimaryKey: []*schema.Column{WorktreesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "worktrees_sessions_worktree",
				Columns:    []*schema.Column{WorktreesColumns[6]},
				RefColumns: []*schema.Column{SessionsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
	}
	// SessionTagsColumns holds the columns for the "session_tags" table.
	SessionTagsColumns = []*schema.Column{
		{Name: "session_id", Type: field.TypeInt},
		{Name: "tag_id", Type: field.TypeInt},
	}
	// SessionTagsTable holds the schema information for the "session_tags" table.
	SessionTagsTable = &schema.Table{
		Name:       "session_tags",
		Columns:    SessionTagsColumns,
		PrimaryKey: []*schema.Column{SessionTagsColumns[0], SessionTagsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "session_tags_session_id",
				Columns:    []*schema.Column{SessionTagsColumns[0]},
				RefColumns: []*schema.Column{SessionsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "session_tags_tag_id",
				Columns:    []*schema.Column{SessionTagsColumns[1]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		ApprovalRulesTable,
		ClassificationAnalyticsTable,
		ClaudeMetadataTable,
		ClaudeSessionsTable,
		DiffStatsTable,
		SessionsTable,
		TagsTable,
		WorktreesTable,
		SessionTagsTable,
	}
)

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