Add DB creation from schema

This commit is contained in:
nemunaire 2016-01-07 18:28:16 +01:00
parent a5dc600f28
commit abd5e2025e
3 changed files with 128 additions and 1 deletions

2
admin/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
admin
fic.db

109
admin/db.go Normal file
View File

@ -0,0 +1,109 @@
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func DBInit(path string) error {
var err error
if db, err = sql.Open("sqlite3", path); err != nil {
return err
}
_, err = db.Exec(`
PRAGMA foreign_keys=ON;
`)
return err
}
func DBCreate() error {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS themes(
id_theme INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS teams(
id_team INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS team_certificates(
id_team INTEGER NOT NULL PRIMARY KEY,
revoked INTEGER NOT NULL,
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
CREATE TABLE IF NOT EXISTS team_members(
id_member INTEGER NOT NULL PRIMARY KEY,
id_team INTEGER,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
nickname TEXT NOT NULL,
company TEXT NOT NULL,
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
CREATE TABLE IF NOT EXISTS exercices(
id_exercice INTEGER NOT NULL PRIMARY KEY,
id_theme INTEGER NOT NULL,
title TEXT NOT NULL,
statement TEXT NOT NULL,
hint TEXT NOT NULL,
depend INTEGER,
gain INTEGER NOT NULL,
video_uri TEXT NOT NULL,
FOREIGN KEY(id_theme) REFERENCES themes(id_theme),
FOREIGN KEY(depend) REFERENCES exercices(id_exercice)
);
CREATE TABLE IF NOT EXISTS exercice_files(
id_file INTEGER NOT NULL PRIMARY KEY,
path TEXT NOT NULL UNIQUE,
id_exercice INTEGER NOT NULL,
name TEXT NOT NULL,
sha1 BLOB NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
CREATE TABLE IF NOT EXISTS exercice_keys(
id_key INTEGER NOT NULL PRIMARY KEY,
id_exercice INTEGER NOT NULL,
type TEXT NOT NULL,
value BLOB NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
CREATE TABLE IF NOT EXISTS exercice_solved(
id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
CREATE TABLE IF NOT EXISTS exercice_tries(
id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
`)
return err
}
func DBClose() error {
return db.Close()
}
func DBPrepare(query string) (*sql.Stmt, error) {
return db.Prepare(query)
}
func DBQuery(query string, args ...interface{}) (*sql.Rows, error) {
return db.Query(query, args...)
}
func DBExec(query string, args ...interface{}) (sql.Result, error) {
return db.Exec(query, args...)
}
func DBQueryRow(query string, args ...interface{}) *sql.Row {
return db.QueryRow(query, args...)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"os"
)
var SubmissionDir string
@ -12,11 +13,26 @@ var BaseURL string
func main() {
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
var _ = flag.String("db", "fic.db", "Path to the DB")
var dbfile = flag.String("db", "fic.db", "Path to the DB")
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.Parse()
log.Println("Opening database...")
if err := DBInit(*dbfile); err != nil {
log.Fatal("Cannot open the database: ", err)
os.Exit(1)
}
defer DBClose()
log.Println("Creating database...")
if err := DBCreate(); err != nil {
log.Fatal("Cannot create database: ", err)
os.Exit(1)
}
log.Println("Registering handlers...")
http.HandleFunc("/", ApiRouting)