migrate

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 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
	// WithFixture sets the foreign-key renaming option to the migration when upgrading
	// ent from v0.1.0 (issue-#285). Defaults to false.
	WithFixture = schema.WithFixture
	// WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
	WithForeignKeys = schema.WithForeignKeys
)
View Source
var (
	// CredentialsColumns holds the columns for the "credentials" table.
	CredentialsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "principal", Type: field.TypeString},
		{Name: "secret", Type: field.TypeString, Size: 3000},
		{Name: "kind", Type: field.TypeEnum, Enums: []string{"password", "key", "certificate"}},
		{Name: "fails", Type: field.TypeInt, Default: 0},
		{Name: "target_credentials", Type: field.TypeInt, Nullable: true},
	}
	// CredentialsTable holds the schema information for the "credentials" table.
	CredentialsTable = &schema.Table{
		Name:       "credentials",
		Columns:    CredentialsColumns,
		PrimaryKey: []*schema.Column{CredentialsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "credentials_targets_credentials",
				Columns:    []*schema.Column{CredentialsColumns[5]},
				RefColumns: []*schema.Column{TargetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// EventsColumns holds the columns for the "events" table.
	EventsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "creation_time", Type: field.TypeTime},
		{Name: "kind", Type: field.TypeEnum, Enums: []string{"CREATE_JOB", "CREATE_TAG", "APPLY_TAG_TO_TASK", "APPLY_TAG_TO_TARGET", "APPLY_TAG_TO_JOB", "REMOVE_TAG_FROM_TASK", "REMOVE_TAG_FROM_TARGET", "REMOVE_TAG_FROM_JOB", "CREATE_TARGET", "SET_TARGET_FIELDS", "DELETE_TARGET", "ADD_CREDENTIAL_FOR_TARGET", "UPLOAD_FILE", "CREATE_LINK", "SET_LINK_FIELDS", "ACTIVATE_USER", "CREATE_USER", "MAKE_ADMIN", "REMOVE_ADMIN", "CHANGE_NAME", "ACTIVATE_SERVICE", "CREATE_SERVICE", "LIKE_EVENT", "OTHER"}},
		{Name: "event_job", Type: field.TypeInt, Nullable: true},
		{Name: "event_file", Type: field.TypeInt, Nullable: true},
		{Name: "event_credential", Type: field.TypeInt, Nullable: true},
		{Name: "event_link", Type: field.TypeInt, Nullable: true},
		{Name: "event_tag", Type: field.TypeInt, Nullable: true},
		{Name: "event_target", Type: field.TypeInt, Nullable: true},
		{Name: "event_task", Type: field.TypeInt, Nullable: true},
		{Name: "event_user", Type: field.TypeInt, Nullable: true},
		{Name: "event_event", Type: field.TypeInt, Unique: true, Nullable: true},
		{Name: "event_service", Type: field.TypeInt, Nullable: true},
		{Name: "service_events", Type: field.TypeInt, Nullable: true},
		{Name: "user_events", Type: field.TypeInt, Nullable: true},
	}
	// EventsTable holds the schema information for the "events" table.
	EventsTable = &schema.Table{
		Name:       "events",
		Columns:    EventsColumns,
		PrimaryKey: []*schema.Column{EventsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "events_jobs_job",
				Columns:    []*schema.Column{EventsColumns[3]},
				RefColumns: []*schema.Column{JobsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_files_file",
				Columns:    []*schema.Column{EventsColumns[4]},
				RefColumns: []*schema.Column{FilesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_credentials_credential",
				Columns:    []*schema.Column{EventsColumns[5]},
				RefColumns: []*schema.Column{CredentialsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_links_link",
				Columns:    []*schema.Column{EventsColumns[6]},
				RefColumns: []*schema.Column{LinksColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_tags_tag",
				Columns:    []*schema.Column{EventsColumns[7]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_targets_target",
				Columns:    []*schema.Column{EventsColumns[8]},
				RefColumns: []*schema.Column{TargetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_tasks_task",
				Columns:    []*schema.Column{EventsColumns[9]},
				RefColumns: []*schema.Column{TasksColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_users_user",
				Columns:    []*schema.Column{EventsColumns[10]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_events_event",
				Columns:    []*schema.Column{EventsColumns[11]},
				RefColumns: []*schema.Column{EventsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_services_service",
				Columns:    []*schema.Column{EventsColumns[12]},
				RefColumns: []*schema.Column{ServicesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_services_events",
				Columns:    []*schema.Column{EventsColumns[13]},
				RefColumns: []*schema.Column{ServicesColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "events_users_events",
				Columns:    []*schema.Column{EventsColumns[14]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// FilesColumns holds the columns for the "files" table.
	FilesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true},
		{Name: "creation_time", Type: field.TypeTime},
		{Name: "last_modified_time", Type: field.TypeTime},
		{Name: "size", Type: field.TypeInt, Default: 0},
		{Name: "content", Type: field.TypeBytes, Size: 4294967295},
		{Name: "hash", Type: field.TypeString, Size: 100},
		{Name: "content_type", Type: field.TypeString},
	}
	// FilesTable holds the schema information for the "files" table.
	FilesTable = &schema.Table{
		Name:       "files",
		Columns:    FilesColumns,
		PrimaryKey: []*schema.Column{FilesColumns[0]},
	}
	// JobsColumns holds the columns for the "jobs" table.
	JobsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString},
		{Name: "creation_time", Type: field.TypeTime},
		{Name: "content", Type: field.TypeString, Size: 2147483647},
		{Name: "staged", Type: field.TypeBool},
		{Name: "job_next", Type: field.TypeInt, Unique: true, Nullable: true},
		{Name: "user_jobs", Type: field.TypeInt, Nullable: true},
	}
	// JobsTable holds the schema information for the "jobs" table.
	JobsTable = &schema.Table{
		Name:       "jobs",
		Columns:    JobsColumns,
		PrimaryKey: []*schema.Column{JobsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "jobs_jobs_next",
				Columns:    []*schema.Column{JobsColumns[5]},
				RefColumns: []*schema.Column{JobsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "jobs_users_jobs",
				Columns:    []*schema.Column{JobsColumns[6]},
				RefColumns: []*schema.Column{UsersColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// LinksColumns holds the columns for the "links" table.
	LinksColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "alias", Type: field.TypeString, Unique: true},
		{Name: "expiration_time", Type: field.TypeTime, Nullable: true},
		{Name: "clicks", Type: field.TypeInt, Default: -1},
		{Name: "file_links", Type: field.TypeInt, Nullable: true},
	}
	// LinksTable holds the schema information for the "links" table.
	LinksTable = &schema.Table{
		Name:       "links",
		Columns:    LinksColumns,
		PrimaryKey: []*schema.Column{LinksColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "links_files_links",
				Columns:    []*schema.Column{LinksColumns[4]},
				RefColumns: []*schema.Column{FilesColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// ServicesColumns holds the columns for the "services" table.
	ServicesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString},
		{Name: "pub_key", Type: field.TypeString, Unique: true, Size: 250},
		{Name: "config", Type: field.TypeString, Size: 2147483647, Default: ""},
		{Name: "is_activated", Type: field.TypeBool, Default: false},
		{Name: "service_tag", Type: field.TypeInt, Nullable: true},
	}
	// ServicesTable holds the schema information for the "services" table.
	ServicesTable = &schema.Table{
		Name:       "services",
		Columns:    ServicesColumns,
		PrimaryKey: []*schema.Column{ServicesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "services_tags_tag",
				Columns:    []*schema.Column{ServicesColumns[5]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// 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]},
	}
	// TargetsColumns holds the columns for the "targets" table.
	TargetsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Unique: true},
		{Name: "os", Type: field.TypeEnum, Enums: []string{"LINUX", "WINDOWS", "BSD", "MACOS"}},
		{Name: "primary_ip", Type: field.TypeString},
		{Name: "machine_uuid", Type: field.TypeString, Unique: true, Nullable: true, Size: 250},
		{Name: "public_ip", Type: field.TypeString, Nullable: true},
		{Name: "primary_mac", Type: field.TypeString, Nullable: true},
		{Name: "hostname", Type: field.TypeString, Nullable: true},
		{Name: "last_seen", Type: field.TypeTime, Nullable: true},
	}
	// TargetsTable holds the schema information for the "targets" table.
	TargetsTable = &schema.Table{
		Name:       "targets",
		Columns:    TargetsColumns,
		PrimaryKey: []*schema.Column{TargetsColumns[0]},
	}
	// TasksColumns holds the columns for the "tasks" table.
	TasksColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "queue_time", Type: field.TypeTime},
		{Name: "last_changed_time", Type: field.TypeTime},
		{Name: "claim_time", Type: field.TypeTime, Nullable: true},
		{Name: "exec_start_time", Type: field.TypeTime, Nullable: true},
		{Name: "exec_stop_time", Type: field.TypeTime, Nullable: true},
		{Name: "content", Type: field.TypeString, Size: 2147483647},
		{Name: "output", Type: field.TypeString, Nullable: true, Size: 2147483647},
		{Name: "error", Type: field.TypeString, Nullable: true},
		{Name: "session_id", Type: field.TypeString, Nullable: true, Size: 250},
		{Name: "job_tasks", Type: field.TypeInt, Nullable: true},
		{Name: "target_tasks", Type: field.TypeInt, Nullable: true},
	}
	// TasksTable holds the schema information for the "tasks" table.
	TasksTable = &schema.Table{
		Name:       "tasks",
		Columns:    TasksColumns,
		PrimaryKey: []*schema.Column{TasksColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "tasks_jobs_tasks",
				Columns:    []*schema.Column{TasksColumns[10]},
				RefColumns: []*schema.Column{JobsColumns[0]},
				OnDelete:   schema.SetNull,
			},
			{
				Symbol:     "tasks_targets_tasks",
				Columns:    []*schema.Column{TasksColumns[11]},
				RefColumns: []*schema.Column{TargetsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// UsersColumns holds the columns for the "users" table.
	UsersColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt, Increment: true},
		{Name: "name", Type: field.TypeString, Size: 25},
		{Name: "oauth_id", Type: field.TypeString, Unique: true},
		{Name: "photo_url", Type: field.TypeString},
		{Name: "session_token", Type: field.TypeString, Nullable: true, Size: 1000},
		{Name: "is_activated", Type: field.TypeBool, Default: false},
		{Name: "is_admin", Type: field.TypeBool, Default: false},
		{Name: "event_likers", 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_events_likers",
				Columns:    []*schema.Column{UsersColumns[7]},
				RefColumns: []*schema.Column{EventsColumns[0]},
				OnDelete:   schema.SetNull,
			},
		},
	}
	// JobTagsColumns holds the columns for the "job_tags" table.
	JobTagsColumns = []*schema.Column{
		{Name: "job_id", Type: field.TypeInt},
		{Name: "tag_id", Type: field.TypeInt},
	}
	// JobTagsTable holds the schema information for the "job_tags" table.
	JobTagsTable = &schema.Table{
		Name:       "job_tags",
		Columns:    JobTagsColumns,
		PrimaryKey: []*schema.Column{JobTagsColumns[0], JobTagsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "job_tags_job_id",
				Columns:    []*schema.Column{JobTagsColumns[0]},
				RefColumns: []*schema.Column{JobsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "job_tags_tag_id",
				Columns:    []*schema.Column{JobTagsColumns[1]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// TargetTagsColumns holds the columns for the "target_tags" table.
	TargetTagsColumns = []*schema.Column{
		{Name: "target_id", Type: field.TypeInt},
		{Name: "tag_id", Type: field.TypeInt},
	}
	// TargetTagsTable holds the schema information for the "target_tags" table.
	TargetTagsTable = &schema.Table{
		Name:       "target_tags",
		Columns:    TargetTagsColumns,
		PrimaryKey: []*schema.Column{TargetTagsColumns[0], TargetTagsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "target_tags_target_id",
				Columns:    []*schema.Column{TargetTagsColumns[0]},
				RefColumns: []*schema.Column{TargetsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "target_tags_tag_id",
				Columns:    []*schema.Column{TargetTagsColumns[1]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// TaskTagsColumns holds the columns for the "task_tags" table.
	TaskTagsColumns = []*schema.Column{
		{Name: "task_id", Type: field.TypeInt},
		{Name: "tag_id", Type: field.TypeInt},
	}
	// TaskTagsTable holds the schema information for the "task_tags" table.
	TaskTagsTable = &schema.Table{
		Name:       "task_tags",
		Columns:    TaskTagsColumns,
		PrimaryKey: []*schema.Column{TaskTagsColumns[0], TaskTagsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "task_tags_task_id",
				Columns:    []*schema.Column{TaskTagsColumns[0]},
				RefColumns: []*schema.Column{TasksColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "task_tags_tag_id",
				Columns:    []*schema.Column{TaskTagsColumns[1]},
				RefColumns: []*schema.Column{TagsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		CredentialsTable,
		EventsTable,
		FilesTable,
		JobsTable,
		LinksTable,
		ServicesTable,
		TagsTable,
		TargetsTable,
		TasksTable,
		UsersTable,
		JobTagsTable,
		TargetTagsTable,
		TaskTagsTable,
	}
)

Functions

This section is empty.

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