schema

package
v0.0.13-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCheckConstraintNameBuilder = func(table TableName, constraintName string) string {
	return fmt.Sprintf("ck_%s_%s", table.Name(), constraintName)
}
View Source
var DefaultForeignKeyNameBuilder = func(fromTable TableName, toTable TableName) string {
	return fmt.Sprintf("fk_%s_%s", fromTable.Name(), toTable.Name())
}
View Source
var DefaultIndexNameBuilder = func(table TableName, columns []string) string {
	return fmt.Sprintf("idx_%s_%s", table.Name(), strings.Join(columns, "_"))
}
View Source
var DefaultPrimaryKeyNameBuilder = func(table TableName) string {
	return fmt.Sprintf("%s_pkey", table.Name())
}

Functions

This section is empty.

Types

type AddEnumValueOptions

type AddEnumValueOptions struct {
	Name   string
	Schema string

	Value string

	BeforeValue string
	AfterValue  string
}

func (AddEnumValueOptions) EventName

func (a AddEnumValueOptions) EventName() string

func (AddEnumValueOptions) String

func (a AddEnumValueOptions) String() string

type AddForeignKeyConstraintOptions

type AddForeignKeyConstraintOptions struct {
	FromTable      TableName
	ToTable        TableName
	ForeignKeyName string

	// ForeignKeyNameBuilder will build the name of the foreign key. If nil, a default name will be used.
	ForeignKeyNameBuilder func(fromTable TableName, toTable TableName) string

	// Column is the foreign key column name on FromTable. Defaults to ToTable.Singularize + "_id". Pass an array to
	// create a composite foreign key.
	Column string

	// PrimaryKey is the primary key column name on ToTable. Defaults to "id".
	// foreign key.
	PrimaryKey string

	// CompositePrimaryKey is the primary key column names on ToTable.
	CompositePrimaryKey []string

	// OnDelete is the action that happens ON DELETE. Valid values are "nullify", "cascade", and "restrict" or custom action.
	OnDelete string

	// OnUpdate is the action that happens ON UPDATE. Valid values are "nullify", "cascade", and "restrict" or custom action.
	OnUpdate string

	// IfNotExists specifies if the foreign key already exists to not try to re-add it. This will avoid duplicate column errors.
	IfNotExists bool

	// Validate specifies whether the constraint should be validated. Defaults to true.
	// Postgres only.
	Validate *bool

	// Deferrable specifies whether the foreign key should be deferrable.
	// Could be DEFERRABLE, NOT DEFERRABLE, INITIALLY DEFERRED, INITIALLY IMMEDIATE or both.
	// Postgres only.
	Deferrable string
}

func (AddForeignKeyConstraintOptions) BuildForeignKeyName

func (f AddForeignKeyConstraintOptions) BuildForeignKeyName(fromTable TableName, toTable TableName) string

BuildForeignKeyName returns the name of the foreign key. If ForeignKeyNameBuilder is set, it will be used to build the name. Default name is `fk_{from_table}_{to_table}`

func (AddForeignKeyConstraintOptions) ConstraintType

func (f AddForeignKeyConstraintOptions) ConstraintType() string

ConstraintType returns the type of the constraint.

func (AddForeignKeyConstraintOptions) EventName

func (f AddForeignKeyConstraintOptions) EventName() string

func (AddForeignKeyConstraintOptions) String

type ChangeColumnDefaultOptions

type ChangeColumnDefaultOptions struct {
	Table      TableName
	ColumnName string
	Value      string

	Reversible *ChangeColumnDefaultOptions
}

func (*ChangeColumnDefaultOptions) EventName

func (c *ChangeColumnDefaultOptions) EventName() string

func (*ChangeColumnDefaultOptions) String

func (c *ChangeColumnDefaultOptions) String() string

type ChangeColumnTypeOptions

