-- Add sector_id to locations table and populate from projects if needed
-- Run after backing up DB

ALTER TABLE locations ADD COLUMN sector_id BIGINT;
ALTER TABLE locations ADD CONSTRAINT fk_locations_sector FOREIGN KEY (sector_id) REFERENCES sectors(id);

-- If you have existing projects table and want to migrate their sector association into locations,
-- you would need a data migration step (example assumes projects table had id->location mapping available):
-- UPDATE locations l
-- SET sector_id = p.sector_id
-- FROM projects p
-- WHERE ST_Equals(l.geometry, p.geometry) OR (l.name = p.name AND l.map_id = p.map_id);

-- After verifying, you can drop projects tables (see drop_projects_tables.sql)
