Skip to content

Commit

Permalink
PostgreSQL, goose env created
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMartynenko committed Feb 20, 2024
1 parent 91e8f50 commit ad69568
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# auth
# auth

# Migration
In the context of databases, migration refers to the process of evolving the structure of a database schema over time while preserving the existing data. Database migration typically involves making changes to the schema, such as adding new tables, modifying existing tables, creating indexes, or altering constraints.

utility goose for migration

9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ go 1.20

require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/jackc/pgx/v4 v4.18.1
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
)

require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
198 changes: 198 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions postgres/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PG_DATABASE_NAME=note
PG_USER=note-user
PG_PASSWORD=note-password
PG_PORT=54321
MIGRATION_DIR=./migrations
18 changes: 18 additions & 0 deletions postgres/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include .env

LOCAL_BIN:=$(CURDIR)/bin

LOCAL_MIGRATION_DIR=$(MIGRATION_DIR)
LOCAL_MIGRATION_DSN="host=localhost port=$(PG_PORT) dbname=$(PG_DATABASE_NAME) user=$(PG_USER) password=$(PG_PASSWORD) sslmode=disable"

install-deps:
GOBIN=$(LOCAL_BIN) go install github.com/pressly/goose/v3/cmd/[email protected]

local-migration-status:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} status -v

local-migration-up:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} up -v

local-migration-down:
${LOCAL_BIN}/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} down -v
Binary file added postgres/bin/goose
Binary file not shown.
55 changes: 55 additions & 0 deletions postgres/cmd/raw_query/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

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

"github.com/brianvoe/gofakeit"
"github.com/jackc/pgx/v4"
)

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

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

// Create a connection to the database
con, err := pgx.Connect(ctx, dbDSN)
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
defer con.Close(ctx)

// Making a request to insert a record into the note table
res, err := con.Exec(ctx, "INSERT INTO note (title, body) VALUES ($1, $2)", gofakeit.City(), gofakeit.Address().Street)
if err != nil {
log.Fatalf("failed to insert note: %v", err)
}

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

// We make a request to select records from the note table
rows, err := con.Query(ctx, "SELECT id, title, body, created_at, updated_at FROM note")
if err != nil {
log.Fatalf("failed to select notes: %v", err)
}
defer rows.Close()

for rows.Next() {
var id int
var title, body string
var createdAt time.Time
var updatedAt sql.NullTime

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)
}
}
21 changes: 21 additions & 0 deletions postgres/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

volumes:
postgres_volume:

services:
# pg - name to coonect db from other services
pg:
image: postgres:14-alpine3.17
# sets environment variables needed for the PostgreSQL instance to run correctly.
environment:
- "POSTGRES_DB=${PG_DATABASE_NAME}"
- "POSTGRES_USER=${PG_USER}"
- "POSTGRES_PASSWORD=${PG_PASSWORD}"
ports:
- "${PG_PORT}:5432"
# Volumes Mounts the postgres_volume volume to the /var/lib/postgresql/data
# 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
9 changes: 9 additions & 0 deletions postgres/migrations/20240219145029_migrations_bla_bla.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
-- +goose StatementEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
create table note (
id serial primary key,
title text not null,
body text not null,
created_at timestamp not null default now(),
updated_at timestamp
);

-- +goose Down
drop table note;
9 changes: 9 additions & 0 deletions postgres/migrations/20240220152524_add_user_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
-- +goose StatementEnd

0 comments on commit ad69568

Please sign in to comment.