Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maragudk/migrate
A simple database migration tool.
https://github.com/maragudk/migrate
database-migrations go
Last synced: 4 days ago
JSON representation
A simple database migration tool.
- Host: GitHub
- URL: https://github.com/maragudk/migrate
- Owner: maragudk
- License: mit
- Created: 2021-06-16T12:06:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-10T09:59:41.000Z (4 months ago)
- Last Synced: 2024-12-29T19:30:56.209Z (11 days ago)
- Topics: database-migrations, go
- Language: Go
- Homepage:
- Size: 80.1 KB
- Stars: 50
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Migrate
[![GoDoc](https://pkg.go.dev/badge/maragu.dev/migrate)](https://pkg.go.dev/maragu.dev/migrate)
[![Go](https://github.com/maragudk/migrate/actions/workflows/ci.yml/badge.svg)](https://github.com/maragudk/migrate/actions/workflows/ci.yml)A simple database migration tool using an `sql.DB` connection and `fs.FS` for the migration source. It has no non-test dependencies.
Made in 🇩🇰 by [maragu](https://www.maragu.dk), maker of [online Go courses](https://www.golang.dk/).
## Features
- Simple: The common usage is a one-liner.
- Safe: Each migration is run in a transaction, and automatically rolled back on errors.
- Flexible: Setup a custom migrations table and use callbacks before and after each migration.## Usage
```shell
go get maragu.dev/migrate
``````go
package mainimport (
"context"
"database/sql"
"os"_ "github.com/jackc/pgx/v4/stdlib"
"maragu.dev/migrate"
)// migrations is a directory with sql files that look something like this:
// migrations/1-accounts.up.sql
// migrations/1-accounts.down.sql
// migrations/2-users.up.sql
// migrations/2-users.down.sql
var migrations = os.DirFS("migrations")func main() {
db, err := sql.Open("pgx", "postgresql://postgres:123@localhost:5432/postgres?sslmode=disable")
if err != nil {
panic(err)
}if err := migrate.Up(context.Background(), db, migrations); err != nil {
panic(err)
}if err := migrate.Down(context.Background(), db, migrations); err != nil {
panic(err)
}if err := migrate.To(context.Background(), db, migrations, "1-accounts"); err != nil {
panic(err)
}
}
```### Helper tool
To install the helper tool, run:
```shell
go install maragu.dev/migrate/cmd/migrate@latest
```Then you can run `migrate` in your terminal.
```shell
migrate create sql/migrations accounts
```