mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Use golang migrate for migrations
This commit is contained in:
@@ -2,9 +2,11 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lemma/internal/db"
|
||||
"lemma/internal/logging"
|
||||
"lemma/internal/secrets"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -12,7 +14,8 @@ import (
|
||||
|
||||
// Config holds the configuration for the application
|
||||
type Config struct {
|
||||
DBPath string
|
||||
DBURL string
|
||||
DBType db.DBType
|
||||
WorkDir string
|
||||
StaticPath string
|
||||
Port string
|
||||
@@ -31,7 +34,7 @@ type Config struct {
|
||||
// DefaultConfig returns a new Config instance with default values
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
DBPath: "./lemma.db",
|
||||
DBURL: "sqlite://lemma.db",
|
||||
WorkDir: "./data",
|
||||
StaticPath: "../app/dist",
|
||||
Port: "8080",
|
||||
@@ -65,6 +68,31 @@ func (c *Config) Redact() *Config {
|
||||
return &redacted
|
||||
}
|
||||
|
||||
// ParseDBURL parses a database URL and returns the driver name and data source
|
||||
func ParseDBURL(dbURL string) (db.DBType, string, error) {
|
||||
if strings.HasPrefix(dbURL, "sqlite://") || strings.HasPrefix(dbURL, "sqlite3://") {
|
||||
|
||||
path := strings.TrimPrefix(dbURL, "sqlite://")
|
||||
path = strings.TrimPrefix(path, "sqlite3://")
|
||||
|
||||
if path == ":memory:" {
|
||||
return db.DBTypeSQLite, path, nil
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Clean(path)
|
||||
}
|
||||
return db.DBTypeSQLite, path, nil
|
||||
}
|
||||
|
||||
// Try to parse as postgres URL
|
||||
if strings.HasPrefix(dbURL, "postgres://") || strings.HasPrefix(dbURL, "postgresql://") {
|
||||
return db.DBTypePostgres, dbURL, nil
|
||||
}
|
||||
|
||||
return "", "", fmt.Errorf("unsupported database URL format: %s", dbURL)
|
||||
}
|
||||
|
||||
// LoadConfig creates a new Config instance with values from environment variables
|
||||
func LoadConfig() (*Config, error) {
|
||||
config := DefaultConfig()
|
||||
@@ -73,8 +101,13 @@ func LoadConfig() (*Config, error) {
|
||||
config.IsDevelopment = env == "development"
|
||||
}
|
||||
|
||||
if dbPath := os.Getenv("LEMMA_DB_PATH"); dbPath != "" {
|
||||
config.DBPath = dbPath
|
||||
if dbURL := os.Getenv("LEMMA_DB_URL"); dbURL != "" {
|
||||
dbType, dataSource, err := ParseDBURL(dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.DBURL = dataSource
|
||||
config.DBType = dbType
|
||||
}
|
||||
|
||||
if workDir := os.Getenv("LEMMA_WORKDIR"); workDir != "" {
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestDefaultConfig(t *testing.T) {
|
||||
got interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{"DBPath", cfg.DBPath, "./lemma.db"},
|
||||
{"DBPath", cfg.DBURL, "./lemma.db"},
|
||||
{"WorkDir", cfg.WorkDir, "./data"},
|
||||
{"StaticPath", cfg.StaticPath, "../app/dist"},
|
||||
{"Port", cfg.Port, "8080"},
|
||||
@@ -81,8 +81,8 @@ func TestLoad(t *testing.T) {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
|
||||
if cfg.DBPath != "./lemma.db" {
|
||||
t.Errorf("default DBPath = %v, want %v", cfg.DBPath, "./lemma.db")
|
||||
if cfg.DBURL != "./lemma.db" {
|
||||
t.Errorf("default DBPath = %v, want %v", cfg.DBURL, "./lemma.db")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -122,7 +122,7 @@ func TestLoad(t *testing.T) {
|
||||
expected interface{}
|
||||
}{
|
||||
{"IsDevelopment", cfg.IsDevelopment, true},
|
||||
{"DBPath", cfg.DBPath, "/custom/db/path.db"},
|
||||
{"DBPath", cfg.DBURL, "/custom/db/path.db"},
|
||||
{"WorkDir", cfg.WorkDir, "/custom/work/dir"},
|
||||
{"StaticPath", cfg.StaticPath, "/custom/static/path"},
|
||||
{"Port", cfg.Port, "3000"},
|
||||
|
||||
@@ -28,9 +28,9 @@ func initSecretsService(cfg *Config) (secrets.Service, error) {
|
||||
|
||||
// initDatabase initializes and migrates the database
|
||||
func initDatabase(cfg *Config, secretsService secrets.Service) (db.Database, error) {
|
||||
logging.Debug("initializing database", "path", cfg.DBPath)
|
||||
logging.Debug("initializing database", "path", cfg.DBURL)
|
||||
|
||||
database, err := db.Init(cfg.DBPath, secretsService)
|
||||
database, err := db.Init(cfg.DBURL, secretsService)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user