Skip to content

Commit

Permalink
migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Nov 23, 2024
1 parent dbc5db7 commit 2110c11
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ go.work.sum
# env file
.env

internal/core/grpc/generated/
internal/core/grpc/generated/
bin/*
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ COPY . .
# Install tools for code generation
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
RUN go install github.com/99designs/gqlgen@latest

# Generate gRPC and GraphQL code
# Generate gRPC code
RUN make grpc-update
RUN make grpc-gen
RUN make gql-gen

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o app ./cmd/server/main.go
Expand Down
82 changes: 70 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
APP_NAME = lotof.sample.svc
BUILD_DIR = bin
MAIN_FILE = cmd/server/main.go
GOFMT = gofmt
GO_TEST = go test
GO_COVERAGE = $(GO_TEST) -cover
PG_MIGRATION_DIR = internal/core/db/pg/migrations
PROTOC = protoc
PROTOC_GEN_GO = $(GOPATH)/bin/protoc-gen-go
PROTOC_GEN_GRPC_GO = $(GOPATH)/bin/protoc-gen-go-grpc
PROTOC_PKG = github.com/pieceowater-dev/lotof.sample.proto
PROTOC_PKG_PATH = $(shell go list -m -f '{{.Dir}}' $(PROTOC_PKG))
PROTOC_DIR = protos
PROTOC_OUT_DIR = ./internal/core/grpc/generated

PG_DB_DSN = $(shell grep POSTGRES_DB_DSN .env | cut -d '"' -f2)
DOCKER_COMPOSE = docker-compose

.PHONY: all generate run clean build-dev build-main compose-up compose-down
export PATH := /usr/local/bin:$(PATH)

.PHONY: all clean build run test update migration migrate db-sync setup install-flyway install-atlas install-postgres install-atlas-cli \
grpc-gen grpc-clean grpc-update compose-up compose-down gql-gen gql-clean

# Setup the environment
setup: install-atlas-cli grpc-update
@echo "Setup completed!"; \
go mod tidy

# Default build target
all: build

# Update dependencies
update:
go mod tidy

# Build the project
build:
@mkdir -p $(BUILD_DIR)
go build -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_FILE)

# Run the application
run: build
./$(BUILD_DIR)/$(APP_NAME)

all: gql-gen run
# Clean build artifacts
clean:
rm -rf $(BUILD_DIR) gql-clean grpc-clean

# Install Atlas CLI
install-atlas-cli:
@brew install ariga/tap/atlas

# Generate new migration files with Atlas
pg-migration:
@mkdir -p $(PG_MIGRATION_DIR); \
PATH=/usr/local/bin:$$PATH atlas migrate diff --env postgres; \
echo "Migration files generated in $(PG_MIGRATION_DIR)"; \
git add $(PG_MIGRATION_DIR)/*

# Apply migrations with Atlas
pg-migrate:
@PATH=/usr/local/bin:$$PATH atlas migrate apply --url "$(PG_DB_DSN)" --dir="file://$(shell pwd)/$(PG_MIGRATION_DIR)"

# Sync migrations: generate and apply them
db-sync: pg-migration pg-migrate

# gRPC code generation
grpc-gen: grpc-clean
Expand All @@ -22,24 +72,32 @@ grpc-gen: grpc-clean
--go-grpc_out=$(PROTOC_OUT_DIR) \
$(PROTOC_PKG_PATH)/$(PROTOC_DIR)/*/*/*.proto

# Clean gRPC generated files
grpc-clean:
rm -rf $(PROTOC_OUT_DIR)

# Update gRPC dependencies
grpc-update:
go get -u $(PROTOC_PKG)@latest

run:
go run ./cmd/server/main.go


build:
docker build -t gtw .
# Docker build target
build-docker:
docker build -t $(APP_NAME) .

# Build Docker image and run the container
build-and-run-docker: build-docker
docker stop $(APP_NAME)
docker rm $(APP_NAME)
docker run -d -p 50051:50051 \
-e POSTGRES_DB_DSN="$(PG_DB_DSN)" \
--network lotofsamplesvc_pieceonetwork \
--name $(APP_NAME) \
$(APP_NAME)

# Start Docker Compose services
compose-up:
$(DOCKER_COMPOSE) up -d

# Stop Docker Compose services
compose-down:
$(DOCKER_COMPOSE) down

clean: gql-clean grpc-clean
$(DOCKER_COMPOSE) down
17 changes: 17 additions & 0 deletions atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "external_schema" "postgres" {
program = ["go", "run", "./cmd/server/db/pg/pg-migrate.go"]
}

env "postgres" {
src = data.external_schema.postgres.url
dev = "docker://postgres/16/dev"
migration {
dir = "file://internal/core/db/pg/migrations"
}

format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
30 changes: 30 additions & 0 deletions cmd/server/db/pg/pg-migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"app/internal/core/cfg"
"ariga.io/atlas-provider-gorm/gormschema"
"fmt"
"io"
"os"
"strings"
)

func main() {
sb := &strings.Builder{}
loadModels(sb)

_, err := io.WriteString(os.Stdout, sb.String())
if err != nil {
return
}
}

func loadModels(sb *strings.Builder) {
stmts, err := gormschema.New("postgres").Load(cfg.Inst().PostgresModels...)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load gorm schema: %v\n", err)
os.Exit(1)
}
sb.WriteString(stmts)
sb.WriteString(";\n")
}
29 changes: 20 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@ module app
go 1.23.0

require (
ariga.io/atlas-provider-gorm v0.5.0
github.com/joho/godotenv v1.5.1
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.34.2
gorm.io/driver/postgres v1.5.10
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)

require (
ariga.io/atlas-go-sdk v0.6.4 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/sqlite v1.5.6 // indirect
gorm.io/driver/sqlserver v1.5.4 // indirect
)
Loading

0 comments on commit 2110c11

Please sign in to comment.