Skip to content

Commit

Permalink
go embed with database package
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzuo committed May 19, 2021
1 parent 619b123 commit 0e2a56b
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- remove Pagination & items in scaffold
- js `export default function()` one liner
- use go embed for project migration files, add database package

## 0.4.2

Expand Down
81 changes: 81 additions & 0 deletions database/embedmigrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package database

import (
"bytes"
"embed"

"github.com/gobuffalo/pop"
"github.com/pkg/errors"
)

type EmbedMigrator struct {
pop.Migrator
fs *embed.FS
}

func NewEmbedMigrator(fs *embed.FS, c *pop.Connection) (EmbedMigrator, error) {
migrator := EmbedMigrator{
Migrator: pop.NewMigrator(c),
fs: fs,
}

runner := func(mf pop.Migration, tx *pop.Connection) error {
b, err := migrator.fs.ReadFile(mf.Path)
if err != nil {
return err
}

content, err := pop.MigrationContent(mf, tx, bytes.NewReader(b), true)
if err != nil {
return errors.Wrapf(err, "error processing %s", mf.Path)
}
if content == "" {
return nil
}

err = tx.RawQuery(content).Exec()
if err != nil {
return errors.Wrapf(err, "error executing %s, sql: %s", mf.Path, content)
}
return nil
}

err := migrator.findMigrations(runner)
if err != nil {
return migrator, err
}

return migrator, nil
}

func (migrator *EmbedMigrator) findMigrations(runner func(mf pop.Migration, tx *pop.Connection) error) error {
files, err := migrator.fs.ReadDir(("migrations"))
if err != nil {
return err
}

for _, file := range files {
if !file.IsDir() {
match, err := pop.ParseMigrationFilename(file.Name())
if err != nil {
return err
}
if match == nil {
return nil
}

mf := pop.Migration{
Path: "migrations/" + file.Name(),
Version: match.Version,
Name: match.Name,
DBType: match.DBType,
Direction: match.Direction,
Type: match.Type,
Runner: runner,
}
migrator.Migrations[mf.Direction] = append(migrator.Migrations[mf.Direction], mf)
}
}

return nil
}
1 change: 0 additions & 1 deletion generators/new/templates/Makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ image:
docker build -t {{ .opts.Name }} .

build:
find . -name ".DS_Store" -delete
dashi g graphql
go mod tidy
go build -ldflags "-X {{ .opts.Package }}/cmd.Version=`git rev-parse HEAD`" -o ./bin/{{ .opts.Name }}
Expand Down
10 changes: 7 additions & 3 deletions generators/new/templates/cmd/database/database.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package database

import (
"github.com/gobuffalo/packr/v2"
"embed"

"github.com/swifcarrot/dashi/database"
"github.com/gobuffalo/pop"
"{{ .opts.Package }}/config"
)

var MigrationsFS embed.FS

func Connect() (*pop.Connection, error) {
config, err := config.Load()
if err != nil {
Expand Down Expand Up @@ -52,7 +56,7 @@ func DatabaseMigrate() error {
return err
}

mig, err := pop.NewMigrationBox(packr.New("Migrations", "../../migrations"), conn)
mig, err := database.NewEmbedMigrator(&MigrationsFS, conn)
if err != nil {
return err
}
Expand All @@ -66,7 +70,7 @@ func DatabaseRollback() error {
return err
}

mig, err := pop.NewMigrationBox(packr.New("Migrations", "../../migrations"), conn)
mig, err := database.NewEmbedMigrator(&MigrationsFS, conn)
if err != nil {
return err
}
Expand Down
12 changes: 11 additions & 1 deletion generators/new/templates/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package main

import "{{ .opts.Package }}/cmd"
import (
"embed"

"{{ .opts.Package }}/cmd"
"{{ .opts.Package }}/cmd/database"
)

//go:embed migrations/*
var migrationsFS embed.FS

func main() {
database.MigrationsFS = migrationsFS

cmd.Execute()
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/swiftcarrot/dashi

go 1.13
go 1.16

require (
github.com/99designs/gqlgen v0.11.3
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/cockroachdb/cockroach-go v2.0.1+incompatible // indirect
github.com/fatih/color v1.11.0 // indirect
github.com/gobuffalo/envy v1.9.0 // indirect
github.com/gobuffalo/fizz v1.13.0 // indirect
github.com/gobuffalo/genny v0.6.0
github.com/gobuffalo/genny/v2 v2.0.6
github.com/gobuffalo/logger v1.0.3
Expand All @@ -15,7 +17,6 @@ require (
github.com/gobuffalo/pop v4.13.1+incompatible
github.com/gofrs/uuid v3.3.0+incompatible
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/lib/pq v1.5.2
github.com/pkg/errors v0.9.1
github.com/rogpeppe/go-internal v1.6.0 // indirect
Expand Down
146 changes: 103 additions & 43 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e2a56b

Please sign in to comment.