Skip to content

Commit

Permalink
feat(migrator): added migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
defany committed Feb 21, 2024
1 parent 20d39eb commit 2f5e885
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 42 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
REGISTRY: "defany"
IMAGE_NAME: "user-service"
CONTAINER_NAME: "user-service"
IMAGE_NAME_MIGRATOR: "migrator"

jobs:
build-test:
Expand Down Expand Up @@ -66,6 +67,9 @@ jobs:
TAG_NAME=$(echo $GITHUB_SHA | head -c7)
docker buildx create --use
docker buildx build --no-cache --push --tag $REGISTRY/$IMAGE_NAME:$TAG_NAME .
docker buildx create --use -f migrator.Dockerfile
docker buildx build --no-cache --push --tag $REGISTRY/$IMAGE_NAME-$IMAGE_NAME_MIGRATOR:$TAG_NAME .
deploy:
runs-on: ubuntu-latest
Expand All @@ -83,7 +87,7 @@ jobs:
# Set up variables
TAG_NAME=$(echo $GITHUB_SHA | head -c7)
# Login into Selectel Registry
# Login into docker
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} $REGISTRY
# Stop running container
Expand All @@ -92,5 +96,8 @@ jobs:
# Remove old container
docker rm $CONTAINER_NAME
# Run migrations before next container
docker run --name $CONTAINER_NAME -t $REGISTRY/$IMAGE_NAME-$IMAGE_NAME_MIGRATOR:$TAG_NAME
# Run a new container from a new image
docker run -d -p 50100:50001 --restart=always --name $CONTAINER_NAME -t $REGISTRY/$IMAGE_NAME:$TAG_NAME
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /github.com/defany/auth-service/source

RUN go mod download
RUN go mod tidy -e
RUN go build -o ./bin/server app/cmd/main.go
RUN go build -o ./bin/server app/cmd/app/main.go

FROM alpine:latest

Expand Down
File renamed without changes.
46 changes: 46 additions & 0 deletions app/cmd/migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"github.com/defany/auth-service/app/internal/app"
"github.com/defany/auth-service/app/pkg/logger/sl"
"github.com/defany/auth-service/app/pkg/postgres"
"log/slog"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

a := app.NewApp()

db := a.DI().Database(ctx)

log := a.DI().Log(ctx)

migrator, err := postgres.NewMigrator(db.Pool(), a.DI().Config(ctx).Database.MigrationsDir)
if err != nil {
log.Error("failed to setup migrator", sl.ErrAttr(err))

return
}

log.Info("upping the migrations")

upped, err := migrator.Up(ctx)
if err != nil {
log.Error("failed to up migrations", sl.ErrAttr(err))

return
}

if len(upped) == 0 {
log.Info("there is no migrations to up")

return
}

for _, migration := range upped {
log.Info("migration upped!", slog.String("name", migration.Source.Path))
}
}
14 changes: 11 additions & 3 deletions app/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
)

type App struct {
di *di
di *DI
grpcServer *grpc.Server
}

