Skip to content

Commit

Permalink
use binary/ filepath, and nicer cli errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanCoughlan5 committed Jan 14, 2025
1 parent b129fd8 commit edf55ab
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 51 deletions.
13 changes: 5 additions & 8 deletions cmd/createSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/pkg/snapshot"
"github.com/Layr-Labs/sidecar/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -25,20 +24,18 @@ var createSnapshotCmd = &cobra.Command{
return fmt.Errorf("failed to initialize logger: %w", err)
}

outputFile, err := utils.ExpandHomeDir(cfg.SnapshotConfig.OutputFile)
if err != nil {
return err
}

svc := snapshot.NewSnapshotService(&snapshot.SnapshotConfig{
OutputFile: outputFile,
svc, err := snapshot.NewSnapshotService(&snapshot.SnapshotConfig{
OutputFile: cfg.SnapshotConfig.OutputFile,
Host: cfg.DatabaseConfig.Host,
Port: cfg.DatabaseConfig.Port,
User: cfg.DatabaseConfig.User,
Password: cfg.DatabaseConfig.Password,
DbName: cfg.DatabaseConfig.DbName,
SchemaName: cfg.DatabaseConfig.SchemaName,
}, l)
if err != nil {
return err
}

if err := svc.CreateSnapshot(); err != nil {
return fmt.Errorf("failed to create snapshot: %w", err)
Expand Down
13 changes: 5 additions & 8 deletions cmd/restoreSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/pkg/snapshot"
"github.com/Layr-Labs/sidecar/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -28,20 +27,18 @@ Follow the snapshot docs if you need to convert the snapshot to a different sche
return fmt.Errorf("failed to initialize logger: %w", err)
}

inputFile, err := utils.ExpandHomeDir(cfg.SnapshotConfig.InputFile)
if err != nil {
return err
}

svc := snapshot.NewSnapshotService(&snapshot.SnapshotConfig{
InputFile: inputFile,
svc, err := snapshot.NewSnapshotService(&snapshot.SnapshotConfig{
InputFile: cfg.SnapshotConfig.InputFile,
Host: cfg.DatabaseConfig.Host,
Port: cfg.DatabaseConfig.Port,
User: cfg.DatabaseConfig.User,
Password: cfg.DatabaseConfig.Password,
DbName: cfg.DatabaseConfig.DbName,
SchemaName: cfg.DatabaseConfig.SchemaName,
}, l)
if err != nil {
return err
}

if err := svc.RestoreSnapshot(); err != nil {
return fmt.Errorf("failed to restore snapshot: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions docs/snapshots_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Commonly the input schema is
psql -c "CREATE DATABASE temp_sidecar_dump_schema_conversion_db;"

# Use the Sidecar CLI to restore the snapshot dump into the temporary database:
go run main.go restore-snapshot \
./bin/sidecar restore-snapshot \
--database.host=localhost \
--database.user=... \
--database.password=... \
Expand All @@ -127,7 +127,7 @@ go run main.go restore-snapshot \
psql -d temp_sidecar_dump_schema_conversion_db -c "ALTER SCHEMA <input schema name> RENAME TO <output schema name>;"

# Use the Sidecar CLI to create a new snapshot with the updated schema:
go run main.go create-snapshot \
./bin/sidecar create-snapshot \
--database.host=localhost \
--database.user=... \
--database.password=... \
Expand Down
64 changes: 47 additions & 17 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package snapshot
import (
"fmt"
"os"
"path/filepath"
"strings"

pgcommands "github.com/habx/pg-commands"
"go.uber.org/zap"
Expand All @@ -27,17 +29,49 @@ type SnapshotService struct {
}

// NewSnapshotService initializes a new SnapshotService with the given configuration and logger.
func NewSnapshotService(cfg *SnapshotConfig, l *zap.Logger) *SnapshotService {
func NewSnapshotService(cfg *SnapshotConfig, l *zap.Logger) (*SnapshotService, error) {
var err error

cfg.InputFile, err = resolveFilePath(cfg.InputFile)
if err != nil {
return nil, fmt.Errorf("failed to resolve input file path: %w", err)
}
cfg.OutputFile, err = resolveFilePath(cfg.OutputFile)
if err != nil {
return nil, fmt.Errorf("failed to resolve output file path: %w", err)
}

l.Sugar().Infow("Resolved file paths", "inputFile", cfg.InputFile, "outputFile", cfg.OutputFile)

return &SnapshotService{
cfg: cfg,
l: l,
}, nil
}

// resolveFilePath expands the ~ in file paths to the user's home directory and converts relative paths to absolute paths.
func resolveFilePath(path string) (string, error) {
if path == "" {
return "", nil
}
if strings.HasPrefix(path, "~/") {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
path = filepath.Join(homeDir, path[2:])
}
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}
return absPath, nil
}

// CreateSnapshot creates a snapshot of the database based on the provided configuration.
func (s *SnapshotService) CreateSnapshot() error {
if !s.validateCreateSnapshotConfig() {
return fmt.Errorf("invalid snapshot configuration")
if err := s.validateCreateSnapshotConfig(); err != nil {
return err
}

dump, err := s.setupSnapshotDump()
Expand All @@ -57,8 +91,8 @@ func (s *SnapshotService) CreateSnapshot() error {

// RestoreSnapshot restores a snapshot of the database based on the provided configuration.
func (s *SnapshotService) RestoreSnapshot() error {
if !s.validateRestoreConfig() {
return fmt.Errorf("invalid restore configuration")
if err := s.validateRestoreConfig(); err != nil {
return err
}

restore, err := s.setupRestore()
Expand All @@ -79,18 +113,16 @@ func (s *SnapshotService) RestoreSnapshot() error {
return nil
}

func (s *SnapshotService) validateCreateSnapshotConfig() bool {
func (s *SnapshotService) validateCreateSnapshotConfig() error {
if s.cfg.Host == "" {
s.l.Sugar().Error("Database host is required")
return false
return fmt.Errorf("database host is required")
}

if s.cfg.OutputFile == "" {
s.l.Sugar().Error("Output path i.e. `output-file` must be specified")
return false
return fmt.Errorf("output path i.e. `output-file` must be specified")
}

return true
return nil
}

func (s *SnapshotService) setupSnapshotDump() (*pgcommands.Dump, error) {
Expand All @@ -115,19 +147,17 @@ func (s *SnapshotService) setupSnapshotDump() (*pgcommands.Dump, error) {
return dump, nil
}

func (s *SnapshotService) validateRestoreConfig() bool {
func (s *SnapshotService) validateRestoreConfig() error {
if s.cfg.InputFile == "" {
s.l.Sugar().Error("Restore snapshot file path i.e. `input-file` must be specified")
return false
return fmt.Errorf("restore snapshot file path i.e. `input-file` must be specified")
}

info, err := os.Stat(s.cfg.InputFile)
if err != nil || info.IsDir() {
s.l.Sugar().Errorw("Snapshot file does not exist", "path", s.cfg.InputFile)
return false
return fmt.Errorf("snapshot file does not exist: %s", s.cfg.InputFile)
}

return true
return nil
}

func (s *SnapshotService) setupRestore() (*pgcommands.Restore, error) {
Expand Down
14 changes: 0 additions & 14 deletions pkg/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package utils
import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
Expand All @@ -26,15 +24,3 @@ func SnakeCase(s string) string {
notSnake := regexp.MustCompile(`[_-]`)
return notSnake.ReplaceAllString(s, "_")
}

// ExpandHomeDir expands the ~ in file paths to the user's home directory.
func ExpandHomeDir(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
path = filepath.Join(homeDir, path[2:])
}
return path, nil
}
4 changes: 2 additions & 2 deletions scripts/convertSnapshotSchema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PGPASSWORD=$DB_PASSWORD psql -U $DB_USER -c "CREATE DATABASE $TEMP_DB_NAME;" ||

# Restore the snapshot dump into the temporary database
echo "Restoring snapshot into temporary database"
go run main.go restore-snapshot \
./bin/sidecar restore-snapshot \
--database.host=localhost \
--database.port=5432 \
--database.user=$DB_USER \
Expand All @@ -42,7 +42,7 @@ PGPASSWORD=$DB_PASSWORD psql -U $DB_USER -d $TEMP_DB_NAME -c "ALTER SCHEMA $INPU

# Create a new snapshot with the updated schema
echo "Creating new snapshot with updated schema"
go run main.go create-snapshot \
./bin/sidecar create-snapshot \
--database.host=localhost \
--database.port=5432 \
--database.user=$DB_USER \
Expand Down

0 comments on commit edf55ab

Please sign in to comment.