Browse Source

Support reverse from reader (#7)

Support reverse from reader

Reviewed-on: #7
pull/10/head
Lunny Xiao 3 months ago
parent
commit
35776e56ac
2 changed files with 44 additions and 1 deletions
  1. +5
    -1
      cmd/reverse.go
  2. +39
    -0
      cmd/reverse_test.go

+ 5
- 1
cmd/reverse.go View File

@@ -8,6 +8,7 @@ import (
"bytes"
"errors"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
@@ -29,9 +30,12 @@ func reverse(rFile string) error {
return err
}
defer f.Close()
return reverseFromReader(f)
}

func reverseFromReader(rd io.Reader) error {
var cfg ReverseConfig
err = yaml.NewDecoder(f).Decode(&cfg)
err := yaml.NewDecoder(rd).Decode(&cfg)
if err != nil {
return err
}


+ 39
- 0
cmd/reverse_test.go View File

@@ -7,10 +7,14 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"xorm.io/xorm"
)

var result = fmt.Sprintf(`package models
@@ -32,3 +36,38 @@ func TestReverse(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, result, string(bs))
}

func TestReverse2(t *testing.T) {
type Outfw struct {
Id int `xorm:"not null pk autoincr"`
Sql string `xorm:"default '' TEXT"`
Template string `xorm:"default '' TEXT"`
Filename string `xorm:"VARCHAR(50)"`
}

dir, err := ioutil.TempDir(os.TempDir(), "reverse")
assert.NoError(t, err)

e, err := xorm.NewEngine("sqlite3", filepath.Join(dir, "db.db"))
assert.NoError(t, err)

assert.NoError(t, e.Sync2(new(Outfw)))

err = reverseFromReader(strings.NewReader(`
kind: reverse
name: mydb
source:
database: sqlite3
conn_str: '../testdata/test.db'
targets:
- type: codes
include_tables:
- a
- b
exclude_tables:
- c
language: golang
output_dir: ../models
`))
assert.NoError(t, err)
}

Loading…
Cancel
Save