master
migrate
migrate is a package for SQL database migrations in the spirit of dbstore. It is intended to keep its footprint small, requiring only an additional table in the database. There is no rollback support as you should only ever roll forward. SQLite and PostgreSQL support is provided, support for other databases can be added by implementing the Dialect interface.
Installation
go get git.sdf.org/jchenry/migrate
Usage
SQLite Example
import (
"database/sql"
"git.sdf.org/jchenry/migrate"
_ "modernc.org/sqlite"
)
db, _ := sql.Open("sqlite", "database.db")
changes := []migrate.Change{
{
Description: "create people table",
Apply: func(ctx migrate.Context) error {
_, err := ctx.Exec(`
CREATE TABLE people (
given_name VARCHAR(20),
surname VARCHAR(30),
sex CHAR(1),
age SMALLINT);
`)
return err
},
},
{
Description: "Insert a person into people",
Apply: func(ctx migrate.Context) error {
_, err := ctx.Exec(`INSERT INTO people VALUES('Henry','Colin','M', 42)`)
return err
},
},
}
err := migrate.Apply(db, migrate.Sqlite3(), changes)
if err != nil {
// handle error
}
PostgreSQL Example
import (
"database/sql"
"git.sdf.org/jchenry/migrate"
_ "github.com/lib/pq"
)
db, _ := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")
changes := []migrate.Change{
{
Description: "create people table",
Apply: func(ctx migrate.Context) error {
_, err := ctx.Exec(`
CREATE TABLE people (
given_name VARCHAR(20),
surname VARCHAR(30),
sex CHAR(1),
age SMALLINT);
`)
return err
},
},
}
err := migrate.Apply(db, migrate.Postgres(), changes)
if err != nil {
// handle error
}
Supported Databases
- SQLite - Use
migrate.Sqlite3() - PostgreSQL - Use
migrate.Postgres()
To add support for other databases, implement the Dialect interface.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Description
a simple go library to do database migration
Readme
MIT
62 KiB
0 Stars
1 Watchers
0 Forks
Languages
Go
100%