fix(atlas): a place that moves no longer keeps its old country (#2527)

place_regions caches the country and region Atlas resolved for a place,
and a row that was already there was never re-derived. Correcting a
place's location in the trip left the old row in charge, so Atlas, the
dashboard stats and the journey stats kept counting the country the place
had just left, with the trip's dates on it.

A trigger on places now drops the cached row whenever lat, lng or address
actually change, so every writer is covered: the place editor, the MCP
update tools, the plugin RPC and the import enrichment and address
backfill. An edit that writes the same location back keeps the row.

The background lookup that fills the cache also only writes while the
place still sits where it was resolved from. A place moved while its
lookup was running would otherwise get the old answer written back.

Places corrected before this fix would have kept their old country until
they were moved again, because the trigger only sees new edits.
PlaceRegionsRepairJob goes over place_regions once, on the first start
after the update, and remembers that in app_settings. It re-derives each
row from the bundled borders only, never Nominatim, and replaces a row
whose country or region no longer matches where its place is now. A row
the bundle cannot answer for is left alone unless both the coordinates
and the address put the place in another country; then it is dropped and
the next Atlas load looks the place up again. A place without
coordinates keeps no row. Its writes are guarded the same way as the
background lookup, so a place edited during the pass is not overwritten.

The in-memory region cache is now keyed by the country the address names
as well as the coordinates, so correcting only the address of a border
point no longer returns the old answer until a restart.

On dev the trigger takes schema version 244, the slot it has on main, so a
4.3.3 install upgrading to 4.4.0 does not skip it. Web Push moves up to
245 and is idempotent, so a 4.3.3 install gets its table there. An
instance that already ran Web Push at 244 never runs 244 again, so 246
creates the trigger once more; everywhere else that is a no-op.
This commit is contained in:
Maurice
2026-09-25 23:52:47 +02:00
parent 8b6a8d3098
commit ae73527362
13 changed files with 819 additions and 65 deletions
+70
View File
@@ -536,3 +536,73 @@ describe('Regions geo', () => {
expect(res.body).toHaveProperty('type', 'FeatureCollection');
});
});
describe('A place that moves takes its Atlas country with it (#2527)', () => {
// The region cache is filled by a background task that GET /regions only starts,
// so wait for the row the way the next Atlas load would find it.
async function regionRowOf(placeId: number): Promise<{ country_code: string; region_code: string } | undefined> {
for (let i = 0; i < 100; i++) {
const row = testDb.prepare('SELECT country_code, region_code FROM place_regions WHERE place_id = ?').get(placeId) as
| { country_code: string; region_code: string }
| undefined;
if (row) return row;
await new Promise((resolve) => setTimeout(resolve, 20));
}
return undefined;
}
async function atlasCountries(userId: number): Promise<string[]> {
const res = await request(app).get('/api/addons/atlas/stats').set('Cookie', authCookie(userId));
expect(res.status).toBe(200);
return (res.body.countries as { code: string }[]).map((c) => c.code);
}
it('ATLAS-015: correcting a place from France to Germany moves it on the Atlas', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Wrongly imported', start_date: '2025-05-01', end_date: '2025-05-05' });
const place = testDb
.prepare('INSERT INTO places (trip_id, name, lat, lng, address) VALUES (?, ?, ?, ?, ?) RETURNING id')
.get(trip.id, 'Hotel', 48.8566, 2.3522, 'Rue de Rivoli, Paris, France') as { id: number };
// The place is seen on the Atlas once, which caches France for it.
await request(app).get('/api/addons/atlas/regions').set('Cookie', authCookie(user.id));
expect((await regionRowOf(place.id))?.country_code).toBe('FR');
expect(await atlasCountries(user.id)).toEqual(['FR']);
// The user fixes the location in the trip.
const put = await request(app)
.put(`/api/trips/${trip.id}/places/${place.id}`)
.set('Cookie', authCookie(user.id))
.send({ lat: 52.5163, lng: 13.3777, address: 'Pariser Platz, Berlin, Germany' });
expect(put.status).toBe(200);
expect(await atlasCountries(user.id)).toEqual(['DE']);
// And the regions follow on the next load, with nothing left in France.
await request(app).get('/api/addons/atlas/regions').set('Cookie', authCookie(user.id));
expect((await regionRowOf(place.id))?.country_code).toBe('DE');
const regions = await request(app).get('/api/addons/atlas/regions').set('Cookie', authCookie(user.id));
expect(Object.keys(regions.body.regions)).toEqual(['DE']);
expect(await atlasCountries(user.id)).toEqual(['DE']);
});
it('ATLAS-015: an edit that leaves the location alone keeps the cached country', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Paris', start_date: '2025-05-01', end_date: '2025-05-05' });
const place = testDb
.prepare('INSERT INTO places (trip_id, name, lat, lng, address) VALUES (?, ?, ?, ?, ?) RETURNING id')
.get(trip.id, 'Louvre', 48.8606, 2.3376, 'Rue de Rivoli, Paris, France') as { id: number };
await request(app).get('/api/addons/atlas/regions').set('Cookie', authCookie(user.id));
const cached = await regionRowOf(place.id);
expect(cached?.country_code).toBe('FR');
// The place editor sends the whole form back, location included, for a notes edit.
const put = await request(app)
.put(`/api/trips/${trip.id}/places/${place.id}`)
.set('Cookie', authCookie(user.id))
.send({ notes: 'Closed on Tuesdays', lat: 48.8606, lng: 2.3376, address: 'Rue de Rivoli, Paris, France' });
expect(put.status).toBe(200);
expect(testDb.prepare('SELECT country_code, region_code FROM place_regions WHERE place_id = ?').get(place.id)).toEqual(cached);
});
});