From 76b75dfaaa5875045e43006db46c428cd32600a4 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Mon, 12 Sep 2016 22:43:29 -0700 Subject: [PATCH] Remove foreign keys from mysql dump --- imports.go | 9 ++- templates_test/main_test/mysql_main.tpl | 4 +- templates_test/main_test/postgres_main.tpl | 2 +- .../singleton/boil_queries_test.tpl | 60 ++++++------------- 4 files changed, 27 insertions(+), 48 deletions(-) diff --git a/imports.go b/imports.go index ec3be4c..66917a5 100644 --- a/imports.go +++ b/imports.go @@ -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"`, }, }, diff --git a/templates_test/main_test/mysql_main.tpl b/templates_test/main_test/mysql_main.tpl index c3c7c4d..a9daf05 100644 --- a/templates_test/main_test/mysql_main.tpl +++ b/templates_test/main_test/mysql_main.tpl @@ -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") diff --git a/templates_test/main_test/postgres_main.tpl b/templates_test/main_test/postgres_main.tpl index 7d19f1b..585beaf 100644 --- a/templates_test/main_test/postgres_main.tpl +++ b/templates_test/main_test/postgres_main.tpl @@ -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 { diff --git a/templates_test/singleton/boil_queries_test.tpl b/templates_test/singleton/boil_queries_test.tpl index 26f09d3..d7a6dd3 100644 --- a/templates_test/singleton/boil_queries_test.tpl +++ b/templates_test/singleton/boil_queries_test.tpl @@ -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) }