The storage system and conversion tools for the new Logs website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

  1. package main
  2. import (
  3. "database/sql"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strings"
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. var flagUser = flag.String("user", "logbot4", "MySQL user")
  13. var flagPassword = flag.String("password", "logbot4", "MySQL password")
  14. var flagDatabase = flag.String("database", "logbot4", "MySQL database")
  15. func main() {
  16. flag.Parse()
  17. connString := fmt.Sprintf("%s:%s@/%s", *flagUser, *flagPassword, *flagDatabase)
  18. db, err := sql.Open("mysql", connString)
  19. if err != nil {
  20. log.Fatalf("Could not open SQL connection: %s", err)
  21. }
  22. file, err := os.Open("./data/setup.sql")
  23. if err != nil {
  24. log.Fatalf("Could not open ./data/setup.sql: %s", err)
  25. }
  26. data, err := ioutil.ReadAll(file)
  27. if err != nil {
  28. log.Fatalf("Could not read setup.sql: %s", err)
  29. }
  30. statements := strings.Split(string(data), ";")
  31. for _, statement := range statements {
  32. statement := strings.Trim(statement, "\n\t  ")
  33. if len(statement) <= 1 {
  34. continue
  35. }
  36. result, err := db.Exec(statement)
  37. if err != nil {
  38. log.Fatalf("Could not execute statement: %s", err)
  39. }
  40. affected, _ := result.RowsAffected()
  41. log.Printf("%d rows affected", affected)
  42. }
  43. }