func NewApp() App {
return App{}
func NewApp() *App {
a := &App{}

a.setupDI()

return a
}

func (a *App) Run(ctx context.Context) error {
Expand All @@ -35,6 +39,10 @@ func (a *App) Run(ctx context.Context) error {
return a.runGRPCServer(ctx)
}

func (a *App) DI() *DI {
return a.di
}

func (a *App) setupDI() {
a.di = newDI()
}
Expand Down
22 changes: 11 additions & 11 deletions app/internal/app/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"os"
)

type di struct {
type DI struct {
log *slog.Logger

cfg *config.Config
Expand All @@ -33,14 +33,14 @@ type di struct {
}

txManager postgres.TxManager
db postgres.Querier
db postgres.Postgres
}

func newDI() *di {
return &di{}
func newDI() *DI {
return &DI{}
}

func (d *di) Log(ctx context.Context) *slog.Logger {
func (d *DI) Log(ctx context.Context) *slog.Logger {
if d.log != nil {
return d.log
}
Expand All @@ -50,7 +50,7 @@ func (d *di) Log(ctx context.Context) *slog.Logger {
return d.log
}

func (d *di) Config(_ context.Context) *config.Config {
func (d *DI) Config(_ context.Context) *config.Config {
if d.cfg != nil {
return d.cfg
}
Expand All @@ -60,7 +60,7 @@ func (d *di) Config(_ context.Context) *config.Config {
return d.cfg
}

func (d *di) Database(ctx context.Context) postgres.Querier {
func (d *DI) Database(ctx context.Context) postgres.Postgres {
if d.db != nil {
return d.db
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (d *di) Database(ctx context.Context) postgres.Querier {
return d.db
}

func (d *di) TxManager(ctx context.Context) postgres.TxManager {
func (d *DI) TxManager(ctx context.Context) postgres.TxManager {
if d.txManager != nil {
return d.txManager
}
Expand All @@ -100,7 +100,7 @@ func (d *di) TxManager(ctx context.Context) postgres.TxManager {
return d.txManager
}

func (d *di) UserRepo(ctx context.Context) repository.UserRepository {
func (d *DI) UserRepo(ctx context.Context) repository.UserRepository {
if d.repositories.user != nil {
return d.repositories.user
}
Expand All @@ -110,7 +110,7 @@ func (d *di) UserRepo(ctx context.Context) repository.UserRepository {
return d.repositories.user
}

func (d *di) UserService(ctx context.Context) defserv.UserService {
func (d *DI) UserService(ctx context.Context) defserv.UserService {
if d.services.user != nil {
return d.services.user
}
Expand All @@ -120,7 +120,7 @@ func (d *di) UserService(ctx context.Context) defserv.UserService {
return d.services.user
}

func (d *di) UserImpl(ctx context.Context) *user.Implementation {
func (d *DI) UserImpl(ctx context.Context) *user.Implementation {
if d.implementations.user != nil {
return d.implementations.user
}
Expand Down
4 changes: 2 additions & 2 deletions app/internal/repository/user/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const (
)

type repository struct {
db postgres.Querier
db postgres.Postgres
qb squirrel.StatementBuilderType
}

func NewRepository(db postgres.Querier) repo.UserRepository {
func NewRepository(db postgres.Postgres) repo.UserRepository {
return &repository{
db: db,
qb: squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar),
Expand Down
33 changes: 33 additions & 0 deletions app/pkg/postgres/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package postgres

import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"os"
)

type Migrator struct {
provider *goose.Provider

Check failure on line 12 in app/pkg/postgres/migrations.go

View workflow job for this annotation

GitHub Actions / lint

undefined: goose (typecheck)
}

func NewMigrator(db *pgxpool.Pool, migrationsDir string) (*Migrator, error) {
provider, err := goose.NewProvider(goose.DialectPostgres, stdlib.OpenDBFromPool(db), os.DirFS(migrationsDir))

Check failure on line 16 in app/pkg/postgres/migrations.go

View workflow job for this annotation

GitHub Actions / lint

undefined: goose (typecheck)
if err != nil {
return nil, err
}

return &Migrator{
provider: provider,
}, nil
}

func (u *Migrator) Up(ctx context.Context) ([]*goose.MigrationResult, error) {

Check failure on line 26 in app/pkg/postgres/migrations.go

View workflow job for this annotation

GitHub Actions / lint

undefined: goose (typecheck)
res, err := u.provider.Up(ctx)
if err != nil {
return nil, err
}

return res, nil
}
10 changes: 8 additions & 2 deletions app/pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"log/slog"
)

type Querier interface {
type Postgres interface {
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)

Check failure on line 12 in app/pkg/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pgx (typecheck)
QueryRow(ctx context.Context, query string, args ...interface{}) pgx.Row

Check failure on line 13 in app/pkg/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pgx (typecheck)
Exec(ctx context.Context, query string, args ...interface{}) (commandTag pgconn.CommandTag, err error)

BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

Check failure on line 16 in app/pkg/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / lint

undefined: pgx (typecheck)

Pool() *pgxpool.Pool

Close()
}

Expand All @@ -24,7 +26,7 @@ type postgres struct {
db *pgxpool.Pool
}

func NewPostgres(ctx context.Context, log *slog.Logger, cfg *Config) (Querier, error) {
func NewPostgres(ctx context.Context, log *slog.Logger, cfg *Config) (Postgres, error) {
p := &postgres{
log: log,
}
Expand Down Expand Up @@ -70,6 +72,10 @@ func (p *postgres) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx
return p.db.BeginTx(ctx, txOptions)
}

func (p *postgres) Pool() *pgxpool.Pool {
return p.db
}

func (p *postgres) Close() {
p.db.Close()
}
4 changes: 2 additions & 2 deletions app/pkg/postgres/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type TxManager interface {
type Handler func(ctx context.Context) error

type txManager struct {
db Querier
db Postgres
}

func NewTxManager(db Querier) TxManager {
func NewTxManager(db Postgres) TxManager {
return &txManager{
db: db,
}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
postgres:
condition: service_healthy
environment:
CONFIG_PATH: ./local.json
DB_HOST: postgres

server:
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jackc/pgx/v5 v5.5.3
github.com/pkg/errors v0.9.1
github.com/pressly/goose/v3 v3.18.0
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -25,10 +26,13 @@ require (
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014 // indirect
Expand Down
Loading

0 comments on commit 2f5e885

Please sign in to comment.