Download project at step 5 (.zip)

5 / 14

Step 5 · Build a live chat app

🤖 Give rooms a table

Chat happens in rooms, so rooms come first. A room is a row in the database, and you describe it with a model: add Room to models.go, and list it in Models.

Declare the room table in migrations.go as Shared. Rooms don't belong to one person, and the tests fail if a table has no declaration.

Note is the scaffold's example model. Leave it for now; removing it means changing the account page too.

Changing a model doesn't change the database. rastrillo migration generate compares the models with the migrations already written, and writes a new migration for the difference. The app applies it the next time it starts, which rastrillo dev does as soon as the file appears.

Check it worked

internal/chat/migrations/ has a new file, 0002_create_rooms.sql, and the dev server logs that it applied a migration.

🤖 These pages were written by an LLM, on the ideas, instruction, and editing of humans. AI-written text here is always marked, visibly — that's factor X.

models.go Edit

internal/chat/models.go
package chat

import "time"

// Models is every model rastrillo migration generate manages; migration
// check fails the gate when it and migrations/ disagree.
var Models = []any{
	&Note{},
	&Room{},
}

// Note is an example model: rename it, replace it, add more beside it.
// Query it through owned (auth.go), and never bind a request
// body onto a model; AGENTS.md says why.
type Note struct {
	ID        int64
	Owner     string `gorm:"index"`
	Title     string
	Body      string
	CreatedAt time.Time
	UpdatedAt time.Time
}

// Room is a place to talk. Admins make, rename and delete rooms.
type Room struct {
	ID        int64
	Name      string `gorm:"uniqueIndex"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

migrations.go Edit

internal/chat/migrations.go
package chat

import (
	"embed"

	"amadan.net/rastrillo/rastrillo/apitoken"
	"amadan.net/rastrillo/rastrillo/auth"
	"amadan.net/rastrillo/rastrillo/migrate"
	"amadan.net/rastrillo/rastrillo/passkey"
	"amadan.net/rastrillo/rastrillo/pow"
	"amadan.net/rastrillo/rastrillo/sessions"
	"amadan.net/rastrillo/rastrillo/signin"
	"amadan.net/rastrillo/rastrillo/totp"
)

//go:embed migrations/*.sql
var migrationFS embed.FS

// Schema is this app's own migrations, which migration generate and
// check diff against Models. Never edit one that has shipped.
var Schema = migrate.MustFromFS(migrationFS, "chat").
	Declare(migrate.Data{Name: "notes", Key: []string{"id"},
		Kind: migrate.Personal, Owner: migrate.Subject("owner"),
		Erase: migrate.EraseRow, Export: true}).
	Declare(migrate.Data{Name: "rooms", Key: []string{"id"},
		Kind: migrate.Shared})

// BootSchema is what boot applies. A subsystem's schema goes here, in the
// order it applies, and never in Schema: check would drop its tables.
var BootSchema = migrate.Merge(sessions.Schema, auth.Schema,
	pow.Schema, passkey.Schema, totp.Schema, signin.Schema,
	apitoken.Schema, Schema)
Terminal
rastrillo migration generate