fix: sql transactions

This commit is contained in:
Reinaldy Rafli 2021-09-27 20:02:58 +07:00
parent cf28bf4213
commit a5c665fe4f
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
9 changed files with 131 additions and 115 deletions

View File

@ -24,7 +24,7 @@ import (
func New() *fiber.App { func New() *fiber.App {
// Setup Context // Setup Context
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) ctx, cancel := context.WithCancel(context.Background())
// Setup PostgreSQL // Setup PostgreSQL
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL")) poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
@ -62,15 +62,14 @@ func New() *fiber.App {
Debug: true, Debug: true,
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatalln(err)
} }
defer sentry.Flush(2 * time.Second) defer sentry.Flush(2 * time.Second)
err = database.Setup(db, &ctx) err = database.Setup(db, &ctx)
if err != nil { if err != nil {
sentry.CaptureException(err) sentry.CaptureException(err)
log.Fatal(err) log.Fatalln(err)
} }
err = core.SetAllJSONJoke(db, memory, &ctx) err = core.SetAllJSONJoke(db, memory, &ctx)

View File

@ -3,7 +3,9 @@ package joke
import ( import (
"jokes-bapak2-api/app/core" "jokes-bapak2-api/app/core"
"github.com/Masterminds/squirrel"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4"
) )
func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error { func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error {
@ -38,8 +40,29 @@ func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error {
Error: "URL provided is not a valid image", Error: "URL provided is not a valid image",
}) })
} }
// Validate if link already exists
sql, args, err := d.Query. sql, args, err := d.Query.
Select("link").
From("jokesbapak2").
Where(squirrel.Eq{"link": body.Link}).
ToSql()
if err != nil {
return err
}
v, err := conn.Query(*d.Context, sql, args...)
if err != nil && err != pgx.ErrNoRows {
return err
}
defer v.Close()
if err == nil {
return c.Status(fiber.StatusConflict).JSON(Error{
Error: "Given link is already on the jokesbapak2 database",
})
}
sql, args, err = d.Query.
Insert("jokesbapak2"). Insert("jokesbapak2").
Columns("link", "creator"). Columns("link", "creator").
Values(body.Link, c.Locals("userID")). Values(body.Link, c.Locals("userID")).
@ -48,7 +71,6 @@ func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error {
return err return err
} }
// TODO: Implement solution if the link provided already exists.
_, err = tx.Exec(*d.Context, sql, args...) _, err = tx.Exec(*d.Context, sql, args...)
if err != nil { if err != nil {
return err return err

View File

@ -17,9 +17,10 @@ type SubmissionQuery struct {
} }
type ResponseSubmission struct { type ResponseSubmission struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
Data Submission `json:"data,omitempty"` Submission Submission `json:"submission,omitempty"`
AuthorPage string `json:"author_page,omitempty"`
} }
type Error struct { type Error struct {

View File

@ -2,16 +2,25 @@ package submit
import ( import (
"jokes-bapak2-api/app/core" "jokes-bapak2-api/app/core"
"net/url"
"strings" "strings"
"time" "time"
"github.com/Masterminds/squirrel"
"github.com/georgysavva/scany/pgxscan" "github.com/georgysavva/scany/pgxscan"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4"
) )
func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error { func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
conn, err := d.DB.Acquire(*d.Context)
if err != nil {
return err
}
defer conn.Release()
var body Submission var body Submission
err := c.BodyParser(&body) err = c.BodyParser(&body)
if err != nil { if err != nil {
return err return err
} }
@ -19,26 +28,26 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
// Image and/or Link should not be empty // Image and/or Link should not be empty
if body.Image == "" && body.Link == "" { if body.Image == "" && body.Link == "" {
return c.Status(fiber.StatusBadRequest).JSON(Error{ return c.Status(fiber.StatusBadRequest).JSON(Error{
Error: "a link or an image should be supplied in a form of multipart/form-data", Error: "A link or an image should be supplied in a form of multipart/form-data",
}) })
} }
// Author should be supplied // Author should be supplied
if body.Author == "" { if body.Author == "" {
return c.Status(fiber.StatusBadRequest).JSON(Error{ return c.Status(fiber.StatusBadRequest).JSON(Error{
Error: "an author key consisting on the format \"yourname <youremail@mail>\" must be supplied", Error: "An author key consisting on the format \"yourname <youremail@mail>\" must be supplied",
}) })
} else { } else {
// Validate format // Validate format
valid := core.ValidateAuthor(body.Author) valid := core.ValidateAuthor(body.Author)
if !valid { if !valid {
return c.Status(fiber.StatusBadRequest).JSON(Error{ return c.Status(fiber.StatusBadRequest).JSON(Error{
Error: "please stick to the format of \"yourname <youremail@mail>\" and within 200 characters", Error: "Please stick to the format of \"yourname <youremail@mail>\" and within 200 characters",
}) })
} }
} }
var url string var link string
// Check link validity if link was provided // Check link validity if link was provided
if body.Link != "" { if body.Link != "" {
@ -52,25 +61,46 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
}) })
} }
url = body.Link link = body.Link
} }
// If image was provided // If image was provided
if body.Image != "" { if body.Image != "" {
image := strings.NewReader(body.Image) image := strings.NewReader(body.Image)
url, err = core.UploadImage(d.HTTP, image) link, err = core.UploadImage(d.HTTP, image)
if err != nil { if err != nil {
return err return err
} }
} }
// Validate if link already exists
sql, args, err := d.Query.
Select("link").
From("submission").
Where(squirrel.Eq{"link": link}).
ToSql()
if err != nil {
return err
}
v, err := conn.Query(*d.Context, sql, args...)
if err != nil && err != pgx.ErrNoRows {
return err
}
defer v.Close()
if err == nil {
return c.Status(fiber.StatusConflict).JSON(Error{
Error: "Given link is already on the submission queue.",
})
}
now := time.Now().UTC().Format(time.RFC3339) now := time.Now().UTC().Format(time.RFC3339)
sql, args, err := d.Query. sql, args, err = d.Query.
Insert("submission"). Insert("submission").
Columns("link", "created_at", "author"). Columns("link", "created_at", "author").
Values(url, now, body.Author). Values(link, now, body.Author).
Suffix("RETURNING id,created_at,link,author,status"). Suffix("RETURNING id,created_at,link,author,status").
ToSql() ToSql()
if err != nil { if err != nil {
@ -78,7 +108,7 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
} }
var submission []Submission var submission []Submission
result, err := d.DB.Query(*d.Context, sql, args...) result, err := conn.Query(*d.Context, sql, args...)
if err != nil { if err != nil {
return err return err
} }
@ -92,7 +122,8 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
return c. return c.
Status(fiber.StatusCreated). Status(fiber.StatusCreated).
JSON(ResponseSubmission{ JSON(ResponseSubmission{
Message: "Joke submitted. Please wait for a few days for admin to approve your submission.", Message: "Joke submitted. Please wait for a few days for admin to approve your submission.",
Data: submission[0], Submission: submission[0],
AuthorPage: "/submit?author=" + url.QueryEscape(body.Author),
}) })
} }