type ChangeColumnTypeOptions struct {
	Table      TableName
	ColumnName string

	// ColumnType is the type of the column.
	// Could be a custom one but it's recommended to use the predefined ones for portability.
	ColumnType ColumnType

	// Limit is a maximum column length. This is the number of characters for a ColumnTypeString column and number of
	// bytes for ColumnTypeText, ColumnTypeBinary, ColumnTypeBlob, and ColumnTypeInteger Columns.
	Limit int

	// Scale is the scale of the column.
	// Mainly supported for Specifies the scale for the ColumnTypeDecimal, ColumnTypeNumeric
	// The scale is the number of digits to the right of the decimal point in a number.
	Scale int

	// Precision is the precision of the column.
	// Mainly supported for the ColumnTypeDecimal, ColumnTypeNumeric, ColumnTypeDatetime, and ColumnTypeTime
	// The precision is the number of significant digits in a number.
	Precision int

	// Array specifies if the column is an array.
	Array bool

	// Using is the USING clause for the change column type.
	// Postgres only.
	Using string

	// Reversible will allow the migrator to reverse the operation by creating the column.
	// Specify the old column type.
	Reversible *ChangeColumnTypeOptions
}

func (*ChangeColumnTypeOptions) EventName

func (c *ChangeColumnTypeOptions) EventName() string

func (*ChangeColumnTypeOptions) GetLimit

func (c *ChangeColumnTypeOptions) GetLimit() int

func (*ChangeColumnTypeOptions) GetPrecision

func (c *ChangeColumnTypeOptions) GetPrecision() int

func (*ChangeColumnTypeOptions) GetScale

func (c *ChangeColumnTypeOptions) GetScale() int

func (*ChangeColumnTypeOptions) GetType

func (c *ChangeColumnTypeOptions) GetType() ColumnType

func (*ChangeColumnTypeOptions) IsArray

func (c *ChangeColumnTypeOptions) IsArray() bool

func (*ChangeColumnTypeOptions) SetLimit

func (c *ChangeColumnTypeOptions) SetLimit(limit int)

func (*ChangeColumnTypeOptions) SetNotNull

func (c *ChangeColumnTypeOptions) SetNotNull(_ bool)

func (*ChangeColumnTypeOptions) SetPrecision

func (c *ChangeColumnTypeOptions) SetPrecision(precision int)

func (*ChangeColumnTypeOptions) SetPrimaryKey

func (c *ChangeColumnTypeOptions) SetPrimaryKey(_ bool)

func (*ChangeColumnTypeOptions) String

func (c *ChangeColumnTypeOptions) String() string

type CheckConstraintOptions

type CheckConstraintOptions struct {
	Table          TableName
	Expression     string
	ConstraintName string

	// ConstraintNameBuilder will build the name of the constraint. If nil, a default name will be used.
	// By default, is nil.
	ConstraintNameBuilder func(table TableName, constraintName string) string

	// IfNotExists Silently ignore if the constraint already exists, rather than raise an error.
	IfNotExists bool

	// Validate Specify whether the constraint should be validated. Defaults to true.
	// Postgres only.
	Validate *bool
}

func (CheckConstraintOptions) BuildConstraintName

func (c CheckConstraintOptions) BuildConstraintName(table TableName, constraintName string) string

BuildConstraintName returns the name of the constraint. If ConstraintNameBuilder is set, it will be used to build the name. Default name is `ck_{table_name}_{constraint_name}`

func (CheckConstraintOptions) ConstraintType

func (c CheckConstraintOptions) ConstraintType() string

ConstraintType returns the type of the constraint.

func (CheckConstraintOptions) EventName

func (c CheckConstraintOptions) EventName() string

func (CheckConstraintOptions) String

func (c CheckConstraintOptions) String() string

type ColumnCommentOptions

type ColumnCommentOptions struct {
	Table TableName

	// ColumnName is the name of the column.
	ColumnName string

	// Comment is the Comment of the column.
	Comment *string

	// Reversible will allow the migrator to reverse the operation.
	Reversible *ColumnCommentOptions
}

