Skip to content

Data migrations that reach every deployment: start-up runs retired SQL copies - #2805

Closed
peterdrier wants to merge 17 commits into
mainfrom
claude/project-thread-9lbtzy
Closed

peterdrier wants to merge 17 commits into
mainfrom
claude/project-thread-9lbtzy

Conversation

@peterdrier

@peterdrier peterdrier commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Requested by Peter · project thread

What

Other groups run Humans. Nobody presses our Apply button on their database, so a retired data migration never ran there and the drop that follows loses their data. This makes a retired copy run by itself, in order, on every deployment.

  • Start-up (SectionMigrationRunner, DatabaseMigrationHostedService): applies migrations one at a time with public EF services only, never undoing one; holds a drop that a retired SQL copy names as its Before until the copy has run; runs the copy in one transaction with one audit entry per changed row. If the chain fails or cannot move, start-up stops and the app serves only a page saying what is wrong (StartupHaltPage, /health/live stays 200). No retry.
  • SQL copies (ISectionSqlCopies, SqlCopy): pending query, statements, declared tables, audit actions, After, Before, AfterCopy, Retired, Reload. Until retired a copy shows on /Debug/DataMigrations and Apply runs the same statements.
  • Process: docs/architecture/database-changes.md and Debug.md describe move, retire, drop with SQL copies. Atoms no-data-backfills and no-startup-guards each name their one exception; removed-section-migrations and data-migration-hook-gotchas updated. The agent hook blocks raw data SQL outside a repository. SqlCopyRegistrationTests fails the build when a later migration changes a table a copy uses.
  • First case: governance.body-links-from-teams, teams.retire-governance-bodies and teams.remove-governance-body-roles are now SQL copies under the same keys. None is retired. Production reports all three done (read 2026-11-29), so the button shows nothing pending there. Retire the done governance data migrations (PR 5b) #2775 is then redone as "mark retired, delete the fallback".

Design: docs/superpowers/specs/2026-11-29-data-migrations-for-other-deployments-design.md. Not in this PR: code migrations at start-up, checkpoints, the other SQL-shaped copies.

Why

Peter, 2026-11-28/29: the manual flow only proves something about our database. He chose SQL copies that start-up runs before the drop; stopping start-up is right when going on would break data; never a background retry.

Existing surface checked

New public surface in Humans.Base, for Peter's approval: ISectionSqlCopies, SqlCopy, MigrationStep, SectionMigrationsHistory.NameFor. ISectionDataMigrations/DataMigration could not carry it: start-up needs statements it can run without the app's services, and the migrations each copy sits between. Removed: ITeamManagementService.RetireGovernanceBodyTeamsAsync, RemoveGovernanceBodyRolesAsync, and Governance's copy-from-Teams code with its ITeamService dependency.

Checklist

  • Review tier stated: tier:thorough (start-up, migrations, cross-section contracts).
  • No migration edited by hand: no migration in this PR. The only hand-written migrations are test-only chains in Humans.Integration.Tests.
  • No NOT NULL column added to an existing table.
  • Section labeled: Base, Debug, Teams, Governance.
  • Targeting main on peterdrier/Humans, current main merged in.
  • Issue refs are qualified.
  • New project rule? four atoms updated, INDEX lines in the same commits.
  • Reuse-first checked: see above.
  • Build + test pass locally: full dotnet test Humans.slnx; integration Infrastructure (start-up chains, stop test, 28 sections from empty) and GovernanceBodyCopiesTests against Postgres 16.
  • Nav coverage no new page. The halt page was looked at on the running app.

https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd


Generated by Claude Code

Problem statement and plan. Nothing built.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
@coolify-nuc-humans

coolify-nuc-humans Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

The preview deployment for humans-qa is ready. 🟢

Open Preview | Open Build Logs | Open Application Logs

Last updated at: 2026-11-29 13:55:39 CET

@peterdrier peterdrier self-assigned this Nov 29, 2026
claude and others added 8 commits November 29, 2026 11:59
…ckpoint stamp, nine migrations, simpler alternative)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
No empty migration per copy: a copy names what it runs after and the drop it
runs before. Stops show a page. No date for the first checkpoint.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
EF's MigrateAsync(target) reverts every applied migration above the
target, so start-up could not stop a section in front of a drop. The
runner now applies each pending migration itself, in its own transaction
under EF's migration lock, and can stop before a named one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
… when it cannot

A retired SqlCopy names the migrations it runs after and the drop it runs
before. Start-up advances sections in order, stops a section in front of
such a drop, runs every copy that is ready in one transaction each, audits
the rows it moved, and goes on. A copy with nothing pending changes
nothing and takes no snapshot. A failing copy, a round that moves nothing,
or any failure after the database changed is a StartupHaltException: the
app is torn down and a bare one serves a 503 page saying what stopped,
with /health/live healthy so it replaces the old version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
A registered SQL copy appears on /Debug/DataMigrations like any data migration:
its pending query fills the preview, Apply runs the same statements start-up
will run elsewhere, audits each changed row as the admin and lets the section
reload its cache. The agent hook now also blocks raw data SQL outside a
repository, so a copy cannot be written as a side door.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
A SQL copy declares the tables it touches. SqlCopyRegistrationTests fails the
build when a key is reused, a named migration or copy does not exist, the SQL
uses an undeclared table, or a later migration changes a declared table without
the copy being moved along. The rule atoms name the two exceptions (start-up
runs retired copies; start-up stops when the chain cannot go on), and
database-changes.md and Debug.md describe the move, retire, drop steps as built.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
governance.body-links-from-teams, teams.retire-governance-bodies and
teams.remove-governance-body-roles keep their keys and stay on the data
migrations page, but are now SQL copies, so start-up can run them on other
deployments once they retire. Their service methods go; Governance no longer
calls ITeamService. A copy statement may return target_type and target_id for
its audit entry. Tested against the real tables in GovernanceBodyCopiesTests.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
@peterdrier peterdrier changed the title Design: data migrations that reach every deployment Data migrations that reach every deployment: start-up runs retired SQL copies Nov 29, 2026
@peterdrier
peterdrier marked this pull request as ready for review November 29, 2026 18:22
@peterdrier