View File

@ -16,15 +16,9 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
} }
defer conn.Release() defer conn.Release()
tx, err := conn.Begin(*ctx)
if err != nil {
return err
}
defer tx.Rollback(*ctx)
// administrators table // administrators table
var tableAuthExists bool var tableAuthExists bool
err = db.QueryRow(*ctx, `SELECT EXISTS ( err = conn.QueryRow(*ctx, `SELECT EXISTS (
SELECT FROM information_schema.tables SELECT FROM information_schema.tables
WHERE table_schema = 'public' WHERE table_schema = 'public'
AND table_name = 'administrators' AND table_name = 'administrators'
@ -47,7 +41,7 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
return err return err
} }
_, err = tx.Exec(*ctx, sql) _, err = conn.Query(*ctx, sql)
if err != nil { if err != nil {
log.Fatalln("18 - failed on table creation: ", err) log.Fatalln("18 - failed on table creation: ", err)
return err return err
@ -58,7 +52,7 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
// Check if table exists // Check if table exists
var tableJokesExists bool var tableJokesExists bool
err = db.QueryRow(*ctx, `SELECT EXISTS ( err = conn.QueryRow(*ctx, `SELECT EXISTS (
SELECT FROM information_schema.tables SELECT FROM information_schema.tables
WHERE table_schema = 'public' WHERE table_schema = 'public'
AND table_name = 'jokesbapak2' AND table_name = 'jokesbapak2'
@ -80,7 +74,7 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
return err return err
} }
_, err = tx.Exec(*ctx, sql) _, err = conn.Query(*ctx, sql)
if err != nil { if err != nil {
log.Fatalln("12 - failed on table creation: ", err) log.Fatalln("12 - failed on table creation: ", err)
return err return err
@ -91,7 +85,7 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
//Check if table exists //Check if table exists
var tableSubmissionExists bool var tableSubmissionExists bool
err = db.QueryRow(*ctx, `SELECT EXISTS ( err = conn.QueryRow(*ctx, `SELECT EXISTS (
SELECT FROM information_schema.tables SELECT FROM information_schema.tables
WHERE table_schema = 'public' WHERE table_schema = 'public'
AND table_name = 'submission' AND table_name = 'submission'
@ -114,16 +108,11 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
log.Fatalln("14 - failed on table creation: ", err) log.Fatalln("14 - failed on table creation: ", err)
} }
_, err = tx.Query(*ctx, sql) _, err = conn.Query(*ctx, sql)
if err != nil { if err != nil {
log.Fatalln("15 - failed on table creation: ", err) log.Fatalln("15 - failed on table creation: ", err)
} }
} }
err = tx.Commit(*ctx)
if err != nil {
return err
}
return nil return nil
} }

