#11 Fix Saving Settings Issue and Prevent Duplicate Rows#12
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: b-at-neu <144247685+b-at-neu@users.noreply.github.com>
…pilot/fix-saving-settings-issue
|
@copilot the bug persists. To recreate, edit the settings and then click save. Then reload the page and the settings will go back to outdated values. Curiously, it seems if you do this twice in a row maybe the settings from the first time saving appear? |
Co-authored-by: b-at-neu <144247685+b-at-neu@users.noreply.github.com>
I've identified and fixed the issue. The problem was that after saving settings, the client-side UI wasn't refreshing even though the database was updated and The fix adds The "settings from the first time saving appear on the second save" behavior you noticed was because Now when you save settings, the page will automatically refresh and show the updated values immediately. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot why does my settings table have two rows in it? Use the built-in create or update from prisma instead of the weird pattern that is happening right now |
Co-authored-by: b-at-neu <144247685+b-at-neu@users.noreply.github.com>
Co-authored-by: b-at-neu <144247685+b-at-neu@users.noreply.github.com>
Fixed in commits 749992c and d8fde4d. The duplicate rows were caused by a race condition in the find-then-create-or-update pattern. I've refactored both The upsert pattern ensures only one settings record will exist going forward. You may need to manually delete the extra row(s) from your database. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Fixed settings not persisting after save and eliminated duplicate settings rows in the database caused by race conditions.
Problems Addressed
Root Causes
getSettings()was wrapped with React'scache(), which maintains a request-scoped cache that persists beyond Next.js'srevalidatePath()invalidationSolution
Settings Persistence (commits 74e3b33, b16d523)
cache()wrapper fromgetSettings()which was preventing fresh data fetches afterrevalidatePath()was calledrouter.refresh()call in the settings form after successful save to trigger an immediate client-side refreshDuplicate Row Prevention (commits 749992c, d8fde4d)
SETTINGS_ID = 'default'for singleton settings recordupsertoperation in bothgetSettings()andupdateSettings()The
upsertoperation atomically creates or updates the settings record in a single database transaction:where: { id: SETTINGS_ID }- looks for record with fixed ID'default'update: settingsData- updates if foundcreate: { id: SETTINGS_ID, ...settingsData }- creates with fixed ID if not foundThis eliminates the race condition that caused duplicate rows and ensures there's only ever one settings record in the database.
Testing
Note: Any existing duplicate settings rows in the database will need to be manually deleted. Going forward, only one settings record with ID
'default'will exist.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.