2018-03-31 05:25:27 +00:00
package main
import (
"bufio"
2018-04-22 12:33:56 +00:00
"database/sql"
2018-04-23 08:38:25 +00:00
"encoding/json"
2018-03-31 05:25:27 +00:00
"fmt"
2018-04-23 08:38:25 +00:00
"io/ioutil"
2018-04-22 14:27:04 +00:00
"log"
2018-03-31 05:25:27 +00:00
"os"
"runtime/debug"
2018-05-14 08:56:56 +00:00
"strconv"
2018-03-31 05:25:27 +00:00
2018-04-23 21:08:31 +00:00
"../common"
"../config"
2018-03-31 05:25:27 +00:00
"../query_gen/lib"
2018-04-22 14:27:04 +00:00
_ "github.com/go-sql-driver/mysql"
2018-03-31 05:25:27 +00:00
)
func main ( ) {
scanner := bufio . NewScanner ( os . Stdin )
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func ( ) {
r := recover ( )
if r != nil {
fmt . Println ( r )
debug . PrintStack ( )
pressAnyKey ( scanner )
2018-05-11 05:41:51 +00:00
log . Fatal ( "" )
2018-03-31 05:25:27 +00:00
return
}
} ( )
2018-04-23 21:08:31 +00:00
config . Config ( )
if common . DbConfig . Adapter != "mysql" && common . DbConfig . Adapter != "" {
2018-04-22 14:27:04 +00:00
log . Fatal ( "Only MySQL is supported for upgrades right now, please wait for a newer build of the patcher" )
}
err := prepMySQL ( )
2018-03-31 05:25:27 +00:00
if err != nil {
2018-04-22 14:27:04 +00:00
log . Fatal ( err )
}
err = patcher ( scanner )
if err != nil {
log . Fatal ( err )
}
2018-03-31 05:25:27 +00:00
}
func pressAnyKey ( scanner * bufio . Scanner ) {
fmt . Println ( "Please press enter to exit..." )
for scanner . Scan ( ) {
_ = scanner . Text ( )
return
}
}
2018-04-22 14:27:04 +00:00
func prepMySQL ( ) error {
2018-04-23 08:38:25 +00:00
return qgen . Builder . Init ( "mysql" , map [ string ] string {
2018-04-22 14:27:04 +00:00
"host" : common . DbConfig . Host ,
"port" : common . DbConfig . Port ,
"name" : common . DbConfig . Dbname ,
"username" : common . DbConfig . Username ,
"password" : common . DbConfig . Password ,
"collation" : "utf8mb4_general_ci" ,
} )
}
2018-04-23 08:38:25 +00:00
type SchemaFile struct {
2018-04-23 21:08:31 +00:00
DBVersion string // Current version of the database schema
DynamicFileVersion string
2018-04-23 08:38:25 +00:00
MinGoVersion string // TODO: Minimum version of Go needed to install this version
MinVersion string // TODO: Minimum version of Gosora to jump to this version, might be tricky as we don't store this in the schema file, maybe store it in the database
2018-04-22 14:27:04 +00:00
}
2018-03-31 05:25:27 +00:00
func patcher ( scanner * bufio . Scanner ) error {
2018-05-11 05:41:51 +00:00
fmt . Println ( "Loading the schema file" )
2018-04-23 08:38:25 +00:00
data , err := ioutil . ReadFile ( "./schema/lastSchema.json" )
2018-04-22 14:27:04 +00:00
if err != nil {
return err
}
2018-04-23 21:08:31 +00:00
var schemaFile SchemaFile
2018-04-23 08:38:25 +00:00
err = json . Unmarshal ( data , & schemaFile )
2018-04-22 14:27:04 +00:00
if err != nil {
return err
}
2018-05-14 08:56:56 +00:00
dbVersion , err := strconv . Atoi ( schemaFile . DBVersion )
if err != nil {
return err
}
2018-05-11 05:41:51 +00:00
fmt . Println ( "Applying the patches" )
2018-05-15 05:59:52 +00:00
if dbVersion < 2 {
if dbVersion < 1 {
err := patch0 ( scanner )
if err != nil {
return err
}
2018-05-14 08:56:56 +00:00
}
2018-05-15 05:59:52 +00:00
return patch1 ( scanner )
2018-05-14 08:56:56 +00:00
}
2018-05-15 05:59:52 +00:00
return patch2 ( scanner )
2018-04-23 08:38:25 +00:00
}
2018-04-22 14:27:04 +00:00
2018-04-23 08:38:25 +00:00
func execStmt ( stmt * sql . Stmt , err error ) error {
2018-04-22 14:27:04 +00:00
if err != nil {
return err
}
2018-04-23 08:38:25 +00:00
_ , err = stmt . Exec ( )
return err
2018-03-31 05:25:27 +00:00
}
/ * func eachUserQuick ( handle func ( int ) ) error {
stmt , err := qgen . Builder . Select ( "users" ) . Orderby ( "uid desc" ) . Limit ( 1 ) . Prepare ( )
if err != nil {
return err
}
var topID int
err := stmt . QueryRow ( topID )
if err != nil {
return err
}
for i := 1 ; i <= topID ; i ++ {
err = handle ( i )
if err != nil {
return err
}
}
} * /
2018-04-07 05:27:25 +00:00
func eachUser ( handle func ( int ) error ) error {
acc := qgen . Builder . Accumulator ( )
2018-04-22 12:33:56 +00:00
err := acc . Select ( "users" ) . Each ( func ( rows * sql . Rows ) error {
2018-03-31 05:25:27 +00:00
var uid int
err := rows . Scan ( & uid )
if err != nil {
return err
}
2018-04-22 12:33:56 +00:00
return handle ( uid )
} )
return err
2018-03-31 05:25:27 +00:00
}