Back out the table install changes to fix a bug.

This commit is contained in:
Azareal 2019-05-19 16:28:33 +10:00
parent c57f176939
commit 3a000ad177
1 changed files with 7 additions and 66 deletions

View File

@ -10,12 +10,11 @@ import (
"bytes" "bytes"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/Azareal/Gosora/query_gen" "github.com/Azareal/Gosora/query_gen"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -102,7 +101,7 @@ func (ins *MysqlInstaller) InitDatabase() (err error) {
}*/ }*/
db.Close() 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 { if err != nil {
return err return err
} }
@ -125,20 +124,20 @@ func (ins *MysqlInstaller) InitDatabase() (err error) {
return qgen.Builder.SetAdapter("mysql") 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_") table := strings.TrimPrefix(f.Name(), "query_")
ext := filepath.Ext(table) ext := filepath.Ext(table)
if ext != ".sql" { if ext != ".sql" {
return nil return nil
} }
table = strings.TrimSuffix(table, ext) 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 // ? - 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 + "`;" q := "DROP TABLE IF EXISTS `" + table + "`;"
_, err := ins.db.Exec(q) _, err := ins.db.Exec(q)
if err != nil { if err != nil {
fmt.Println("Failed query:", q) fmt.Println("Failed query:", q)
fmt.Println("e:",err) fmt.Println("e:", err)
return err return err
} }
@ -151,7 +150,7 @@ func(ins *MysqlInstaller) createTable(f os.FileInfo) error {
_, err = ins.db.Exec(string(data)) _, err = ins.db.Exec(string(data))
if err != nil { if err != nil {
fmt.Println("Failed query:", string(data)) fmt.Println("Failed query:", string(data))
fmt.Println("e:",err) fmt.Println("e:", err)
return err return err
} }
fmt.Printf("Created table '%s'\n", table) fmt.Printf("Created table '%s'\n", table)
@ -166,65 +165,7 @@ func (ins *MysqlInstaller) TableDefs() (err error) {
return err return err
} }
// TODO: Can we reduce the amount of boilerplate here? for _, f := range files {
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 {
if !strings.HasPrefix(f.Name(), "query_") { if !strings.HasPrefix(f.Name(), "query_") {
continue continue
} }