init
This commit is contained in:
69
cmd/migrate/main.go
Normal file
69
cmd/migrate/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"accounting-app/internal/config"
|
||||
"accounting-app/internal/database"
|
||||
"accounting-app/internal/models"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load .env file from project root (try multiple locations)
|
||||
envPaths := []string{
|
||||
".env", // Current directory
|
||||
"../.env", // Parent directory (when running from backend/)
|
||||
"../../.env", // Two levels up (when running from backend/cmd/migrate/)
|
||||
filepath.Join("..", "..", ".env"), // Explicit path
|
||||
}
|
||||
|
||||
for _, envPath := range envPaths {
|
||||
if err := godotenv.Load(envPath); err == nil {
|
||||
log.Printf("Loaded environment from: %s", envPath)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Load configuration
|
||||
cfg := config.Load()
|
||||
|
||||
// Initialize database connection
|
||||
db, err := database.Initialize(
|
||||
cfg.DBHost,
|
||||
cfg.DBPort,
|
||||
cfg.DBUser,
|
||||
cfg.DBPassword,
|
||||
cfg.DBName,
|
||||
cfg.DBCharset,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// Get underlying SQL DB for cleanup
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get underlying database: %v", err)
|
||||
}
|
||||
defer sqlDB.Close()
|
||||
|
||||
log.Println("Starting database migration...")
|
||||
|
||||
// Run auto migration for all models
|
||||
if err := db.AutoMigrate(models.AllModels()...); err != nil {
|
||||
log.Fatalf("Failed to run migrations: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Database migration completed successfully!")
|
||||
|
||||
// Initialize system categories (refund and reimbursement)
|
||||
log.Println("Initializing system categories...")
|
||||
if err := models.InitSystemCategories(db); err != nil {
|
||||
log.Fatalf("Failed to initialize system categories: %v", err)
|
||||
}
|
||||
|
||||
log.Println("System categories initialized successfully!")
|
||||
}
|
||||
Reference in New Issue
Block a user