From 7fd12546286e96ef6475982d66091d624282ccc1 Mon Sep 17 00:00:00 2001 From: Luis Angel Arvelo Date: Wed, 18 Oct 2023 17:41:59 +0000 Subject: [PATCH 1/5] Update env variables to specify migrations dir --- app.env | 1 + util/config.go | 1 + 2 files changed, 2 insertions(+) diff --git a/app.env b/app.env index 3c16a93..aa35146 100644 --- a/app.env +++ b/app.env @@ -1,4 +1,5 @@ DB_SOURCE=postgresql://root:secret@localhost:5432/rdb?sslmode=disable +MIGRATION_URL=file://db/migrations HTTP_SERVER_ADDRESS=0.0.0.0:8080 GRPC_SERVER_ADDRESS=0.0.0.0:9090 TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012 diff --git a/util/config.go b/util/config.go index 44881c2..b1e815a 100644 --- a/util/config.go +++ b/util/config.go @@ -10,6 +10,7 @@ import ( // The values are read by viper from a config file or environment variable. type Config struct { DBSource string `mapstructure:"DB_SOURCE"` + MigrationURL string `mapstructure:"MIGRATION_URL"` HTTPServerAddress string `mapstructure:"HTTP_SERVER_ADDRESS"` GRPCServerAddress string `mapstructure:"GRPC_SERVER_ADDRESS"` TokenSymmetricKey string `mapstructure:"TOKEN_SYMMETRIC_KEY"` From 30ea5320679da9c87003048fbebdfae5ac1c7445 Mon Sep 17 00:00:00 2001 From: Luis Angel Arvelo Date: Wed, 18 Oct 2023 17:42:35 +0000 Subject: [PATCH 2/5] Update start.sh file --- start.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/start.sh b/start.sh index 1075f6a..000d789 100755 --- a/start.sh +++ b/start.sh @@ -2,10 +2,5 @@ set -e -echo "run db migrations" -source /app/app.env -echo "$DB_SOURCE" -/app/migrate -path /app/migrations -database "$DB_SOURCE" -verbose up - echo "start the app" exec "$@" From f2201c94b76321aee43cd7d7003ff2f5f15c0965 Mon Sep 17 00:00:00 2001 From: Luis Angel Arvelo Date: Wed, 18 Oct 2023 17:43:16 +0000 Subject: [PATCH 3/5] Update Docker file instructions to delete migrate binary install --- Dockerfile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index b59e53d..58297c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,18 +3,15 @@ FROM golang:1.21.2-alpine3.18 AS build WORKDIR /app COPY . . RUN go build -o main main.go -RUN apk add curl -RUN curl -L https://github.com/golang-migrate/migrate/releases/download/v4.16.2/migrate.linux-amd64.tar.gz | tar xvz # Run stage FROM alpine:3.18 WORKDIR /app COPY --from=build /app/main . -COPY --from=build /app/migrate ./migrate COPY app.env . COPY start.sh . COPY wait-for.sh . -COPY db/migrations ./migrations +COPY db/migrations ./db/migrations EXPOSE 8080 CMD [ "/app/main" ] From f865bbcde99220a0035f7bd9e5633676426fa1a0 Mon Sep 17 00:00:00 2001 From: Luis Angel Arvelo Date: Wed, 18 Oct 2023 17:43:37 +0000 Subject: [PATCH 4/5] Update main.go file to run migrations directly inside go server --- main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.go b/main.go index a9d518a..7781c88 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,9 @@ import ( "github.com/Delavalom/RBD/gapi" "github.com/Delavalom/RBD/pb" "github.com/Delavalom/RBD/util" + "github.com/golang-migrate/migrate/v4" + _ "github.com/golang-migrate/migrate/v4/database/postgres" + _ "github.com/golang-migrate/migrate/v4/source/file" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/jackc/pgx/v5/pgxpool" "google.golang.org/grpc" @@ -28,11 +31,24 @@ func main() { log.Fatal("cannot connect to db") } + runDBMigration(config.MigrationURL, config.DBSource) + store := db.NewStore(conn) go runGatewayServer(config, store) runGrpcServer(config, store) } +func runDBMigration(migrationURL, dbSource string) { + migration, err := migrate.New(migrationURL, dbSource) + if err != nil { + log.Fatal("cannot create new migrate instance: ", err) + } + if err = migration.Up(); err != nil && err != migrate.ErrNoChange { + log.Fatal("failed to run migrate up: ", err) + } + log.Println("db migrated successfully") +} + func runGrpcServer(config util.Config, store db.Store) { server, err := gapi.NewServer(config, store) if err != nil { From 6e9eac35daeba3128399b32f9be43ec6cd4dba69 Mon Sep 17 00:00:00 2001 From: Luis Angel Arvelo Date: Wed, 18 Oct 2023 17:45:54 +0000 Subject: [PATCH 5/5] Update go dependencies --- go.mod | 5 +++++ go.sum | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/go.mod b/go.mod index aa58820..7752de3 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.1 require ( github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 + github.com/golang-migrate/migrate/v4 v4.16.2 github.com/golang/protobuf v1.5.3 github.com/google/uuid v1.3.1 github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 @@ -22,7 +23,11 @@ require ( github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect github.com/golang/glog v1.1.0 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/lib/pq v1.10.2 // indirect github.com/pkg/errors v0.9.1 // indirect + go.uber.org/atomic v1.9.0 // indirect google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect ) diff --git a/go.sum b/go.sum index 96917ec..36fd99b 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7N github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA= +github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ= @@ -162,6 +164,11 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 h1:RtRsiaGvWxcwd8y3BiRZxsylPT8hLWZ5SPcfI+3IDNk= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0/go.mod h1:TzP6duP4Py2pHLVPPQp42aoYI92+PCrVotyR5e8Vqlk= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -195,6 +202,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= +github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= @@ -262,6 +271,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo= go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=