Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 48 additions & 125 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func downCreateUsersTable(c schema.Context) error {

### Running Migrations

Create a CLI tool to manage migrations within your Go application:
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
Expand All @@ -68,131 +74,66 @@ import (
"log"
"os"

"github.com/akfaiz/migris"
_ "github.com/akfaiz/migris/examples/basic/migrations" // Import migrations directory
"github.com/akfaiz/migris/extra/migriscli"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/joho/godotenv"
"github.com/urfave/cli/v3"
)

const migrationDir = "migrations"

func loadDatabaseURL() string {
err := godotenv.Load()
func main() {
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal("Error loading .env file")
}
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
log.Fatal("DATABASE_URL is not set in the environment")
log.Fatal(err)
}
return databaseURL
}
defer db.Close()

func createMigrator(dryRun bool) (*migris.Migrate, error) {
databaseURL := loadDatabaseURL()
db, err := sql.Open("pgx", databaseURL)
if err != nil {
return nil, err
cfg := migriscli.Config{
DB: db,
Dialect: "pgx",
MigrationsDir: "./migrations",
}

options := []migris.Option{
migris.WithDB(db),
migris.WithMigrationDir(migrationDir),
cmd := migriscli.NewCLI(cfg)
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
```

if dryRun {
options = append(options, migris.WithDryRun())
}
#### Using Cobra

return migris.New("pgx", options...)
}
```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()

cmd := &cli.Command{
Name: "migrate",
Usage: "Migration tool",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"d"},
Usage: "Run migrations in dry-run mode (print SQL without executing)",
},
},
Commands: []*cli.Command{
{
Name: "create",
Usage: "Create a new migration file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of the migration",
Required: true,
},
},
Action: func(ctx context.Context, c *cli.Command) error {
migrator, err := createMigrator(false)
if err != nil {
return err
}
return migrator.Create(c.String("name"))
},
},
{
Name: "up",
Usage: "Run all pending migrations",
Action: func(ctx context.Context, c *cli.Command) error {
migrator, err := createMigrator(c.Bool("dry-run"))
if err != nil {
return err
}
return migrator.UpContext(ctx)
},
},
{
Name: "reset",
Usage: "Rollback all migrations",
Action: func(ctx context.Context, c *cli.Command) error {
migrator, err := createMigrator(c.Bool("dry-run"))
if err != nil {
return err
}
return migrator.ResetContext(ctx)
},
},
{
Name: "down",
Usage: "Rollback the last migration",
Action: func(ctx context.Context, c *cli.Command) error {
migrator, err := createMigrator(c.Bool("dry-run"))
if err != nil {
return err
}
return migrator.DownContext(ctx)
},
},
{
Name: "status",
Usage: "Show the status of migrations",
Action: func(ctx context.Context, c *cli.Command) error {
migrator, err := createMigrator(false)
if err != nil {
return err
}
return migrator.StatusContext(ctx)
},
},
},
cfg := migriscobra.Config{
DB: db,
Dialect: "pgx",
MigrationsDir: "./migrations",
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Printf("Error running app: %v\n", err)
os.Exit(1)

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:
Expand Down Expand Up @@ -253,24 +194,6 @@ Dry-run mode shows:
- Execution timing and summary statistics
- Clear indication that no database changes are made

## Dry-Run Mode

Enable dry-run mode to preview migrations without executing them:

```go
migrator, err := migris.New("pgx",
migris.WithDB(db),
migris.WithMigrationDir(migrationDir),
migris.WithDryRun(true), // Enable dry-run mode
)
```

Dry-run mode automatically displays:

- Migration progress and status
- All generated SQL statements
- Summary of pending migrations

## Database Support

Currently supported databases:
Expand Down
32 changes: 32 additions & 0 deletions examples/migriscli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/akfaiz/migris/examples/migriscli

go 1.24.0

require (
github.com/akfaiz/migris v0.4.0
github.com/akfaiz/migris/extra/migriscli v0.0.0
github.com/jackc/pgx/v5 v5.8.0
github.com/joho/godotenv v1.5.1
)

require (
github.com/fatih/color v1.18.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/pressly/goose/v3 v3.26.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/urfave/cli/v3 v3.6.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/term v0.39.0 // indirect
golang.org/x/text v0.33.0 // indirect
)

replace github.com/akfaiz/migris => ../..

replace github.com/akfaiz/migris/extra/migriscli => ../../extra/migriscli
71 changes: 71 additions & 0 deletions examples/migriscli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.8.0 h1:TYPDoleBBme0xGSAX3/+NujXXtpZn9HBONkQC7IEZSo=
github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.26.0 h1:KJakav68jdH0WDvoAcj8+n61WqOIaPGgH0bJWS6jpmM=
github.com/pressly/goose/v3 v3.26.0/go.mod h1:4hC1KrritdCxtuFsqgs1R4AU5bWtTAf+cnWvfhf2DNY=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
46 changes: 46 additions & 0 deletions examples/migriscli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"database/sql"
"log"
"os"

_ "github.com/akfaiz/migris/examples/migriscli/migrations" // Import migrations directory
"github.com/akfaiz/migris/extra/migriscli"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/joho/godotenv"
)

const migrationDir = "migrations"

func loadDatabaseURL() string {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
log.Fatal("DATABASE_URL is not set in the environment")
}
return databaseURL
}

func main() {
databaseURL := loadDatabaseURL()
db, err := sql.Open("pgx", databaseURL)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()

cmd := migriscli.NewCLI(migriscli.Config{
DB: db,
Dialect: "pgx",
MigrationsDir: migrationDir,
})
err = cmd.Run(context.Background(), os.Args)
if err != nil {
log.Fatalf("Command failed: %v", err)
}
}
23 changes: 23 additions & 0 deletions examples/migriscli/migrations/20250904164848_create_users_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.Increments("id").Primary()
table.String("username")
table.String("email").Unique()
table.Timestamp("created_at").UseCurrent()
})
}

func downCreateUsersTable(c schema.Context) error {
return schema.DropIfExists(c, "users")
}
Loading