View File

@ -8,14 +8,14 @@ INSERT INTO "administrators" ("id", "key", "token", "last_used") VALUES
-- 10 jokes is enough right? -- 10 jokes is enough right?
INSERT INTO "jokesbapak2" ("id", "link", "creator") VALUES INSERT INTO "jokesbapak2" ("link", "creator") VALUES
(1, 'https://picsum.photos/id/1000/500/500', 1), ('https://picsum.photos/id/1000/500/500', 1),
(2, 'https://picsum.photos/id/1001/500/500', 1), ('https://picsum.photos/id/1001/500/500', 1),
(3, 'https://picsum.photos/id/1002/500/500', 1), ('https://picsum.photos/id/1002/500/500', 1),
(4, 'https://picsum.photos/id/1003/500/500', 1), ('https://picsum.photos/id/1003/500/500', 1),
(5, 'https://picsum.photos/id/1004/500/500', 1), ('https://picsum.photos/id/1004/500/500', 1),
(6, 'https://picsum.photos/id/1005/500/500', 1), ('https://picsum.photos/id/1005/500/500', 1),
(7, 'https://picsum.photos/id/1006/500/500', 1), ('https://picsum.photos/id/1006/500/500', 1),
(8, 'https://picsum.photos/id/1010/500/500', 1), ('https://picsum.photos/id/1010/500/500', 1),
(9, 'https://picsum.photos/id/1008/500/500', 1), ('https://picsum.photos/id/1008/500/500', 1),
(10, 'https://picsum.photos/id/1009/500/500', 1); ('https://picsum.photos/id/1009/500/500', 1);

View File

@ -14,6 +14,7 @@ func (d *Dependencies) Health() {
Redis: d.Redis, Redis: d.Redis,
Context: d.Context, Context: d.Context,
} }
d.App.Get("/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), deps.Health) d.App.Get("/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), deps.Health)
d.App.Get("/v1/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), deps.Health) d.App.Get("/v1/health", cache.New(cache.Config{Expiration: 30 * time.Minute}), deps.Health)
} }

View File

