Skip to content

chore(database): one migration builds the schema, since the move wipes every database - #532

Merged
PiquelChips merged 3 commits into
mainfrom
chore/cleanup-migrations
Nov 15, 2026
Merged

PiquelChips merged 3 commits into
mainfrom
chore/cleanup-migrations

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

piqueld's only instance is being shut down and moved, and all its data wiped. Nothing needs 50 migrations of history any more, nor the code that read rows only those migrations wrote. Now one 0001_initial.sql builds the final schema, and its comments describe the schema as it is, not how it got there.

Proof the schema is the same: pg_dump --schema-only of the old 50 migrations and of the new file match exactly (tables, column order, constraint names, indexes, functions, triggers), except for the intended changes below.

Intended schema changes (only old rows needed the old shape):

  • digest_key is NOT NULL on authorization requests, codes and refresh tokens. NULL only meant a digest made before fingerprints were recorded, so keys digest-usage loses its unrecorded key.
  • invitations.id loses its uuidv7() default, which served old rows and the previous release; auth always passes an ID.
  • domain_events_readable is a validated constraint, no longer NOT VALID.
  • The audit chain has one format, format 1, which names itself ("format": 1) in the canonical event. The old untagged format 1 and format 2 are gone, along with the sealer's format switch.

Removed code that only old data used:

  • The imported event (DomainEvent::Imported, ImportedAudit), its legacy history entry and timeline wording, and Actor::Unrecorded / HistoryActor::Unrecorded, which only imported rows had.
  • SigningKeyChange::Imported, which only earlier releases wrote.
  • The tests of old data migrations (the audit-log import, the back-channel logout backfill, the handles migration) and the scratch_schema / apply_migrations helpers only they used.

The recovery and redeemed-code triggers still run as their owner (SECURITY DEFINER), so they hold even for a role not granted account_recoveries, as tests/recoveries.rs checks; only their comments changed. Docs and comments that cited migration numbers now name the triggers, functions or sections instead.

Deploying: this needs a fresh database. An existing one has 50 applied migrations whose checksums no longer match.

Tests: just db test (497 passed, then prepare-check), the common and ui tests, just docs, and workspace clippy. One caveat: the Rust 1.100 toolchain's map_unwrap_or lint already fails across code this PR doesn't touch, so clippy ran with that lint allowed.

Council review

Rounds: 2

Delegate Round 1 Round 2
GPT 6.1 Sol 9/10 10/10
Claude Opus 5.5 9/10 9.5/10
Claude Fable 5.2 8.5/10 9/10

Each reviewer rebuilt both schemas and compared the dumps on their own, and found no functional problems. Their findings were leftover migration numbers and out-of-date wording, all fixed, including three comment nits from round 2. Declined as beyond this PR: making the session device columns (sessions.user_agent_family, IPs) NOT NULL, which would also change the settings UI's "unrecorded device" wording; and removing incidents.md's note about the old signing-key secret, which lives in piqueld, not the database.

Note

Written by Claude Opus 5.5 in Claude Code and reviewed by GPT 6.1 Sol, Claude Opus 5.5 and Claude Fable 5.2 (via T3 Code)

…s every database

piqueld's only instance is being moved and its data wiped, so no database
needs the history of 50 migrations. 0001_initial.sql now creates the final
schema directly, with comments that describe it as it is.

What only old rows needed goes with it: NULL digest keys ("unrecorded"),
the legacy invitation ID default, the imported audit-log event and its
unrecorded actor, the signing key "imported" change, and audit chain
format 1. The one chain format is now format 1, which names itself.
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The initial database migration now contains schema previously defined by later migrations, which are removed. Audit sealing uses format 1 as the newest format. Imported audit-event types and legacy history entries are removed, and digest-key usage now requires a fingerprint.

Changes: Schema Baseline and Audit Updates

