From 3a000ad17795ebc9f1f6b9734f265e27c69fcb8f Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 19 May 2019 16:28:33 +1000 Subject: [PATCH] Back out the table install changes to fix a bug. --- install/mysql.go | 73 +++++------------------------------------------- 1 file changed, 7 insertions(+), 66 deletions(-) diff --git a/install/mysql.go b/install/mysql.go index 8949c44d..ca6e3f1c 100644 --- a/install/mysql.go +++ b/install/mysql.go @@ -10,12 +10,11 @@ import ( "bytes" "database/sql" "fmt" - "os" "io/ioutil" + "os" "path/filepath" "strconv" "strings" - "sync" "github.com/Azareal/Gosora/query_gen" _ "github.com/go-sql-driver/mysql" @@ -102,7 +101,7 @@ func (ins *MysqlInstaller) InitDatabase() (err error) { }*/ db.Close() - db, err = sql.Open("mysql", ins.dbUsername+_dbPassword+"@tcp("+ins.dbHost+":"+ins.dbPort+")/" + ins.dbName) + db, err = sql.Open("mysql", ins.dbUsername+_dbPassword+"@tcp("+ins.dbHost+":"+ins.dbPort+")/"+ins.dbName) if err != nil { return err } @@ -125,20 +124,20 @@ func (ins *MysqlInstaller) InitDatabase() (err error) { return qgen.Builder.SetAdapter("mysql") } -func(ins *MysqlInstaller) createTable(f os.FileInfo) error { +func (ins *MysqlInstaller) createTable(f os.FileInfo) error { table := strings.TrimPrefix(f.Name(), "query_") ext := filepath.Ext(table) if ext != ".sql" { return nil } table = strings.TrimSuffix(table, ext) - + // ? - This is mainly here for tests, although it might allow the installer to overwrite a production database, so we might want to proceed with caution q := "DROP TABLE IF EXISTS `" + table + "`;" _, err := ins.db.Exec(q) if err != nil { fmt.Println("Failed query:", q) - fmt.Println("e:",err) + fmt.Println("e:", err) return err } @@ -151,7 +150,7 @@ func(ins *MysqlInstaller) createTable(f os.FileInfo) error { _, err = ins.db.Exec(string(data)) if err != nil { fmt.Println("Failed query:", string(data)) - fmt.Println("e:",err) + fmt.Println("e:", err) return err } fmt.Printf("Created table '%s'\n", table) @@ -166,65 +165,7 @@ func (ins *MysqlInstaller) TableDefs() (err error) { return err } - // TODO: Can we reduce the amount of boilerplate here? - after := []string{"activity_stream_matches"} - c1 := make(chan os.FileInfo) - c2 := make(chan os.FileInfo) - e := make(chan error) - var wg sync.WaitGroup - r := func(c chan os.FileInfo) { - wg.Add(1) - for f := range c { - err := ins.createTable(f) - if err != nil { - e <- err - } - } - wg.Done() - } - go r(c1) - go r(c2) - - var a []os.FileInfo -Outer: - for i, f := range files { - if !strings.HasPrefix(f.Name(), "query_") { - continue - } - table := strings.TrimPrefix(f.Name(), "query_") - ext := filepath.Ext(table) - if ext != ".sql" { - continue - } - table = strings.TrimSuffix(table, ext) - for _, tbl := range after { - if tbl == table { - a = append(a, f) - continue Outer - } - } - if i%2 == 0 { - c1 <- f - } else { - c2 <- f - } - } - close(c1) - close(c2) - wg.Wait() - close(e) - - var first error - for err := range e { - if first == nil { - first = err - } - } - if first != nil { - return first - } - - for _, f := range a { + for _, f := range files { if !strings.HasPrefix(f.Name(), "query_") { continue }