Documentation ¶
Index ¶
- Variables
- type AddForeignKeyConstraintOptions
- type CheckConstraintOptions
- type ColumnCommentOptions
- type ColumnOptions
- type ColumnType
- type ConstraintOption
- type DB
- type DatabaseCredentials
- type DetailedMigration
- type Directions
- type DropCheckConstraintOptions
- type DropColumnOptions
- type DropExtensionOptions
- type DropForeignKeyConstraintOptions
- type DropIndexOptions
- type DropPrimaryKeyConstraintOptions
- type DropTableOptions
- type ExtensionOptions
- type ForceStopError
- type IndexOptions
- type Migration
- type MigrationEvents
- type Migrator
- type MigratorContext
- func (m *MigratorContext) AddCheckConstraintCreated(options CheckConstraintOptions)
- func (m *MigratorContext) AddCheckConstraintDropped(options DropCheckConstraintOptions)
- func (m *MigratorContext) AddColumnComment(options ColumnCommentOptions)
- func (m *MigratorContext) AddColumnCreated(options ColumnOptions)
- func (m *MigratorContext) AddColumnDropped(options DropColumnOptions)
- func (m *MigratorContext) AddExtensionCreated(options ExtensionOptions)
- func (m *MigratorContext) AddExtensionDropped(options DropExtensionOptions)
- func (m *MigratorContext) AddForeignKeyConstraintDropped(options DropForeignKeyConstraintOptions)
- func (m *MigratorContext) AddForeignKeyCreated(options AddForeignKeyConstraintOptions)
- func (m *MigratorContext) AddIndexCreated(options IndexOptions)
- func (m *MigratorContext) AddIndexDropped(options DropIndexOptions)
- func (m *MigratorContext) AddPrimaryKeyConstraintDropped(options DropPrimaryKeyConstraintOptions)
- func (m *MigratorContext) AddPrimaryKeyCreated(options PrimaryKeyConstraintOptions)
- func (m *MigratorContext) AddRenameColumn(options RenameColumnOptions)
- func (m *MigratorContext) AddTableCreated(options TableOptions)
- func (m *MigratorContext) AddTableDropped(options DropTableOptions)
- func (m *MigratorContext) Error() error
- func (m *MigratorContext) RaiseError(err error)
- type MigratorOption
- type PrimaryKeyConstraintOptions
- type RenameColumnOptions
- type ReversibleMigrationExecutor
- type Schema
- type SchemaFactory
- type SimpleMigration
- type TableDef
- type TableName
- type TableOptions
Constants ¶
This section is empty.
Variables ¶
var DefaultCheckConstraintNameBuilder = func(table TableName, constraintName string) string { return fmt.Sprintf("ck_%s_%s", table.Name(), constraintName) }
var DefaultForeignKeyNameBuilder = func(fromTable TableName, toTable TableName) string { return fmt.Sprintf("fk_%s_%s", fromTable.Name(), toTable.Name()) }
var DefaultIndexNameBuilder = func(table TableName, columns []string) string { return fmt.Sprintf("idx_%s_%s", table.Name(), strings.Join(columns, "_")) }
var DefaultPrimaryKeyNameBuilder = func(table TableName) string { return fmt.Sprintf("%s_pkey", table.Name()) }
Functions ¶
This section is empty.
Types ¶
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 ¶
func (f AddForeignKeyConstraintOptions) 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 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 ColumnTypeString column and number of // bytes for ColumnTypeText, ColumnTypeBinary, ColumnTypeBlob, and ColumnTypeInteger Columns. 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) 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 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 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 ¶
DetailedMigration is the interface that describes a migration with up and down operations.
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 ¶
func (d DropCheckConstraintOptions) String() 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 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 }
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 (d DropForeignKeyConstraintOptions) EventName() string
func (DropForeignKeyConstraintOptions) String ¶
func (d DropForeignKeyConstraintOptions) String() 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 (d DropPrimaryKeyConstraintOptions) EventName() string
func (DropPrimaryKeyConstraintOptions) String ¶
func (d DropPrimaryKeyConstraintOptions) String() 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 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 }
func (ExtensionOptions) EventName ¶
func (e ExtensionOptions) EventName() string
func (ExtensionOptions) String ¶
func (e ExtensionOptions) String() string
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 // Concurrent specifies if the index should be created concurrently. Concurrent bool // Method specifies the index method. Method string // 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 only. Predicate string }
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 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 *sql.DB, tracker tracker.Tracker, schemaFactory SchemaFactory[T], opts *MigratorOption, ) *Migrator[T]
NewMigrator creates a new migrator.
func (*Migrator[T]) Options ¶
func (m *Migrator[T]) Options() MigratorOption
Options returns a copy of the options.
type MigratorContext ¶
type MigratorContext struct { Context context.Context MigratorOptions *MigratorOption MigrationEvents *MigrationEvents Track tracker.Tracker 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) 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) 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) AddTableCreated ¶
func (m *MigratorContext) AddTableCreated(options TableOptions)
func (*MigratorContext) AddTableDropped ¶
func (m *MigratorContext) AddTableDropped(options DropTableOptions)
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 }
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 ¶
func (p PrimaryKeyConstraintOptions) String() string
type RenameColumnOptions ¶
func (RenameColumnOptions) EventName ¶
func (r RenameColumnOptions) EventName() string
func (RenameColumnOptions) String ¶
func (r RenameColumnOptions) String() string
type ReversibleMigrationExecutor ¶
type ReversibleMigrationExecutor struct {
// contains filtered or unexported fields
}
func NewReversibleMigrationExecutor ¶
func NewReversibleMigrationExecutor(ctx *MigratorContext) *ReversibleMigrationExecutor
func (*ReversibleMigrationExecutor) Reversible ¶
func (r *ReversibleMigrationExecutor) Reversible(directions Directions)
type SchemaFactory ¶
type SchemaFactory[T Schema] func(*MigratorContext, DB) T
type SimpleMigration ¶
SimpleMigration is the interface that describes a migration with a single operation.
type TableDef ¶
type TableDef interface { Columns() []ColumnOptions AfterTableCreate() []func() }
type TableName ¶
type TableName string
func Table ¶
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)
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 // 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