Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement fizz renderer #2

Merged
merged 3 commits into from
Jun 18, 2020
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
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ builds:
id: ory
flags:
- -a
- -tags sqlite
ldflags:
- -s -w -X github.com/ory/cli/cmd.Version={{.Tag}} -X github.com/ory/cli/cmd.Commit={{.FullCommit}} -X github.com/ory/cli/cmd.Date={{.Date}}
binary: ory
env:
- CGO_ENABLED=0
- CGO_ENABLED=1
goarch:
- amd64
- 386
Expand Down
2 changes: 1 addition & 1 deletion cmd/dev/newsletter/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Example:
release campaign send 12345
`,
Run: func(cmd *cobra.Command, args []string) {
SendCampaign(args[0],flagx.MustGetBool(cmd, "dry"))
SendCampaign(args[0], flagx.MustGetBool(cmd, "dry"))
},
}

Expand Down
105 changes: 105 additions & 0 deletions cmd/dev/pop/migration/fizzx/file_migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package fizzx

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

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

type DumpMigrator struct {
pop.Migrator
Path string
}

func NewDumpMigrator(path string, dest string, shouldReplace, dumpSchema bool, c *pop.Connection) (DumpMigrator, error) {
fm := DumpMigrator{
Migrator: pop.NewMigrator(c),
Path: path,
}

if dumpSchema {
d, err := ioutil.TempDir(os.TempDir(),
fmt.Sprintf("schema-%s-*", c.Dialect.Name()))
if err != nil {
return fm, err
}
fm.SchemaPath = d
}

runner := func(mf pop.Migration, tx *pop.Connection) error {
f, err := os.Open(mf.Path)
if err != nil {
return err
}
defer f.Close()
content, err := pop.MigrationContent(mf, tx, f, true)
if err != nil {
return errors.Wrapf(err, "error processing %s", mf.Path)
}
if content == "" {
return nil
}

_, fn := filepath.Split(mf.Path)
fn = strings.Replace(fn, ".up.fizz", fmt.Sprintf(".%s.up.sql", tx.Dialect.Name()), -1)
fn = strings.Replace(fn, ".down.fizz", fmt.Sprintf(".%s.down.sql", tx.Dialect.Name()), -1)
if err := writeFile(filepath.Join(dest, fn), []byte(content), shouldReplace); err != nil {
return err
}

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

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

return fm, nil
}

func (fm *DumpMigrator) findMigrations(runner func(mf pop.Migration, tx *pop.Connection) error) error {
dir := fm.Path
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
// directory doesn't exist
return nil
}
return filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
if !info.IsDir() {
match, err := pop.ParseMigrationFilename(info.Name())
if err != nil {
return err
}
if match == nil {
return nil
}
mf := pop.Migration{
Path: p,
Version: match.Version,
Name: match.Name,
DBType: match.DBType,
Direction: match.Direction,
Type: match.Type,
Runner: runner,
}
fm.Migrations[mf.Direction] = append(fm.Migrations[mf.Direction], mf)
}
return nil
})
}

func writeFile(path string, contents []byte, replace bool) error {
if _, err := os.Stat(path); os.IsNotExist(err) || replace {
return ioutil.WriteFile(path, contents, 0666)
}
return nil
}
118 changes: 118 additions & 0 deletions cmd/dev/pop/migration/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package migration

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"

"github.com/go-sql-driver/mysql"
"github.com/gobuffalo/pop/v5"
"github.com/gobuffalo/pop/v5/logging"
"github.com/ory/x/flagx"
"github.com/ory/x/randx"
"github.com/ory/x/sqlcon/dockertest"
"github.com/spf13/cobra"

_ "github.com/jackc/pgx/v4/stdlib"

"github.com/avast/retry-go"

"github.com/ory/cli/cmd/dev/pop/migration/fizzx"
"github.com/ory/cli/cmd/pkg"
)

var render = &cobra.Command{
Use: "render [path/to/fizz-templates] [path/to/output]",
Short: "Renders all fizz templates to their SQL counterparts",
Long: `This command takes fizz migration templates and renders them as SQL.

It currently supports MySQL, SQLite, PostgreSQL, and CockroachDB (SQL). To use this tool you need Docker installed.
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
defer dockertest.KillAllTestDatabases()

// Disable log outputs
pop.SetLogger(func(lvl logging.Level, s string, args ...interface{}) {})
_ = mysql.SetLogger(log.New(ioutil.Discard, "", 0))

var l sync.Mutex
dsns := map[string]string{
"sqlite": "sqlite3://" + filepath.Join(os.TempDir(), randx.MustString(12, randx.AlphaNum)) + ".sql?mode=memory&_fk=true"}

dockertest.Parallel([]func(){
func() {
u, err := dockertest.RunPostgreSQL()
pkg.Check(err)
l.Lock()
dsns["postgres"] = u
l.Unlock()
},
func() {
u, err := dockertest.RunMySQL()
pkg.Check(err)
l.Lock()
dsns["mysql"] = u
l.Unlock()
},
func() {
u, err := dockertest.RunCockroachDB()
pkg.Check(err)
l.Lock()
dsns["cockroach"] = u
l.Unlock()
},
})

pkg.Check(os.MkdirAll(args[1], 0777))

dump := flagx.MustGetBool(cmd, "dump")
replace := flagx.MustGetBool(cmd, "replace")