func (ColumnCommentOptions) EventName

func (c ColumnCommentOptions) EventName() string

func (ColumnCommentOptions) String

func (c ColumnCommentOptions) String() string

type ColumnData

type ColumnData interface {
	SetLimit(int)
	GetLimit() int
	GetPrecision() int
	SetNotNull(bool)
	SetPrimaryKey(bool)
	SetPrecision(int)
	GetType() ColumnType
	GetScale() int
	IsArray() bool
}

type ColumnOptions

type ColumnOptions struct {
	Table      TableName
	ColumnName string

	// PrimaryKey specifies if the column is a primary key.
	// It will automatically add a PrimaryKeyConstraintOptions to the Constraints list.
	PrimaryKey bool

	// Precision is the precision of the column.
	// Mainly supported for the ColumnTypeDecimal, ColumnTypeNumeric, ColumnTypeDatetime, and ColumnTypeTime
	// The precision is the number of significant digits in a number.
	Precision int

	// Scale is the scale of the column.
	// Mainly supported for Specifies the scale for the ColumnTypeDecimal, ColumnTypeNumeric
	// The scale is the number of digits to the right of the decimal point in a number.
	Scale int

	// ColumnType is the type of the column.
	// Could be a custom one but it's recommended to use the predefined ones for portability.
	ColumnType ColumnType

	// Default is the default value of the column.
	Default string

	// NotNull specifies if the column can be null.
	NotNull bool

	// Limit is a maximum column length. This is the number of characters for a varchar column.
	Limit int

	// IfNotExists specifies if the column already exists to not try to re-add it. This will avoid duplicate column errors.
	IfNotExists bool

	// Array specifies if the column is an array.
	Array bool

	// Comment is the Comment of the column.
	Comment string

	// Constraints is a list of constraints for the column.
	Constraints []ConstraintOption
}

func (*ColumnOptions) EventName

func (c *ColumnOptions) EventName() string

func (*ColumnOptions) GetLimit

func (c *ColumnOptions) GetLimit() int

func (*ColumnOptions) GetPrecision

func (c *ColumnOptions) GetPrecision() int

func (*ColumnOptions) GetScale

func (c *ColumnOptions) GetScale() int

func (*ColumnOptions) GetType

func (c *ColumnOptions) GetType() ColumnType

func (*ColumnOptions) IsArray

func (c *ColumnOptions) IsArray() bool

func (*ColumnOptions) SetLimit

func (c *ColumnOptions) SetLimit(limit int)

func (*ColumnOptions) SetNotNull

func (c *ColumnOptions) SetNotNull(notNull bool)

func (*ColumnOptions) SetPrecision

func (c *ColumnOptions) SetPrecision(precision int)

func (*ColumnOptions) SetPrimaryKey

func (c *ColumnOptions) SetPrimaryKey(primaryKey bool)

func (*ColumnOptions) String

func (c *ColumnOptions) String() string

type ColumnType

type ColumnType = string
const (
	ColumnTypeString   ColumnType = "string"
	ColumnTypeText     ColumnType = "text"
	ColumnTypeInteger  ColumnType = "integer"
	ColumnTypeBigInt   ColumnType = "bigint"
	ColumnTypeFloat    ColumnType = "float"
	ColumnTypeDecimal  ColumnType = "decimal"
	ColumnTypeNumeric  ColumnType = "numeric"
	ColumnTypeDatetime ColumnType = "datetime"
	ColumnTypeTime     ColumnType = "time"
	ColumnTypeDate     ColumnType = "date"
	ColumnTypeBinary   ColumnType = "binary"
	ColumnTypeBlob     ColumnType = "blob"
	ColumnTypeBoolean  ColumnType = "boolean"
	ColumnTypeUUID     ColumnType = "uuid"
	ColumnTypeJSON     ColumnType = "json"

	// ColumnTypePrimaryKey is a special column type for primary keys.
	ColumnTypePrimaryKey ColumnType = "primary_key"

	// ColumnTypeSmallSerial is an auto-incrementing integer column. Postgres only.
	ColumnTypeSmallSerial ColumnType = "smallserial"

	// ColumnTypeSerial is an auto-incrementing integer column. Postgres only.
	ColumnTypeSerial ColumnType = "serial"

	// ColumnTypeBigSerial is an auto-incrementing integer column. Postgres only.
	ColumnTypeBigSerial ColumnType = "bigserial"

	// ColumnTypeJSONB is a binary JSON column. Postgres only.
	ColumnTypeJSONB ColumnType = "jsonb"

	// ColumnTypeHstore is a key-value store column. Postgres only.
	ColumnTypeHstore ColumnType = "hstore"
)

