-
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
91e8f50
commit ad69568
Showing
11 changed files
with
342 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 |
---|---|---|
@@ -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 | ||
|
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,5 @@ | ||
PG_DATABASE_NAME=note | ||
PG_USER=note-user | ||
PG_PASSWORD=note-password | ||
PG_PORT=54321 | ||
MIGRATION_DIR=./migrations |
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,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 not shown.
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,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) | ||
} | ||
} |
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 @@ | ||
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 |
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,9 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
SELECT 'up SQL query'; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
SELECT 'down SQL query'; | ||
-- +goose StatementEnd |
11 changes: 11 additions & 0 deletions
11
postgres/migrations/20240219145226_migrations_create_note_table_sql.sql
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,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; |
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,9 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
SELECT 'up SQL query'; | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
SELECT 'down SQL query'; | ||
-- +goose StatementEnd |