2021-07-09 06:11:11 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/aldy505/bob"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set up the table connection, create table if not exists
|
|
|
|
func Setup() error {
|
|
|
|
db := New()
|
|
|
|
|
|
|
|
// Jokesbapak2 table & data
|
2021-07-09 12:13:19 +00:00
|
|
|
sql, _, err := bob.CreateTableIfNotExists("jokesbapak2").
|
|
|
|
Columns("id", "link", "key").
|
|
|
|
Types("SERIAL", "TEXT", "VARCHAR(255)").
|
|
|
|
Primary("id").ToSql()
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-09 12:13:19 +00:00
|
|
|
log.Fatalln("10 - failed on table creation: ", err)
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 12:13:19 +00:00
|
|
|
splitSql := strings.Split(sql, ";")
|
|
|
|
for i := range splitSql {
|
|
|
|
_, err = db.Query(context.Background(), splitSql[i])
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-09 12:13:19 +00:00
|
|
|
log.Fatalln("11 - failed on table creation: ", err)
|
2021-07-09 06:11:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorization
|
2021-07-09 12:13:19 +00:00
|
|
|
sql, _, err = bob.CreateTableIfNotExists("authorization").
|
|
|
|
Columns("id", "token", "key").
|
|
|
|
Types("SERIAL", "TEXT", "VARCHAR(255)").
|
|
|
|
Primary("id").
|
|
|
|
Unique("token").
|
|
|
|
ToSql()
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-09 12:13:19 +00:00
|
|
|
log.Fatalln("14 - failed on table creation: ", err)
|
|
|
|
return err
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 12:13:19 +00:00
|
|
|
splitSql = strings.Split(sql, ";")
|
|
|
|
for i := range splitSql {
|
|
|
|
_, err = db.Query(context.Background(), splitSql[i])
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-09 12:13:19 +00:00
|
|
|
log.Fatalln("15 - failed on table creation: ", err)
|
2021-07-09 06:11:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:13:19 +00:00
|
|
|
_, err = db.Query(context.Background(), "ALTER TABLE jokesbapak2 ADD CONSTRAINT fk_jokesbapak2_key FOREIGN KEY (key) REFERENCES authorization (id)")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("16 - failed on foreign key iteration: ", err)
|
|
|
|
}
|
2021-07-09 06:11:11 +00:00
|
|
|
return nil
|
|
|
|
}
|