https://github.com/akfaiz/migris
Database migration library for Go with fluent schema builder and multi-database support
https://github.com/akfaiz/migris
database go go-migration hacktoberfest migrations
Last synced: about 2 months ago
JSON representation
Database migration library for Go with fluent schema builder and multi-database support
- Host: GitHub
- URL: https://github.com/akfaiz/migris
- Owner: akfaiz
- License: mit
- Created: 2025-06-28T08:44:36.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2026-05-02T04:06:13.000Z (about 2 months ago)
- Last Synced: 2026-05-02T06:16:47.705Z (about 2 months ago)
- Topics: database, go, go-migration, hacktoberfest, migrations
- Language: Go
- Homepage: https://pkg.go.dev/github.com/akfaiz/migris
- Size: 194 KB
- Stars: 9
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Migris
[](https://pkg.go.dev/github.com/akfaiz/migris)
[](https://goreportcard.com/report/github.com/akfaiz/migris)
[](https://codecov.io/gh/akfaiz/migris)
[](https://github.com/akfaiz/migris/blob/main/LICENSE)
**Migris** is a database migration library for Go, inspired by Laravel's migrations. It combines the power of [pressly/goose](https://github.com/pressly/goose) with a fluent schema builder, making migrations easy to write, run, and maintain.
## Features
- **Migration management** - Run up, down, reset, status, and create operations
- **Dry-run mode** - Preview migrations without executing them to see generated SQL
- **Fluent schema builder** - Laravel-inspired API for defining database schemas
- **Multi-database support** - Works with PostgreSQL, MySQL, MariaDB, and SQLite3
- **Transaction safety** - All migrations run within database transactions
- **Native Go integration** - No external CLI tools required
## Installation
```bash
go get -u github.com/akfaiz/migris
```
## Quick Start
### Creating Migrations
Define migrations using the fluent schema builder API:
```go
package migrations
import (
"github.com/akfaiz/migris"
"github.com/akfaiz/migris/schema"
)
func init() {
migris.AddMigrationContext(upCreateUsersTable, downCreateUsersTable)
}
func upCreateUsersTable(c schema.Context) error {
return schema.Create(c, "users", func(table *schema.Blueprint) {
table.ID()
table.String("name")
table.String("email").Unique()
table.Timestamp("email_verified_at").Nullable()
table.String("password")
table.Timestamps()
})
}
func downCreateUsersTable(c schema.Context) error {
return schema.DropIfExists(c, "users")
}
```
### Running Migrations
For a complete CLI setup example, see [examples/basic](examples/basic/). For quick setup, use the CLI helpers below.
### CLI Helpers
For simpler CLI integration, use the pre-built CLI helpers:
#### Using urfave/cli
```go
package main
import (
"context"
"database/sql"
"log"
"os"
"github.com/akfaiz/migris/extra/migriscli"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
cfg := migriscli.Config{
DB: db,
Dialect: "pgx",
MigrationsDir: "./migrations",
}
cmd := migriscli.NewCLI(cfg)
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
```
#### Using Cobra
```go
package main
import (
"database/sql"
"log"
"os"
"github.com/akfaiz/migris/extra/migriscobra"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
cfg := migriscobra.Config{
DB: db,
Dialect: "pgx",
MigrationsDir: "./migrations",
}
cmd := migriscobra.NewCLI(cfg)
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
```
Both CLI helpers support all migration commands: `create`, `up`, `up-to`, `down`, `down-to`, `reset`, `status` with `--dry-run` support.
## Schema Builder API
The schema builder provides a fluent interface for defining database schemas:
```go
// Creating tables
schema.Create(c, "posts", func(table *schema.Blueprint) {
table.ID()
table.String("title")
table.Text("content")
table.UnsignedBigInteger("user_id")
table.Boolean("published").Default(false)
table.Timestamps()
// Foreign key constraints
table.Foreign("user_id").References("id").On("users")
// Indexes
table.Index([]string{"title", "published"})
})
// Modifying existing tables
schema.Table(c, "posts", func(table *schema.Blueprint) {
table.String("slug")
table.DropColumn("old_column")
})
```
## Migration Operations
Migris supports all standard migration operations:
```go
migrator.Up() // Run all pending migrations
migrator.Down() // Rollback the last migration
migrator.Reset() // Rollback all migrations
migrator.Status() // Show migration status
migrator.Create(name) // Create a new migration file
```
### Dry-Run Mode
Preview migrations without executing them:
```bash
# Preview pending migrations
go run main.go --dry-run up
# Preview rollback operations
go run main.go --dry-run down
go run main.go --dry-run reset
```
Dry-run mode shows:
- Which migrations would be executed
- The exact SQL statements that would be generated
- Execution timing and summary statistics
- Clear indication that no database changes are made
## Database Support
Currently supported databases:
- **PostgreSQL** (via pgx driver)
- **MySQL**
- **MariaDB**
- **SQLite3**
## Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
## License
Released under the MIT License. See [LICENSE](./LICENSE) for details.