forked from mtusi2026/mtusi2026lab3
feat(db): add idempotency keys table
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
-- +goose Up
|
||||||
|
CREATE TABLE idempotency_keys (
|
||||||
|
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||||
|
method text NOT NULL,
|
||||||
|
path text NOT NULL,
|
||||||
|
key text NOT NULL,
|
||||||
|
request_hash text NOT NULL,
|
||||||
|
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE
|
||||||
|
DEFERRABLE INITIALLY DEFERRED,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
PRIMARY KEY (user_id, method, path, key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at);
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP TABLE idempotency_keys;
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
-- name: CreateIdempotencyKey :execrows
|
||||||
|
INSERT INTO idempotency_keys (
|
||||||
|
user_id,
|
||||||
|
method,
|
||||||
|
path,
|
||||||
|
key,
|
||||||
|
request_hash,
|
||||||
|
message_id
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(user_id),
|
||||||
|
sqlc.arg(method),
|
||||||
|
sqlc.arg(path),
|
||||||
|
sqlc.arg(key),
|
||||||
|
sqlc.arg(request_hash),
|
||||||
|
sqlc.arg(message_id)
|
||||||
|
)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
|
-- name: GetIdempotentMessage :one
|
||||||
|
SELECT
|
||||||
|
k.request_hash,
|
||||||
|
m.id,
|
||||||
|
m.chat_id,
|
||||||
|
m.sender_id,
|
||||||
|
m.text,
|
||||||
|
m.created_at
|
||||||
|
FROM idempotency_keys AS k
|
||||||
|
JOIN messages AS m ON m.id = k.message_id
|
||||||
|
WHERE k.user_id = sqlc.arg(user_id)
|
||||||
|
AND k.method = sqlc.arg(method)
|
||||||
|
AND k.path = sqlc.arg(path)
|
||||||
|
AND k.key = sqlc.arg(key);
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: idempotency_keys.sql
|
||||||
|
|
||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createIdempotencyKey = `-- name: CreateIdempotencyKey :execrows
|
||||||
|
INSERT INTO idempotency_keys (
|
||||||
|
user_id,
|
||||||
|
method,
|
||||||
|
path,
|
||||||
|
key,
|
||||||
|
request_hash,
|
||||||
|
message_id
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3,
|
||||||
|
$4,
|
||||||
|
$5,
|
||||||
|
$6
|
||||||
|
)
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateIdempotencyKeyParams struct {
|
||||||
|
UserID uuid.UUID
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
Key string
|
||||||
|
RequestHash string
|
||||||
|
MessageID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateIdempotencyKey(ctx context.Context, arg CreateIdempotencyKeyParams) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, createIdempotencyKey,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Method,
|
||||||
|
arg.Path,
|
||||||
|
arg.Key,
|
||||||
|
arg.RequestHash,
|
||||||
|
arg.MessageID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
|
||||||
|
SELECT
|
||||||
|
k.request_hash,
|
||||||
|
m.id,
|
||||||
|
m.chat_id,
|
||||||
|
m.sender_id,
|
||||||
|
m.text,
|
||||||
|
m.created_at
|
||||||
|
FROM idempotency_keys AS k
|
||||||
|
JOIN messages AS m ON m.id = k.message_id
|
||||||
|
WHERE k.user_id = $1
|
||||||
|
AND k.method = $2
|
||||||
|
AND k.path = $3
|
||||||
|
AND k.key = $4
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetIdempotentMessageParams struct {
|
||||||
|
UserID uuid.UUID
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
Key string
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetIdempotentMessageRow struct {
|
||||||
|
RequestHash string
|
||||||
|
ID uuid.UUID
|
||||||
|
ChatID uuid.UUID
|
||||||
|
SenderID uuid.UUID
|
||||||
|
Text string
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (GetIdempotentMessageRow, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getIdempotentMessage,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Method,
|
||||||
|
arg.Path,
|
||||||
|
arg.Key,
|
||||||
|
)
|
||||||
|
var i GetIdempotentMessageRow
|
||||||
|
err := row.Scan(
|
||||||
|
&i.RequestHash,
|
||||||
|
&i.ID,
|
||||||
|
&i.ChatID,
|
||||||
|
&i.SenderID,
|
||||||
|
&i.Text,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -21,6 +21,16 @@ type ChatMember struct {
|
|||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IdempotencyKey struct {
|
||||||
|
UserID uuid.UUID
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
Key string
|
||||||
|
RequestHash string
|
||||||
|
MessageID uuid.UUID
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
ChatID uuid.UUID
|
ChatID uuid.UUID
|
||||||
|
|||||||
Reference in New Issue
Block a user