Skip to content

Commit

Permalink
refactor: expose migrate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GAlexIHU committed Aug 9, 2024
1 parent dbd459d commit 3838aaa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ events:

postgres:
url: postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable
autoMigrate: true # Runs migrations as part of the service startup

meters:
# Sample meter to count API requests
Expand Down
20 changes: 15 additions & 5 deletions tools/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
// A lightweigh migration tool to replace `ent.Schema.Create` calls.
// A very lightweigh migration tool to replace `ent.Schema.Create` calls.
package migrate

import (
"embed"
"io/fs"

"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
)

type Migrate = migrate.Migrate

//go:embed migrations
var fs embed.FS
var omMigrations embed.FS

func Up(conn string) error {
// NewMigrate creates a new migrate instance.
// fs is expected to contain a migrations directory with the migration files.
func NewMigrate(conn string, fs fs.FS) (*Migrate, error) {
d, err := iofs.New(fs, "migrations")
if err != nil {
return err
return nil, err
}
m, err := migrate.NewWithSourceInstance("iofs", d, conn)
return migrate.NewWithSourceInstance("iofs", d, conn)
}

func Up(conn string) error {
m, err := NewMigrate(conn, omMigrations)
if err != nil {
return err
}

defer m.Close()
err = m.Up()
if err != nil {
Expand Down

0 comments on commit 3838aaa

Please sign in to comment.