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
Given this file:
-- Version: 1.1 -- Description: Create table users CREATE TABLE users ( user_id UUID, name TEXT, email TEXT UNIQUE, roles TEXT[], password_hash TEXT, date_created TIMESTAMP, date_updated TIMESTAMP, PRIMARY KEY (user_id) ); -- Version: 1.2 -- Description: Create table products CREATE TABLE products ( product_id UUID, name TEXT, cost INT, quantity INT, date_created TIMESTAMP, date_updated TIMESTAMP, PRIMARY KEY (product_id) );
You can write this code:
package main import ( "database/sql" "log" "github.com/ardanlabs/darwin/v3" "github.com/ardanlabs/darwin/v3/dialects/postgres" "github.com/ardanlabs/darwin/v3/drivers/generic" _ "github.com/go-sql-driver/mysql" ) var ( //go:embed sql/schema.sql schemaDoc string ) func main() { db, err := sql.Open("mysql", "root:@/darwin") if err != nil { log.Fatal(err) } driver, err := generic.New(db.DB, postgres.Dialect{}) if err != nil { return fmt.Errorf("construct darwin driver: %w", err) } d := darwin.New(driver, darwin.ParseMigrations(schemaDoc)) if err := d.Migrate(); err != nil { log.Println(err) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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.
func (*Darwin) UpdateChecksums ¶
UpdateChecksums updates the checksum values in the migrations table for all existing migrations.
type Driver ¶
type Driver interface { Create() error Insert(MigrationRecord) error UpdateChecksum(checksum string, version float64) 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
Error implements the error interface.
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
Error implements the error interface.
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
Error implements the error interface.
type Migration ¶
Migration represents a database migrations.
func ParseMigrations ¶
ParseMigrations takes a string that represents a text formatted set of migrations and parse them for use.
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 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
Error implements the error interface.
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 )
Directories
¶
Path | Synopsis |
---|---|
dialects
|
|
mysql
Package mysql provides support to work with a mysql database.
|
Package mysql provides support to work with a mysql database. |
postgres
Package postgres provides support to work with a postgres database.
|
Package postgres provides support to work with a postgres database. |
sqlite
Package sqlite provides support to work with a sqlite database.
|
Package sqlite provides support to work with a sqlite database. |
drivers
|
|
generic
Package generic implements a generic driver.
|
Package generic implements a generic driver. |