Copy link
Copy Markdown
Owner Author

@codex review


Generated by Claude Code

@peterdrier peterdrier added the tier:thorough Deep review, high complexity label Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T18:25:19.738877Z 2f4d8db Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2f4d8db0e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown

Reviewed commit 2f4d8db. 5 inline finding(s) posted.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

PR Surface Report

Compared 3bbd5dde...7d82e694. Scored with reforge 0.34.0.

Summary: 43 changed file(s) | EF migrations: 0 added file(s), max 0/1 per context

Reforge Surface Score

metric base head delta
total 76639 76741 +102
surface 17591 17607 +16

Section Deltas

section base head delta
Base 4901 5094 +193
Teams 3703 3626 -77
Governance 2444 2420 -24
Web 2374 2384 +10

Section Size & Complexity Deltas

section LOC delta files delta classes delta cognitive p95 delta
Base +475 +3 +6 0
Debug +7 0 0 0
Governance -9 +1 +1 0
Teams -3 +1 +1 -1
Web +44 +1 +1 0
Corpus +514 +6 +9 0

Corpus is the solution-wide rollup (same source as Corpus Size & Complexity below), not a sum of the section rows above - cognitive p95 in particular doesn't compose across sections.

Rule Deltas

rule base head delta
sectionSize 9450 9520 +70
classSize 6975 6935 -40
methodCount 19419 19449 +30
methodComplexity 22058 22084 +26
exportedReadMethod 2852 2864 +12
internalWiderThanUse 938 946 +8
crossSectionReadInterface 4566 4560 -6
exportedUnusedExternally 776 778 +2

Corpus Size & Complexity

metric base head delta
locProd 201358 201872 +514
classes 3656 3665 +9
methods 9041 9048 +7
files 2552 2558 +6
interfaces 376 377 +1

At head: largest class Service, most complex method Service.BookOneTransferAsync.

Published Write Surface

15 of 57 sections publish write capability, 47 interfaces (0).

Interface Surface

New interfaces

  • ISectionSqlCopies (src/Humans.Base/Interfaces/ISectionSqlCopies.cs)

Diff Size

bucket added deleted
code 794 244
tests 811 118
docs 317 19
other 53 5

New Files

  • docs/architecture/database-changes.md
  • docs/superpowers/specs/2026-11-29-data-migrations-for-other-deployments-design.md
  • src/Humans.Base/Hosting/SqlCopyRunner.cs
  • src/Humans.Base/Hosting/StartupHaltException.cs
  • src/Humans.Base/Interfaces/ISectionSqlCopies.cs
  • src/Humans.Web/Hosting/StartupHaltPage.cs
  • src/Sections/Humans.Governance/Data/GovernanceBodyLinksCopy.cs
  • src/Sections/Humans.Teams/Data/GovernanceBodyCopies.cs
  • tests/Humans.Integration.Tests/Infrastructure/SectionMigrationRunnerStopTests.cs
  • tests/Humans.Integration.Tests/Infrastructure/SqlCopyTests.cs
  • tests/Humans.Integration.Tests/Services/GovernanceBodyCopiesTests.cs
  • tests/Humans.Web.Tests/Architecture/SqlCopyRegistrationTests.cs
  • tests/Humans.Web.Tests/Hosting/StartupHaltPageTests.cs

A section that applied one migration and failed on the next never reported
the first, so the failure skipped the halt page. The snapshot hook now marks
the database as changing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
…nner

- SqlCopy loses Tables: the build check reads the tables from the SQL.
- The chain loop's progress is a copy finishing; AppliedNow and the
  second and third places that marked the database changed are gone.
- The per-migration apply drops the suppressed-transaction branch no
  shipped migration uses.
- The pending read loses its first-row flag.
- The role delete statement returns its audit row directly.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186neA54jAb2Cvdb6xgYZmd
@peterdrier

Copy link
Copy Markdown
Owner Author

Two red checks on earlier commits, neither from this PR's code:

  • build on 2f4d8db: the step that runs dotnet ef migrations has-pending-model-changes four at a time hit a file race (BuildHost-net472/System.IO.Pipelines.dll already exists). It passed on the next commit.
  • deploy on 5494df6: the Coolify API answered 500 to the deploy request, before any app code ran.

The latest push reruns both.


Generated by Claude Code

@peterdrier

Copy link
Copy Markdown
Owner Author

Closed in favour of #2820, the same branch as a new PR: this PR's preview could not deploy (Coolify held a duplicate preview row for it).


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tier:thorough Deep review, high complexity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants