File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed
internal/infrastructure/storage Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff 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
2623FROM gcr.io/distroless/static-debian12:nonroot
2724
@@ -36,9 +33,6 @@ WORKDIR /app
3633COPY --from=builder --chown=nonroot:nonroot /app/simulator /app/test-charger ./
3734COPY --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-
4236EXPOSE 8080
4337
4438CMD ["./simulator" ]
Original file line number Diff line number Diff line change 11package storage
22
33import (
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
2124func 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
3861func (s * SQLiteDB ) GetDB () * gorm.DB {
3962 return s .db
You can’t perform that action at this time.
0 commit comments