migrate

package
v0.0.0-...-ab1f78f Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: Apache-2.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 (
	// ItemsColumns holds the columns for the "items" table.
	ItemsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "identifier", Type: field.TypeString, Unique: true},
	}
	// ItemsTable holds the schema information for the "items" table.
	ItemsTable = &schema.Table{
		Name:       "items",
		Columns:    ItemsColumns,
		PrimaryKey: []*schema.Column{ItemsColumns[0]},
	}
	// MetadataFormatsColumns holds the columns for the "metadata_formats" table.
	MetadataFormatsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "prefix", Type: field.TypeString, Unique: true},
		{Name: "schema", Type: field.TypeString},
		{Name: "namespace", Type: field.TypeString},
	}
	// MetadataFormatsTable holds the schema information for the "metadata_formats" table.
	MetadataFormatsTable = &schema.Table{
		Name:       "metadata_formats",
		Columns:    MetadataFormatsColumns,
		PrimaryKey: []*schema.Column{MetadataFormatsColumns[0]},
	}
	// RecordsColumns holds the columns for the "records" table.
	RecordsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "metadata", Type: field.TypeString, Nullable: true},
		{Name: "datestamp", Type: field.TypeTime},
		{Name: "item_id", Type: field.TypeInt64},
		{Name: "metadata_format_id", Type: field.TypeInt64},
	}
	// RecordsTable holds the schema information for the "records" table.
	RecordsTable = &schema.Table{
		Name:       "records",
		Columns:    RecordsColumns,
		PrimaryKey: []*schema.Column{RecordsColumns[0]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "records_items_records",
				Columns:    []*schema.Column{RecordsColumns[3]},
				RefColumns: []*schema.Column{ItemsColumns[0]},
				OnDelete:   schema.NoAction,
			},
			{
				Symbol:     "records_metadata_formats_records",
				Columns:    []*schema.Column{RecordsColumns[4]},
				RefColumns: []*schema.Column{MetadataFormatsColumns[0]},
				OnDelete:   schema.NoAction,
			},
		},
		Indexes: []*schema.Index{
			{
				Name:    "record_metadata_format_id_item_id",
				Unique:  true,
				Columns: []*schema.Column{RecordsColumns[4], RecordsColumns[3]},
			},
			{
				Name:    "record_datestamp",
				Unique:  false,
				Columns: []*schema.Column{RecordsColumns[2]},
			},
		},
	}
	// SetsColumns holds the columns for the "sets" table.
	SetsColumns = []*schema.Column{
		{Name: "id", Type: field.TypeInt64, Increment: true},
		{Name: "spec", Type: field.TypeString, Unique: true},
		{Name: "name", Type: field.TypeString},
		{Name: "description", Type: field.TypeString, Nullable: true},
	}
	// SetsTable holds the schema information for the "sets" table.
	SetsTable = &schema.Table{
		Name:       "sets",
		Columns:    SetsColumns,
		PrimaryKey: []*schema.Column{SetsColumns[0]},
	}
	// ItemSetsColumns holds the columns for the "item_sets" table.
	ItemSetsColumns = []*schema.Column{
		{Name: "item_id", Type: field.TypeInt64},
		{Name: "set_id", Type: field.TypeInt64},
	}
	// ItemSetsTable holds the schema information for the "item_sets" table.
	ItemSetsTable = &schema.Table{
		Name:       "item_sets",
		Columns:    ItemSetsColumns,
		PrimaryKey: []*schema.Column{ItemSetsColumns[0], ItemSetsColumns[1]},
		ForeignKeys: []*schema.ForeignKey{
			{
				Symbol:     "item_sets_item_id",
				Columns:    []*schema.Column{ItemSetsColumns[0]},
				RefColumns: []*schema.Column{ItemsColumns[0]},
				OnDelete:   schema.Cascade,
			},
			{
				Symbol:     "item_sets_set_id",
				Columns:    []*schema.Column{ItemSetsColumns[1]},
				RefColumns: []*schema.Column{SetsColumns[0]},
				OnDelete:   schema.Cascade,
			},
		},
	}
	// Tables holds all the tables in the schema.
	Tables = []*schema.Table{
		ItemsTable,
		MetadataFormatsTable,
		RecordsTable,
		SetsTable,
		ItemSetsTable,
	}
)

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