jokes-bapak2/api/platform/database/create.go

145 lines
3.0 KiB
Go
Raw Normal View History

2021-07-09 06:11:11 +00:00
package database
import (
"context"
"github.com/aldy505/bob"
"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
func Populate(db *pgxpool.Pool, ctx context.Context) error {
err := setupAuthTable(db, ctx)
2021-09-28 06:34:02 +00:00
if err != nil {
return err
}
err = setupJokesTable(db, ctx)
2021-09-28 06:34:02 +00:00
if err != nil {
return err
}
err = setupSubmissionTable(db, ctx)
2021-09-28 06:34:02 +00:00
if err != nil {
return err
}
2021-10-30 03:24:53 +00:00
return nil
}
2021-09-28 06:34:02 +00:00
func setupAuthTable(db *pgxpool.Pool, ctx context.Context) error {
conn, err := db.Acquire(ctx)
2021-09-28 06:34:02 +00:00
if err != nil {
return err
}
defer conn.Release()
2021-09-28 06:34:02 +00:00
// Check if table exists
var tableAuthExists bool
err = conn.QueryRow(ctx, `SELECT EXISTS (
2021-07-14 18:17:01 +00:00
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'administrators'
);`).Scan(&tableAuthExists)
2021-07-09 06:11:11 +00:00
if err != nil {
2021-07-14 18:17:01 +00:00
return err
2021-07-09 06:11:11 +00:00
}
if !tableAuthExists {
sql, _, err := bob.
CreateTable("administrators").
AddColumn(bob.ColumnDef{Name: "id", Type: "SERIAL", Extras: []string{"PRIMARY KEY"}}).
StringColumn("key", "NOT NULL", "UNIQUE").
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 {
return err
}
2021-08-04 05:56:14 +00:00
_, err = conn.Exec(ctx, sql)
2021-07-14 18:17:01 +00:00
if err != nil {
return err
}
2021-07-09 06:11:11 +00:00
}
2021-09-28 06:34:02 +00:00
return nil
}
2021-07-09 06:11:11 +00:00
func setupJokesTable(db *pgxpool.Pool, ctx context.Context) error {
conn, err := db.Acquire(ctx)
2021-10-30 03:24:53 +00:00
if err != nil {
return err
}
defer conn.Release()
// Check if table exists
var tableJokesExists bool
err = conn.QueryRow(ctx, `SELECT EXISTS (
2021-07-14 18:17:01 +00:00
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'jokesbapak2'
);`).Scan(&tableJokesExists)
2021-07-09 06:11:11 +00:00
if err != nil {
2021-07-09 12:13:19 +00:00
return err
2021-07-09 06:11:11 +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 {
return err
}
_, err = conn.Exec(ctx, sql)
2021-07-14 18:17:01 +00:00
if err != nil {
return err
}
2021-07-09 12:13:19 +00:00
}
2021-09-28 06:34:02 +00:00
return nil
}
2021-08-04 05:56:14 +00:00
func setupSubmissionTable(db *pgxpool.Pool, ctx context.Context) error {
conn, err := db.Acquire(ctx)
2021-10-30 03:24:53 +00:00
if err != nil {
return err
}
defer conn.Release()
2021-08-04 05:56:14 +00:00
//Check if table exists
var tableSubmissionExists bool
err = conn.QueryRow(ctx, `SELECT EXISTS (
2021-09-28 06:34:02 +00:00
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'submission'
);`).Scan(&tableSubmissionExists)
2021-08-04 05:56:14 +00:00
if err != nil {
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 {
2021-09-28 06:34:02 +00:00
return err
2021-08-04 05:56:14 +00:00
}
_, err = conn.Exec(ctx, sql)
2021-08-04 05:56:14 +00:00
if err != nil {
2021-09-28 06:34:02 +00:00
return err
2021-08-04 05:56:14 +00:00
}
}
2021-07-09 06:11:11 +00:00
return nil
}