2021-07-09 06:11:11 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/aldy505/bob"
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-07-09 06:11:11 +00:00
|
|
|
)
|
|
|
|
|
2021-07-17 10:34:02 +00:00
|
|
|
// Setup the table connection, create table if not exists
|
2021-09-27 10:10:19 +00:00
|
|
|
func Setup(db *pgxpool.Pool, ctx *context.Context) error {
|
|
|
|
conn, err := db.Acquire(*ctx)
|
|
|
|
if err != nil {
|
2021-09-28 04:29:39 +00:00
|
|
|
log.Fatalln("30 - err here")
|
2021-09-27 10:10:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-07-23 15:02:39 +00:00
|
|
|
// administrators table
|
|
|
|
var tableAuthExists bool
|
2021-09-27 13:02:58 +00:00
|
|
|
err = conn.QueryRow(*ctx, `SELECT EXISTS (
|
2021-07-14 18:17:01 +00:00
|
|
|
SELECT FROM information_schema.tables
|
|
|
|
WHERE table_schema = 'public'
|
2021-07-23 15:02:39 +00:00
|
|
|
AND table_name = 'administrators'
|
|
|
|
);`).Scan(&tableAuthExists)
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("16 - failed on checking table: ", err)
|
2021-07-14 18:17:01 +00:00
|
|
|
return err
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 15:02:39 +00:00
|
|
|
if !tableAuthExists {
|
|
|
|
sql, _, err := bob.
|
|
|
|
CreateTable("administrators").
|
|
|
|
AddColumn(bob.ColumnDef{Name: "id", Type: "SERIAL", Extras: []string{"PRIMARY KEY"}}).
|
2021-09-26 19:13:38 +00:00
|
|
|
StringColumn("key", "NOT NULL", "UNIQUE").
|
2021-07-23 15:02:39 +00:00
|
|
|
TextColumn("token").
|
|
|
|
StringColumn("last_used").
|
2021-07-14 18:17:01 +00:00
|
|
|
ToSql()
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("17 - failed on table creation: ", err)
|
2021-07-09 06:11:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
q, err := conn.Query(*ctx, sql)
|
2021-07-14 18:17:01 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("18 - failed on table creation: ", err)
|
2021-07-14 18:17:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-28 04:29:39 +00:00
|
|
|
defer q.Close()
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
conn2, err := db.Acquire(*ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("32 - err here")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn2.Release()
|
|
|
|
|
2021-07-23 15:02:39 +00:00
|
|
|
// Jokesbapak2 table
|
|
|
|
|
|
|
|
// Check if table exists
|
|
|
|
var tableJokesExists bool
|
2021-09-28 04:29:39 +00:00
|
|
|
err = conn2.QueryRow(*ctx, `SELECT EXISTS (
|
2021-07-14 18:17:01 +00:00
|
|
|
SELECT FROM information_schema.tables
|
|
|
|
WHERE table_schema = 'public'
|
2021-07-23 15:02:39 +00:00
|
|
|
AND table_name = 'jokesbapak2'
|
|
|
|
);`).Scan(&tableJokesExists)
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("10 - failed on checking table: ", err)
|
2021-07-09 12:13:19 +00:00
|
|
|
return err
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 15:02:39 +00:00
|
|
|
if !tableJokesExists {
|
|
|
|
sql, _, err := bob.
|
|
|
|
CreateTable("jokesbapak2").
|
|
|
|
AddColumn(bob.ColumnDef{Name: "id", Type: "SERIAL", Extras: []string{"PRIMARY KEY"}}).
|
|
|
|
TextColumn("link", "UNIQUE").
|
|
|
|
AddColumn(bob.ColumnDef{Name: "creator", Type: "INT", Extras: []string{"NOT NULL", "REFERENCES \"administrators\" (\"id\")"}}).
|
2021-07-14 18:17:01 +00:00
|
|
|
ToSql()
|
2021-07-09 06:11:11 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("11 - failed on table creation: ", err)
|
2021-07-09 06:11:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
q, err := conn2.Query(*ctx, sql)
|
2021-07-14 18:17:01 +00:00
|
|
|
if err != nil {
|
2021-07-23 15:02:39 +00:00
|
|
|
log.Fatalln("12 - failed on table creation: ", err)
|
2021-07-14 18:17:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-28 04:29:39 +00:00
|
|
|
defer q.Close()
|
2021-07-09 12:13:19 +00:00
|
|
|
}
|
2021-07-23 15:02:39 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
// Submission table
|
2021-09-28 04:29:39 +00:00
|
|
|
conn3, err := db.Acquire(*ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn3.Release()
|
2021-08-04 05:56:14 +00:00
|
|
|
|
|
|
|
//Check if table exists
|
|
|
|
var tableSubmissionExists bool
|
2021-09-28 04:29:39 +00:00
|
|
|
err = conn3.QueryRow(*ctx, `SELECT EXISTS (
|
2021-08-04 05:56:14 +00:00
|
|
|
SELECT FROM information_schema.tables
|
|
|
|
WHERE table_schema = 'public'
|
|
|
|
AND table_name = 'submission'
|
2021-09-28 04:29:39 +00:00
|
|
|
);`).Scan(&tableSubmissionExists)
|
2021-08-04 05:56:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("13 - failed on checking table: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tableSubmissionExists {
|
|
|
|
sql, _, err := bob.
|
|
|
|
CreateTable("submission").
|
|
|
|
AddColumn(bob.ColumnDef{Name: "id", Type: "SERIAL", Extras: []string{"PRIMARY KEY"}}).
|
|
|
|
TextColumn("link", "UNIQUE", "NOT NULL").
|
|
|
|
StringColumn("created_at").
|
|
|
|
StringColumn("author", "NOT NULL").
|
|
|
|
AddColumn(bob.ColumnDef{Name: "status", Type: "SMALLINT", Extras: []string{"DEFAULT 0"}}).
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("14 - failed on table creation: ", err)
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
q, err := conn3.Query(*ctx, sql)
|
2021-08-04 05:56:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("15 - failed on table creation: ", err)
|
|
|
|
}
|
2021-09-28 04:29:39 +00:00
|
|
|
defer q.Close()
|
2021-08-04 05:56:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:11:11 +00:00
|
|
|
return nil
|
|
|
|
}
|