postgres
This commit is contained in:
parent
608f7c36e7
commit
5df204164e
@ -10,9 +10,10 @@ import (
|
||||
"gitea.michaelthomson.dev/mthomson/habits/internal/middleware"
|
||||
"gitea.michaelthomson.dev/mthomson/habits/internal/migrate"
|
||||
todohandler "gitea.michaelthomson.dev/mthomson/habits/internal/todo/handler"
|
||||
todorepository "gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository/sqlite"
|
||||
todorepository "gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository/postgres"
|
||||
todoservice "gitea.michaelthomson.dev/mthomson/habits/internal/todo/service"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -23,18 +24,18 @@ func main() {
|
||||
loggingMiddleware := middleware.LoggingMiddleware(httpLogger)
|
||||
|
||||
// create db pool
|
||||
db, err := sql.Open("sqlite3", "./habits.db")
|
||||
postgresUrl := "postgres://todo:password@localhost:5432/todo"
|
||||
db, err := sql.Open("pgx", postgresUrl)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to open db pool")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
// run migrations
|
||||
migrate.Migrate(db)
|
||||
|
||||
// create repos
|
||||
todoRepository := todorepository.NewSqliteTodoRepository(db)
|
||||
todoRepository := todorepository.NewPostgresTodoRepository(db)
|
||||
|
||||
// create services
|
||||
todoService := todoservice.NewTodoService(todoRepository)
|
||||
|
20
docker-compose.yaml
Normal file
20
docker-compose.yaml
Normal file
@ -0,0 +1,20 @@
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
restart: always
|
||||
environment:
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_USER: todo
|
||||
POSTGRES_DB: todo
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
ports:
|
||||
- 5432:5432
|
||||
adminer:
|
||||
image: adminer
|
||||
restart: always
|
||||
ports:
|
||||
- 8081:8080
|
||||
|
||||
volumes:
|
||||
pgdata:
|
14
go.mod
14
go.mod
@ -2,4 +2,16 @@ module gitea.michaelthomson.dev/mthomson/habits
|
||||
|
||||
go 1.22.9
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.7.2
|
||||
github.com/mattn/go-sqlite3 v1.14.24
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
)
|
||||
|
28
go.sum
28
go.sum
@ -1,2 +1,30 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
|
||||
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
||||
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -22,7 +22,7 @@ func Migrate(db *sql.DB) {
|
||||
slog.Info("Running migrations...")
|
||||
migrationTableSql := `
|
||||
CREATE TABLE IF NOT EXISTS migrations(
|
||||
version INTEGER PRIMARY KEY,
|
||||
version SERIAL PRIMARY KEY,
|
||||
name VARCHAR(50)
|
||||
);`
|
||||
|
||||
@ -38,7 +38,7 @@ func Migrate(db *sql.DB) {
|
||||
|
||||
for _, file := range files {
|
||||
var migration Migration
|
||||
row := db.QueryRow("SELECT * FROM migrations WHERE name = ?;", file.Name())
|
||||
row := db.QueryRow("SELECT * FROM migrations WHERE name = $1;", file.Name())
|
||||
err = row.Scan(&migration.Version, &migration.Name)
|
||||
if err == sql.ErrNoRows {
|
||||
slog.Info(fmt.Sprintf("Running migration: %s", file.Name()))
|
||||
@ -52,7 +52,7 @@ func Migrate(db *sql.DB) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.Exec("INSERT INTO migrations(name) VALUES(?);", file.Name())
|
||||
_, err = db.Exec("INSERT INTO migrations(name) VALUES($1);", file.Name())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
CREATE TABLE todo(
|
||||
id INTEGER PRIMARY KEY,
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(50),
|
||||
done INTEGER
|
||||
done BOOLEAN
|
||||
);
|
||||
|
84
internal/todo/repository/postgres/postgres.go
Normal file
84
internal/todo/repository/postgres/postgres.go
Normal file
@ -0,0 +1,84 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository"
|
||||
)
|
||||
|
||||
type PostgresTodoRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewPostgresTodoRepository(db *sql.DB) *PostgresTodoRepository {
|
||||
return &PostgresTodoRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *PostgresTodoRepository) GetById(id int64) (repository.TodoRow, error) {
|
||||
todo := repository.TodoRow{}
|
||||
|
||||
err := r.db.QueryRow("SELECT * FROM todo WHERE id = $1;", id).Scan(&todo.Id, &todo.Name, &todo.Done)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return todo, repository.ErrNotFound
|
||||
}
|
||||
|
||||
return todo, err
|
||||
}
|
||||
|
||||
return todo, nil
|
||||
}
|
||||
|
||||
func (r *PostgresTodoRepository) Create(todo repository.TodoRow) (repository.TodoRow, error) {
|
||||
result := r.db.QueryRow("INSERT INTO todo (name, done) VALUES ($1, $2) RETURNING id;", todo.Name, todo.Done)
|
||||
err := result.Scan(&todo.Id)
|
||||
|
||||
if err != nil {
|
||||
return repository.TodoRow{}, err
|
||||
}
|
||||
|
||||
return todo, nil
|
||||
}
|
||||
|
||||
func (r *PostgresTodoRepository) Update(todo repository.TodoRow) error {
|
||||
result, err := r.db.Exec("UPDATE todo SET name = $1, done = $2 WHERE id = $3;", todo.Name, todo.Done, todo.Id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *PostgresTodoRepository) Delete(id int64) error {
|
||||
result, err := r.db.Exec("DELETE FROM todo WHERE id = $1;", id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"gitea.michaelthomson.dev/mthomson/habits/internal/todo/repository"
|
||||
)
|
||||
@ -67,6 +68,7 @@ func (s *TodoService) CreateTodo(todo Todo) (Todo, error) {
|
||||
newTodoRow, err := s.repo.Create(todoRow)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return Todo{}, err
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user