Skip to content

Commit acbf9dd

Browse files
committed
Fix: Add verification step to prevent race condition with cache rebuild
After syncing geometries to the database, the script now verifies that the writes are visible before exiting. This prevents a race condition where rebuild-cache.js could start reading from the database before the new geometries were fully committed/visible, resulting in stale cache data. The fix: Adds a retry loop (up to 5 attempts with 1 second delays) that queries the database to confirm the expected number of geometries are visible Only proceeds once verification passes, ensuring the subsequent cache rebuild sees the new data
1 parent b63d1db commit acbf9dd

6 files changed

Lines changed: 217 additions & 6 deletions

File tree

.dev/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.dev/sync-geometries-table.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,45 @@ async function syncGeometriesTable() {
224224
console.log(` ❌ Failed: ${failed}`)
225225
console.log(` 📁 Local files: ${localFileSet ? localFileSet.size : 'N/A'}`)
226226

227-
if (failed === 0) {
228-
console.log('\n✅ Geometries table synced successfully!')
229-
console.log('\n📝 Next steps:')
230-
console.log(` Run: ${isStaging ? 'STAGING=true ' : ''}node rebuild-cache.js`)
231-
} else {
227+
if (failed > 0) {
232228
console.log('\n⚠️ Some entries failed to insert. Please check errors above.')
233229
process.exit(1)
234230
}
231+
232+
// Verify all writes are visible before exiting (prevents race condition with rebuild-cache.js)
233+
if (inserted > 0 || updated > 0 || deletedFromTable > 0) {
234+
console.log('\n🔍 Verifying database writes are visible...')
235+
const expectedCount = localFileSet ? localFileSet.size : geojsonFiles.length
236+
237+
// Retry up to 5 times with 1 second delay
238+
for (let attempt = 1; attempt <= 5; attempt++) {
239+
const { data: verifyData, error: verifyError } = await supabase
240+
.from(table)
241+
.select('geometry_name', { count: 'exact' })
242+
243+
if (verifyError) {
244+
console.error(` ❌ Verification query failed: ${verifyError.message}`)
245+
break
246+
}
247+
248+
const actualCount = verifyData?.length || 0
249+
if (actualCount === expectedCount) {
250+
console.log(` ✅ Verified: ${actualCount} entries in table (expected ${expectedCount})`)
251+
break
252+
}
253+
254+
if (attempt < 5) {
255+
console.log(` ⏳ Attempt ${attempt}: Found ${actualCount} entries, expected ${expectedCount}. Waiting...`)
256+
await new Promise(resolve => setTimeout(resolve, 1000))
257+
} else {
258+
console.log(` ⚠️ Final count: ${actualCount} entries (expected ${expectedCount}) - proceeding anyway`)
259+
}
260+
}
261+
}
262+
263+
console.log('\n✅ Geometries table synced successfully!')
264+
console.log('\n📝 Next steps:')
265+
console.log(` Run: ${isStaging ? 'STAGING=true ' : ''}node rebuild-cache.js`)
235266
}
236267

237268
syncGeometriesTable().catch(error => {

0 commit comments

Comments
 (0)