var wg sync.WaitGroup
runner := func(name, dsn string) {
defer wg.Done()
c, err := pop.NewConnection(&pop.ConnectionDetails{URL: dsn})
pkg.Check(err)

pkg.Check(retry.Do(func() error {
if err := c.Open(); err != nil {
return err
}
return c.RawQuery("SELECT 1").Exec()
}))

m, err := fizzx.NewDumpMigrator(args[0], args[1], replace, dump, c)
pkg.Check(err)

pkg.Check(m.Up())

if dump {
_ = m.DumpMigrationSchema()
_, _ = fmt.Fprintf(os.Stderr, "Dumped %s schema to: %s\n", name, m.SchemaPath)
}

pkg.Check(m.Down(-1))
pkg.Check(c.Close())
}

wg.Add(len(dsns))
// Ensure a connection exists and works before running the translators.
for name, dsn := range dsns {
go runner(name, dsn)
}

wg.Wait()
return nil
},
}

func init() {
Main.AddCommand(render)

render.Flags().BoolP("replace", "r", false, "Replaces existing files if set.")
render.Flags().BoolP("dump", "d", false, "If set dumps the schema to a temporary location.")
}
2 changes: 1 addition & 1 deletion cmd/dev/pop/migration/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func makeFileIfNotExist(p string) error {
var syncCmd = &cobra.Command{
Use: "sync [path/to/migrations] [path/to/testdata] [path/to/fixtures]",
Short: "Creates testdata files and fixtures directories",
Args: cobra.RangeArgs(2,3),
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
return filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
if info.IsDir() || (filepath.Ext(path) != ".sql" && filepath.Ext(path) != ".fizz") {
Expand Down
1 change: 0 additions & 1 deletion cmd/dev/release/notify/draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ var draft = &cobra.Command{
%s
`, tagMessage, changelog)


return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dev/release/notify/draft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Super multiline
require.False(t, o)
} else {
require.True(t, o)
assert.EqualValues(t, tc.v, "v"+ v.String())
assert.EqualValues(t, tc.v, "v"+v.String())
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/dev/release/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var publish = &cobra.Command{
dry := flagx.MustGetBool(cmd, "dry")
gitCleanTags()

currentVersion, err := semver.StrictNewVersion(strings.TrimPrefix(pkg.GitGetCurrentTag(),"v"))
currentVersion, err := semver.StrictNewVersion(strings.TrimPrefix(pkg.GitGetCurrentTag(), "v"))
pkg.Check(err, "Unable to parse current git tag %s: %s", pkg.GitGetCurrentTag(), err)

var nextVersion semver.Version
Expand All @@ -44,7 +44,7 @@ var publish = &cobra.Command{
case "patch":
nextVersion = currentVersion.IncPatch()
default:
nv, err := semver.StrictNewVersion(strings.TrimPrefix(args[0],"v"))
nv, err := semver.StrictNewVersion(strings.TrimPrefix(args[0], "v"))
pkg.Check(err)
nextVersion = *nv
}
Expand All @@ -70,7 +70,7 @@ var publish = &cobra.Command{

var fromVersion *semver.Version
if ov := flagx.MustGetString(cmd, "from-version"); len(ov) > 0 {
fromVersion, err = semver.StrictNewVersion(strings.TrimPrefix(ov,"v"))
fromVersion, err = semver.StrictNewVersion(strings.TrimPrefix(ov, "v"))
pkg.Check(err, "Unable to parse from-version git tag v%s: %s", ov, err)
checkIfTagExists(fromVersion)
}
Expand All @@ -81,13 +81,13 @@ var publish = &cobra.Command{
}

func checkForDuplicateTag(v *semver.Version) {
if stringslice.Has(strings.Split(pkg.GitListTags(), "\n"), fmt.Sprintf("v%s",v)) {
if stringslice.Has(strings.Split(pkg.GitListTags(), "\n"), fmt.Sprintf("v%s", v)) {
pkg.Fatalf(`Version v%s exists already and can not be re-released!`, v.String())
}
}

func checkIfTagExists(v *semver.Version) {
if !stringslice.Has(strings.Split(pkg.GitListTags(), "\n"), fmt.Sprintf("v%s",v)) {
if !stringslice.Has(strings.Split(pkg.GitListTags(), "\n"), fmt.Sprintf("v%s", v)) {
pkg.Fatalf(`Version v%s does not exist!`, v.String())
}
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/pkg/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func MustGetEnv(key string) (v string) {

func Fatalf(message string, args ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, message+"\n", args...)
_, _ = fmt.Fprintf(os.Stderr,"Stack trace: %s\n", debug.Stack())
if os.Getenv("LOG_LEVEL") == "trace" {
_, _ = fmt.Fprintf(os.Stderr, "Stack trace: %s\n", debug.Stack())
}
os.Exit(1)
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ go 1.14

require (
github.com/Masterminds/semver/v3 v3.0.3
github.com/avast/retry-go v2.6.0+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/fizz v1.10.0 // indirect
github.com/gobuffalo/logger v1.0.3
github.com/gobuffalo/packr/v2 v2.7.1
github.com/gobuffalo/pop/v5 v5.0.11
github.com/gomarkdown/markdown v0.0.0-20200609195525-3f9352745725
github.com/google/uuid v1.1.0
github.com/jackc/pgx/v4 v4.4.1
github.com/markbates/pkger v0.17.0
github.com/ory/gochimp3 v0.0.0-20200417124117-ccd242db3655
github.com/ory/viper v1.7.5
github.com/ory/x v0.0.128
github.com/ory/x v0.0.131
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.5.1
Expand Down
Loading