From abd5e2025ea4ce344668eadc39da20b90d7d9881 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 7 Jan 2016 18:28:16 +0100 Subject: [PATCH] Add DB creation from schema --- admin/.gitignore | 2 + admin/db.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++ admin/main.go | 18 +++++++- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 admin/.gitignore create mode 100644 admin/db.go diff --git a/admin/.gitignore b/admin/.gitignore new file mode 100644 index 00000000..6a514f21 --- /dev/null +++ b/admin/.gitignore @@ -0,0 +1,2 @@ +admin +fic.db diff --git a/admin/db.go b/admin/db.go new file mode 100644 index 00000000..0a8bc61e --- /dev/null +++ b/admin/db.go @@ -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...) +} diff --git a/admin/main.go b/admin/main.go index 87b856fc..1979ca64 100644 --- a/admin/main.go +++ b/admin/main.go @@ -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)