Skip to content

Commit

Permalink
Add lunar replace command
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvogt committed Apr 22, 2024
1 parent ee8a338 commit 4b7f4eb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/replace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"

"github.com/leonvogt/lunar/internal"
"github.com/spf13/cobra"
)

var (
replaceCmd = &cobra.Command{
Use: "replace",
Short: "Replaces a snapshot",
Run: func(_ *cobra.Command, args []string) {
replaceSnapshot(args)
},
}
)

func replaceSnapshot(args []string) {
if !internal.DoesConfigExist() {
fmt.Println("There seems to be no configuration file. Please run 'lunar init' first.")
return
}

if len(args) != 1 {
fmt.Println("Please provide a snapshot name.")
return
}

snapshotName := args[0]
config, _ := internal.ReadConfig()
snapshotDatabaseName := internal.SnapshotDatabaseName(config.DatabaseName, snapshotName)

fmt.Println("Removing snapshot ", snapshotName)
internal.TerminateAllCurrentConnections(snapshotDatabaseName)
internal.DropDatabase(snapshotDatabaseName)

fmt.Println("Creating snapshot for database", config.DatabaseName, "with name", snapshotName)
internal.TerminateAllCurrentConnections(snapshotDatabaseName)
internal.TerminateAllCurrentConnections(config.DatabaseName)
internal.CreateSnapshot(config.DatabaseName, snapshotDatabaseName)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ func init() {
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(restoreCmd)
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(replaceCmd)
}
33 changes: 33 additions & 0 deletions tests/replace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tests

import (
"os/exec"
"testing"

"github.com/leonvogt/lunar/internal"
)

func TestReplace(t *testing.T) {
SetupTestDatabase()

// Create a snapshot
command := "go run ../main.go snapshot production"
err := exec.Command("sh", "-c", command).Run()
if err != nil {
t.Errorf("Error: %v", err)
}

if !DoesDatabaseExists("lunar_snapshot__lunar_test__production") {
t.Errorf("Expected database `lunar_snapshot__lunar_test__production` to exist - but it does not")
}

// Replace the snapshot
command = "go run ../main.go replace production"
err = exec.Command("sh", "-c", command).Run()
if err != nil {
t.Errorf("Error: %v", err)
}

// Cleanup
internal.DropDatabase("lunar_snapshot__lunar_test__production")
}

0 comments on commit 4b7f4eb

Please sign in to comment.