Documentation ¶
Overview ¶
Example ¶
package main import ( "log" "github.com/rickbassham/database" "github.com/rickbassham/database/migrate" ) func main() { db, err := database.New(nil) if err != nil { panic(err.Error()) } migration, err := migrate.NewMigrateDB(db) if err != nil { panic(err.Error()) } err = migration.Init() if err != nil { panic(err.Error()) } migration.AddMigration(migrate.NewSQLMigration(1, "CREATE TABLE test (id integer, name varchar(50));")) version, err := migration.CurrentVersion() if err != nil { panic(err.Error()) } log.Printf("starting version: %d\n", version) for migration.Upgrade() { version, err := migration.CurrentVersion() if err != nil { panic(err.Error()) } log.Printf("now at version: %d\n", version) } if err = migration.Err(); err != nil { panic(err.Error()) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnknownMigrationType is returned when we don't recognize the migration file type. ErrUnknownMigrationType = errors.New("unknown migration type") // ErrInvalidMigrationFileName is returned when we the migration file doens't match the regex `(\d+)_(.*?)\.(sql|so)`. ErrInvalidMigrationFileName = errors.New("invalid migration file name") // ErrInvalidPluginMigration is returned when an invalid .so file was loaded as a plugin migration. ErrInvalidPluginMigration = errors.New("invalid plugin migration") )
Functions ¶
This section is empty.
Types ¶
type PluginMigration ¶
type PluginMigration struct {
// contains filtered or unexported fields
}
PluginMigration is a migration loaded from a golang .so plugin.
func NewPluginMigration ¶
func NewPluginMigration(path string) (m PluginMigration, err error)
NewPluginMigration creates a Migration from a golang .so plugin file.
func (PluginMigration) Version ¶
func (m PluginMigration) Version() int
Version is the version number of the migration.
type SQLMigration ¶
type SQLMigration struct {
// contains filtered or unexported fields
}
SQLMigration is a simple SQL statement based migration.
func NewSQLMigration ¶
func NewSQLMigration(version int, statement string) SQLMigration
NewSQLMigration creates a new SQL migration.
func NewSQLMigrationFile ¶
func NewSQLMigrationFile(path string) (m SQLMigration, err error)
NewSQLMigrationFile creates a new SQL migration from the given file. The file name must match the regex `(\d+)_(.*?)\.(sql|so)`.
func (SQLMigration) Version ¶
func (m SQLMigration) Version() int
Version is the version number of the migration.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is responsible for holding state to upgrade the database.
func NewService ¶
NewService creates a new Service.
func (*Service) AddMigration ¶
AddMigration allows the user to add any custom migrations to be run. This is useful for more complex code-based migrations.
func (*Service) CurrentVersion ¶
CurrentVersion returns the current version of our database.
func (*Service) LoadMigrations ¶
LoadMigrations will load all of the sql and so files in the given path.