Use MySQL instead of SQLite

This commit is contained in:
nemunaire 2016-01-23 13:16:31 +01:00
parent 32e8f931b9
commit 8788eea4f0
4 changed files with 72 additions and 49 deletions

View file

@ -20,7 +20,7 @@ var CloudPassword string
func main() { func main() {
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket") var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
var dbfile = flag.String("db", "fic.db", "Path to the DB") var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL") flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions") flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts") flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
@ -56,7 +56,7 @@ func main() {
} }
log.Println("Opening database...") log.Println("Opening database...")
if err := fic.DBInit(*dbfile); err != nil { if err := fic.DBInit(fmt.Sprintf("%s?parseTime=true", *dsn)); err != nil {
log.Fatal("Cannot open the database: ", err) log.Fatal("Cannot open the database: ", err)
os.Exit(1) os.Exit(1)

View file

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
@ -42,7 +43,7 @@ func watchsubdir(watcher *inotify.Watcher, pathname string) error {
} }
func main() { func main() {
var dbfile = flag.String("db", "fic.db", "Path to the DB") var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL") flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions") flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files") flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
@ -60,7 +61,7 @@ func main() {
} }
log.Println("Opening DB...") log.Println("Opening DB...")
if err := fic.DBInit(*dbfile); err != nil { if err := fic.DBInit(fmt.Sprintf("%s?parseTime=true", *dsn)); err != nil {
log.Fatal("Cannot open the database: ", err) log.Fatal("Cannot open the database: ", err)
os.Exit(1) os.Exit(1)

View file

@ -2,78 +2,93 @@ package fic
import ( import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/go-sql-driver/mysql"
) )
var db *sql.DB var db *sql.DB
func DBInit(path string) error { func DBInit(dsn string) error {
var err error var err error
if db, err = sql.Open("sqlite3", path); err != nil { if db, err = sql.Open("mysql", dsn); err != nil {
return err return err
} }
return nil
_, err = db.Exec(`
PRAGMA foreign_keys=ON;
`)
return err
} }
func DBCreate() error { func DBCreate() error {
_, err := db.Exec(` if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS themes( CREATE TABLE IF NOT EXISTS themes(
id_theme INTEGER NOT NULL PRIMARY KEY, id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name TEXT NOT NULL UNIQUE, name VARCHAR(255) NOT NULL UNIQUE,
authors TEXT NOT NULL authors VARCHAR(255) NOT NULL
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS teams( CREATE TABLE IF NOT EXISTS teams(
id_team INTEGER NOT NULL PRIMARY KEY, id_team INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name TEXT NOT NULL, name VARCHAR(255) NOT NULL,
color INTEGER NOT NULL color INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS team_certificates( `); err != nil {
id_team INTEGER NOT NULL PRIMARY KEY, return err
revoked INTEGER NOT NULL, }
FOREIGN KEY(id_team) REFERENCES teams(id_team) if _, err := db.Exec(`
);
CREATE TABLE IF NOT EXISTS team_members( CREATE TABLE IF NOT EXISTS team_members(
id_member INTEGER NOT NULL PRIMARY KEY, id_member INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_team INTEGER, id_team INTEGER,
firstname TEXT NOT NULL, firstname VARCHAR(255) NOT NULL,
lastname TEXT NOT NULL, lastname VARCHAR(255) NOT NULL,
nickname TEXT NOT NULL, nickname VARCHAR(255) NOT NULL,
company TEXT NOT NULL, company VARCHAR(255) NOT NULL,
FOREIGN KEY(id_team) REFERENCES teams(id_team) FOREIGN KEY(id_team) REFERENCES teams(id_team)
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercices( CREATE TABLE IF NOT EXISTS exercices(
id_exercice INTEGER NOT NULL PRIMARY KEY, id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_theme INTEGER NOT NULL, id_theme INTEGER NOT NULL,
title TEXT NOT NULL, title VARCHAR(255) NOT NULL,
statement TEXT NOT NULL, statement TEXT NOT NULL,
hint TEXT NOT NULL, hint TEXT NOT NULL,
depend INTEGER, depend INTEGER,
gain INTEGER NOT NULL, gain INTEGER NOT NULL,
video_uri TEXT NOT NULL, video_uri VARCHAR(255) NOT NULL,
FOREIGN KEY(id_theme) REFERENCES themes(id_theme), FOREIGN KEY(id_theme) REFERENCES themes(id_theme),
FOREIGN KEY(depend) REFERENCES exercices(id_exercice) FOREIGN KEY(depend) REFERENCES exercices(id_exercice)
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_files( CREATE TABLE IF NOT EXISTS exercice_files(
id_file INTEGER NOT NULL PRIMARY KEY, id_file INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
origin TEXT NOT NULL, origin VARCHAR(255) NOT NULL,
path TEXT NOT NULL UNIQUE, path VARCHAR(255) NOT NULL UNIQUE,
id_exercice INTEGER NOT NULL, id_exercice INTEGER NOT NULL,
name TEXT NOT NULL, name VARCHAR(255) NOT NULL,
sha1 BLOB NOT NULL, sha1 BINARY(20) NOT NULL,
size INTEGER NOT NULL, size INTEGER NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice) FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_keys( CREATE TABLE IF NOT EXISTS exercice_keys(
id_key INTEGER NOT NULL PRIMARY KEY, id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_exercice INTEGER NOT NULL, id_exercice INTEGER NOT NULL,
type TEXT NOT NULL, type VARCHAR(255) NOT NULL,
value BLOB NOT NULL, value BINARY(64) NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice) FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_solved( CREATE TABLE IF NOT EXISTS exercice_solved(
id_exercice INTEGER NOT NULL, id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL, id_team INTEGER NOT NULL,
@ -81,6 +96,10 @@ CREATE TABLE IF NOT EXISTS exercice_solved(
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice), FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team) FOREIGN KEY(id_team) REFERENCES teams(id_team)
); );
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_tries( CREATE TABLE IF NOT EXISTS exercice_tries(
id_exercice INTEGER NOT NULL, id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL, id_team INTEGER NOT NULL,
@ -88,9 +107,11 @@ CREATE TABLE IF NOT EXISTS exercice_tries(
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice), FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team) FOREIGN KEY(id_team) REFERENCES teams(id_team)
); );
`) `); err != nil {
return err return err
} }
return nil
}
func DBClose() error { func DBClose() error {
return db.Close() return db.Close()

View file

@ -119,18 +119,19 @@ func (t Team) HasAccess(e Exercice) bool {
} }
func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) { func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) {
var nb int64 var nb *int64
var tm int64 var tm *time.Time
var mt *time.Time if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); tm == nil {
if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&mt); mt == nil { if DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &tm); tm == nil {
if err := DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &mt); err != nil {
return false, time.Unix(0, 0), 0 return false, time.Unix(0, 0), 0
} else if nb == nil {
return false, *tm, 0
} else { } else {
return false, *mt, nb return false, *tm, *nb
} }
} else if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); err != nil { } else if DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); nb == nil {
return true, time.Unix(tm, 0), 0 return true, *tm, 0
} else { } else {
return true, time.Unix(tm, 0), nb + 1 return true, *tm, *nb + 1
} }
} }