migrate

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 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 (
	// CacheEntriesColumns holds the columns for the "cache_entries" table.
	CacheEntriesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
		{Name: "key", Type: field.TypeString, Size: 512, SchemaType: map[string]string{"mysql": "varchar(512)", "postgres": "text", "sqlite3": "text"}},
		{Name: "version", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "scope", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "repoId", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "updatedAt", Type: field.TypeInt64},
		{Name: "locationId", Type: field.TypeString, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
	}
	// CacheEntriesTable holds the schema information for the "cache_entries" table.
	CacheEntriesTable = &schema.Table{
		Name:       "cache_entries",
		Columns:    CacheEntriesColumns,
		PrimaryKey: []*schema.Column{CacheEntriesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "cache_entries_storage_locations_cacheEntries",
				Columns:    []*schema.Column{CacheEntriesColumns[6]},
				RefColumns: []*schema.Column{StorageLocationsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "idx_cache_entries_key_version",
				Unique:  false,
				Columns: []*schema.Column{CacheEntriesColumns[1], CacheEntriesColumns[2]},
			},
			{
				Name:    "idx_cache_entries_scope",
				Unique:  false,
				Columns: []*schema.Column{CacheEntriesColumns[3]},
			},
			{
				Name:    "idx_cache_entries_repoId",
				Unique:  false,
				Columns: []*schema.Column{CacheEntriesColumns[4]},
			},
			{
				Name:    "idx_cache_entries_repo_scope_version_key",
				Unique:  false,
				Columns: []*schema.Column{CacheEntriesColumns[4], CacheEntriesColumns[3], CacheEntriesColumns[2], CacheEntriesColumns[1]},
				Annotation: &entsql.IndexAnnotation{
					PrefixColumns: map[string]uint{
						CacheEntriesColumns[1].Name: 191,

						CacheEntriesColumns[4].Name: 64,

						CacheEntriesColumns[3].Name: 191,

						CacheEntriesColumns[2].Name: 64,
					},
					OpClassColumns: map[string]string{
						CacheEntriesColumns[1].Name: "text_pattern_ops",
					},
				},
			},
			{
				Name:    "idx_cache_entries_location_updated_at",
				Unique:  false,
				Columns: []*schema.Column{CacheEntriesColumns[6], CacheEntriesColumns[5]},
			},
		},
	}
	// StorageDeletionsColumns holds the columns for the "storage_deletions" table.
	StorageDeletionsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "folderName", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text", "postgres": "text", "sqlite3": "text"}},
		{Name: "createdAt", Type: field.TypeInt64},
		{Name: "attemptCount", Type: field.TypeInt, Default: 0},
		{Name: "lastAttemptedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "lastError", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "text", "postgres": "text", "sqlite3": "text"}},
	}
	// StorageDeletionsTable holds the schema information for the "storage_deletions" table.
	StorageDeletionsTable = &schema.Table{
		Name:       "storage_deletions",
		Columns:    StorageDeletionsColumns,
		PrimaryKey: []*schema.Column{StorageDeletionsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "idx_storage_deletions_folder_name",
				Unique:  false,
				Columns: []*schema.Column{StorageDeletionsColumns[1]},
				Annotation: &entsql.IndexAnnotation{
					PrefixColumns: map[string]uint{
						StorageDeletionsColumns[1].Name: 191,
					},
					OpClassColumns: map[string]string{
						StorageDeletionsColumns[1].Name: "text_pattern_ops",
					},
				},
			},
		},
	}
	// StorageLocationsColumns holds the columns for the "storage_locations" table.
	StorageLocationsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
		{Name: "folderName", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text", "postgres": "text", "sqlite3": "text"}},
		{Name: "partCount", Type: field.TypeInt},
		{Name: "sizeBytes", Type: field.TypeInt64, Nullable: true},
		{Name: "leaseVersion", Type: field.TypeInt64, Default: 0},
		{Name: "deletionRequestedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "mergeStartedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "mergeLeaseToken", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
		{Name: "mergeLeaseExpiresAt", Type: field.TypeInt64, Nullable: true},
		{Name: "mergedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "materializationUnsupportedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "partsDeletedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "lastDownloadedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "recencyAt", Type: field.TypeInt64, Default: 0},
	}
	// StorageLocationsTable holds the schema information for the "storage_locations" table.
	StorageLocationsTable = &schema.Table{
		Name:       "storage_locations",
		Columns:    StorageLocationsColumns,
		PrimaryKey: []*schema.Column{StorageLocationsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "idx_storage_locations_folder_name",
				Unique:  false,
				Columns: []*schema.Column{StorageLocationsColumns[1]},
				Annotation: &entsql.IndexAnnotation{
					PrefixColumns: map[string]uint{
						StorageLocationsColumns[1].Name: 191,
					},
					OpClassColumns: map[string]string{
						StorageLocationsColumns[1].Name: "text_pattern_ops",
					},
				},
			},
			{
				Name:    "idx_storage_locations_recency",
				Unique:  false,
				Columns: []*schema.Column{StorageLocationsColumns[13], StorageLocationsColumns[0]},
			},
		},
	}
	// StorageReaderLeasesColumns holds the columns for the "storage_reader_leases" table.
	StorageReaderLeasesColumns = []*schema.Column{
		{Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
		{Name: "scope", Type: field.TypeEnum, Enums: []string{"parts", "storage"}},
		{Name: "expiresAt", Type: field.TypeInt64},
		{Name: "storageLocationId", Type: field.TypeString, SchemaType: map[string]string{"mysql": "varchar(36)", "postgres": "text", "sqlite3": "text"}},
	}
	// StorageReaderLeasesTable holds the schema information for the "storage_reader_leases" table.
	StorageReaderLeasesTable = &schema.Table{
		Name:       "storage_reader_leases",
		Columns:    StorageReaderLeasesColumns,
		PrimaryKey: []*schema.Column{StorageReaderLeasesColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "storage_reader_leases_storage_locations_readerLeases",
				Columns:    []*schema.Column{StorageReaderLeasesColumns[3]},
				RefColumns: []*schema.Column{StorageLocationsColumns[0]},
				OnDelete:   schema.Restrict,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "idx_storage_reader_leases_location_scope_expiry",
				Unique:  false,
				Columns: []*schema.Column{StorageReaderLeasesColumns[3], StorageReaderLeasesColumns[1], StorageReaderLeasesColumns[2]},
			},
		},
	}
	// UploadsColumns holds the columns for the "uploads" table.
	UploadsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64},
		{Name: "key", Type: field.TypeString, Size: 512, SchemaType: map[string]string{"mysql": "varchar(512)", "postgres": "text", "sqlite3": "text"}},
		{Name: "version", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "scope", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "repoId", Type: field.TypeString, Size: 255, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "text", "sqlite3": "text"}},
		{Name: "createdAt", Type: field.TypeInt64},
		{Name: "lastPartUploadedAt", Type: field.TypeInt64, Nullable: true},
		{Name: "startedPartUploadCount", Type: field.TypeInt, Default: 0},
		{Name: "finishedPartUploadCount", Type: field.TypeInt, Default: 0},
		{Name: "folderName", Type: field.TypeString, SchemaType: map[string]string{"mysql": "text", "postgres": "text", "sqlite3": "text"}},
		{Name: "committedPartCount", Type: field.TypeInt, Nullable: true},
		{Name: "tupleHash", Type: field.TypeString, Nullable: true, Size: 64, SchemaType: map[string]string{"mysql": "varchar(64)", "postgres": "text", "sqlite3": "text"}},
	}
	// UploadsTable holds the schema information for the "uploads" table.
	UploadsTable = &schema.Table{
		Name:       "uploads",
		Columns:    UploadsColumns,
		PrimaryKey: []*schema.Column{UploadsColumns[0]},
		Indexes: []*schema.Index{
			{
				Name:    "idx_uploads_key_version",
				Unique:  false,
				Columns: []*schema.Column{UploadsColumns[1], UploadsColumns[2]},
			},
			{
				Name:    "idx_uploads_scope",
				Unique:  false,
				Columns: []*schema.Column{UploadsColumns[3]},
			},
			{
				Name:    "idx_uploads_repoId",
				Unique:  false,
				Columns: []*schema.Column{UploadsColumns[4]},
			},
			{
				Name:    "idx_uploads_folder_name",
				Unique:  false,
				Columns: []*schema.Column{UploadsColumns[9]},
				Annotation: &entsql.IndexAnnotation{
					PrefixColumns: map[string]uint{
						UploadsColumns[9].Name: 191,
					},
					OpClassColumns: map[string]string{
						UploadsColumns[9].Name: "text_pattern_ops",
					},
				},
			},
			{
				Name:    "idx_uploads_tuple_hash",
				Unique:  true,
				Columns: []*schema.Column{UploadsColumns[11]},
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		CacheEntriesTable,
		StorageDeletionsTable,
		StorageLocationsTable,
		StorageReaderLeasesTable,
		UploadsTable,
	}
)

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