Skip to content

Commit

Permalink
feat: embed migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Oct 21, 2022
1 parent 602cf0d commit 4f82f49
Show file tree
Hide file tree
Showing 28 changed files with 54 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ database:
database-reset: migrate-down migrate-up models

migrate-up:
migrate -database 'postgres://punchr:password@localhost:5432/punchr?sslmode=disable' -path migrations up
migrate -database 'postgres://punchr:password@localhost:5432/punchr?sslmode=disable' -path pkg/db/migrations up

migrate-down:
migrate -database 'postgres://punchr:password@localhost:5432/punchr?sslmode=disable' -path migrations down
migrate -database 'postgres://punchr:password@localhost:5432/punchr?sslmode=disable' -path pkg/db/migrations down

59 changes: 52 additions & 7 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package db
import (
"context"
"database/sql"
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
Expand All @@ -28,6 +32,9 @@ import (
"github.com/dennis-tra/punchr/pkg/util"
)

//go:embed migrations
var migrations embed.FS

type Client struct {
*sql.DB

Expand Down Expand Up @@ -73,22 +80,60 @@ func NewClient(c *cli.Context) (*Client, error) {
return nil, errors.Wrap(err, "new maxmind client")
}

client := &Client{DB: dbh, MMClient: mmClient}
client.applyMigrations(c, dbh)

return client, nil
}

func (c *Client) applyMigrations(ctx *cli.Context, dbh *sql.DB) {
tmpDir, err := os.MkdirTemp("", "punchr-"+ctx.App.Version)
if err != nil {
log.WithError(err).WithField("pattern", "punchr-"+ctx.App.Version).Warnln("Could not create tmp directory for migrations")
return
}
defer func() {
if err = os.RemoveAll(tmpDir); err != nil {
log.WithError(err).WithField("tmpDir", tmpDir).Warnln("Could not clean up tmp directory")
}
}()
log.WithField("dir", tmpDir).Debugln("Created temporary directory")

err = fs.WalkDir(migrations, ".", func(path string, d fs.DirEntry, err error) error {
join := filepath.Join(tmpDir, path)
if d.IsDir() {
return os.MkdirAll(join, 0o755)
}

data, err := migrations.ReadFile(path)
if err != nil {
return errors.Wrap(err, "read file")
}

return os.WriteFile(join, data, 0o644)
})
if err != nil {
log.WithError(err).Warnln("Could not create migrations files")
return
}

// Apply migrations
driver, err := postgres.WithInstance(dbh, &postgres.Config{})
if err != nil {
return nil, err
log.WithError(err).Warnln("Could not create driver instance")
return
}

m, err := migrate.NewWithDatabaseInstance("file://./migrations", "punchr", driver)
m, err := migrate.NewWithDatabaseInstance("file://"+filepath.Join(tmpDir, "migrations"), ctx.String("db-name"), driver)
if err != nil {
return nil, err
log.WithError(err).Warnln("Could not create migrate instance")
return
}

if err = m.Up(); err != nil && err != migrate.ErrNoChange {
return nil, err
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
log.WithError(err).Warnln("Couldn't apply migrations")
return
}

return &Client{DB: dbh, MMClient: mmClient}, nil
}

// DeferRollback calls rollback on the given transaction and logs the potential error.
Expand Down
File renamed without changes.

0 comments on commit 4f82f49

Please sign in to comment.