Skip to content

Commit 1a461b4

Browse files
committed
Add GORM models and database initialization
1 parent 4ef5621 commit 1a461b4

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module tinychess
33
go 1.22.2
44

55
require (
6-
github.com/google/uuid v1.5.0 // indirect
7-
github.com/notnil/chess v1.10.0 // indirect
6+
gorm.io/driver/postgres v1.5.4
7+
gorm.io/gorm v1.25.5
8+
github.com/google/uuid v1.5.0
9+
github.com/notnil/chess v1.10.0
810
)
11+

internal/storage/db.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package storage
2+
3+
import (
4+
"gorm.io/driver/postgres"
5+
"gorm.io/gorm"
6+
)
7+
8+
// New initializes the database connection and performs migrations.
9+
func New(dsn string) (*gorm.DB, error) {
10+
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
11+
if err != nil {
12+
return nil, err
13+
}
14+
if err := db.AutoMigrate(&Game{}, &GameSession{}, &UserSession{}, &Move{}); err != nil {
15+
return nil, err
16+
}
17+
return db, nil
18+
}

internal/storage/models.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package storage
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
// Game represents a chess game.
10+
type Game struct {
11+
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey"`
12+
FEN string
13+
PGN string
14+
CreatedAt time.Time
15+
UpdatedAt time.Time
16+
Sessions []GameSession
17+
Moves []Move
18+
}
19+
20+
// GameSession represents an instance of a game session.
21+
type GameSession struct {
22+
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey"`
23+
GameID uuid.UUID `gorm:"type:uuid;index"`
24+
Game Game `gorm:"constraint:OnDelete:CASCADE;"`
25+
CreatedAt time.Time
26+
UpdatedAt time.Time
27+
Users []UserSession
28+
}
29+
30+
// UserSession links a user to a game session.
31+
type UserSession struct {
32+
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey"`
33+
GameID uuid.UUID `gorm:"type:uuid;index"`
34+
GameSessionID uuid.UUID `gorm:"type:uuid;index"`
35+
Color string
36+
Role string
37+
CreatedAt time.Time
38+
UpdatedAt time.Time
39+
}
40+
41+
// Move stores a single move in a game.
42+
type Move struct {
43+
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey"`
44+
GameID uuid.UUID `gorm:"type:uuid;index"`
45+
Number int
46+
UCI string
47+
CreatedAt time.Time
48+
}

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"flag"
55
"log"
66
"net/http"
7+
"os"
78

89
"tinychess/internal/game"
910
"tinychess/internal/handlers"
1011
"tinychess/internal/logging"
12+
"tinychess/internal/storage"
1113
"tinychess/internal/templates"
1214
)
1315

@@ -18,6 +20,12 @@ func main() {
1820

1921
templates.SetCommit(commit)
2022

23+
if dsn := os.Getenv("DATABASE_URL"); dsn != "" {
24+
if _, err := storage.New(dsn); err != nil {
25+
log.Fatalf("failed to initialize database: %v", err)
26+
}
27+
}
28+
2129
// Initialize game hub
2230
hub := game.NewHub()
2331

0 commit comments

Comments
 (0)