Remove foreign keys from mysql dump
This commit is contained in:
parent
f1f311b70f
commit
76b75dfaaa
4 changed files with 27 additions and 48 deletions
|
@ -206,11 +206,12 @@ var defaultSingletonTestTemplateImports = map[string]imports{
|
||||||
},
|
},
|
||||||
"boil_queries_test": {
|
"boil_queries_test": {
|
||||||
standard: importList{
|
standard: importList{
|
||||||
`"crypto/md5"`,
|
`"bytes"`,
|
||||||
`"fmt"`,
|
`"fmt"`,
|
||||||
`"os"`,
|
`"io"`,
|
||||||
`"strconv"`,
|
`"io/ioutil"`,
|
||||||
`"math/rand"`,
|
`"math/rand"`,
|
||||||
|
`"regexp"`,
|
||||||
},
|
},
|
||||||
thirdParty: importList{
|
thirdParty: importList{
|
||||||
`"github.com/vattle/sqlboiler/boil"`,
|
`"github.com/vattle/sqlboiler/boil"`,
|
||||||
|
@ -238,6 +239,7 @@ var defaultTestMainImports = map[string]imports{
|
||||||
`"github.com/pkg/errors"`,
|
`"github.com/pkg/errors"`,
|
||||||
`"github.com/spf13/viper"`,
|
`"github.com/spf13/viper"`,
|
||||||
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
||||||
|
`"github.com/vattle/sqlboiler/boil/randomize"`,
|
||||||
`_ "github.com/lib/pq"`,
|
`_ "github.com/lib/pq"`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -256,6 +258,7 @@ var defaultTestMainImports = map[string]imports{
|
||||||
`"github.com/pkg/errors"`,
|
`"github.com/pkg/errors"`,
|
||||||
`"github.com/spf13/viper"`,
|
`"github.com/spf13/viper"`,
|
||||||
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
||||||
|
`"github.com/vattle/sqlboiler/boil/randomize"`,
|
||||||
`_ "github.com/go-sql-driver/mysql"`,
|
`_ "github.com/go-sql-driver/mysql"`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (m *mysqlTester) setup() error {
|
||||||
m.port = viper.GetInt("mysql.port")
|
m.port = viper.GetInt("mysql.port")
|
||||||
m.sslmode = viper.GetString("mysql.sslmode")
|
m.sslmode = viper.GetString("mysql.sslmode")
|
||||||
// Create a randomized db name.
|
// Create a randomized db name.
|
||||||
m.testDBName = getDBNameHash(m.dbName)
|
m.testDBName = randomize.StableDBName(m.dbName)
|
||||||
|
|
||||||
if err = m.makeOptionFile(); err != nil {
|
if err = m.makeOptionFile(); err != nil {
|
||||||
return errors.Wrap(err, "couldn't make option file")
|
return errors.Wrap(err, "couldn't make option file")
|
||||||
|
@ -45,7 +45,7 @@ func (m *mysqlTester) setup() error {
|
||||||
|
|
||||||
r, w := io.Pipe()
|
r, w := io.Pipe()
|
||||||
dumpCmd.Stdout = w
|
dumpCmd.Stdout = w
|
||||||
createCmd.Stdin = io.TeeReader(r, os.Stdout)
|
createCmd.Stdin = newFKeyDestroyer(r)
|
||||||
|
|
||||||
if err = dumpCmd.Start(); err != nil {
|
if err = dumpCmd.Start(); err != nil {
|
||||||
return errors.Wrap(err, "failed to start mysqldump command")
|
return errors.Wrap(err, "failed to start mysqldump command")
|
||||||
|
|
|
@ -92,7 +92,7 @@ func (p *pgTester) setup() error {
|
||||||
p.port = viper.GetInt("postgres.port")
|
p.port = viper.GetInt("postgres.port")
|
||||||
p.sslmode = viper.GetString("postgres.sslmode")
|
p.sslmode = viper.GetString("postgres.sslmode")
|
||||||
// Create a randomized db name.
|
// Create a randomized db name.
|
||||||
p.testDBName = getDBNameHash(p.dbName)
|
p.testDBName = randomize.StableDBName(p.dbName)
|
||||||
|
|
||||||
err = p.dropTestDB()
|
err = p.dropTestDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,53 +7,29 @@ func MustTx(transactor boil.Transactor, err error) boil.Transactor {
|
||||||
return transactor
|
return transactor
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDBNameRand(input string) {
|
var rgxPGFkey = regexp.MustCompile(`(?m)(?s)^ALTER TABLE ONLY.*?ADD CONSTRAINT.*?FOREIGN KEY.*?;\n`)
|
||||||
sum := md5.Sum([]byte(input))
|
var rgxMySQLkey = regexp.MustCompile(`(?m)((,\n)?\s+CONSTRAINT.*?FOREIGN KEY.*?\n)+`)
|
||||||
|
|
||||||
var sumInt string
|
func newFKeyDestroyer(reader io.Reader) io.Reader {
|
||||||
for _, v := range sum {
|
return &fKeyDestroyer{
|
||||||
sumInt = sumInt + strconv.Itoa(int(v))
|
reader: reader,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cut integer to 18 digits to ensure no int64 overflow.
|
type fKeyDestroyer struct {
|
||||||
sumInt = sumInt[:18]
|
reader io.Reader
|
||||||
|
buf *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
sumTmp := sumInt
|
func (f *fKeyDestroyer) Read(b []byte) (int, error) {
|
||||||
for i, v := range sumInt {
|
if f.buf == nil {
|
||||||
if v == '0' {
|
all, err := ioutil.ReadAll(f.reader)
|
||||||
sumTmp = sumInt[i+1:]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
sumInt = sumTmp
|
|
||||||
|
|
||||||
randSeed, err := strconv.ParseInt(sumInt, 0, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to parse sumInt: %s", err)
|
return 0, err
|
||||||
os.Exit(-1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbNameRand = rand.New(rand.NewSource(randSeed))
|
f.buf = bytes.NewBuffer(rgxMySQLkey.ReplaceAll(rgxPGFkey.ReplaceAll(all, []byte{}), []byte{}))
|
||||||
}
|
|
||||||
|
|
||||||
var alphabetChars = "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
func randStr(length int) string {
|
|
||||||
c := len(alphabetChars)
|
|
||||||
|
|
||||||
output := make([]rune, length)
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
output[i] = rune(alphabetChars[dbNameRand.Intn(c)])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(output)
|
return f.buf.Read(b)
|
||||||
}
|
|
||||||
|
|
||||||
// getDBNameHash takes a database name in, and generates
|
|
||||||
// a random string using the database name as the rand Seed.
|
|
||||||
// getDBNameHash is used to generate unique test database names.
|
|
||||||
func getDBNameHash(input string) string {
|
|
||||||
initDBNameRand(input)
|
|
||||||
return randStr(40)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue