Skip to content

Use RETURNING clause for INSERT, UPDATE, DELETE#426

Merged
davideme merged 1 commit intomainfrom
claude/eager-mclaren
Feb 26, 2026
Merged

Use RETURNING clause for INSERT, UPDATE, DELETE#426
davideme merged 1 commit intomainfrom
claude/eager-mclaren

Conversation

@davideme
Copy link
Owner

Refactor PostgreSQL operations to use SQLAlchemy's insert(), update() statements with .returning() instead of manual object manipulation and flush/refresh cycles.

Changes:

  • Replace LampModel() instantiation + add() + flush() + refresh() with insert().values().returning()
  • Replace select() + manual attribute updates with update().values().returning()
  • Replace select() + manual soft-delete with update().values(deleted_at=...).returning()
  • Add git fetch/rebase commands to Claude settings

This approach is more efficient and aligns with PostgreSQL best practices by retrieving updated rows in a single operation.

Replace the flush()+refresh() pattern with single-query RETURNING:
- INSERT: insert().returning(LampModel) eliminates post-insert SELECT
- UPDATE: update().returning(LampModel) eliminates pre-UPDATE SELECT and
  post-UPDATE refresh; trigger-updated updated_at is captured in the same query
- DELETE: update().returning(LampModel.id) eliminates pre-UPDATE SELECT;
  missing row (not found) is detected from empty RETURNING result

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 26, 2026 12:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the PostgreSQL lamp repository to leverage PostgreSQL RETURNING via SQLAlchemy insert()/update().returning() so create/update/delete operations can retrieve affected rows in a single round-trip, and updates Claude local settings permissions.

Changes:

  • Refactor create() to use insert(...).returning(...) instead of add()/flush()/refresh().
  • Refactor update() and soft delete() to use update(...).returning(...) rather than select() + manual mutation.
  • Add git fetch / git rebase commands to Claude local settings allow-list.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/python/src/openapi_server/repositories/postgres_lamp_repository.py Moves CRUD writes to RETURNING-based SQLAlchemy statements to fetch updated rows in one operation.
.claude/settings.local.json Expands allowed bash commands for local Claude tooling workflows.

Comment on lines +128 to +131
update(LampModel)
.where(LampModel.id == UUID(lamp_entity.id), LampModel.deleted_at.is_(None))
.values(is_on=lamp_entity.status) # Map status -> is_on; updated_at set by trigger
.returning(LampModel)
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

update(LampModel)...values(is_on=...) will execute an UPDATE every time update() is called, even when lamp_entity.status matches the current is_on value. With the updated_at trigger, this will bump updated_at and take a write lock on idempotent updates, which is a behavioral/operational change vs loading the row and letting the ORM skip no-op writes. Consider guarding the UPDATE with a predicate (e.g., only update when is_on is distinct from the new value) so no-op updates don’t modify timestamps or generate unnecessary writes.

Copilot uses AI. Check for mistakes.
Comment on lines +81 to +83
"Bash(tree:*)",
"Bash(git fetch origin main)",
"Bash(git rebase origin/main)"
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

This repo’s .gitignore ignores .claude/ except for .claude/settings.json and .claude/hooks/, so committing/modifying .claude/settings.local.json appears to go against the intended convention of keeping local-only Claude settings out of version control. Consider moving these permission additions into .claude/settings.json (if they should be shared) or removing/untracking settings.local.json from the repository so developers can keep local overrides without PR churn.

Copilot uses AI. Check for mistakes.
@davideme davideme merged commit f4a0466 into main Feb 26, 2026
7 checks passed
@davideme davideme deleted the claude/eager-mclaren branch February 26, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants