Skip to content

Commit

Permalink
Merge pull request #28 from schemahero/fixtures
Browse files Browse the repository at this point in the history
First pass at fixture generation
  • Loading branch information
marccampbell committed May 29, 2019
2 parents 2e74732 + 4287e75 commit a66fb0f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/cli/fixtures.go
@@ -0,0 +1,41 @@
package cli

import (
"github.com/schemahero/schemahero/pkg/fixtures"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Fixtures() *cobra.Command {
cmd := &cobra.Command{
Use: "fixtures",
Short: "fixtures creates sql statements from a schemahero definition",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("driver", cmd.Flags().Lookup("driver"))
viper.BindPFlag("dbname", cmd.Flags().Lookup("dbname"))
viper.BindPFlag("input-dir", cmd.Flags().Lookup("input-dir"))
viper.BindPFlag("output-dir", cmd.Flags().Lookup("output-dir"))
},
RunE: func(cmd *cobra.Command, args []string) error {
f := fixtures.NewFixturator()
return f.RunSync()
},
}

cmd.Flags().String("driver", "", "name of the database driver to use")
cmd.Flags().String("dbname", "", "schemahero database name to write in the yaml")
cmd.Flags().String("input-dir", "", "directory to read schema files from")
cmd.Flags().String("output-dir", "", "directory to write fixture files to")

cmd.MarkFlagRequired("driver")
cmd.MarkFlagRequired("dbname")
cmd.MarkFlagRequired("input-dir")
cmd.MarkFlagRequired("output-dir")

viper.BindPFlags(cmd.Flags())

return cmd
}
1 change: 1 addition & 0 deletions pkg/cli/root.go
Expand Up @@ -24,6 +24,7 @@ func RootCmd() *cobra.Command {

cmd.AddCommand(Watch())
cmd.AddCommand(Generate())
cmd.AddCommand(Fixtures())

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return cmd
Expand Down
86 changes: 86 additions & 0 deletions pkg/fixtures/fixtures.go
@@ -0,0 +1,86 @@
package fixtures

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

schemasv1alpha1 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha1"
"github.com/schemahero/schemahero/pkg/database/postgres"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

type Fixturator struct {
Viper *viper.Viper
}

func NewFixturator() *Fixturator {
return &Fixturator{
Viper: viper.GetViper(),
}
}

func (f *Fixturator) RunSync() error {
fmt.Printf("generating fixtures for resources in %s\n", f.Viper.GetString("input-dir"))

statements := []string{}
handleFile := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

fileData, err := ioutil.ReadFile(filepath.Join(f.Viper.GetString("input-dir"), info.Name()))
if err != nil {
return err
}

parsed := schemasv1alpha1.Table{}
if err := yaml.Unmarshal(fileData, &parsed); err != nil {
fmt.Printf("%s\n", err)
return err
}

if parsed.Spec.Schema == nil {
fmt.Printf("skipping file %s because there is no schema\n", info.Name())
return nil
}

if f.Viper.GetString("driver") == "postgres" {
if parsed.Spec.Schema.Postgres == nil {
fmt.Printf("skipping file %s because there is no postgres spec\n", info.Name())
}

statement, err := postgres.CreateTableStatement(parsed.Spec.Name, parsed.Spec.Schema.Postgres)
if err != nil {
return err
}

statements = append(statements, statement)
}
return nil
}

err := filepath.Walk(f.Viper.GetString("input-dir"), handleFile)
if err != nil {
fmt.Printf("%#v\n", err)
return err
}

output := strings.Join(statements, ";\n")
output = fmt.Sprintf("/* Auto generated file. Do not edit by hand. This file was generated by SchemaHero. */\n\n %s;\n\n", output)

err = ioutil.WriteFile(filepath.Join(f.Viper.GetString("output-dir"), "fixtures.sql"), []byte(output), 0644)
if err != nil {
fmt.Printf("%#v\n", err)
return err
}

return nil
}

0 comments on commit a66fb0f

Please sign in to comment.