Skip to content

Commit fb6ebdd

Browse files
committed
fix(deployment):updating sqlite ro db issue)
1 parent 9422cfa commit fb6ebdd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

deployments/Dockerfile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
1919
--mount=type=cache,target=/go/pkg/mod \
2020
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o test-charger ./cmd/test-charger
2121

22-
# Create data directory with proper permissions
23-
RUN mkdir -p /app/data && chmod 755 /app/data
24-
2522
# Use distroless for minimal, secure image
2623
FROM gcr.io/distroless/static-debian12:nonroot
2724

@@ -36,9 +33,6 @@ WORKDIR /app
3633
COPY --from=builder --chown=nonroot:nonroot /app/simulator /app/test-charger ./
3734
COPY --from=builder --chown=nonroot:nonroot /app/configs ./configs
3835

39-
# Copy data directory with proper ownership
40-
COPY --from=builder --chown=nonroot:nonroot /app/data ./data
41-
4236
EXPOSE 8080
4337

4438
CMD ["./simulator"]

internal/infrastructure/storage/database.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package storage
22

33
import (
4+
"os"
5+
"path/filepath"
6+
47
"github.com/glebarez/sqlite" // Pure-Go SQLite driver
58
"gorm.io/gorm"
69
)
@@ -19,13 +22,18 @@ type SQLiteDB struct {
1922

2023
// NewSQLiteDB creates a new SQLite database connection
2124
func NewSQLiteDB(path string) (Database, error) {
25+
// Ensure the directory exists before creating the database file
26+
if err := ensureDirectoryExists(path); err != nil {
27+
return nil, err
28+
}
29+
2230
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
2331
if err != nil {
2432
return nil, err
2533
}
2634

2735
sqliteDB := &SQLiteDB{db: db}
28-
36+
2937
// Auto-migrate tables
3038
if err := sqliteDB.Migrate(); err != nil {
3139
return nil, err
@@ -34,6 +42,21 @@ func NewSQLiteDB(path string) (Database, error) {
3442
return sqliteDB, nil
3543
}
3644

45+
// ensureDirectoryExists creates the directory for the database file if it doesn't exist
46+
func ensureDirectoryExists(dbPath string) error {
47+
// Skip directory creation for special SQLite paths
48+
if dbPath == ":memory:" || dbPath == "" {
49+
return nil
50+
}
51+
52+
// Get the directory from the database file path
53+
dir := filepath.Dir(dbPath)
54+
55+
// Create directory with proper permissions (0755)
56+
// This will work even if the directory already exists
57+
return os.MkdirAll(dir, 0755)
58+
}
59+
3760
// GetDB returns the GORM database instance
3861
func (s *SQLiteDB) GetDB() *gorm.DB {
3962
return s.db

0 commit comments

Comments
 (0)