Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions prisma/migrations/20260414200000_add_admin_level_1/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- Migration: Add admin_level_1 field to aed_locations
--
-- admin_level_1 stores the first-level administrative division (region/state)
-- derived from coordinates via Nominatim reverse geocoding.
-- This is universal: "Comunidad de Madrid" (ES), "Île-de-France" (FR),
-- "Bayern" (DE), "California" (US), etc.
--
-- Step 1: Add the column
-- Step 2: Backfill for Spain using the province INE code → community mapping
-- Step 3: Add index for geographic queries

-- Step 1: Add column
ALTER TABLE "aed_locations" ADD COLUMN IF NOT EXISTS "admin_level_1" TEXT;

-- Step 2: Backfill existing Spanish records from city_code/postal_code → community name
-- Uses COALESCE(city_code, postal_code) to get the best available province prefix.
-- The first 2 digits of both fields are the INE province code in Spain.
UPDATE "aed_locations"
SET "admin_level_1" = CASE LEFT(COALESCE(NULLIF("city_code", ''), NULLIF("postal_code", '')), 2)
WHEN '01' THEN 'País Vasco'
WHEN '02' THEN 'Castilla-La Mancha'
WHEN '03' THEN 'Comunitat Valenciana'
WHEN '04' THEN 'Andalucía'
WHEN '05' THEN 'Castilla y León'
WHEN '06' THEN 'Extremadura'
WHEN '07' THEN 'Illes Balears'
WHEN '08' THEN 'Cataluña'
WHEN '09' THEN 'Castilla y León'
WHEN '10' THEN 'Extremadura'
WHEN '11' THEN 'Andalucía'
WHEN '12' THEN 'Comunitat Valenciana'
WHEN '13' THEN 'Castilla-La Mancha'
WHEN '14' THEN 'Andalucía'
WHEN '15' THEN 'Galicia'
WHEN '16' THEN 'Castilla-La Mancha'
WHEN '17' THEN 'Cataluña'
WHEN '18' THEN 'Andalucía'
WHEN '19' THEN 'Castilla-La Mancha'
WHEN '20' THEN 'País Vasco'
WHEN '21' THEN 'Andalucía'
WHEN '22' THEN 'Aragón'
WHEN '23' THEN 'Andalucía'
WHEN '24' THEN 'Castilla y León'
WHEN '25' THEN 'Cataluña'
WHEN '26' THEN 'La Rioja'
WHEN '27' THEN 'Galicia'
WHEN '28' THEN 'Comunidad de Madrid'
WHEN '29' THEN 'Andalucía'
WHEN '30' THEN 'Región de Murcia'
WHEN '31' THEN 'Navarra'
WHEN '32' THEN 'Galicia'
WHEN '33' THEN 'Asturias'
WHEN '34' THEN 'Castilla y León'
WHEN '35' THEN 'Canarias'
WHEN '36' THEN 'Galicia'
WHEN '37' THEN 'Castilla y León'
WHEN '38' THEN 'Canarias'
WHEN '39' THEN 'Cantabria'
WHEN '40' THEN 'Castilla y León'
WHEN '41' THEN 'Andalucía'
WHEN '42' THEN 'Castilla y León'
WHEN '43' THEN 'Cataluña'
WHEN '44' THEN 'Aragón'
WHEN '45' THEN 'Castilla-La Mancha'
WHEN '46' THEN 'Comunitat Valenciana'
WHEN '47' THEN 'Castilla y León'
WHEN '48' THEN 'País Vasco'
WHEN '49' THEN 'Castilla y León'
WHEN '50' THEN 'Aragón'
WHEN '51' THEN 'Ceuta'
WHEN '52' THEN 'Melilla'
ELSE NULL
END
WHERE "admin_level_1" IS NULL
AND COALESCE(NULLIF("city_code", ''), NULLIF("postal_code", '')) IS NOT NULL;

-- Step 3: Add index for geographic hierarchy queries
CREATE INDEX IF NOT EXISTS "idx_aed_locations_admin_level_1" ON "aed_locations" ("admin_level_1");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add nominatim_verified_at to track which records have been verified via Nominatim
-- This allows the enrichment script to skip already-verified records on re-runs
ALTER TABLE "aed_locations" ADD COLUMN IF NOT EXISTS "nominatim_verified_at" TIMESTAMPTZ;
5 changes: 4 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ model AedLocation {
// Geographic information by city (no FK - multi-city support)
city_name String?
city_code String?
admin_level_1 String? // Region/State from reverse geocoding (e.g., "Comunidad de Madrid", "Île-de-France")
district_code String?
district_name String?
neighborhood_code String?
Expand All @@ -353,7 +354,8 @@ model AedLocation {
access_instructions String? // How to access the AED

// Geocoding enrichment validation (automatic coordinate validation)
geocoding_validation Json? // Validation result from geocoding enrichment: {status, distance_meters, original_coords, geocoded_coords, reason, validated_at}
geocoding_validation Json? // Validation result from geocoding enrichment: {status, distance_meters, original_coords, geocoded_coords, reason, validated_at}
nominatim_verified_at DateTime? // When admin_level_1 was last verified via Nominatim reverse geocoding

aed Aed?
address_validation AedAddressValidation?
Expand All @@ -364,6 +366,7 @@ model AedLocation {
@@index([postal_code])
@@index([city_name], map: "idx_aed_locations_city_name")
@@index([city_code], map: "idx_aed_locations_city_code")
@@index([admin_level_1], map: "idx_aed_locations_admin_level_1")
@@index([city_code, district_code])
@@index([city_code, postal_code])
@@map("aed_locations")
Expand Down
1 change: 1 addition & 0 deletions public/58a9f826f33ed4f000b44c285246099e.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
58a9f826f33ed4f000b44c285246099e
1 change: 1 addition & 0 deletions public/7b8f16c9e24b4a02b2bf3e1f5c9d8a3e.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7b8f16c9e24b4a02b2bf3e1f5c9d8a3e
Loading
Loading