I am batman

This commit is contained in:
2026-07-12 20:26:16 -04:00
commit 5a3a6357cf
37 changed files with 2938 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
package database
import (
"log"
"os"
"OpsMastery.v5/internal/models"
_ "github.com/joho/godotenv/autoload"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Global GORM DB instance
// This variable holds the database connection and is shared across the app.
var db *gorm.DB
// DB returns the global db instance
// Use this function to get the database connection anywhere in your codebase.
func DB() *gorm.DB {
return db
}
// Init initializes the global db instance and runs automigrate
// This function sets up the database connection using environment variables,
// and automatically migrates all models to ensure the schema is up to date.
func Init() {
// Load environment variables for database connection.
// These should be set in your .env file or container environment.
dbHost := os.Getenv("BLUEPRINT_DB_HOST") // Database host (e.g., psql_bp or localhost)
dbUser := os.Getenv("BLUEPRINT_DB_USERNAME") // Database username
dbPassword := os.Getenv("BLUEPRINT_DB_PASSWORD") // Database password
dbName := os.Getenv("BLUEPRINT_DB_DATABASE") // Database name
dbPort := os.Getenv("BLUEPRINT_DB_PORT") // Database port (usually 5432 for Postgres)
dbSSLMode := "disable" // SSL mode for local/dev; can be set via env for prod
dbSchema := os.Getenv("BLUEPRINT_DB_SCHEMA") // Database schema (e.g., public)
// Build the Data Source Name (DSN) string for Postgres connection.
// This string contains all connection parameters.
dsn := "host=" + dbHost +
" user=" + dbUser +
" password=" + dbPassword +
" dbname=" + dbName +
" port=" + dbPort +
" sslmode=" + dbSSLMode +
" search_path=" + dbSchema
// Attempt to open a connection to the database using GORM and the DSN.
var err error
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
// If connection fails, log the error and exit the application.
log.Fatalf("failed to connect to database: %v", err)
}
// Automigrate all models.
// This will create or update tables for User, Ticket, Client, Chat, and ChatMessage.
// It ensures your database schema matches your Go models.
if err := db.AutoMigrate(
&models.User{},
&models.Ticket{},
&models.TaskSection{},
&models.Tasks{},
&models.Client{},
&models.Chat{},
&models.ChatMessage{},
); err != nil {
// If migration fails, log the error and exit the application.
log.Fatalf("failed to auto migrate models: %v", err)
}
}

View File

@@ -0,0 +1,100 @@
package database
// import (
// "context"
// "log"
// "testing"
// "time"
// "github.com/testcontainers/testcontainers-go"
// "github.com/testcontainers/testcontainers-go/modules/postgres"
// "github.com/testcontainers/testcontainers-go/wait"
// )
// func mustStartPostgresContainer() (func(context.Context, ...testcontainers.TerminateOption) error, error) {
// var (
// dbName = "database"
// dbPwd = "password"
// dbUser = "user"
// )
// dbContainer, err := postgres.Run(
// context.Background(),
// "postgres:latest",
// postgres.WithDatabase(dbName),
// postgres.WithUsername(dbUser),
// postgres.WithPassword(dbPwd),
// testcontainers.WithWaitStrategy(
// wait.ForLog("database system is ready to accept connections").
// WithOccurrence(2).
// WithStartupTimeout(5*time.Second)),
// )
// if err != nil {
// return nil, err
// }
// database = dbName
// password = dbPwd
// username = dbUser
// dbHost, err := dbContainer.Host(context.Background())
// if err != nil {
// return dbContainer.Terminate, err
// }
// dbPort, err := dbContainer.MappedPort(context.Background(), "5432/tcp")
// if err != nil {
// return dbContainer.Terminate, err
// }
// host = dbHost
// port = dbPort.Port()
// return dbContainer.Terminate, err
// }
// func TestMain(m *testing.M) {
// teardown, err := mustStartPostgresContainer()
// if err != nil {
// log.Fatalf("could not start postgres container: %v", err)
// }
// m.Run()
// if teardown != nil && teardown(context.Background()) != nil {
// log.Fatalf("could not teardown postgres container: %v", err)
// }
// }
// func TestNew(t *testing.T) {
// srv := New()
// if srv == nil {
// t.Fatal("New() returned nil")
// }
// }
// func TestHealth(t *testing.T) {
// srv := New()
// stats := srv.Health()
// if stats["status"] != "up" {
// t.Fatalf("expected status to be up, got %s", stats["status"])
// }
// if _, ok := stats["error"]; ok {
// t.Fatalf("expected error not to be present")
// }
// if stats["message"] != "It's healthy" {
// t.Fatalf("expected message to be 'It's healthy', got %s", stats["message"])
// }
// }
// func TestClose(t *testing.T) {
// srv := New()
// if srv.Close() != nil {
// t.Fatalf("expected Close() to return nil")
// }
// }