-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad69568
commit fcf3995
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
"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(¬eID) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |