Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate postgres data to a docker volume for compatibility #797

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clienv/clienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type CliEnv struct {
stderr io.Writer
Path *PathStructure
domain string
branch string
nhclient *nhostclient.Client
projectName string
}
Expand All @@ -29,13 +30,15 @@ func New(
stderr io.Writer,
path *PathStructure,
domain string,
branch string,
projectName string,
) *CliEnv {
return &CliEnv{
stdout: stdout,
stderr: stderr,
Path: path,
domain: domain,
branch: branch,
nhclient: nil,
projectName: projectName,
}
Expand All @@ -57,6 +60,7 @@ func FromCLI(cCtx *cli.Context) *CliEnv {
cCtx.String(flagNhostFolder),
),
domain: cCtx.String(flagDomain),
branch: cCtx.String(flagBranch),
projectName: sanitizeName(cCtx.String(flagProjectName)),
nhclient: nil,
}
Expand All @@ -70,6 +74,10 @@ func (ce *CliEnv) Domain() string {
return ce.domain
}

func (ce *CliEnv) Branch() string {
return ce.branch
}

func (ce *CliEnv) GetNhostClient() *nhostclient.Client {
if ce.nhclient == nil {
ce.nhclient = nhostclient.New(ce.domain)
Expand Down
14 changes: 12 additions & 2 deletions clienv/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const (
flagDomain = "domain"
flagBranch = "branch"
flagProjectName = "project-name"
flagRootFolder = "root-folder"
flagDataFolder = "data-folder"
Expand All @@ -35,15 +36,17 @@ func getGitBranchName() string {
return head.Name().Short()
}

func Flags() ([]cli.Flag, error) {
func Flags() ([]cli.Flag, error) { //nolint:funlen
fullWorkingDir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get working directory: %w", err)
}

branch := getGitBranchName()

workingDir := "."
dotNhostFolder := filepath.Join(workingDir, ".nhost")
dataFolder := filepath.Join(dotNhostFolder, "data", getGitBranchName())
dataFolder := filepath.Join(dotNhostFolder, "data", branch)
nhostFolder := filepath.Join(workingDir, "nhost")

return []cli.Flag{
Expand All @@ -54,6 +57,13 @@ func Flags() ([]cli.Flag, error) {
Value: "nhost.run",
Hidden: true,
},
&cli.StringFlag{ //nolint:exhaustruct
Name: flagBranch,
Usage: "Git branch name",
EnvVars: []string{"BRANCH"},
Value: branch,
Hidden: true,
},
&cli.StringFlag{ //nolint:exhaustruct
Name: flagRootFolder,
Usage: "Root folder of project\n\t",
Expand Down
1 change: 1 addition & 0 deletions cmd/config/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func TestValidate(t *testing.T) {
filepath.Join("testdata", "validate", tc.path, "nhost"),
),
"fakedomain",
"fakebranch",
"",
)

Expand Down
14 changes: 12 additions & 2 deletions cmd/dev/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import (
"github.com/urfave/cli/v2"
)

const (
flagVolumes = "volumes"
)

func CommandDown() *cli.Command {
return &cli.Command{ //nolint:exhaustruct
Name: "down",
Aliases: []string{},
Usage: "Stop local development environment",
Action: commandDown,
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.BoolFlag{ //nolint:exhaustruct
Name: flagVolumes,
Usage: "Remove volumes",
Value: false,
},
},
}
}

Expand All @@ -21,7 +31,7 @@ func commandDown(cCtx *cli.Context) error {

dc := dockercompose.New(ce.Path.WorkingDir(), ce.Path.DockerCompose(), ce.ProjectName())

if err := dc.Stop(cCtx.Context); err != nil {
if err := dc.Stop(cCtx.Context, cCtx.Bool(flagVolumes)); err != nil {
ce.Warnln("failed to stop Nhost development environment: %s", err)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/dev/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func up( //nolint:funlen
ce.Path.DotNhostFolder(),
ce.Path.Root(),
ports,
ce.Branch(),
)
if err != nil {
return fmt.Errorf("failed to generate docker-compose.yaml: %w", err)
Expand Down Expand Up @@ -300,7 +301,7 @@ func Up(
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

if err := dc.Stop(ctx); err != nil { //nolint:contextcheck
if err := dc.Stop(ctx, false); err != nil { //nolint:contextcheck
ce.Warnln("failed to stop Nhost development environment: %s", err)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/dockercredentials/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func getToken(ctx context.Context, domain string) (string, error) {
&clienv.PathStructure{},
domain,
"unneeded",
"unneeded",
)
session, err := ce.LoadSession(ctx)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion dockercompose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/nhost/be/services/mimir/model"
"github.com/nhost/cli/ssl"
Expand Down Expand Up @@ -417,6 +419,11 @@ type ExposePorts struct {
Functions uint
}

func sanitizeBranch(name string) string {
re := regexp.MustCompile(`[^a-zA-Z0-9_-]`)
return strings.ToLower(re.ReplaceAllString(name, ""))
}

func ComposeFileFromConfig( //nolint:funlen
cfg *model.ConfigConfig,
projectName string,
Expand All @@ -428,6 +435,7 @@ func ComposeFileFromConfig( //nolint:funlen
dotNhostFolder string,
rootFolder string,
ports ExposePorts,
branch string,
) (*ComposeFile, error) {
minio, err := minio(dataFolder)
if err != nil {
Expand All @@ -444,7 +452,8 @@ func ComposeFileFromConfig( //nolint:funlen
return nil, err
}

postgres, err := postgres(cfg, postgresPort, dataFolder)
pgVolumeName := fmt.Sprintf("pgdata_%s", sanitizeBranch(branch))
postgres, err := postgres(cfg, postgresPort, dataFolder, pgVolumeName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -494,6 +503,7 @@ func ComposeFileFromConfig( //nolint:funlen
Volumes: map[string]struct{}{
"functions_node_modules": {},
"root_node_modules": {},
pgVolumeName: {},
},
}
return c, nil
Expand Down
5 changes: 4 additions & 1 deletion dockercompose/dockercompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (dc *DockerCompose) Start(ctx context.Context) error {
return nil
}

func (dc *DockerCompose) Stop(ctx context.Context) error {
func (dc *DockerCompose) Stop(ctx context.Context, volumes bool) error {
cmd := exec.CommandContext( //nolint:gosec
ctx,
"docker", "compose",
Expand All @@ -76,6 +76,9 @@ func (dc *DockerCompose) Stop(ctx context.Context) error {
"-p", dc.projectName,
"down",
)
if volumes {
cmd.Args = append(cmd.Args, "--volumes")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down
7 changes: 4 additions & 3 deletions dockercompose/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func postgres( //nolint:funlen
cfg *model.ConfigConfig,
port uint,
dataFolder string,
volumeName string,
) (*Service, error) {
if err := os.MkdirAll(fmt.Sprintf("%s/db/pgdata", dataFolder), 0o755); err != nil { //nolint:gomnd
return nil, fmt.Errorf("failed to create postgres data folder: %w", err)
Expand Down Expand Up @@ -75,9 +76,9 @@ func postgres( //nolint:funlen
Restart: "always",
Volumes: []Volume{
{
Type: "bind",
Source: fmt.Sprintf("%s/db/pgdata", dataFolder),
Target: "/var/lib/postgresql/data/pgdata:Z",
Type: "volume",
Source: volumeName,
Target: "/var/lib/postgresql/data/pgdata",
},
{
Type: "bind",
Expand Down
8 changes: 4 additions & 4 deletions dockercompose/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func expectedPostgres(tmpdir string) *Service {
Restart: "always",
Volumes: []Volume{
{
Type: "bind",
Source: filepath.Join(tmpdir, "db/pgdata"),
Target: "/var/lib/postgresql/data/pgdata:Z",
Type: "volume",
Source: "pgdate_test",
Target: "/var/lib/postgresql/data/pgdata",
},
{
Type: "bind",
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestPostgres(t *testing.T) {
tc := tc

tmpdir := filepath.Join(os.TempDir(), "data")
got, err := postgres(tc.cfg(), 5432, tmpdir)
got, err := postgres(tc.cfg(), 5432, tmpdir, "pgdate_test")
if err != nil {
t.Errorf("got error: %v", err)
}
Expand Down
Loading