Fix v0.2.8 migration: backfill org_id instead of a bare NOT NULL add #20

Merged
alice merged 1 commit from fix/token-org-id-migration into main 2026-08-28 13:47:53 +00:00
Owner

Follow-up to #19. Token.OrgID landed as NOT NULL with no default, so AutoMigrate's ALTER TABLE api_tokens ADD COLUMN org_id ... NOT NULL fails against Postgres once the table has existing rows — which it does, since tokens were created via the tokens page before org scoping existed. This is why the v0.2.8 migration failed on redeploy.

Fix, following the same pattern already used for org_members.role (default:member) and the existing post-AutoMigrate backfill steps in db.go:

  • org_id gets default:0 so the column add succeeds even with existing rows
  • a new idempotent backfillTokenOrgID step sets each org-less token's org_id to its account's earliest org membership (accounts with no org membership are left at 0)
  • added TestBackfillTokenOrgID covering: backfill happens, an already-scoped token is left untouched, and an orphan account (no org membership) is left at the default

go build/go test ./... pass.

Follow-up to #19. `Token.OrgID` landed as `NOT NULL` with no default, so AutoMigrate's `ALTER TABLE api_tokens ADD COLUMN org_id ... NOT NULL` fails against Postgres once the table has existing rows — which it does, since tokens were created via the tokens page before org scoping existed. This is why the v0.2.8 migration failed on redeploy. Fix, following the same pattern already used for `org_members.role` (`default:member`) and the existing post-AutoMigrate backfill steps in `db.go`: - `org_id` gets `default:0` so the column add succeeds even with existing rows - a new idempotent `backfillTokenOrgID` step sets each org-less token's `org_id` to its account's earliest org membership (accounts with no org membership are left at 0) - added `TestBackfillTokenOrgID` covering: backfill happens, an already-scoped token is left untouched, and an orphan account (no org membership) is left at the default `go build`/`go test ./...` pass.
Fix v0.2.8 migration: backfill org_id instead of a bare NOT NULL add
All checks were successful
service-dmarc-api CI / build-and-push (pull_request) Successful in 3m9s
Go lint / lint (pull_request) Successful in 4m1s
60ad857b94
Token.OrgID landed as a NOT NULL column with no default, so AutoMigrate's
ALTER TABLE ADD COLUMN fails on Postgres against the api_tokens table's
existing rows (tokens created before org scoping existed). Give the column
a default of 0 so the migration succeeds, then backfill each org-less
token from the owning account's earliest org membership.
alice merged commit 0465db9c8e into main 2026-08-28 13:47:53 +00:00
alice deleted branch fix/token-org-id-migration 2026-08-28 13:47:53 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
dmarc-ing/service-dmarc-api!20
No description provided.