💥
This commit is contained in:
66
db/install.go
Normal file
66
db/install.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package db
|
||||
|
||||
import "log"
|
||||
|
||||
func install() {
|
||||
var err error
|
||||
log.Println("Installing tables")
|
||||
tx, err := DB.Begin()
|
||||
if err != nil{
|
||||
log.Fatal(err)
|
||||
tx.Rollback()
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
CREATE TABLE timetables (
|
||||
id serial primary key,
|
||||
"name" text NOT NULL,
|
||||
status bool NOT NULL DEFAULT false,
|
||||
created timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
`)
|
||||
if err != nil{
|
||||
log.Fatal(err)
|
||||
tx.Rollback()
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
CREATE TABLE timeslots (
|
||||
id serial primary key,
|
||||
ttid integer not null references timetables(id),
|
||||
"name" text NOT NULL,
|
||||
"time" text NOT NULL,
|
||||
capacity integer NOT NULL DEFAULT 1,
|
||||
take integer NOT NULL DEFAULT 0 check (take <= capacity),
|
||||
created timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
`)
|
||||
|
||||
|
||||
if err != nil{
|
||||
log.Fatal(err)
|
||||
tx.Rollback()
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
CREATE TABLE takes (
|
||||
username text NOT NULL,
|
||||
tsid integer NOT null references timeslots(id),
|
||||
created timestamp not null default now(),
|
||||
PRIMARY KEY (username, tsid)
|
||||
);
|
||||
`)
|
||||
|
||||
if err != nil{
|
||||
log.Fatal(err)
|
||||
tx.Rollback()
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil{
|
||||
log.Fatal(err)
|
||||
tx.Rollback()
|
||||
}
|
||||
|
||||
log.Println("Successfully installed all tables")
|
||||
}
|
||||
Reference in New Issue
Block a user