Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const ( // NilVersion is a Claytons version // "the version you are at when you are not at a version" NilVersion = -1 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Migration ¶
type Migration interface {
// The version of this migration
Version() int
// Run the migration
Run(*sql.Tx) error
}
Migration interface
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
A Migrator collates and runs migrations
func NewFSMigrator ¶
Example ¶
package main
import (
"database/sql"
"embed"
"fmt"
"src.userspace.com.au/migrate"
)
func main() {
//go:embed testdata/*.sql
var migrations embed.FS
db, err := sql.Open("sqlite3", "file:test?mode=memory&cache=shared")
if err != nil {
fmt.Printf("error: %s\n", err)
}
defer db.Close()
// Relative path to migration files
migrator, err := migrate.NewFSMigrator(db, migrations)
if err != nil {
fmt.Printf("error: %s\n", err)
}
// Migrate all the way
err = migrator.Migrate()
if err != nil {
fmt.Printf("error: %s\n", err)
}
v, err := migrator.Version()
fmt.Printf("database at version %d\n", v)
}
Output: database at version 2
func NewFileMigrator ¶
NewFileMigrator creates a new set of migrations from a path Each one is run in a transaction.
Example ¶
package main
import (
"database/sql"
"fmt"
"src.userspace.com.au/migrate"
)
func main() {
db, err := sql.Open("sqlite3", "file:test?mode=memory&cache=shared")
if err != nil {
fmt.Printf("error: %s\n", err)
}
defer db.Close()
// Relative path to migration files
migrator, err := migrate.NewFileMigrator(db, "file://testdata/")
if err != nil {
fmt.Printf("error: %s\n", err)
}
// Migrate all the way
err = migrator.Migrate()
if err != nil {
fmt.Printf("error: %s\n", err)
}
v, err := migrator.Version()
fmt.Printf("database at version %d\n", v)
}
Output: database at version 2
func NewStringMigrator ¶
NewStringMigrator creates a new set of migrations from a slice of strings Each one is run in a transaction.
Example ¶
package main
import (
"database/sql"
"fmt"
"src.userspace.com.au/migrate"
)
func main() {
migrations := []string{
"create table if not exists test1 (pk bigint not null primary key);",
"insert into test1 (pk) values (1)",
}
db, err := sql.Open("sqlite3", "file:test?mode=memory&cache=shared")
if err != nil {
fmt.Printf("error: %s\n", err)
}
defer db.Close()
// Relative path to migration files
migrator, err := migrate.NewStringMigrator(db, migrations)
if err != nil {
fmt.Printf("error: %s\n", err)
}
// Migrate all the way
err = migrator.Migrate()
if err != nil {
fmt.Printf("error: %s\n", err)
}
v, err := migrator.Version()
fmt.Printf("database at version %d\n", v)
}
Output: database at version 2
func (*Migrator) Migrate ¶
Migrate migrates the database to the highest possible version
func (*Migrator) MigrateTo ¶
MigrateTo migrates the database to the specified version
type Option ¶
An Option configures a migrator
func SetCallback ¶
func SetCallback(cb ResultFunc) Option
SetCallback configures the table used for recording the schema version
Source Files
¶
- file.go
- fs.go
- migrate.go
- options.go
- string.go
Click to show internal directories.
Click to hide internal directories.