mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 05:53:45 +03:00
[hw-8] attempt to fix tests
This commit is contained in:
@@ -27,10 +27,12 @@ db-create-migration:
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) create -s $(n) sql
|
||||
|
||||
db-migrate:
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "$(LOCAL_DB_DSN)" up
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@192.168.64.5:5438/comments_db?sslmode=disable" up
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@192.168.64.5:5439/comments_db?sslmode=disable" up
|
||||
|
||||
db-migrate-down:
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "$(LOCAL_DB_DSN)" down
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@192.168.64.5:5438/comments_db?sslmode=disable" down
|
||||
$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@192.168.64.5:5439/comments_db?sslmode=disable" down
|
||||
|
||||
db-reset-local:
|
||||
psql -c "drop database if exists \"$(LOCAL_DB_NAME)\""
|
||||
|
||||
@@ -7,14 +7,14 @@ service:
|
||||
http_port: 8086
|
||||
|
||||
db_shards:
|
||||
- host: localhost
|
||||
port: 5434
|
||||
- host: 192.168.64.5
|
||||
port: 5438
|
||||
user: comments-user-1
|
||||
password: comments-password-1
|
||||
db_name: comments_db
|
||||
|
||||
- host: localhost
|
||||
port: 5435
|
||||
- host: 192.168.64.5
|
||||
port: 5439
|
||||
user: comments-user-2
|
||||
password: comments-password-2
|
||||
db_name: comments_db
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE comments (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
id BIGINT PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
sku BIGINT NOT NULL,
|
||||
text VARCHAR(255) NOT NULL,
|
||||
@@ -12,5 +12,3 @@ CREATE INDEX user_id_idx ON comments(user_id, created_at DESC);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE comments;
|
||||
DROP INDEX sku_idx;
|
||||
DROP INDEX user_id_idx;
|
||||
@@ -1,5 +1,5 @@
|
||||
-- name: InsertComment :one
|
||||
INSERT INTO comments (user_id, sku, text) VALUES ($1, $2, $3)
|
||||
INSERT INTO comments (id, user_id, sku, text) VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, user_id, sku, text, created_at;
|
||||
|
||||
-- name: GetCommentByID :one
|
||||
|
||||
@@ -27,18 +27,24 @@ func (q *Queries) GetCommentByID(ctx context.Context, id int64) (*Comment, error
|
||||
}
|
||||
|
||||
const insertComment = `-- name: InsertComment :one
|
||||
INSERT INTO comments (user_id, sku, text) VALUES ($1, $2, $3)
|
||||
INSERT INTO comments (id, user_id, sku, text) VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, user_id, sku, text, created_at
|
||||
`
|
||||
|
||||
type InsertCommentParams struct {
|
||||
ID int64
|
||||
UserID int64
|
||||
Sku int64
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) InsertComment(ctx context.Context, arg *InsertCommentParams) (*Comment, error) {
|
||||
row := q.db.QueryRow(ctx, insertComment, arg.UserID, arg.Sku, arg.Text)
|
||||
row := q.db.QueryRow(ctx, insertComment,
|
||||
arg.ID,
|
||||
arg.UserID,
|
||||
arg.Sku,
|
||||
arg.Text,
|
||||
)
|
||||
var i Comment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
|
||||
@@ -3,6 +3,7 @@ package sqlc
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -15,21 +16,25 @@ import (
|
||||
type commentsRepo struct {
|
||||
shard1 *pgxpool.Pool
|
||||
shard2 *pgxpool.Pool
|
||||
|
||||
curID int64
|
||||
idMx sync.Mutex
|
||||
}
|
||||
|
||||
func NewCommentsRepository(shard1, shard2 *pgxpool.Pool) *commentsRepo {
|
||||
return &commentsRepo{
|
||||
shard1: shard1,
|
||||
shard2: shard2,
|
||||
curID: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *commentsRepo) pickShard(sku int64) *pgxpool.Pool {
|
||||
func (r *commentsRepo) pickShard(sku int64) (*pgxpool.Pool, int64) {
|
||||
if sku%2 == 0 {
|
||||
return r.shard1
|
||||
return r.shard1, 0
|
||||
}
|
||||
|
||||
return r.shard2
|
||||
return r.shard2, 1
|
||||
}
|
||||
|
||||
func mapComment(c *Comment) *entity.Comment {
|
||||
@@ -67,13 +72,19 @@ func (r *commentsRepo) GetCommentByID(ctx context.Context, id int64) (*entity.Co
|
||||
}
|
||||
|
||||
func (r *commentsRepo) InsertComment(ctx context.Context, comment *entity.Comment) (*entity.Comment, error) {
|
||||
shard := r.pickShard(comment.SKU)
|
||||
shard, shardID := r.pickShard(comment.SKU)
|
||||
q := New(shard)
|
||||
|
||||
r.idMx.Lock()
|
||||
id := r.curID*1000 + shardID
|
||||
r.curID++
|
||||
r.idMx.Unlock()
|
||||
|
||||
req := &InsertCommentParams{
|
||||
UserID: comment.UserID,
|
||||
Sku: comment.SKU,
|
||||
Text: comment.Text,
|
||||
ID: id,
|
||||
}
|
||||
|
||||
c, err := q.InsertComment(ctx, req)
|
||||
@@ -85,7 +96,7 @@ func (r *commentsRepo) InsertComment(ctx context.Context, comment *entity.Commen
|
||||
}
|
||||
|
||||
func (r *commentsRepo) ListCommentsBySku(ctx context.Context, sku int64) ([]*entity.Comment, error) {
|
||||
shard := r.pickShard(sku)
|
||||
shard, _ := r.pickShard(sku)
|
||||
q := New(shard)
|
||||
|
||||
list, err := q.ListCommentsBySku(ctx, sku)
|
||||
|
||||
Reference in New Issue
Block a user