@ -10,12 +10,14 @@ import (
func (d *Dependencies) Submit() { func (d *Dependencies) Submit() {
deps := submit.Dependencies{ deps := submit.Dependencies{
DB: d.DB, DB: d.DB,
Redis: d.Redis, Redis: d.Redis,
Memory: d.Memory, Memory: d.Memory,
HTTP: d.HTTP, HTTP: d.HTTP,
Query: d.Query, Query: d.Query,
Context: d.Context,
} }
// Get pending submitted joke // Get pending submitted joke
d.App.Get( d.App.Get(
"/submit", "/submit",

View File

@ -16,51 +16,31 @@ import (
var jokesData = []interface{}{1, "https://via.placeholder.com/300/06f/fff.png", 1, 2, "https://via.placeholder.com/300/07f/fff.png", 1, 3, "https://via.placeholder.com/300/08f/fff.png", 1} var jokesData = []interface{}{1, "https://via.placeholder.com/300/06f/fff.png", 1, 2, "https://via.placeholder.com/300/07f/fff.png", 1, 3, "https://via.placeholder.com/300/08f/fff.png", 1}
var submissionData = []interface{}{1, "https://via.placeholder.com/300/01f/fff.png", "2021-08-03T18:20:38Z", "Test <test@example.com>", 0, 2, "https://via.placeholder.com/300/02f/fff.png", "2021-08-04T18:20:38Z", "Test <test@example.com>", 1} var submissionData = []interface{}{1, "https://via.placeholder.com/300/01f/fff.png", "2021-08-03T18:20:38Z", "Test <test@example.com>", 0, 2, "https://via.placeholder.com/300/02f/fff.png", "2021-08-04T18:20:38Z", "Test <test@example.com>", 1}
var administratorsData = []interface{}{1, "very secure", "not the real one", time.Now().Format(time.RFC3339), 2, "test", "$argon2id$v=19$m=65536,t=16,p=4$3a08c79fbf2222467a623df9a9ebf75802c65a4f9be36eb1df2f5d2052d53cb7$ce434bd38f7ba1fc1f2eb773afb8a1f7f2dad49140803ac6cb9d7256ce9826fb3b4afa1e2488da511c852fc6c33a76d5657eba6298a8e49d617b9972645b7106", ""} var administratorsData = []interface{}{1, "very secure", "not the real one", time.Now().Format(time.RFC3339), 2, "test", "$argon2id$v=19$m=65536,t=16,p=4$3a08c79fbf2222467a623df9a9ebf75802c65a4f9be36eb1df2f5d2052d53cb7$ce434bd38f7ba1fc1f2eb773afb8a1f7f2dad49140803ac6cb9d7256ce9826fb3b4afa1e2488da511c852fc6c33a76d5657eba6298a8e49d617b9972645b7106", ""}
var db *pgxpool.Pool
var ctx context.Context = context.Background() var ctx context.Context = context.Background()
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.Parse() flag.Parse()
log.Println("---- Preparing for integration test")
err := setup() err := setup()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.Println("---- Preparation complete")
log.Print("\n")
code := m.Run() code := m.Run()
os.Exit(code) os.Exit(code)
} }
func cleanup() {
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalln("Unable to create pool config", err)
}
poolConfig.MaxConns = 3
poolConfig.MinConns = 1
db, err = pgxpool.ConnectConfig(ctx, poolConfig)
if err != nil {
log.Fatalln("Unable to create connection", err)
}
defer db.Close()
d, err := db.Query(ctx, "BEGIN; DROP TABLE \"jokesbapak2\"; DROP TABLE \"administrators\"; DROP TABLE \"submission\"; COMMIT;")
if err != nil {
panic(err)
}
defer d.Close()
}
func setup() error { func setup() error {
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL")) poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil { if err != nil {
return errors.New("Unable to create pool config: " + err.Error()) return errors.New("Unable to create pool config: " + err.Error())
} }
poolConfig.MaxConns = 3 poolConfig.MaxConns = 15
poolConfig.MinConns = 1 poolConfig.MinConns = 2
db, err = pgxpool.ConnectConfig(ctx, poolConfig) db, err := pgxpool.ConnectConfig(ctx, poolConfig)
if err != nil { if err != nil {
return errors.New("Unable to create connection: " + err.Error()) return errors.New("Unable to create connection: " + err.Error())
} }
@ -72,59 +52,50 @@ func setup() error {
} }
defer conn.Release() defer conn.Release()
tx, err := conn.Begin(ctx) // dj, err := conn.Query(ctx, "DROP TABLE \"jokesbapak2\"")
if err != nil { // if err != nil {
return err // log.Println("busy here - 57")
} // return err
defer tx.Rollback(ctx) // }
// defer dj.Close()
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS \"jokesbapak2\"") // ds, err := conn.Query(ctx, "DROP TABLE \"submission\"")
if err != nil { // if err != nil {
return err // log.Println("busy here - 67")
} // return err
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS \"administrators\"") // }
if err != nil { // defer ds.Close()
return err
} // da, err := conn.Query(ctx, "DROP TABLE \"administrators\"")
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS \"submission\"") // if err != nil {
if err != nil { // log.Println("busy here - 62")
return err // return err
} // }
err = tx.Commit(ctx) // defer da.Close()
if err != nil {
return err
}
err = database.Setup(db, &ctx) err = database.Setup(db, &ctx)
if err != nil { if err != nil {
log.Println("busy here - 73")
return err return err
} }
tx, err = conn.Begin(ctx) ia, err := conn.Query(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...)
if err != nil { if err != nil {
return err return err
} }
defer tx.Rollback(ctx) defer ia.Close()
_, err = tx.Exec(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...) ij, err := conn.Query(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...)
if err != nil { if err != nil {
return err return err
} }
defer ij.Close()
_, err = tx.Exec(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) is, err := conn.Query(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...)
if err != nil {
return err
}
_, err = tx.Exec(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...)
if err != nil {
return err
}
err = tx.Commit(ctx)
if err != nil { if err != nil {
return err return err
} }
defer is.Close()
return nil return nil
} }