Layer / File(s) Summary
Consolidate schema in the initial migration
crates/database/migrations/0001_initial.sql, crates/database/migrations/0002_...–0050_....sql
The initial migration defines the expanded account, session, recovery, OIDC, token, event, delivery, notification, and dashboard schema. Later migration files are removed.
Update audit event and sealing contracts
apps/auth-service/src/audit/*, crates/common/src/events/*, crates/database/src/repositories/audit/*, crates/ui/src/admin/events.rs, crates/ui/src/settings/timeline.rs
Imported audit-event types and legacy history entries are removed. The audit repository and sealer use format 1 as the newest format. Actor handling, tests, and audit displays are updated.
Require digest-key fingerprints
apps/auth-service/src/cli/keys.rs, apps/auth-service/src/config/keys.rs, crates/database/src/repositories/maintenance/mod.rs, crates/database/.sqlx/*, crates/database/src/repositories/*/tests.rs
Digest usage stores a required fingerprint string. Key naming, CLI output, query metadata, and database test fixtures use the non-optional value.
Align migration support and references
crates/database/src/connection/*, crates/database/src/repositories/{events,handles,oidc}/tests.rs, crates/database/tests/recoveries.rs, docs/dev/*
Test-only migration helpers and migration-specific tests are removed. Documentation and comments identify schema behavior without relying on migration numbers or release-specific compatibility wording.

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Other

Merge Risk: Minimal · up to f6d6e

This change folds the migration history into a single initial schema and simplifies audit sealing to one format. Inspected write paths match the stricter schema, and no concrete defects were found. Deployment requires a fresh database by design, so existing databases cannot be upgraded in place.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "chore(database): one migration builds the schema, since the move wipes every database" matches the main change. The PR replaces the 50-migration history with a single 0001_initial.sql that …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "chore(database): one migration builds the schema, since the move wipes every database" matches the main change. The PR replaces the 50-migration history with a single 0001_initial.sql that builds the final schema, and it removes the code and tests that supported the old migrations and legacy data shapes. The title is specific and uses a conventional commit prefix. It does not mention the removal of legacy data handling (imported audit events, Actor::Unrecorded) or the intended schema changes such as non-null digest_key and the audit-chain format standardization, but it covers the primary change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit reads the schema at dawn,
And finds the old migrations gone.
Format one now seals the trail,
While tidy fingerprints fill the tale.
I twitch my nose and hop along!
The event chain keeps moving on.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 42d2a20e-e8e5-4ca0-9a7e-487c99dfb332
Commits

Reviewing files that changed from the base of the PR and between 1f2f4a2 and f6d6e62.

Files selected for processing: 89

  • apps/auth-service/src/audit/sealer.rs
  • apps/auth-service/src/audit/tests.rs
  • apps/auth-service/src/cli/audit.rs
  • apps/auth-service/src/cli/keys.rs
  • apps/auth-service/src/config/keys.rs
  • apps/auth-service/src/history/mod.rs
  • apps/auth-service/src/recovery/mod.rs
  • crates/common/src/events/context.rs
  • crates/common/src/events/history.rs
  • crates/common/src/events/mod.rs
  • crates/common/src/events/payload.rs
  • crates/common/src/notifications/mod.rs
  • crates/common/src/tests.rs
  • crates/database/.sqlx/query-494455c0e245ebe29680a5658e7653c1f91ec55aa6979046f1005949e0141013.json
  • crates/database/.sqlx/query-564e9b1ce7cb468f78cb854239c5448fe3f4cc7d5870dbbbc7bcf40788919844.json
  • crates/database/.sqlx/query-58efe40b8d8fd9f229e34655f6ec57f30356010782b3b365466116e43d62b0e4.json
  • crates/database/migrations/0001_initial.sql
  • crates/database/migrations/0002_refresh_family_authorization_code.sql
  • crates/database/migrations/0003_session_passkey_confirmation.sql
  • crates/database/migrations/0004_authentication_claims.sql
  • crates/database/migrations/0005_domain_events.sql
  • crates/database/migrations/0006_deliveries.sql
  • crates/database/migrations/0007_administrators.sql
  • crates/database/migrations/0008_oidc_client_registry.sql
  • crates/database/migrations/0009_pushed_authorization_requests.sql
  • crates/database/migrations/0010_session_devices.sql
  • crates/database/migrations/0011_pin_function_search_path.sql
  • crates/database/migrations/0012_client_owners.sql
  • crates/database/migrations/0013_backchannel_logout.sql
  • crates/database/migrations/0014_email_verification.sql
  • crates/database/migrations/0015_event_reasons.sql
  • crates/database/migrations/0016_session_passkeys.sql
  • crates/database/migrations/0017_signing_keys.sql
  • crates/database/migrations/0018_contract_audit_log_and_legacy_secrets.sql
  • crates/database/migrations/0019_account_recoveries.sql
  • crates/database/migrations/0020_dashboard_sessions.sql
  • crates/database/migrations/0021_handles.sql
  • crates/database/migrations/0022_account_deletion.sql
  • crates/database/migrations/0023_related_origins.sql
  • crates/database/migrations/0024_audit_chain.sql
  • crates/database/migrations/0025_drop_legacy_client_secret.sql
  • crates/database/migrations/0026_account_purge.sql
  • crates/database/migrations/0027_event_counts.sql
  • crates/database/migrations/0028_authorization_request_interaction.sql
  • crates/database/migrations/0029_reserved_handles.sql
  • crates/database/migrations/0030_profile_pictures.sql
  • crates/database/migrations/0031_pending_federation_picture.sql
  • crates/database/migrations/0032_pending_email_authority.sql
  • crates/database/migrations/0033_notifications.sql
  • crates/database/migrations/0034_notification_emails.sql
  • crates/database/migrations/0035_history_writes.sql
  • crates/database/migrations/0036_chain_format.sql
  • crates/database/migrations/0037_announcements.sql
  • crates/database/migrations/0038_alerts.sql
  • crates/database/migrations/0039_notification_preferences.sql
  • crates/database/migrations/0040_alert_updates.sql
  • crates/database/migrations/0041_dashboard_session_limits.sql
  • crates/database/migrations/0042_retention.sql
  • crates/database/migrations/0043_known_devices.sql
  • crates/database/migrations/0044_drop_unused_delivery_machinery.sql
  • crates/database/migrations/0045_invitation_recipients.sql
  • crates/database/migrations/0046_first_handle_change.sql
  • crates/database/migrations/0047_refresh_token_successors.sql
  • crates/database/migrations/0048_optional_pkce.sql
  • crates/database/migrations/0049_optional_state.sql
  • crates/database/migrations/0050_contract_deliveries_and_history_index.sql
  • crates/database/src/connection/mod.rs
  • crates/database/src/connection/tests.rs
  • crates/database/src/repositories/audit/mod.rs
  • crates/database/src/repositories/audit/tests.rs
  • crates/database/src/repositories/deletions/mod.rs
  • crates/database/src/repositories/deletions/tests.rs
  • crates/database/src/repositories/deliveries/tests.rs
  • crates/database/src/repositories/events/mod.rs
  • crates/database/src/repositories/events/tests.rs
  • crates/database/src/repositories/handles/tests.rs
  • crates/database/src/repositories/maintenance/mod.rs
  • crates/database/src/repositories/oidc/clients.rs
  • crates/database/src/repositories/oidc/tests.rs
  • crates/database/src/repositories/tokens/tests.rs
  • crates/database/tests/recoveries.rs
  • crates/ui/src/admin/events.rs
  • crates/ui/src/settings/timeline.rs
  • docs/dev/architecture/database.md
  • docs/dev/extending/migration.md
  • docs/dev/extending/notification.md
  • docs/dev/operations/signing-keys.md
  • docs/dev/security/account-recovery.md
  • docs/dev/security/audit-history.md
Files with no reviewable changes: 54

  • apps/auth-service/src/cli/audit.rs
  • crates/common/src/tests.rs
  • crates/database/.sqlx/query-58efe40b8d8fd9f229e34655f6ec57f30356010782b3b365466116e43d62b0e4.json
  • crates/database/migrations/0002_refresh_family_authorization_code.sql
  • crates/database/migrations/0003_session_passkey_confirmation.sql
  • crates/database/migrations/0004_authentication_claims.sql
  • crates/database/migrations/0005_domain_events.sql
  • crates/database/migrations/0006_deliveries.sql
  • crates/database/migrations/0007_administrators.sql
  • crates/database/migrations/0008_oidc_client_registry.sql
  • crates/database/migrations/0009_pushed_authorization_requests.sql
  • crates/database/migrations/0010_session_devices.sql
  • crates/database/migrations/0011_pin_function_search_path.sql
  • crates/database/migrations/0012_client_owners.sql
  • crates/database/migrations/0013_backchannel_logout.sql
  • crates/database/migrations/0014_email_verification.sql
  • crates/database/migrations/0015_event_reasons.sql
  • crates/database/migrations/0016_session_passkeys.sql
  • crates/database/migrations/0017_signing_keys.sql
  • crates/database/migrations/0018_contract_audit_log_and_legacy_secrets.sql
  • crates/database/migrations/0019_account_recoveries.sql
  • crates/database/migrations/0020_dashboard_sessions.sql
  • crates/database/migrations/0021_handles.sql
  • crates/database/migrations/0022_account_deletion.sql
  • crates/database/migrations/0023_related_origins.sql
  • crates/database/migrations/0024_audit_chain.sql
  • crates/database/migrations/0025_drop_legacy_client_secret.sql
  • crates/database/migrations/0026_account_purge.sql
  • crates/database/migrations/0027_event_counts.sql
  • crates/database/migrations/0028_authorization_request_interaction.sql
  • crates/database/migrations/0029_reserved_handles.sql
  • crates/database/migrations/0030_profile_pictures.sql
  • crates/database/migrations/0031_pending_federation_picture.sql
  • crates/database/migrations/0032_pending_email_authority.sql
  • crates/database/migrations/0033_notifications.sql
  • crates/database/migrations/0034_notification_emails.sql
  • crates/database/migrations/0035_history_writes.sql
  • crates/database/migrations/0036_chain_format.sql
  • crates/database/migrations/0037_announcements.sql
  • crates/database/migrations/0038_alerts.sql
  • crates/database/migrations/0039_notification_preferences.sql
  • crates/database/migrations/0040_alert_updates.sql
  • crates/database/migrations/0041_dashboard_session_limits.sql
  • crates/database/migrations/0042_retention.sql
  • crates/database/migrations/0043_known_devices.sql
  • crates/database/migrations/0044_drop_unused_delivery_machinery.sql
  • crates/database/migrations/0045_invitation_recipients.sql
  • crates/database/migrations/0046_first_handle_change.sql
  • crates/database/migrations/0047_refresh_token_successors.sql
  • crates/database/migrations/0048_optional_pkce.sql
  • crates/database/migrations/0049_optional_state.sql
  • crates/database/migrations/0050_contract_deliveries_and_history_index.sql
  • crates/database/src/connection/mod.rs
  • docs/dev/operations/signing-keys.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Council round 1: the citations of migrations 0006, 0035 and 0042 that
remained, the trace ID and imported-key wording, and where the migration
test helpers went.
Council round 2: limit_passkeys guards passkeys, not the chain, and two
comments still described a release that no longer exists.
@PiquelChips PiquelChips self-assigned this Nov 15, 2026
@PiquelChips PiquelChips moved this from Needs Triage to In Progress in sourceorigin.dev Nov 15, 2026
@PiquelChips PiquelChips added this to the v0.1 milestone Nov 15, 2026
@PiquelChips
PiquelChips added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit 4321cb0 Nov 15, 2026
14 checks passed
@PiquelChips
PiquelChips deleted the chore/cleanup-migrations branch November 15, 2026 20:34
@github-project-automation github-project-automation Bot moved this from In Progress to Done in sourceorigin.dev Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant