-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add generated area.geom column for improved geocode perfomance #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { type Kysely, sql } from "kysely"; | ||
|
|
||
| /** | ||
| * Add a generated `area.geom` column that is the `geometry` version | ||
| * of the source `geography` column. At the time of writing, this column | ||
| * is only used for one query (repositories/Area.ts/findAreasByPoint), as | ||
| * it is *slightly* less accurate to use geometry columns when working with | ||
| * geographical data, so should be avoided. However, this function is the | ||
| * bottleneck when geocoding data records, and using geometry leads to a | ||
| * 10x speedup, so the accuracy/performance tradeoff works out. | ||
| */ | ||
|
|
||
| export async function up(db: Kysely<any>): Promise<void> { | ||
| await sql` | ||
| ALTER TABLE area | ||
| ADD COLUMN geom geometry(MultiPolygon, 4326) NOT NULL | ||
| GENERATED ALWAYS AS (geography::geometry) STORED | ||
| `.execute(db); | ||
|
|
||
| await sql`CREATE INDEX area_area_set_id_geom_gist ON area USING GIST (area_set_id, geom)`.execute( | ||
| db, | ||
| ); | ||
| } | ||
|
|
||
| export async function down(db: Kysely<any>): Promise<void> { | ||
| await sql`ALTER TABLE area DROP COLUMN geom`.execute(db); | ||
| await sql`DROP INDEX IF EXISTS area_area_set_id_geom_gist`.execute(db); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |||||
| * This file represents the complete database schema as defined by all migrations. | ||||||
| * It includes all tables, columns, types, constraints, and relationships. | ||||||
| * | ||||||
| * Last updated: 2026-01-15 | ||||||
| * Last updated: 2026-02-05 | ||||||
| * Based on migrations up to: 1764611637231_map_view_inspector_config.ts | ||||||
|
||||||
| * Based on migrations up to: 1764611637231_map_view_inspector_config.ts | |
| * Based on migrations up to: 1770299199726_area_geom.ts |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The organisationId and userId columns in the OrganisationUser interface have been changed from NOT NULL to NULL. This change appears unrelated to the PR's stated purpose of adding the area.geom column for geocoding performance.
The original migration (migrations/1744117982427_organisation_user.ts) defines both columns as NOT NULL, and there's no migration file included in this PR that would alter these columns to be nullable. This suggests the schema documentation may be incorrect, or there's a missing migration that should be included in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function has changed from using ST_Intersects on a geography column to ST_Covers on a geometry column. While both functions test spatial relationships, they have different semantics:
This means ST_Covers is more restrictive than ST_Intersects. A point that was previously found by ST_Intersects (e.g., on the boundary of an area) might not be found by ST_Covers, potentially changing the behavior of geocoding operations. This semantic difference should be verified to ensure it doesn't affect the correctness of the geocoding logic, particularly for edge cases where points fall on area boundaries.