type ConstraintOption

type ConstraintOption interface {
	ConstraintType() string
}

type CreateEnumOptions

type CreateEnumOptions struct {
	Name   string
	Values []string
	Schema string
}

func (CreateEnumOptions) EventName

func (c CreateEnumOptions) EventName() string

func (CreateEnumOptions) String

func (c CreateEnumOptions) String() string

type DB

type DB interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

DB is the interface that describes a database connection.

type DBTX

type DBTX interface {
	DB
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

type DatabaseCredentials

type DatabaseCredentials struct {
	Host, Port, User, Pass, DB string
}

DatabaseCredentials is the struct that holds the database credentials.

func ExtractCredentials

func ExtractCredentials(dsn string) (DatabaseCredentials, error)

ExtractCredentials extracts the database credentials from the DSN.

type DetailedMigration

type DetailedMigration[T Schema] interface {
	Up(T)
	Down(T)
	Name() string
	Date() time.Time
}

DetailedMigration is the interface that describes a migration with up and down operations.

func NewSQLMigration

func NewSQLMigration[T Schema](fs embed.FS, name string, time string, delimiter string) DetailedMigration[T]

type Directions

type Directions struct {
	Up   func()
	Down func()
}

type DropCheckConstraintOptions

type DropCheckConstraintOptions struct {
	Table          TableName
	ConstraintName string

	// ConstraintNameBuilder will build the name of the constraint. If nil, a default name will be used.
	// By default, is nil.
	ConstraintNameBuilder func(table TableName, constraintName string) string

	// IfExists add checks if the constraint exists before dropping it.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the constraint.
	Reversible *CheckConstraintOptions
}

func (DropCheckConstraintOptions) BuildConstraintName

func (d DropCheckConstraintOptions) BuildConstraintName(table TableName, constraintName string) string

func (DropCheckConstraintOptions) EventName

func (d DropCheckConstraintOptions) EventName() string

func (DropCheckConstraintOptions) String

type DropColumnOptions

type DropColumnOptions struct {
	Table      TableName
	ColumnName string

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the column.
	Reversible *ColumnOptions
}

func (DropColumnOptions) EventName

func (d DropColumnOptions) EventName() string

func (DropColumnOptions) String

func (d DropColumnOptions) String() string

type DropEnumOptions

type DropEnumOptions struct {
	Name   string
	Schema string

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the enum.
	Reversible *CreateEnumOptions
}

func (DropEnumOptions) EventName

func (d DropEnumOptions) EventName() string

func (DropEnumOptions) String

func (d DropEnumOptions) String() string

type DropExtensionOptions

type DropExtensionOptions struct {
	ExtensionName string

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the extension.
	Reversible *ExtensionOptions

	Cascade bool
}

func (DropExtensionOptions) EventName

func (e DropExtensionOptions) EventName() string

func (DropExtensionOptions) String

func (e DropExtensionOptions) String() string

type DropForeignKeyConstraintOptions

type DropForeignKeyConstraintOptions struct {
	FromTable      TableName
	ToTable        TableName
	ForeignKeyName string

	// ForeignKeyNameBuilder will build the name of the foreign key. If nil, a default name will be used.
	ForeignKeyNameBuilder func(fromTable TableName, toTable TableName) string

	// IfExists check if the foreign key exists before dropping it.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the foreign key.
	Reversible *AddForeignKeyConstraintOptions
}

func (DropForeignKeyConstraintOptions) BuildForeignKeyName

func (d DropForeignKeyConstraintOptions) BuildForeignKeyName(fromTable TableName, toTable TableName) string

func (DropForeignKeyConstraintOptions) EventName

func (DropForeignKeyConstraintOptions) String

type DropIndexOptions

type DropIndexOptions struct {
	Table     TableName
	Columns   []string
	IndexName string

	// IndexNameBuilder will build the name of the index. If nil, a default name will be used.
	IndexNameBuilder func(table TableName, columns []string) string

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the index.
	Reversible *IndexOptions
}

func (DropIndexOptions) BuildIndexName

func (d DropIndexOptions) BuildIndexName(table TableName, columns []string) string

func (DropIndexOptions) EventName

func (d DropIndexOptions) EventName() string

func (DropIndexOptions) String

func (d DropIndexOptions) String() string

type DropPrimaryKeyConstraintOptions

type DropPrimaryKeyConstraintOptions struct {
	Table TableName

	PrimaryKeyName string

	PrimaryKeyNameBuilder func(table TableName) string

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the primary key.
	Reversible *PrimaryKeyConstraintOptions
}

func (DropPrimaryKeyConstraintOptions) BuildPrimaryKeyName

func (d DropPrimaryKeyConstraintOptions) BuildPrimaryKeyName(table TableName) string

func (DropPrimaryKeyConstraintOptions) EventName

func (DropPrimaryKeyConstraintOptions) String

type DropTableOptions

type DropTableOptions struct {
	Table TableName

	// IfExists add IF EXISTS to the query.
	IfExists bool

	// Reversible will allow the migrator to reverse the operation by creating the table.
	Reversible *TableOptions
}

func (DropTableOptions) EventName

func (d DropTableOptions) EventName() string

func (DropTableOptions) String

func (d DropTableOptions) String() string

type EnumUsage

type EnumUsage struct {
	Table  TableName
	Column string
}

type ExtensionOptions

type ExtensionOptions struct {
	ExtensionName string

	// Schema is the schema where the extension will be created.
	Schema string

	// IfNotExists add IF NOT EXISTS to the query.
	IfNotExists bool

	Reversible *DropExtensionOptions
}

func (ExtensionOptions) EventName

func (e ExtensionOptions) EventName() string

func (ExtensionOptions) String

func (e ExtensionOptions) String() string

type Factory

type Factory[T Schema] func(ctx *MigratorContext, tx DB, db DB) T

type ForceStopError

type ForceStopError struct {
	// contains filtered or unexported fields
}

ForceStopError is an error that stops the migration process even if the `continue_on_error` option is set.

func NewForceStopError

func NewForceStopError(err error) *ForceStopError

NewForceStopError creates a new ForceStopError.

type IndexOptions

type IndexOptions struct {
	Table     TableName
	Columns   []string
	IndexName string

	// IndexNameBuilder will build the name of the index. If nil, a default name will be used.
	IndexNameBuilder func(table TableName, columns []string) string

	// IfNotExists Silently ignore if the index already exists, rather than raise an error.
	IfNotExists bool

	// Unique specifies if the index should be unique.
	Unique bool

	// Order specifies the order of the index.
	Order string

	// OrderPerColumn specifies the order of the index per column.
	OrderPerColumn map[string]string

	// Predicate specifies the predicate for the index.
	// Postgres, SQLite only.
	Predicate string

	// Method specifies the index method. USING {method}.
	// Postgres only.
	Method string

	// Concurrent specifies if the index should be created concurrently.
	// Postgres only.
	Concurrent bool
}

func (IndexOptions) BuildIndexName

func (i IndexOptions) BuildIndexName(table TableName, columns []string) string

BuildIndexName returns the name of the index. If IndexNameBuilder is set, it will be used to build the name. Default name is `idx_{table_name}_{Columns}`

func (IndexOptions) EventName

func (i IndexOptions) EventName() string

func (IndexOptions) String

func (i IndexOptions) String() string

type Migration

type Migration interface {
	Name() string
	Date() time.Time
}

Migration is the interface that describes a migration at is simplest form.

type MigrationEvents

type MigrationEvents struct {
	// contains filtered or unexported fields
}

type Migrator

type Migrator[T Schema] struct {
	// contains filtered or unexported fields
}

Migrator applies the migrations.

func NewMigrator

func NewMigrator[T Schema](
	ctx context.Context,
	db DBTX,
	schemaFactory Factory[T],
	config *amigoconfig.Config,
) *Migrator[T]

NewMigrator creates a new migrator.

func (*Migrator[T]) Apply

func (m *Migrator[T]) Apply(direction types.MigrationDirection, version *string, steps *int, migrations []Migration) bool

func (*Migrator[T]) GetSchema

func (m *Migrator[T]) GetSchema() Schema

func (*Migrator[T]) NewSchema

func (m *Migrator[T]) NewSchema() T

func (*Migrator[T]) ToggleDBLog

func (m *Migrator[T]) ToggleDBLog(b bool)

type MigratorContext

type MigratorContext struct {
	Context context.Context

	Config          *amigoconfig.Config
	MigrationEvents *MigrationEvents

	MigrationDirection types.MigrationDirection
	// contains filtered or unexported fields
}

MigratorContext is the context of the migrator. It contains the context, errors, options, and other useful information.

func (*MigratorContext) AddChangeColumnDefault

func (m *MigratorContext) AddChangeColumnDefault(options ChangeColumnDefaultOptions)

func (*MigratorContext) AddChangeColumnType

func (m *MigratorContext) AddChangeColumnType(options ChangeColumnTypeOptions)

func (*MigratorContext) AddCheckConstraintCreated

func (m *MigratorContext) AddCheckConstraintCreated(options CheckConstraintOptions)

func (*MigratorContext) AddCheckConstraintDropped

func (m *MigratorContext) AddCheckConstraintDropped(options DropCheckConstraintOptions)

func (*MigratorContext) AddColumnComment

func (m *MigratorContext) AddColumnComment(options ColumnCommentOptions)

func (*MigratorContext) AddColumnCreated

func (m *MigratorContext) AddColumnCreated(options ColumnOptions)

func (*MigratorContext) AddColumnDropped

func (m *MigratorContext) AddColumnDropped(options DropColumnOptions)

func (*MigratorContext) AddEnumCreated

func (m *MigratorContext) AddEnumCreated(options CreateEnumOptions)

func (*MigratorContext) AddEnumDropped

func (m *MigratorContext) AddEnumDropped(options DropEnumOptions)

func (*MigratorContext) AddEnumValueCreated

func (m *MigratorContext) AddEnumValueCreated(options AddEnumValueOptions)

func (*MigratorContext) AddExtensionCreated

func (m *MigratorContext) AddExtensionCreated(options ExtensionOptions)

func (*MigratorContext) AddExtensionDropped

func (m *MigratorContext) AddExtensionDropped(options DropExtensionOptions)

func (*MigratorContext) AddForeignKeyConstraintDropped

func (m *MigratorContext) AddForeignKeyConstraintDropped(options DropForeignKeyConstraintOptions)

func (*MigratorContext) AddForeignKeyCreated

func (m *MigratorContext) AddForeignKeyCreated(options AddForeignKeyConstraintOptions)

func (*MigratorContext) AddIndexCreated

func (m *MigratorContext) AddIndexCreated(options IndexOptions)

func (*MigratorContext) AddIndexDropped

func (m *MigratorContext) AddIndexDropped(options DropIndexOptions)

func (*MigratorContext) AddPrimaryKeyConstraintDropped

func (m *MigratorContext) AddPrimaryKeyConstraintDropped(options DropPrimaryKeyConstraintOptions)

func (*MigratorContext) AddPrimaryKeyCreated

func (m *MigratorContext) AddPrimaryKeyCreated(options PrimaryKeyConstraintOptions)

func (*MigratorContext) AddRenameColumn

func (m *MigratorContext) AddRenameColumn(options RenameColumnOptions)

func (*MigratorContext) AddRenameEnum

func (m *MigratorContext) AddRenameEnum(options RenameEnumOptions)

func (*MigratorContext) AddRenameEnumValue

func (m *MigratorContext) AddRenameEnumValue(options RenameEnumValueOptions)

func (*MigratorContext) AddTableComment

func (m *MigratorContext) AddTableComment(options TableCommentOptions)

func (*MigratorContext) AddTableCreated

func (m *MigratorContext) AddTableCreated(options TableOptions)

func (*MigratorContext) AddTableDropped

func (m *MigratorContext) AddTableDropped(options DropTableOptions)

func (*MigratorContext) AddTableRenamed

func (m *MigratorContext) AddTableRenamed(options RenameTableOptions)

func (*MigratorContext) AddVersionCreated

func (m *MigratorContext) AddVersionCreated(version string)

func (*MigratorContext) AddVersionDeleted

func (m *MigratorContext) AddVersionDeleted(version string)

func (*MigratorContext) AddVersionsCreated

func (m *MigratorContext) AddVersionsCreated(versions []string)

func (*MigratorContext) Error

func (m *MigratorContext) Error() error

Error returns the error of the migration process.

func (*MigratorContext) RaiseError

func (m *MigratorContext) RaiseError(err error)

RaiseError will raise an error and stop the migration process if the `continue_on_error` option is not set. That's useful because each operation in the migration process is not a func(...) error, it's more convenient.

If the ContinueOnError option is set, it will log the error and continue the migration process. If the error is a ForceStopError, it will stop the migration process even if the ContinueOnError option is set.

type MigratorOption

type MigratorOption struct {
	// DryRun specifies if the migrator should perform the migrations without actually applying them.
	// Not compatible with TransactionNone.
	DryRun bool

	// ContinueOnError specifies if the migrator should continue running migrations even if an error occurs.
	// If you need to stop the migration process when an error occurs, wrap your error with NewForceStopError.
	ContinueOnError bool

	SchemaVersionTable TableName

	DBLogger dblog.DatabaseLogger

	// DumpSchemaFilePath is the path to the schema dump file.
	DumpSchemaFilePath *string

	// UseSchemaDump specifies if the migrator should use the schema. (if possible -> for fresh installation)
	UseSchemaDump bool
}

MigratorOption is the option of the migrator.

type PrimaryKeyConstraintOptions

type PrimaryKeyConstraintOptions struct {
	Table TableName

	Columns []string

	// IfNotExists Silently ignore if the primary key already exists, rather than raise an error.
	IfNotExists bool
}

func (PrimaryKeyConstraintOptions) ConstraintType

func (p PrimaryKeyConstraintOptions) ConstraintType() string

func (PrimaryKeyConstraintOptions) EventName

func (p PrimaryKeyConstraintOptions) EventName() string

func (PrimaryKeyConstraintOptions) String

type RenameColumnOptions

type RenameColumnOptions struct {
	Table         TableName
	OldColumnName string
	NewColumnName string
}

func (RenameColumnOptions) EventName

func (r RenameColumnOptions) EventName() string

func (RenameColumnOptions) String

func (r RenameColumnOptions) String() string

type RenameEnumOptions

type RenameEnumOptions struct {
	OldName string
	Schema  string

	NewName string
}

func (RenameEnumOptions) EventName

func (r RenameEnumOptions) EventName() string

func (RenameEnumOptions) String

func (r RenameEnumOptions) String() string

type RenameEnumValueOptions

type RenameEnumValueOptions struct {
	Name   string
	Schema string

	OldValue string
	NewValue string
}

type RenameTableOptions

type RenameTableOptions struct {
	OldTable TableName
	NewTable TableName
}

func (RenameTableOptions) EventName

func (r RenameTableOptions) EventName() string

func (RenameTableOptions) String

func (r RenameTableOptions) String() string

type ReversibleMigrationExecutor

type ReversibleMigrationExecutor struct {
	// contains filtered or unexported fields
}

ReversibleMigrationExecutor is a helper to execute reversible migrations in a change method. Since you may have custom code in it, you must provide a way to up and down the user defined code.

func NewReversibleMigrationExecutor

func NewReversibleMigrationExecutor(ctx *MigratorContext) *ReversibleMigrationExecutor

func (*ReversibleMigrationExecutor) Reversible

func (r *ReversibleMigrationExecutor) Reversible(directions Directions)

type SQLMigration

type SQLMigration[T Schema] struct {
	// contains filtered or unexported fields
}

func (SQLMigration[T]) Date

func (s SQLMigration[T]) Date() time.Time

func (SQLMigration[T]) Down

func (s SQLMigration[T]) Down(x T)

func (SQLMigration[T]) Name

func (s SQLMigration[T]) Name() string

func (SQLMigration[T]) Up

func (s SQLMigration[T]) Up(x T)

type Schema

type Schema interface {
	AddVersion(version string)
	AddVersions(versions []string)
	RemoveVersion(version string)
	FindAppliedVersions() []string

	Exec(query string, args ...interface{})
	Query(query string, args []any, rowHandler func(row *sql.Rows) error)
}

Schema is the interface that need to be implemented to support migrations.

type SimpleMigration

type SimpleMigration[T Schema] interface {
	Change(T)
	Name() string
	Date() time.Time
}

SimpleMigration is the interface that describes a migration with a single operation.

type TableCommentOptions

type TableCommentOptions struct {
	Table   TableName
	Comment *string

	Reversible *TableCommentOptions
}

func (TableCommentOptions) EventName

func (t TableCommentOptions) EventName() string

func (TableCommentOptions) String

func (t TableCommentOptions) String() string

type TableDef

type TableDef interface {
	Columns() []ColumnOptions
	AfterTableCreate() []func()
}

type TableName

type TableName string

func Table

func Table(name string, schema ...string) TableName

Table returns a new TableName with the schema and table name. But you can also use regular string, this function is when you have dynamic schema names. (like in the tests)

func (TableName) HasSchema

func (t TableName) HasSchema() bool

HasSchema returns if the table name has a schema.

func (TableName) Name

func (t TableName) Name() string

Name returns the name part of the table name.

func (TableName) Schema

func (t TableName) Schema() string

Schema returns the schema part of the table name.

func (TableName) String

func (t TableName) String() string

String returns the string representation of the table name.

type TableOptions

type TableOptions struct {
	Table TableName

	// IfNotExists create the table if it doesn't exist.
	IfNotExists bool

	// PrimaryKeys is a list of primary keys.
	PrimaryKeys []string

	// WithoutPrimaryKey specifies if the table should be created without a primary key.
	// By default, if you create a table without an "id" column and PrimaryKeys is empty it will not fail.
	// But if you want to explicitly create a table without a primary key, you can set this to true.
	WithoutPrimaryKey bool

	// Option is at the end of the table creation.
	Option string

	Comment *string

	// TableDefinition is the definition of the table. Usually a struct that implements TableDef will allow you to
	// define the columns and other options.
	TableDefinition TableDef
}

func (TableOptions) EventName

func (s TableOptions) EventName() string

func (TableOptions) String

func (s TableOptions) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL