Files
web_server/backend/services/database.go
Adam French 759614e92d
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 3m34s
Add job application quick reference for storing profile links and experience
Auth-protected CRUD for personal info (LinkedIn, GitHub, etc.) and
experience entries, stored in the database so nothing sensitive is in
the public repo. Displayed as a categorized panel on the Job Applications page.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 12:38:53 +01:00

65 lines
1.1 KiB
Go

package services
import (
"fmt"
"adam-french.co.uk/backend/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type SQLConfig struct {
User string
Password string
DBName string
Host string
Port string
}
func connectToPostgreSQL(config *SQLConfig) (*gorm.DB, error) {
dsn := fmt.Sprintf(
"user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
config.User, config.Password, config.DBName, config.Host, config.Port,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return db, nil
}
func migrateDatabase(db *gorm.DB) error {
err := db.AutoMigrate(
&models.User{},
&models.Post{},
&models.Activity{},
&models.Favorite{},
&models.Rowing{},
&models.Message{},
&models.JobApplication{},
&models.JobAppReference{},
&models.Bookmark{},
)
if err != nil {
return err
}
return nil
}
func InitDatabase(config *SQLConfig) (*gorm.DB, error) {
db, err := connectToPostgreSQL(config)
if err != nil {
return nil, err
}
err = migrateDatabase(db)
if err != nil {
return nil, err
}
return db, nil
}