evolveschema

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package evolveschema is the schema of a support desk, and the subject of docs/refactoring-a-database.md.

What makes this example different from the others is what is *not* here. The declaration below is the current state and only the current state — there is no v1 package, no v2 package, and no record in Go of what any column used to be called. That is deliberate, because it is what a real project looks like: the history lives in ../migrations, as files a runner has already applied to databases you cannot edit.

So the example is the pair. This file says what the schema *is*; the migration directory says how it got there; and pgtest/evolve_test.go replays the second and requires it to produce the first. Editing this file without adding a migration fails there, which is the only gate that can catch it — every file-comparison check in this repository would still pass.

The one thing that does survive a revision here is a rename hint. See email_address below.

Index

Constants

This section is empty.

Variables

View Source
var Customer = schema.Table("customers",
	schema.UUIDv7("id").PrimaryKey(),

	schema.Text("email_address").RenamedFrom("email").Unique().Searchable(),

	schema.Text("name").Searchable().Sortable(),
	schema.Timestamps(),
).
	Describe("Whoever a ticket is on behalf of.").
	Expose(schema.REST{
		Ops:             schema.OpCreate | schema.OpRead | schema.OpUpdate | schema.OpList,
		DefaultPageSize: 25,
		MaxPageSize:     100,
	})

Customer is who a ticket belongs to.

View Source
var SupportAgent = schema.Table("support_agents",
	schema.UUIDv7("id").PrimaryKey(),
	schema.Text("email").Unique(),
	schema.Text("name").Searchable().Sortable(),
	schema.Bool("active").Default(schema.Value(true)).Filterable(),
	schema.Timestamps(),
).
	RenamedFrom("agents").
	Describe("Someone who answers tickets.").
	Expose(schema.REST{Ops: schema.OpRead | schema.OpList})

SupportAgent is who a ticket is assigned to.

Revision 2 added the table as "agents"; revision 4 renamed it. The hint is the table-level form of the one on email_address above, and carries the same expiry: it is here for one release, and for as long as the document needs to point at it.

View Source
var Ticket = schema.Table("tickets",
	schema.UUIDv7("id").PrimaryKey(),
	schema.Ref("customer", Customer).OnDelete(schema.Cascade).Filterable().Expandable(),

	schema.Text("subject").Searchable().Sortable(),

	schema.Text("body").Searchable(),
	schema.Enum("status", "open", "pending", "closed").
		Default(schema.Value("open")).
		Filterable().
		Sortable(),

	schema.Enum("priority", "low", "normal", "high", "urgent").
		Default(schema.Value("normal")).
		Filterable().
		Sortable(),

	schema.Timestamps(),
).
	Index("customer_id", "status").
	Describe("One request from one customer.").
	Expose(schema.REST{
		Ops:             schema.OpCreate | schema.OpRead | schema.OpUpdate | schema.OpList,
		DefaultPageSize: 20,
		MaxPageSize:     100,
	})

Ticket is the table every revision in the history touched.

Functions

func SqlbProject

func SqlbProject() codegen.Project

SqlbProject tells `sqlb generate` what this example emits and where.

Go only, and no EjectDir: this example is about what happens to a schema over time, and example/blog is where the exit is kept current.

The generated code matters here for one reason beyond completeness. A schema edit changes two things that drift independently — the code sqlb writes, and the API that code serves — and the repository gates them separately. `generate-check` catches the first, `impact-check` the second, and revision 4 of the history below is the case where they disagree: a rename is a clean migration and a broken client at once.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL