test: drive the whole application over HTTP, and serialise migrations

The behaviour suite in internal/behaviour installs the application into an
empty database through the web installer, signs in, walks every flow, and then
replaces the process to confirm the state was persisted rather than held in
memory. It found four defects, each fixed here or in the preceding commit:

- Migrate was not safe to run from two instances at once. CREATE TABLE IF NOT
  EXISTS is not race free in PostgreSQL and collided on the pg_type unique
  index, and two replicas could apply the same file. The run now holds a
  session-level advisory lock, so a rolling deployment waits instead of failing.

- Bootstrap refused ownership whenever a tenant held any member, so installing
  with the demonstration seed and then setting a local administrator password
  produced an account that could never reach the console. It now grants
  ownership only while the tenant has no owner, which still never takes a tenant
  over from one that does.

Three of the fixes are covered by store-level regression tests in the same
commit: the ownership rule, the duplicate tenant slug conflict, and concurrent
migration runs.

The suite is skipped by go test ./... because it needs a scratch database and
the Google credentials for the installer. Run it with make behaviour-test, which
also resets the database afterwards. make test no longer inherits the database
variables, so unit runs need nothing.

Completes the earlier correction to the docs: the role matrix lives in the
database rather than in internal/role, and a local administrator claims the
system administrator role when no Google account has.
This commit is contained in:
Daniel Noulet 2026-10-05 19:37:09 +02:00
parent c7c81fb65c
commit 8d8970d5a7
18 changed files with 3050 additions and 4 deletions

View file

@ -32,7 +32,45 @@ func Open(ctx context.Context, databaseURL string) (*sql.DB, error) {
return db, nil
}
// migrationLockKey serialises migration runs between instances of this service.
// It is deliberately distinct from the advisory lock used to claim the first
// system administrator.
const migrationLockKey int64 = 554628171918
// querier is the subset of database/sql that migrations need, satisfied by both
// *sql.DB and *sql.Conn.
type querier interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}
// Migrate brings the schema up to date.
//
// The whole run holds a session-level advisory lock, because PostgreSQL's
// CREATE TABLE IF NOT EXISTS is not race free: two instances starting against an
// empty database at the same time can collide on the pg_type unique index and one
// of them fails. The same race would let two instances apply the same migration
// concurrently. Rolling a deployment with more than one replica is enough to
// trigger it.
func Migrate(ctx context.Context, db *sql.DB) error {
conn, err := db.Conn(ctx)
if err != nil {
return fmt.Errorf("reserve migration connection: %w", err)
}
defer conn.Close()
if _, err := conn.ExecContext(ctx, `SELECT pg_advisory_lock($1)`, migrationLockKey); err != nil {
return fmt.Errorf("lock migrations: %w", err)
}
// The unlock runs on a context detached from the caller's, so an application
// shutting down mid-migration still releases the lock for the next instance.
defer func() {
_, _ = conn.ExecContext(context.WithoutCancel(ctx), `SELECT pg_advisory_unlock($1)`, migrationLockKey)
}()
return applyMigrations(ctx, conn)
}
func applyMigrations(ctx context.Context, db querier) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations (
version text PRIMARY KEY,
applied_at timestamptz NOT NULL DEFAULT now()