Documentation ¶
Index ¶
- Constants
- Variables
- type Action
- type FileSource
- type Migration
- type MigrationHistory
- type Migrator
- func (m *Migrator) Down(ctx context.Context) error
- func (m *Migrator) Init(ctx context.Context) error
- func (m *Migrator) New(name string) (string, error)
- func (m *Migrator) Status(ctx context.Context) error
- func (m *Migrator) Undo(ctx context.Context) error
- func (m *Migrator) Up(ctx context.Context) error
- type Option
- type Printer
- type PrinterFunc
- type Reader
- type Repository
- type Source
- type Template
- type TemplateReader
- type VersionFunc
Constants ¶
const ( MigrationUp = Action(0) MigrationDown = Action(1) MigrationStatus = Action(2) )
Variables ¶
var ( ErrVersionMissing = errors.New("sqlize: version part is missing") ErrVersionTypeInvalid = errors.New("sqlize: version type must be int64 and must be a positive number") )
var DefaultPrinter = PrinterFunc(func(format string, args ...interface{}) { fmt.Printf(format, args...) })
DefaultPrinter is a default logger for migrator.
var DefaultTemplating = &templating{}
DefaultTemplating is a default templating engine. This templating engine use token `---+split+---` to split migration actions. for example:
create table if not exists migrator_test_table_1(name text); ---+split+--- drop table if exists migrator_test_table_1;
var DefaultVersioning = func() int64 { return time.Now().UnixNano() }
DefaultVersioning is a default version generator using time unix nano.
Functions ¶
This section is empty.
Types ¶
type FileSource ¶
type FileSource struct {
// contains filtered or unexported fields
}
func (*FileSource) AppendMigration ¶
func (*FileSource) FetchMigrations ¶
type MigrationHistory ¶
MigrationHistory represent a migration history unit.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator knows how to manage database migration.
func NewMigrator ¶
func NewMigrator(source Source, repository Repository, options ...Option) *Migrator
NewMigrator creates a new Migrator instance.
type Option ¶
type Option func(m *Migrator)
Option is an option type that can be used to customize the Migrator configs.
func WithPrinter ¶
WithPrinter knows how to set Migrator printer.
func WithTemplating ¶
func WithTemplating(t TemplateReader) Option
WithTemplating know how to set Migrator templating engine.
func WithVersioning ¶
func WithVersioning(fn VersionFunc) Option
WithVersioning know how to set Migrator version generator.
type Printer ¶
type Printer interface { // Printf knows how to print formatted text. Printf(format string, args ...interface{}) }
Printer is a contract for migration logger.
type PrinterFunc ¶
type PrinterFunc func(format string, args ...interface{})
PrinterFunc is adapter function that can be used to create a new Printer by using function signature.
func (PrinterFunc) Printf ¶
func (p PrinterFunc) Printf(format string, args ...interface{})
type Reader ¶
type Reader interface { // Read knows how to read and parse template based on given action. Read(reader io.Reader, action Action) (string, error) }
Reader knows how to read and parse template.
type Repository ¶
type Repository interface { // CreateMigrationTable knows how to create the migration table. CreateMigrationTable(ctx context.Context) error // FetchCurrentMigrations know how to fetch current migration histories. FetchCurrentMigrations(ctx context.Context) (map[int64]MigrationHistory, error) // ApplyNewMigration knows how to apply a new migration. ApplyNewMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error // ApplyExistingMigration knows how to apply an existing migration. ApplyExistingMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error // UndoExistingMigration knows how to undo existing migration. UndoExistingMigration(ctx context.Context, version int64, script string, appliedAt time.Time) error }
Repository knows how to manage migration at persistence storage level.
func NewPostgreRepository ¶
func NewPostgreRepository(db *sql.DB, table string) Repository
NewPostgreRepository creates a new Repository for PostgreSQL Database.
type Source ¶
type Source interface { // AppendMigration inserts a new migration into source. AppendMigration(version int64, name string, content string) (string, error) // FetchMigrations knows how to get list of migrations from source. FetchMigrations(reader Reader, action Action, ascending bool) ([]Migration, error) }
Source knows how to manage migration source. We can implement this contract to manage migration source from local file system (for example: FileSource)
func NewSourceFromDir ¶
type Template ¶
type Template interface { // Template returns a template format. Template() string }
Template knows how to manage template.
type TemplateReader ¶
TemplateReader knows how to manage and read template.