Skip to content

Commit

Permalink
Auth updated
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMartynenko committed Feb 21, 2024
1 parent ad69568 commit fcf3995
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
117 changes: 117 additions & 0 deletions postgres/cmd/query_with_squirrel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"database/sql"
"log"
"time"

sq "github.com/Masterminds/squirrel"

Check failure on line 9 in postgres/cmd/query_with_squirrel/main.go

View workflow job for this annotation

GitHub Actions / build-and-test

no required module provides package github.com/Masterminds/squirrel; to add it:
"github.com/brianvoe/gofakeit"
"github.com/jackc/pgx/v4/pgxpool"
)

const (
dbDSN = "host=localhost port=54321 dbname=note user=note-user password=note-password sslmode=disable"
)

func main() {
ctx := context.Background()

// Создаем пул соединений с базой данных
pool, err := pgxpool.Connect(ctx, dbDSN)
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
defer pool.Close()

// Делаем запрос на вставку записи в таблицу note
builderInsert := sq.Insert("note").
PlaceholderFormat(sq.Dollar).
Columns("title", "body").
Values(gofakeit.City(), gofakeit.Address().Street).
Suffix("RETURNING id")

query, args, err := builderInsert.ToSql()
if err != nil {
log.Fatalf("failed to build query: %v", err)
}

var noteID int
err = pool.QueryRow(ctx, query, args...).Scan(&noteID)
if err != nil {
log.Fatalf("failed to insert note: %v", err)
}

log.Printf("inserted note with id: %d", noteID)

// Делаем запрос на выборку записей из таблицы note
builderSelect := sq.Select("id", "title", "body", "created_at", "updated_at").
From("note").
PlaceholderFormat(sq.Dollar).
OrderBy("id ASC").
Limit(10)

query, args, err = builderSelect.ToSql()
if err != nil {
log.Fatalf("failed to build query: %v", err)
}

rows, err := pool.Query(ctx, query, args...)
if err != nil {
log.Fatalf("failed to select notes: %v", err)
}

var id int
var title, body string
var createdAt time.Time
var updatedAt sql.NullTime

for rows.Next() {
err = rows.Scan(&id, &title, &body, &createdAt, &updatedAt)
if err != nil {
log.Fatalf("failed to scan note: %v", err)
}

log.Printf("id: %d, title: %s, body: %s, created_at: %v, updated_at: %v\n", id, title, body, createdAt, updatedAt)
}

// Делаем запрос на обновление записи в таблице note
builderUpdate := sq.Update("note").
PlaceholderFormat(sq.Dollar).
Set("title", gofakeit.City()).
Set("body", gofakeit.Address().Street).
Set("updated_at", time.Now()).
Where(sq.Eq{"id": noteID})

query, args, err = builderUpdate.ToSql()
if err != nil {
log.Fatalf("failed to build query: %v", err)
}

res, err := pool.Exec(ctx, query, args...)
if err != nil {
log.Fatalf("failed to update note: %v", err)
}

log.Printf("updated %d rows", res.RowsAffected())

// Делаем запрос на получение измененной записи из таблицы note
builderSelectOne := sq.Select("id", "title", "body", "created_at", "updated_at").
From("note").
PlaceholderFormat(sq.Dollar).
Where(sq.Eq{"id": noteID}).
Limit(1)

query, args, err = builderSelectOne.ToSql()
if err != nil {
log.Fatalf("failed to build query: %v", err)
}

err = pool.QueryRow(ctx, query, args...).Scan(&id, &title, &body, &createdAt, &updatedAt)
if err != nil {
log.Fatalf("failed to select notes: %v", err)
}

log.Printf("id: %d, title: %s, body: %s, created_at: %v, updated_at: %v\n", id, title, body, createdAt, updatedAt)
}
9 changes: 8 additions & 1 deletion postgres/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ services:
# directory within the container. This ensures that the data generated by
# the PostgreSQL container is persisted even if the container is stopped or removed.
volumes:
- postgres_volume:/var/lib/postgresql/data
- postgres_volume:/var/lib/postgresql/data
migrator:
build:
context: .
dockerfile: migration.Dockerfile
restart: on-failure
environment:
DB_HOST: pg
21 changes: 21 additions & 0 deletions postgres/migration.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM alpine:3.13

RUN apk update && \
apk upgrade && \
# adding bash to run the script
apk add bash && \
rm -rf /var/cache/apk/*

ADD https://github.com/pressly/goose/releases/download/v3.14.0/goose_linux_x86_64 /bin/goose
RUN chmod +x /bin/goose

WORKDIR /root

ADD migrations/*.sql migrations/
ADD migration.sh .
# from env we getting param. dsn etc
ADD .env .

RUN chmod +x migration.sh

ENTRYPOINT ["bash", "migration.sh"]
6 changes: 6 additions & 0 deletions postgres/migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
source .env

export MIGRATION_DSN="host=pg port=5432 dbname=$PG_DATABASE_NAME user=$PG_USER password=$PG_PASSWORD sslmode=disable"

sleep 2 && goose -dir "${MIGRATION_DIR}" postgres "${MIGRATION_DSN}" up -v

0 comments on commit fcf3995

Please sign in to comment.