Skip to content

Commit bc9cb04

Browse files
committed
fix target path not checking
1 parent 174b8e3 commit bc9cb04

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

cmd/cronosd/cmd/patch_db.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"strings"
78
"time"
@@ -241,6 +242,14 @@ Examples:
241242
return fmt.Errorf("--target-path must reference a *.db directory (got %q)", dbTargetPath)
242243
}
243244

245+
// Verify target database exists
246+
if _, err := os.Stat(cleanTargetPath); err != nil {
247+
if os.IsNotExist(err) {
248+
return fmt.Errorf("target database does not exist: %s (the target database must already exist before patching; use the migrate command to create a new database)", cleanTargetPath)
249+
}
250+
return fmt.Errorf("failed to access target database: %w", err)
251+
}
252+
244253
logger.Info("Patching database",
245254
"database", dbName,
246255
"target_path", dbTargetPath,

cmd/cronosd/cmd/patch_db_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"os"
45
"path/filepath"
56
"testing"
67

@@ -83,6 +84,60 @@ func TestTargetPathValidation(t *testing.T) {
8384
}
8485
}
8586

87+
// TestTargetPathExistence tests that patching fails if target database doesn't exist
88+
func TestTargetPathExistence(t *testing.T) {
89+
// Create a temporary directory for testing
90+
tmpDir := t.TempDir()
91+
92+
// Create an existing test database
93+
existingDB := filepath.Join(tmpDir, "existing.db")
94+
err := os.MkdirAll(existingDB, 0o755)
95+
require.NoError(t, err)
96+
97+
tests := []struct {
98+
name string
99+
targetPath string
100+
shouldError bool
101+
errorString string
102+
}{
103+
{
104+
name: "existing database - allowed",
105+
targetPath: existingDB,
106+
shouldError: false,
107+
},
108+
{
109+
name: "non-existing database - rejected",
110+
targetPath: filepath.Join(tmpDir, "nonexistent.db"),
111+
shouldError: true,
112+
errorString: "target database does not exist",
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
// Simulate the existence check from patch_db.go
119+
cleanTargetPath := filepath.Clean(tt.targetPath)
120+
var err error
121+
if _, statErr := os.Stat(cleanTargetPath); statErr != nil {
122+
if os.IsNotExist(statErr) {
123+
err = &targetExistenceError{path: cleanTargetPath}
124+
} else {
125+
err = statErr
126+
}
127+
}
128+
129+
if tt.shouldError {
130+
require.Error(t, err)
131+
if tt.errorString != "" {
132+
require.Contains(t, err.Error(), tt.errorString)
133+
}
134+
} else {
135+
require.NoError(t, err)
136+
}
137+
})
138+
}
139+
}
140+
86141
// targetPathError is a helper type to simulate the error from patch_db.go
87142
type targetPathError struct {
88143
path string
@@ -91,3 +146,12 @@ type targetPathError struct {
91146
func (e *targetPathError) Error() string {
92147
return "when patching multiple databases, --target-path must be a data directory (e.g., ~/.cronos/data), not a *.db file path (got \"" + e.path + "\"); remove the .db suffix"
93148
}
149+
150+
// targetExistenceError is a helper type to simulate the existence error from patch_db.go
151+
type targetExistenceError struct {
152+
path string
153+
}
154+
155+
func (e *targetExistenceError) Error() string {
156+
return "target database does not exist: " + e.path + " (the target database must already exist before patching; use the migrate command to create a new database)"
157+
}

0 commit comments

Comments
 (0)