Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noonat/migrate
A simple migration helper for Go's database/sql package.
https://github.com/noonat/migrate
database golang migrations mysql postgres postgresql sql sqlite
Last synced: 28 days ago
JSON representation
A simple migration helper for Go's database/sql package.
- Host: GitHub
- URL: https://github.com/noonat/migrate
- Owner: noonat
- License: mit
- Created: 2018-08-30T01:43:23.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-30T01:59:15.000Z (about 6 years ago)
- Last Synced: 2024-10-03T08:10:20.431Z (about 1 month ago)
- Topics: database, golang, migrations, mysql, postgres, postgresql, sql, sqlite
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# migrate
[![godoc](https://godoc.org/github.com/noonat/migrate?status.svg)][godoc]
[![travis](https://travis-ci.org/noonat/migrate.svg)][travis]
[![report](https://goreportcard.com/badge/github.com/noonat/migrate)][report]Package migrate provides helpers for running SQL database migrations. It's
designed for migrations that are specified in code and distributed as part
of the application binary, and applied as part of the application startup
(rather than via external files and an external tool).## Usage
You can read the [package documentation][godoc] for more information, but here
is a simple example:```go
adapter := migrate.NewPostgreSQLAdapter(log.Printf)
db, err := sql.Open("migrate_test", "")
if err != nil {
log.Panic(err)
}migrations := []migrate.Migration{
{
Comment: "Add user and app tables",
Up: migrate.ExecQueries([]string{
`CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL)`,
`CREATE TABLE apps (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL)`,
}),
Down: migrate.ExecQueries([]string{
`DROP TABLE apps`,
`DROP TABLE users`,
}),
},
{
Comment: "Add user app join table",
Up: migrate.ExecQueries([]string{
`CREATE TABLE user_apps (
user_id INT NOT NULL REFERENCES users(id),
app_id INT NOT NULL REFERENCES apps(id),
UNIQUE (user_id, app_id))`,
}),
Down: migrate.ExecQueries([]string{
`DROP TABLE user_apps`,
}),
},
}err = migrate.Up(context.Background(), db, adapter, migrations)
if err != nil {
log.Panicf("error running migrations: %s", err)
}
```## License
MIT
[travis]: https://travis-ci.org/noonat/migrate
[report]: https://goreportcard.com/report/github.com/noonat/migrate
[godoc]: https://godoc.org/github.com/noonat/migrate