Documentation
¶
Overview ¶
Package darwin provides a database schema evolution api for Go. The purpose of this library is just be a library. You can implement your own way of building the migration list. It is not recommended to put more than one database change per migration, if some migration fail, you exactly what statement caused the error. Also only postgres correctly handle rollback in DDL transactions.
The best way to version your migrations is like this: 1.0, 1.1, 1.2
Please read the following posts for more information on the design principles if this package.
https://flywaydb.org/documentation/faq#downgrade https://flywaydb.org/documentation/faq#rollback https://flywaydb.org/documentation/faq#hot-fixes
Example Usage:
package main import ( "database/sql" "log" "github.com/ardanlabs/darwin" _ "github.com/go-sql-driver/mysql" ) var ( migrations = []darwin.Migration{ { Version: 1, Description: "Creating table posts", Script: `CREATE TABLE posts ( id INT auto_increment, title VARCHAR(255), PRIMARY KEY (id) ) ENGINE=InnoDB CHARACTER SET=utf8;`, }, { Version: 2, Description: "Adding column body", Script: "ALTER TABLE posts ADD body TEXT AFTER title;", }, } ) func main() { database, err := sql.Open("mysql", "root:@/darwin") if err != nil { log.Fatal(err) } driver, err := darwin.NewGenericDriver(database, darwin.MySQLDialect{}) if err != nil { log.Fatal(err) } d := darwin.New(driver, migrations) if err := d.Migrate(); err != nil { log.Println(err) } }
Index ¶
- func Migrate(d Driver, migrations []Migration) error
- func Validate(d Driver, migrations []Migration) error
- type Darwin
- type Dialect
- type Driver
- type DuplicateMigrationVersionError
- type GenericDriver
- type IllegalMigrationVersionError
- type InvalidChecksumError
- type Migration
- type MigrationInfo
- type MigrationRecord
- type MySQLDialect
- type PostgresDialect
- type QLDialect
- type RemovedMigrationError
- type SqliteDialect
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Darwin ¶
type Darwin struct {
// contains filtered or unexported fields
}
Darwin is a helper struct to access the Validate and migration functions.
func (Darwin) Info ¶
func (d Darwin) Info() ([]MigrationInfo, error)
Info returns the status of all migrations.
type Driver ¶
type Driver interface { Create() error Insert(e MigrationRecord) error All() ([]MigrationRecord, error) Exec(string) (time.Duration, error) }
Driver is a database driver abstraction.
type DuplicateMigrationVersionError ¶
type DuplicateMigrationVersionError struct {
Version float64
}
DuplicateMigrationVersionError is used to report when the migration list has duplicated entries.
func (DuplicateMigrationVersionError) Error ¶
func (d DuplicateMigrationVersionError) Error() string
type GenericDriver ¶
GenericDriver is the default Driver, it can be configured to any database.
func NewGenericDriver ¶
func NewGenericDriver(db *sql.DB, dialect Dialect) (*GenericDriver, error)
NewGenericDriver creates a new GenericDriver configured with db and dialect.
func (*GenericDriver) All ¶
func (m *GenericDriver) All() ([]MigrationRecord, error)
All returns all migrations applied.
func (*GenericDriver) Create ¶
func (m *GenericDriver) Create() error
Create create the table darwin_migrations if necessary.
func (*GenericDriver) Exec ¶
func (m *GenericDriver) Exec(script string) (time.Duration, error)
Exec execute sql scripts into database.
func (*GenericDriver) Insert ¶
func (m *GenericDriver) Insert(e MigrationRecord) error
Insert insert a migration entry into database.
type IllegalMigrationVersionError ¶
type IllegalMigrationVersionError struct {
Version float64
}
IllegalMigrationVersionError is used to report when the migration has an illegal Version number.
func (IllegalMigrationVersionError) Error ¶
func (i IllegalMigrationVersionError) Error() string
type InvalidChecksumError ¶
type InvalidChecksumError struct {
Version float64
}
InvalidChecksumError is used to report when a migration was modified.
func (InvalidChecksumError) Error ¶
func (i InvalidChecksumError) Error() string
type MigrationInfo ¶
MigrationInfo is a struct used in the infoChan to inform clients about the migration being applied.
type MigrationRecord ¶
type MigrationRecord struct { Version float64 Description string Checksum string AppliedAt time.Time ExecutionTime time.Duration }
MigrationRecord is the entry in schema table.
type MySQLDialect ¶
type MySQLDialect struct{}
MySQLDialect a Dialect configured for MySQL.
func (MySQLDialect) AllSQL ¶
func (m MySQLDialect) AllSQL() string
AllSQL returns a SQL to get all entries in the table.
func (MySQLDialect) CreateTableSQL ¶
func (m MySQLDialect) CreateTableSQL() string
CreateTableSQL returns the SQL to create the schema table.
func (MySQLDialect) InsertSQL ¶
func (m MySQLDialect) InsertSQL() string
InsertSQL returns the SQL to insert a new migration in the schema table.
type PostgresDialect ¶
type PostgresDialect struct{}
PostgresDialect a Dialect configured for PostgreSQL.
func (PostgresDialect) AllSQL ¶
func (p PostgresDialect) AllSQL() string
AllSQL returns a SQL to get all entries in the table.
func (PostgresDialect) CreateTableSQL ¶
func (p PostgresDialect) CreateTableSQL() string
CreateTableSQL returns the SQL to create the schema table.
func (PostgresDialect) InsertSQL ¶
func (p PostgresDialect) InsertSQL() string
InsertSQL returns the SQL to insert a new migration in the schema table.
type QLDialect ¶
type QLDialect struct{}
QLDialect implements Dialect interface for ql database.
func (QLDialect) CreateTableSQL ¶
CreateTableSQL returns the SQL to create the schema table.
type RemovedMigrationError ¶
type RemovedMigrationError struct {
Version float64
}
RemovedMigrationError is used to report when a migration is removed from the list.
func (RemovedMigrationError) Error ¶
func (r RemovedMigrationError) Error() string
type SqliteDialect ¶
type SqliteDialect struct{}
SqliteDialect a Dialect configured for Sqlite3.
func (SqliteDialect) AllSQL ¶
func (s SqliteDialect) AllSQL() string
AllSQL returns a SQL to get all entries in the table.
func (SqliteDialect) CreateTableSQL ¶
func (s SqliteDialect) CreateTableSQL() string
CreateTableSQL returns the SQL to create the schema table.
func (SqliteDialect) InsertSQL ¶
func (s SqliteDialect) InsertSQL() string
InsertSQL returns the SQL to insert a new migration in the schema table.
type Status ¶
type Status int
Status is a migration status value.
const ( // Ignored means that the migrations was not appied to the database. Ignored Status = iota // Applied means that the migrations was successfully applied to the database. Applied // Pending means that the migrations is a new migration and it is waiting // to be applied to the database. Pending // Error means that the migration could not be applied to the database. Error )