Skip to content

Commit 0273b4f

Browse files
committed
docs: correct Task 6 save() flows in plan after review findings
1 parent 01bc857 commit 0273b4f

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

docs/superpowers/plans/2026-08-01-uuid-to-int-module-facade.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ git commit -m "refactor: user infrastructure models use integer foreign keys"
466466
- Remove `from uuid import UUID`.
467467
- `__init__(self, db, tenant_id: int | None = None)`.
468468
- `get_by_id(self, user_id: int)`, `get_by_id_with_relations(self, user_id: int)`, `_get_user_model(self, user_id: int)`, `_create_default_related_records(self, user_id: int)`.
469-
- In `save()` create branch, do not pass `id` when it is `None` (DB identity assigns). Replace lines 80-91 with:
469+
- In `save()` create branch, do not pass `id` when it is `None` (DB identity assigns) AND flush before creating the default related records so `user_model.id` is populated (otherwise `user_id` is NULL → IntegrityError). Replace lines 80-91 with:
470470

471471
```python
472472
else:
@@ -484,28 +484,32 @@ git commit -m "refactor: user infrastructure models use integer foreign keys"
484484
model_kwargs["id"] = user.id
485485
user_model = UserModel(**model_kwargs)
486486
self._db.add(user_model)
487+
await self._db.flush()
487488

488489
# Create default related records
489490
await self._create_default_related_records(user_model.id)
490491
```
491492

492-
(`user.tenant_id` — add a `tenant_id: int | None = None` field to the `User` dataclass in `user.py` (Task 4) if it does not exist; check first: `user.py` has no `tenant_id` field — add `tenant_id: int | None = None` after `updated_at`.)
493+
(`user.tenant_id` — add a `tenant_id: int | None = None` field to the `User` dataclass in `user.py` (Task 4) if it does not exist; check first: `user.py` has no `tenant_id` field — add `tenant_id: int | None = None` after `updated_at`. The `User` dataclass is `kw_only` (Task 4), so field order is flexible.)
493494

494495
- [ ] **Step 2: Convert `refresh_token_repository.py`**
495496

496497
- Remove `from uuid import UUID`; `__init__(self, db, tenant_id: int | None = None)`.
497-
- `save()`replace the `merge` flow with insert-without-id (identity assigns) and map back from refreshed model:
498+
- `save()`do not pass `id` when `None` (DB identity assigns) AND keep update semantics for existing tokens (the caller `refresh_token/handler.py` revokes an existing token then saves it — insert-only would duplicate the row and never revoke the original). Use merge + conditional id:
498499

499500
```python
500501
async def save(self, refresh_token: RefreshToken) -> RefreshToken:
501-
model = RefreshTokenModel(
502-
user_id=refresh_token.user_id,
503-
tenant_id=self._tenant_id,
504-
refresh_token_hash=refresh_token.token_hash,
505-
expires_at=refresh_token.expires_at,
506-
is_revoked=refresh_token.is_revoked,
507-
)
508-
self.db.add(model)
502+
model_kwargs = {
503+
"user_id": refresh_token.user_id,
504+
"tenant_id": self._tenant_id,
505+
"refresh_token_hash": refresh_token.token_hash,
506+
"expires_at": refresh_token.expires_at,
507+
"is_revoked": refresh_token.is_revoked,
508+
}
509+
if refresh_token.id is not None:
510+
model_kwargs["id"] = refresh_token.id
511+
model = RefreshTokenModel(**model_kwargs)
512+
model = await self.db.merge(model)
509513
await self.db.flush()
510514
await self.db.refresh(model)
511515
return RefreshToken(

0 commit comments

Comments
 (0)