gosora/install/mysql.go

198 lines
4.3 KiB
Go
Raw Normal View History

/*
*
* Gosora MySQL Interface
Added the In-Progress Widget Manager UI. Added the IsoCode field to phrase files. Rewrote a good portion of the widget system logic. Added some tests for the widget system. Added the Online Users widget. Added a few sealed incomplete widgets like the Search & Filter Widget. Added the AllUsers method to WsHubImpl for Online Users. Please don't abuse it. Added the optional *DBTableKey field to AddColumn. Added the panel_analytics_time_range template to reduce the amount of duplication. Failed registrations now show up in red in the registration logs for Nox. Failed logins now show up in red in the login logs for Nox. Added basic h2 CSS to the other themes. Added .show_on_block_edit and .hide_on_block_edit to the other themes. Updated contributing. Updated a bunch of dates to 2019. Replaced tblKey{} with nil where possible. Switched out some &s for &s to reduce the number of possible bugs. Fixed a bug with selector messages where the inspector would get really jittery due to unnecessary DOM updates. Moved header.Zone and associated fields to the bottom of ViewTopic to reduce the chances of problems arising. Added the ZoneData field to *Header. Added IDs to the items in the forum list template. Split the fetchPhrases function into the initPhrases and fetchPhrases functions in init.js Added .colstack_sub_head. Fixed the CSS in the menu list. Removed an inline style from the simple topic like and unlike buttons. Removed an inline style from the simple topic IP button. Simplified the LoginRequired error handler. Fixed a typo in the comment prior to DatabaseError() Reduce the number of false leaves for WebSocket page transitions. Added the error zone. De-duped the logic in WsHubImpl.getUsers. Fixed a potential widget security issue. Added twenty new phrases. Added the wid column to the widgets table. You will need to run the patcher / updater for this commit.
2019-01-21 12:27:59 +00:00
* Copyright Azareal 2017 - 2019
*
*/
package install
2017-07-12 11:05:18 +00:00
import (
"bytes"
"database/sql"
"fmt"
2017-07-12 11:05:18 +00:00
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/Azareal/Gosora/query_gen"
2017-07-12 11:05:18 +00:00
_ "github.com/go-sql-driver/mysql"
)
//var dbCollation string = "utf8mb4_general_ci"
2017-07-12 11:05:18 +00:00
func init() {
adapters["mysql"] = &MysqlInstaller{dbHost: ""}
2017-07-12 11:05:18 +00:00
}
type MysqlInstaller struct {
db *sql.DB
dbHost string
dbUsername string
dbPassword string
dbName string
dbPort string
}
func (ins *MysqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
ins.dbHost = dbHost
ins.dbUsername = dbUsername
ins.dbPassword = dbPassword
ins.dbName = dbName
ins.dbPort = dbPort
}
func (ins *MysqlInstaller) Name() string {
return "mysql"
}
func (ins *MysqlInstaller) DefaultPort() string {
return "3306"
}
func (ins *MysqlInstaller) dbExists(dbName string) (bool, error) {
var waste string
err := ins.db.QueryRow("SHOW DATABASES LIKE '" + dbName + "'").Scan(&waste)
if err != nil && err != sql.ErrNoRows {
return false, err
} else if err == sql.ErrNoRows {
return false, nil
}
return true, nil
}
func (ins *MysqlInstaller) InitDatabase() (err error) {
_dbPassword := ins.dbPassword
if _dbPassword != "" {
_dbPassword = ":" + _dbPassword
2017-07-12 11:05:18 +00:00
}
db, err := sql.Open("mysql", ins.dbUsername+_dbPassword+"@tcp("+ins.dbHost+":"+ins.dbPort+")/")
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
// Make sure that the connection is alive..
err = db.Ping()
if err != nil {
return err
}
fmt.Println("Successfully connected to the database")
ins.db = db
ok, err := ins.dbExists(ins.dbName)
if err != nil {
2017-07-12 11:05:18 +00:00
return err
}
if !ok {
2017-07-12 11:05:18 +00:00
fmt.Println("Unable to find the database. Attempting to create it")
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS " + ins.dbName)
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
fmt.Println("The database was successfully created")
}
fmt.Println("Switching to database ", ins.dbName)
_, err = db.Exec("USE " + ins.dbName)
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
2017-07-12 11:05:18 +00:00
// Ready the query builder
qgen.Builder.SetConn(db)
return qgen.Builder.SetAdapter("mysql")
2017-07-12 11:05:18 +00:00
}
func (ins *MysqlInstaller) TableDefs() (err error) {
fmt.Println("Creating the tables")
2017-07-12 11:05:18 +00:00
files, _ := ioutil.ReadDir("./schema/mysql/")
for _, f := range files {
if !strings.HasPrefix(f.Name(), "query_") {
2017-07-12 11:05:18 +00:00
continue
}
2017-09-22 05:36:15 +00:00
var table, ext string
table = strings.TrimPrefix(f.Name(), "query_")
2017-07-12 11:05:18 +00:00
ext = filepath.Ext(table)
if ext != ".sql" {
continue
}
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
_, err = ins.db.Exec("DROP TABLE IF EXISTS `" + table + "`;")
if err != nil {
fmt.Println("Failed query:", "DROP TABLE IF EXISTS `"+table+"`;")
return err
}
fmt.Printf("Creating table '%s'\n", table)
2017-07-12 11:05:18 +00:00
data, err := ioutil.ReadFile("./schema/mysql/" + f.Name())
if err != nil {
return err
}
data = bytes.TrimSpace(data)
_, err = ins.db.Exec(string(data))
2017-07-12 11:05:18 +00:00
if err != nil {
fmt.Println("Failed query:", string(data))
2017-07-12 11:05:18 +00:00
return err
}
}
return nil
}
2017-09-22 05:36:15 +00:00
// ? - Moved this here since it was breaking the installer, we need to add this at some point
/* TODO: Implement the html-attribute setting type before deploying this */
/*INSERT INTO settings(`name`,`content`,`type`) VALUES ('meta_desc','','html-attribute');*/
func (ins *MysqlInstaller) InitialData() error {
fmt.Println("Seeding the tables")
2017-07-12 11:05:18 +00:00
data, err := ioutil.ReadFile("./schema/mysql/inserts.sql")
if err != nil {
return err
}
data = bytes.TrimSpace(data)
statements := bytes.Split(data, []byte(";"))
for key, sBytes := range statements {
statement := string(sBytes)
if statement == "" {
2017-07-12 11:05:18 +00:00
continue
}
statement += ";"
2017-07-12 11:05:18 +00:00
fmt.Println("Executing query #" + strconv.Itoa(key) + " " + statement)
_, err = ins.db.Exec(statement)
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
}
return nil
}
func (ins *MysqlInstaller) CreateAdmin() error {
return createAdmin()
}
func (ins *MysqlInstaller) DBHost() string {
return ins.dbHost
}
func (ins *MysqlInstaller) DBUsername() string {
return ins.dbUsername
}
func (ins *MysqlInstaller) DBPassword() string {
return ins.dbPassword
}
func (ins *MysqlInstaller) DBName() string {
return ins.dbName
}
func (ins *MysqlInstaller) DBPort() string {
return ins.dbPort
}