backend-service/internal/cli/db.go
ats-tech25 488be7b8ef feat(project): Initialize project structure and core components
- Add initial project scaffolding with Go module and project structure
- Create server and CLI entry points for application
- Implement Makefile with development and build commands
- Add `.env.example` with comprehensive configuration template
- Set up README.md with project documentation and setup instructions
- Configure basic dependencies for server, database, and CLI tools
- Establish internal package structure for models, services, and handlers
- Add initial configuration and environment management
- Prepare for HTTP server, CLI, and database integration
2025-11-05 15:06:07 +00:00

95 lines
2.1 KiB
Go

package cli
import (
"log"
"attune-heart-therapy/internal/config"
"attune-heart-therapy/internal/database"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
)
var dbCmd = &cobra.Command{
Use: "db",
Short: "Database management commands",
Long: "Commands for managing database operations",
}
var dbHealthCmd = &cobra.Command{
Use: "health",
Short: "Check database connectivity",
Long: "Check if the database connection is working properly",
Run: runDBHealth,
}
var dbSeedCmd = &cobra.Command{
Use: "seed",
Short: "Seed database with initial data",
Long: "Populate database with initial data",
Run: runDBSeed,
}
func runDBHealth(cmd *cobra.Command, args []string) {
// Load environment variables
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using system environment variables")
}
// Load configuration
cfg, err := config.Load()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// Initialize database connection
db, err := database.New(cfg)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer db.Close()
// Check database health
if err := db.Health(); err != nil {
log.Fatalf("Database health check failed: %v", err)
}
log.Println("Database connection is healthy")
}
func runDBSeed(cmd *cobra.Command, args []string) {
// Load environment variables
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using system environment variables")
}
// Load configuration
cfg, err := config.Load()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// Initialize database connection
db, err := database.New(cfg)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer db.Close()
// Run seeding
if err := db.Seed(); err != nil {
log.Fatalf("Failed to seed database: %v", err)
}
log.Println("Database seeding completed successfully")
}
func init() {
// Add subcommands to db command
dbCmd.AddCommand(dbHealthCmd)
dbCmd.AddCommand(dbSeedCmd)
// Add db command to root
rootCmd.AddCommand(dbCmd)
}