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": {
|
||||
standard: importList{
|
||||
`"crypto/md5"`,
|
||||
`"bytes"`,
|
||||
`"fmt"`,
|
||||
`"os"`,
|
||||
`"strconv"`,
|
||||
`"io"`,
|
||||
`"io/ioutil"`,
|
||||
`"math/rand"`,
|
||||
`"regexp"`,
|
||||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/vattle/sqlboiler/boil"`,
|
||||
|
@ -238,6 +239,7 @@ var defaultTestMainImports = map[string]imports{
|
|||
`"github.com/pkg/errors"`,
|
||||
`"github.com/spf13/viper"`,
|
||||
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
||||
`"github.com/vattle/sqlboiler/boil/randomize"`,
|
||||
`_ "github.com/lib/pq"`,
|
||||
},
|
||||
},
|
||||
|
@ -256,6 +258,7 @@ var defaultTestMainImports = map[string]imports{
|
|||
`"github.com/pkg/errors"`,
|
||||
`"github.com/spf13/viper"`,
|
||||
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
||||
`"github.com/vattle/sqlboiler/boil/randomize"`,
|
||||
`_ "github.com/go-sql-driver/mysql"`,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -27,7 +27,7 @@ func (m *mysqlTester) setup() error {
|
|||
m.port = viper.GetInt("mysql.port")
|
||||
m.sslmode = viper.GetString("mysql.sslmode")
|
||||
// Create a randomized db name.
|
||||
m.testDBName = getDBNameHash(m.dbName)
|
||||
m.testDBName = randomize.StableDBName(m.dbName)
|
||||
|
||||
if err = m.makeOptionFile(); err != nil {
|
||||
return errors.Wrap(err, "couldn't make option file")
|
||||
|
@ -45,7 +45,7 @@ func (m *mysqlTester) setup() error {
|
|||
|
||||
r, w := io.Pipe()
|
||||
dumpCmd.Stdout = w
|
||||
createCmd.Stdin = io.TeeReader(r, os.Stdout)
|
||||
createCmd.Stdin = newFKeyDestroyer(r)
|
||||
|
||||
if err = dumpCmd.Start(); err != nil {
|
||||
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.sslmode = viper.GetString("postgres.sslmode")
|
||||
// Create a randomized db name.
|
||||
p.testDBName = getDBNameHash(p.dbName)
|
||||
p.testDBName = randomize.StableDBName(p.dbName)
|
||||
|
||||
err = p.dropTestDB()
|
||||
if err != nil {
|
||||
|
|
|
@ -7,53 +7,29 @@ func MustTx(transactor boil.Transactor, err error) boil.Transactor {
|
|||
return transactor
|
||||
}
|
||||
|
||||
func initDBNameRand(input string) {
|
||||
sum := md5.Sum([]byte(input))
|
||||
var rgxPGFkey = regexp.MustCompile(`(?m)(?s)^ALTER TABLE ONLY.*?ADD CONSTRAINT.*?FOREIGN KEY.*?;\n`)
|
||||
var rgxMySQLkey = regexp.MustCompile(`(?m)((,\n)?\s+CONSTRAINT.*?FOREIGN KEY.*?\n)+`)
|
||||
|
||||
var sumInt string
|
||||
for _, v := range sum {
|
||||
sumInt = sumInt + strconv.Itoa(int(v))
|
||||
func newFKeyDestroyer(reader io.Reader) io.Reader {
|
||||
return &fKeyDestroyer{
|
||||
reader: reader,
|
||||
}
|
||||
}
|
||||
|
||||
// Cut integer to 18 digits to ensure no int64 overflow.
|
||||
sumInt = sumInt[:18]
|
||||
type fKeyDestroyer struct {
|
||||
reader io.Reader
|
||||
buf *bytes.Buffer
|
||||
}
|
||||
|
||||
sumTmp := sumInt
|
||||
for i, v := range sumInt {
|
||||
if v == '0' {
|
||||
sumTmp = sumInt[i+1:]
|
||||
continue
|
||||
func (f *fKeyDestroyer) Read(b []byte) (int, error) {
|
||||
if f.buf == nil {
|
||||
all, err := ioutil.ReadAll(f.reader)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
break
|
||||
|
||||
f.buf = bytes.NewBuffer(rgxMySQLkey.ReplaceAll(rgxPGFkey.ReplaceAll(all, []byte{}), []byte{}))
|
||||
}
|
||||
|
||||
sumInt = sumTmp
|
||||
|
||||
randSeed, err := strconv.ParseInt(sumInt, 0, 64)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to parse sumInt: %s", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
dbNameRand = rand.New(rand.NewSource(randSeed))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
return f.buf.Read(b)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue