-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cli tooling to run migrations directly (#145)
* update readme * fixes * add migrate cmd * add migrate cmd * fix path * update readme
- Loading branch information
Showing
8 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ayinke-llc/hermes" | ||
"github.com/ayinke-llc/malak" | ||
"github.com/ayinke-llc/malak/config" | ||
"github.com/golang-migrate/migrate/v4" | ||
_ "github.com/golang-migrate/migrate/v4/database/postgres" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func addMigrateCommand(c *cobra.Command, cfg *config.Config) { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrate your database to the latest version", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
logger, err := getLogger(hermes.DeRef(cfg)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Debug("Migrating the database to the latest version") | ||
|
||
d, err := iofs.New(malak.Migrations, "internal/datastore/postgres/migrations") | ||
if err != nil { | ||
logger.Error("could not set up embedded migrations", zap.Error(err)) | ||
return err | ||
} | ||
|
||
m, err := migrate.NewWithSourceInstance("iofs", d, cfg.Database.Postgres.DSN) | ||
if err != nil { | ||
logger.Error("could not set up migrations", zap.Error(err)) | ||
return err | ||
} | ||
|
||
err = m.Up() | ||
if errors.Is(err, migrate.ErrNoChange) { | ||
logger.Info("no new migration to run") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
logger.Error("could not run migrations", zap.Error(err)) | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
c.AddCommand(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package malak | ||
|
||
import "embed" | ||
|
||
//go:embed internal/datastore/postgres/migrations | ||
var Migrations embed.FS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters