Documentation
¶
Index ¶
- Variables
- func NewSQLDB(db *sql.DB, q *Queries) *sqlDB
- type Database
- type Error
- type Field
- type LogAdapter
- func (a *LogAdapter) Debug(msg string, fields ...Field)
- func (a *LogAdapter) Error(msg string, fields ...Field)
- func (a *LogAdapter) Info(msg string, fields ...Field)
- func (a *LogAdapter) Level() LogLevel
- func (a *LogAdapter) SetLevel(lvl LogLevel)
- func (a *LogAdapter) Warn(msg string, fields ...Field)
- type LogLevel
- type Logger
- type Migrater
- type Migrator
- func (m *Migrator) AllDown() error
- func (m *Migrator) AllDownCtx(ctx context.Context) error
- func (m *Migrator) AllUp() error
- func (m *Migrator) AllUpCtx(ctx context.Context) error
- func (m *Migrator) Init() error
- func (m *Migrator) InitCtx(ctx context.Context) error
- func (m *Migrator) InitDryRun() error
- func (m *Migrator) InitDryRunCtx(ctx context.Context) error
- func (m *Migrator) OneDown() error
- func (m *Migrator) OneDownCtx(ctx context.Context) error
- func (m *Migrator) OneDownDryRun() error
- func (m *Migrator) OneDownDryRunCtx(ctx context.Context) error
- func (m *Migrator) OneUp() error
- func (m *Migrator) OneUpCtx(ctx context.Context) error
- func (m *Migrator) OneUpDryRun() error
- func (m *Migrator) OneUpDryRunCtx(ctx context.Context) error
- func (m *Migrator) Version() (v Version, err error)
- func (m *Migrator) VersionCtx(ctx context.Context) (Version, error)
- type NilAdapter
- func (a *NilAdapter) Debug(msg string, fields ...Field)
- func (a *NilAdapter) Error(msg string, fields ...Field)
- func (a *NilAdapter) Info(msg string, fields ...Field)
- func (a *NilAdapter) Level() LogLevel
- func (a *NilAdapter) SetLevel(lvl LogLevel)
- func (a *NilAdapter) Warn(msg string, fields ...Field)
- type NoTxFunc
- type Queries
- type SQLCommand
- type SQLDB
- type SQLTx
- type SlogAdapter
- func (a *SlogAdapter) Debug(msg string, fields ...Field)
- func (a *SlogAdapter) Error(msg string, fields ...Field)
- func (a *SlogAdapter) Info(msg string, fields ...Field)
- func (a *SlogAdapter) Level() LogLevel
- func (a *SlogAdapter) SetLevel(lvl LogLevel)
- func (a *SlogAdapter) Warn(msg string, fields ...Field)
- type StepFunc
- type StepInfo
- type Stepper
- type Steps
- func (s *Steps) Append(name string, up StepFunc, down StepFunc) error
- func (s *Steps) Check(v Version) error
- func (s *Steps) Down(v Version) (nfo StepInfo, f StepFunc, err error)
- func (s *Steps) Len() int
- func (s *Steps) Name(ID int) (name string, err error)
- func (s *Steps) Up(v Version) (nfo StepInfo, f StepFunc, err error)
- func (s *Steps) Version(ID int) (v Version, err error)
- type TxFunc
- type Version
Constants ¶
This section is empty.
Variables ¶
var BadVersion = badVersion
Functions ¶
Types ¶
type Database ¶
type Database interface {
// InitVersion initialize the version information. Returns ErrAlreadyInitialized
// if the database is already initialized. The ID of the given version MUST be 0.
// Leaves the database unchanged if dryRun is true.
InitVersion(ctx context.Context, v Version, dryRun bool) error
// Version returns the current database version. Returns ErrNotInitialized if
// the database is not initialized.
Version(context.Context) (Version, error)
// The DefaultStepFunc is called when the step function is nil. It changes the version
// from info.From() to info.To() unless an error occurs or dryRun is true.
DefaultStepFunc(ctx context.Context, info StepInfo, dryRun bool, log Logger) error
}
Database is the interface to a database.
type Error ¶
type Error string
const ( // ErrBadParameters is returned when a function received invalid parameters. ErrBadParameters Error = "bad parameters" // ErrAlreadyInitialized is returned by Init if the database is already initialized. ErrAlreadyInitialized Error = "already initialized" // ErrBadVersionID is returned when Init found an out of range version ID in the database. ErrBadVersionID Error = "bad version identifier" // ErrBadVersionChecksum is returned when Init found a version with invalid checksum which is a // hint that the migration steps don't match the one used for the database. Don't change // any migration steps as it will result in changing checksums. ErrBadVersionChecksum Error = "bad version checksum" // ErrNotInitialized is returned when Init has not been called. ErrNotInitialized Error = "not initialized" // ErrBadVersion is returned when the database is not in the expected version. ErrBadVersion Error = "bad version" // ErrEndOfSteps is returned when OneUp or OneDown has no more more migration steps to perform. ErrEndOfSteps Error = "end of steps" // ErrNotSQLDB is returned a database is not an SQL database. ErrNotSQLDB Error = "not an SQL database" // ErrBeginTx is returned when starting a transaction fails. ErrBeginTx Error = "begin transaction" // ErrCommitTx is returned when a committing a transaction fails. ErrCommitTx Error = "commit transaction" // ErrRollbackTx is return when the transaction rollback failed. ErrRollbackTx Error = "rollback transaction" // ErrCancel is returned by a user function to force a dry run. ErrCancel Error = "cancel transaction" // ErrAbort is returned by a user function to aborts a transaction. ErrAbort Error = "abort transaction" )
type LogAdapter ¶
type LogAdapter struct {
// contains filtered or unexported fields
}
LogAdapter adapts the std log to the logger.
func (*LogAdapter) Debug ¶
func (a *LogAdapter) Debug(msg string, fields ...Field)
Debug logs an debug level message.
func (*LogAdapter) Error ¶
func (a *LogAdapter) Error(msg string, fields ...Field)
Error logs an error level message.
func (*LogAdapter) Info ¶
func (a *LogAdapter) Info(msg string, fields ...Field)
Info logs an info level message.
func (*LogAdapter) Level ¶
func (a *LogAdapter) Level() LogLevel
Level returns the current logging level.
func (*LogAdapter) SetLevel ¶
func (a *LogAdapter) SetLevel(lvl LogLevel)
SetLevel set the logging level.
func (*LogAdapter) Warn ¶
func (a *LogAdapter) Warn(msg string, fields ...Field)
Warn logs a warning level message.
type Logger ¶
type Logger interface {
// Error logs an error level message.
Error(msg string, fields ...Field)
// Warn logs a warning level message.
Warn(msg string, fields ...Field)
// Info logs an info level message.
Info(msg string, fields ...Field)
// Debug logs an debug level message.
Debug(msg string, fields ...Field)
// SetLevel sets the log level.
SetLevel(level LogLevel)
// Level returns the log level.
Level() LogLevel
}
Logger is a common logging interface.
func NewLogLogger ¶
NewLogLogger creates a new std logger using the default logger.
func NewLogLoggerWith ¶
NewLogLoggerWith creates a new std logger using the given logger.
func NewNilLogger ¶
func NewNilLogger() Logger
func NewSlogLogger ¶
NewSlogLogger returns a Logger using the default slog logger.
type Migrater ¶
type Migrater interface {
// Init initializes the database version to v0 after verifying that it is not initialized.
Init() error
// InitDryRun simulates the database initialization to version v0 after verifying that
// it is not initialized.
InitDryRun() error
// Version returns the current version of the database.
Version() (Version, error)
// OneUp executes one up step.
OneUp(ctx context.Context) error
// OneDown executes one down step.
OneDown(ctx context.Context) error
// OneUpDryRun simulates one up step.
OneUpDryRun(ctx context.Context) error
// OneDownDryRun simulates one down step.
OneDownDryRun(ctx context.Context) error
// AllUp executes all up steps.
AllUp(ctx context.Context) error
// AllDown executes all down steps.
AllDown(ctx context.Context) error
}
Migrater manages database migration steps.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator is a Migrater for the given database, stepper and logger.
func New ¶
New creates a new migrator. Returns ErrBadParameters if the parameters are invalid, or ErrBadVersion if the version in the database isn't found in the stepper.
func (*Migrator) AllDownCtx ¶
AllDownCtx attempts to executes all migration steps down.
func (*Migrator) Init ¶
Init initializes the database version to v0 after verifying that it is not initialized.
func (*Migrator) InitCtx ¶
InitCtx initializes the database version to v0 after verifying that it is not initialized.
func (*Migrator) InitDryRun ¶
InitDryRun simulates the database initialization to version v0 after verifying that it is not initialized.
func (*Migrator) InitDryRunCtx ¶
InitDryRunCtx simulates the database initialization to version v0 after verifying that it is not initialized.
func (*Migrator) OneDownCtx ¶
OneDownCtx attempts to execute one migration step down.
func (*Migrator) OneDownDryRun ¶
OneDownDryRun attempts to execute one migration step down.
func (*Migrator) OneDownDryRunCtx ¶
OneDownDryRunCtx attempts to execute one migration step down.
func (*Migrator) OneUpDryRun ¶
OneUpDryRun attempts to execute one migration step up.
func (*Migrator) OneUpDryRunCtx ¶
OneUpDryRunCtx attempts to execute one migration step up.
type NilAdapter ¶
type NilAdapter struct {
// contains filtered or unexported fields
}
NilAdapter is a nil logger that doesn't produce any log.
func (*NilAdapter) Debug ¶
func (a *NilAdapter) Debug(msg string, fields ...Field)
Debug logs an debug level message.
func (*NilAdapter) Error ¶
func (a *NilAdapter) Error(msg string, fields ...Field)
Error logs an error level message.
func (*NilAdapter) Info ¶
func (a *NilAdapter) Info(msg string, fields ...Field)
Info logs an info level message.
func (*NilAdapter) Level ¶
func (a *NilAdapter) Level() LogLevel
Level returns the current logging level.
func (*NilAdapter) SetLevel ¶
func (a *NilAdapter) SetLevel(lvl LogLevel)
SetLevel set the logging level.
func (*NilAdapter) Warn ¶
func (a *NilAdapter) Warn(msg string, fields ...Field)
Warn logs a warning level message.
type NoTxFunc ¶
Func is a user provided function executed outside a transaction. It doesn't support dry run and the changes to the database won't be cancelled when the function returns an error.
type Queries ¶
type Queries struct {
// CreateTableQuery is the query to create a table holding its version.
// The version is an integer ID and a 32 character string. The table
// contains at most one row.
CreateTableQuery string
// InitTableQuery is the query to insert the database version.
// The first parameter is the version ID which is an integer and the second
// parameter is the checksum which is a 32 character string.
InitTableQuery string
// VersionQuery is the row query to get the database version. The
// first value is the integer ID and the second is the 32 character
// checksum value.
VersionQuery string
// SetVersionQuery is the update query to set the database version.
// The first parameter is the version ID which is an integer and the second
// parameter is the checksum which is a 32 character string.
SetVersionQuery string // DB specific set version query.
}
queries are the sqlite queries.
type SQLCommand ¶
SQLCommand is an SQL query instruction with arguments.
func Cmd ¶
func Cmd(cmd string, args ...any) SQLCommand
Cmd is a function simplifying the creation of a Command.
func (SQLCommand) String ¶
func (c SQLCommand) String() string
type SQLDB ¶
type SQLDB interface {
Database
// DB return the sql database handle.
DB() *sql.DB
// StartTransaction starts a transaction.
StartTransaction(ctx context.Context, opts *sql.TxOptions) (SQLTx, error)
// VersionTx returns the database version and is called in a transaction.
VersionTx(tx SQLTx) (Version, error)
// SetVersion changes the database version from info.From to info.To.
SetVersion(ctx context.Context, info StepInfo, dryRun bool, log Logger) error
// SetVersionTx changes the database version from info.From to info.To.
// dryRun is provided for information purpose only.
SetVersionTx(tx SQLTx, info StepInfo, dryRun bool, log Logger) error
// Queries returns the database specific queries.
Queries() *Queries
}
SQLDB is an sql database.
type SQLTx ¶
type SQLTx interface {
// Tx returns the sql transaction.
Tx() *sql.Tx
// FinalizeTransaction finalizes a transaction and should be called as a deferred
// after starting the transaction. It commits the transaction when err is nil and dryRun
// is false, otherwise it rolls back the transaction.
FinalizeTransaction(err *error, dryRun bool)
}
SQLTx is an sql database transaction handle.
type SlogAdapter ¶
type SlogAdapter struct {
// contains filtered or unexported fields
}
SlogAdapter adapts slog.Logger to Logger
func (*SlogAdapter) Debug ¶
func (a *SlogAdapter) Debug(msg string, fields ...Field)
Debug logs an debug level message.
func (*SlogAdapter) Error ¶
func (a *SlogAdapter) Error(msg string, fields ...Field)
Error logs an error level message.
func (*SlogAdapter) Info ¶
func (a *SlogAdapter) Info(msg string, fields ...Field)
Info logs an info level message.
func (*SlogAdapter) Level ¶
func (a *SlogAdapter) Level() LogLevel
Level returns the current logging level.
func (*SlogAdapter) SetLevel ¶
func (a *SlogAdapter) SetLevel(lvl LogLevel)
SetLevel set the logging level.
func (*SlogAdapter) Warn ¶
func (a *SlogAdapter) Warn(msg string, fields ...Field)
Warn logs a warning level message.
type StepFunc ¶
StepFunc is a migration step operation. It is required to check the version prior to any operation by calling db.Version(), do its operations and then call db.SetVersion() if no error occurred. These operations should be wrapped in a transaction to ensure database integrity that should be rolled back in case of error of if dryRun is true.
func NoTx ¶
func NoTx(cmds ...SQLCommand) StepFunc
NoTx returns a migration step function that executes the SQL commands in sequence without a wrapping transaction. It terminates as soon as a command returns an error. It doesn't execute any cmds when dryRun is true.
func NoTxF ¶
NoTxF returns a migration step function that executes the user provided functions in sequence without a wrapping transaction. It terminates as soon as a function returns an error. It doesn't execute any function when dryRun is true. The pseudo error ErrCancel is treated as ErrAbort as operations can't be cancelled and database version remains info.From().
Use with care as any error in the function may leave the database is an undefined state.
func Tx ¶
func Tx(cmds ...SQLCommand) StepFunc
Tx returns a migration step function that executes all the SQL commands in sequence wrapped in a transaction. The execution stops and rolls back as soon as an error is returned by one of the commands. It is also rolled back when dryRun is true.
func TxF ¶
TxF returns a migration step function that executes all the user provided functions in sequence wrapped in a transaction. The execution stops and rolls back as soon as an error is returned by one of the function and the step function returns the error.
A user function may return the ErrAbort pseudo error to force a termination of the function execution and the AllUp or AllDown execution which will return the ErrAbort error. To force a roll back of the transaction without terminating the execution of subsequent functions and migration steps, it must return the ErrCancel pseudo error. The migration step function will return nil as error.
type StepInfo ¶
type StepInfo interface {
fmt.Stringer
// Name returns the step name.
Name() string
// From is the version of the database before the migrate step.
From() Version
// To is the version of the database after the migration step if there
// is no error or DryRun is false.
To() Version
}
StepInfo is a step information.
type Stepper ¶
type Stepper interface {
// Len returns the number of steps.
Len() int
// Version returns the version of step ID. ID 0 is the initial state
// of the database after it is initialized for migration. It is also
// the state to which the database is returned with the migration
// AllDown. It is the state where the database should be empty.
Version(ID int) (Version, error)
// Name returns the migration step name. ID 0 is the initial state
// of the database after it is initialized for migration. It is also
// to state to which the database is returned with the migration
// AllDown. It is the state where the database should be empty.
Name(ID int) (string, error)
// Check returns an error if the given version is invalid. It may be
// one of ErrBadVersionID when the ID is out of range, or ErrBadChecksum
// if the checksum doesn't match the one in the stepper.
Check(Version) error
// Up returns the StepInfo and function for one step up migration
// from the given version to the next version.
// It returns a ErrBadVersion if the version is invalid or
// ErrEndOfSteps if there are no more steps.
Up(Version) (StepInfo, StepFunc, error)
// Down returns the StepInfo and function for one step down migration
// from the given version to the next version.
// It returns a ErrBadVersion if the version is invalid or
// ErrEndOfSteps if there are no more steps.
Down(Version) (StepInfo, StepFunc, error)
}
A Stepper manages migration steps.
type Steps ¶
type Steps struct {
// contains filtered or unexported fields
}
Steps is a read only sequence of migration steps.
func NewSteps ¶
NewSteps instantiates a new migration step sequence. The name should not be empty and ideally unique to the database as it is used to compute the root checksum identifying the database.
func (*Steps) Append ¶
Append appends a new migration step to the list. Name must not be empty as it is used to compute a checksum. The functions up or down may be nil.
func (*Steps) Check ¶
Check returns an error if the given version is invalid. It may be one of ErrBadVersionID when the ID is out of range, or ErrBadVersionChecksum if the checksum doesn't match the one in the stepper.
func (*Steps) Down ¶
Down returns the StepInfo and function for one step down migration from the given version to the next version. It returns a ErrBadVersion if the version is invalid or ErrEndOfSteps if there are no more steps.
func (*Steps) Name ¶
Name returns the migration step name. ID 0 is the initial state of the database after it is initialized for migration. It is also to state to which the database is returned with the migration AllDown. It is the state where the database should be empty.
func (*Steps) Up ¶
Up returns the StepInfo and function for one step up migration from the given version to the next version. It returns a ErrBadVersion if the version is invalid or ErrEndOfSteps if there are no more steps.
type Version ¶
Version is a migration version.
func MakeVersion ¶
MakeVersion make a version from an id and checksum value.