Skip to content

Commit

Permalink
adding auto db migrate env var
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Nov 8, 2023
1 parent 13d3fb5 commit f76e3b3
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 10 deletions.
3 changes: 3 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
Expand Down Expand Up @@ -749,6 +750,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -1280,6 +1282,7 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
7 changes: 5 additions & 2 deletions backend/hack/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ if [ "$PG_HOSTNAME" = "db:5432/nucleus" ]; then
PG_OPTIONS="${PG_OPTIONS}&sslmode=disable"
fi

SCRIPT_DIR=$(basename "$0")
SCRIPT_DIR=$(dirname "$0")

DEFAULT_SCHEMA_PATH="${SCRIPT_DIR}/../sql/postgresql/schema"
SCHEMA_PATH="${MIGRATIONS_DIR:-$DEFAULT_SCHEMA_PATH}"

if [ -z "$1" ]; then
echo "Must specify up or down; you can optionally pass in number of steps as well, i.e., 'migrate up 1'"
Expand Down Expand Up @@ -84,4 +87,4 @@ debug "PG_CONNECT_STR: ${PG_CONNECT_STR}"

# make stderr go through stdout so it doesn't end up an err in datadog
# shellcheck disable=2086
migrate -path "${SCRIPT_DIR}/../sql/postgresql/schema" -database "${PG_CONNECT_STR}" "$@" 2>&1
migrate -path "$SCHEMA_PATH" -database "${PG_CONNECT_STR}" "$@" 2>&1
19 changes: 19 additions & 0 deletions backend/internal/cmds/mgmt/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package migrate_cmd

import (
up_cmd "github.com/nucleuscloud/neosync/backend/internal/cmds/mgmt/migrate/up"
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "serve",
Short: "Parent command for serving",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

cmd.AddCommand(up_cmd.NewCmd())
return cmd
}
116 changes: 116 additions & 0 deletions backend/internal/cmds/mgmt/migrate/up/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package up_cmd

import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/golang-migrate/migrate/v4"
"github.com/nucleuscloud/neosync/backend/internal/nucleusdb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "up",
Short: "Run database migrations",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
connStr, err := getDbUrl()
if err != nil {
return err
}
return Up(
cmd.Context(),
connStr,
"",
slog.New(slog.NewJSONHandler(os.Stdout, nil)),
)
},
}
return cmd
}

func Up(
ctx context.Context,
connStr string,
schemaDir string,
logger *slog.Logger,
) error {

var absSchemaDir string
if filepath.IsAbs(schemaDir) {
absSchemaDir = schemaDir
} else {
a, err := filepath.Abs(schemaDir)
if err != nil {
return err
}
absSchemaDir = a
}

m, err := migrate.New(
fmt.Sprintf("file://%s", strings.TrimPrefix(absSchemaDir, "file://")),
connStr,
)
if err != nil {
return err
}

err = m.Up()
if err != nil {
return err
}

return nil
}

func getDbUrl() (string, error) {
dburl := viper.GetString("DB_URL")
if dburl != "" {
return dburl, nil
}

dbHost := viper.GetString("DB_HOST")
if dbHost == "" {
return "", fmt.Errorf("must provide DB_HOST in environment")
}

dbPort := viper.GetInt("DB_PORT")
if dbPort == 0 {
return "", fmt.Errorf("must provide DB_PORT in environment")
}

dbName := viper.GetString("DB_NAME")
if dbName == "" {
return "", fmt.Errorf("must provide DB_NAME in environment")
}

dbUser := viper.GetString("DB_USER")
if dbUser == "" {
return "", fmt.Errorf("must provide DB_USER in environment")
}

dbPass := viper.GetString("DB_PASS")
if dbPass == "" {
return "", fmt.Errorf("must provide DB_PASS in environment")
}

sslMode := "require"
if viper.IsSet("DB_SSL_DISABLE") && viper.GetBool("DB_SSL_DISABLE") {
sslMode = "disable"
}

return nucleusdb.GetDbUrl(&nucleusdb.ConnectConfig{
Host: dbHost,
Port: dbPort,
Database: dbName,
User: dbUser,
Pass: dbPass,
SslMode: &sslMode,
}), nil
}
22 changes: 20 additions & 2 deletions backend/internal/cmds/mgmt/serve/connect/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package serve_connect

import (
"context"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net/http"
Expand All @@ -17,6 +19,7 @@ import (

auth_client "github.com/nucleuscloud/neosync/backend/internal/auth/client"
"github.com/nucleuscloud/neosync/backend/internal/authmw"
up_cmd "github.com/nucleuscloud/neosync/backend/internal/cmds/mgmt/migrate/up"
auth_interceptor "github.com/nucleuscloud/neosync/backend/internal/connect/interceptors/auth"
logger_interceptor "github.com/nucleuscloud/neosync/backend/internal/connect/interceptors/logger"
auth_jwt "github.com/nucleuscloud/neosync/backend/internal/jwt"
Expand All @@ -41,12 +44,12 @@ func NewCmd() *cobra.Command {
Short: "serves up connect",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return serve()
return serve(cmd.Context())
},
}
}

func serve() error {
func serve(ctx context.Context) error {
port := viper.GetInt32("PORT")
if port == 0 {
port = 8080
Expand Down Expand Up @@ -93,6 +96,21 @@ func serve() error {
return err
}

if viper.GetBool("AUTO_DB_MIGRATE") {
schemaDir := viper.GetString("DB_SCHEMA_DIR")
if schemaDir == "" {
return errors.New("must provide DB_SCHEMA_DIR env var to run auto db migrations")
}
if err := up_cmd.Up(
ctx,
nucleusdb.GetDbUrl(dbconfig),
schemaDir,
logger,
); err != nil {
return err
}
}

validateInterceptor, err := validate.NewInterceptor()
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ALTER TABLE
neosync_api.accounts
DROP COLUMN IF NOT EXISTS temporal_config;
DROP COLUMN IF EXISTS temporal_config;
16 changes: 11 additions & 5 deletions docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ COPY backend/gen/ gen/
COPY backend/cmd/ cmd/
COPY backend/internal/ internal/
COPY backend/services/ services/
COPY backend/sql sql/

# Build
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/mgmt cmd/mgmt/*.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
FROM alpine:3.18

RUN apk add --update --no-cache \
ca-certificates \
tzdata \
bash \
curl

WORKDIR /
COPY --from=builder /workspace/backend/bin/mgmt .
COPY --from=migrate-builder /usr/local/bin/migrate /usr/local/bin/migrate
COPY /backend/migrations/ /migrations/
USER 65532:65532
COPY /backend/sql/postgresql/schema/ /migrations/
COPY /backend/hack/migrate.sh /migrate.sh

ENTRYPOINT ["/mgmt"]

Expand Down

0 comments on commit f76e3b3

Please sign in to comment.