Skip to content

Commit 7f48565

Browse files
authored
Merge pull request #28 from dustywusty/codex/create-schemas-for-persistent-game-state
Add GORM models for persistent game storage
2 parents 4ef5621 + 552e376 commit 7f48565

File tree

7 files changed

+114
-3
lines changed

7 files changed

+114
-3
lines changed

bin/tinychess

4.42 MB
Binary file not shown.

go.mod

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ 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+
github.com/google/uuid v1.5.0
7+
github.com/notnil/chess v1.10.0
8+
gorm.io/driver/postgres v1.5.4
9+
gorm.io/gorm v1.25.5
10+
)
11+
12+
require (
13+
github.com/jackc/pgpassfile v1.0.0 // indirect
14+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
15+
github.com/jackc/pgx/v5 v5.4.3 // indirect
16+
github.com/jinzhu/inflection v1.0.0 // indirect
17+
github.com/jinzhu/now v1.1.5 // indirect
18+
golang.org/x/crypto v0.14.0 // indirect
19+
golang.org/x/text v0.13.0 // indirect
820
)

go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
34
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
6+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
7+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
8+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
9+
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
10+
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
11+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
12+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
13+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
14+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
415
github.com/notnil/chess v1.10.0 h1:RR3MgS9G6zZmJ+VPTJolyxdaIgxoUPyUUY+2iaw35G0=
516
github.com/notnil/chess v1.10.0/go.mod h1:cRuJUIBFq9Xki05TWHJxHYkC+fFpq45IWwk94DdlCrA=
17+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
18+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
19+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
20+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
22+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
23+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
24+
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
25+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27+
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
28+
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
29+
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
30+
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

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

tmp/build-errors.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1
1+
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1

0 commit comments

Comments
 (0)