Add database schema for trips, students, parents, and constraints
This commit is contained in:
8
main.go
8
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -10,6 +11,9 @@ import (
|
|||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed schema.sql
|
||||||
|
var schema string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dsn := os.Getenv("PGCONN")
|
dsn := os.Getenv("PGCONN")
|
||||||
if dsn == "" {
|
if dsn == "" {
|
||||||
@@ -27,6 +31,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
log.Println("connected to database")
|
log.Println("connected to database")
|
||||||
|
|
||||||
|
if _, err := db.Exec(schema); err != nil {
|
||||||
|
log.Fatalf("failed to apply schema: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
http.Handle("/", http.FileServer(http.Dir("static")))
|
http.Handle("/", http.FileServer(http.Dir("static")))
|
||||||
|
|
||||||
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
45
schema.sql
Normal file
45
schema.sql
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
DO $$ BEGIN
|
||||||
|
CREATE TYPE constraint_kind AS ENUM ('happy', 'ok', 'never');
|
||||||
|
EXCEPTION WHEN duplicate_object THEN NULL;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
CREATE TYPE constraint_level AS ENUM ('student', 'parent', 'admin');
|
||||||
|
EXCEPTION WHEN duplicate_object THEN NULL;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS trips (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS trip_admins (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
trip_id BIGINT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
UNIQUE(trip_id, email)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS students (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
trip_id BIGINT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
UNIQUE(trip_id, email)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS parents (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
student_id BIGINT NOT NULL REFERENCES students(id) ON DELETE CASCADE,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
UNIQUE(student_id, email)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS roommate_constraints (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
student_a_id BIGINT NOT NULL REFERENCES students(id) ON DELETE CASCADE,
|
||||||
|
student_b_id BIGINT NOT NULL REFERENCES students(id) ON DELETE CASCADE,
|
||||||
|
kind constraint_kind NOT NULL,
|
||||||
|
level constraint_level NOT NULL,
|
||||||
|
UNIQUE(student_a_id, student_b_id, level)
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user