-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from Vizzuality/feature/api/MARXAN-196-migrat…
…e-for-bbox Feature/api/marxan 196 migrate for bbox
- Loading branch information
Showing
8 changed files
with
162 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddBboxToProjects1621439031072 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
----------------------------------------- | ||
-- Generates the new column we need | ||
----------------------------------------- | ||
ALTER TABLE projects ADD COLUMN bbox jsonb; | ||
UPDATE projects SET bbox = jsonb_build_array(ST_XMax(extent), ST_XMin(extent), ST_YMax(extent), ST_YMin(extent)) where extent is not null; | ||
----------------------------------------- | ||
-- tr_GetBbox() | ||
-- Utility func to populate bbox | ||
----------------------------------------- | ||
CREATE OR REPLACE FUNCTION tr_GetBbox() | ||
RETURNS trigger AS $BODY$ | ||
BEGIN | ||
IF NEW.extent IS NOT NULL THEN | ||
NEW.bbox := jsonb_build_array(ST_XMax(NEW.extent), ST_XMin(NEW.extent), ST_YMax(NEW.extent), ST_YMin(NEW.extent)); | ||
ELSE | ||
NEW.bbox := NULL; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$BODY$ LANGUAGE plpgsql; | ||
----------------------------------------- | ||
-- Creates the trigger | ||
----------------------------------------- | ||
DROP TRIGGER IF EXISTS tr_projects_extent ON projects; | ||
CREATE TRIGGER tr_projects_extent | ||
BEFORE INSERT or UPDATE ON projects | ||
FOR EACH ROW EXECUTE | ||
PROCEDURE tr_GetBbox(); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
DROP TRIGGER IF EXISTS tr_projects_extent ON projects; | ||
ALTER TABLE projects | ||
DROP COLUMN bbox; | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
geoprocessing/src/migrations/geoprocessing/1621431520169-AddBboxToAdminAreasAndProjects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
export class AddBboxToAdminAreasAndProjects1621431520169 | ||
implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
----------------------------------------- | ||
-- Generates the new column we need | ||
----------------------------------------- | ||
ALTER TABLE admin_regions ADD COLUMN bbox jsonb; | ||
UPDATE admin_regions SET bbox = jsonb_build_array(ST_XMax(the_geom), ST_XMin(the_geom), ST_YMax(the_geom), ST_YMin(the_geom)); | ||
----------------------------------------- | ||
-- tr_GetBbox() | ||
-- Utility func to populate bbox | ||
----------------------------------------- | ||
CREATE OR REPLACE FUNCTION tr_GetBbox() | ||
RETURNS trigger AS $BODY$ | ||
BEGIN | ||
NEW.bbox := jsonb_build_array(ST_XMax(NEW.the_geom), ST_XMin(NEW.the_geom), ST_YMax(NEW.the_geom), ST_YMin(NEW.the_geom)); | ||
RETURN NEW; | ||
END; | ||
$BODY$ LANGUAGE plpgsql; | ||
----------------------------------------- | ||
-- Creates the trigger | ||
----------------------------------------- | ||
DROP TRIGGER IF EXISTS tr_adminRegions_the_geom ON admin_regions; | ||
CREATE TRIGGER tr_adminRegions_the_geom | ||
BEFORE INSERT or UPDATE ON admin_regions | ||
FOR EACH ROW EXECUTE | ||
PROCEDURE tr_GetBbox(); | ||
----------------------------------------- | ||
-- Recreates the countries view | ||
----------------------------------------- | ||
DROP VIEW countries; | ||
CREATE VIEW countries AS ( | ||
SELECT | ||
id, | ||
gid_0, | ||
name_0, | ||
the_geom, | ||
level, | ||
iso3, | ||
created_at, | ||
created_by, | ||
last_modified_at, | ||
bbox | ||
FROM admin_regions | ||
WHERE gid_0 IS NOT NULL AND gid_1 IS NULL AND gid_2 IS NULL | ||
); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
DROP TRIGGER IF EXISTS tr_adminRegions_the_geom ON admin_regions; | ||
DROP VIEW countries; | ||
CREATE VIEW countries AS ( | ||
SELECT | ||
id, | ||
gid_0, | ||
name_0, | ||
the_geom, | ||
level, | ||
iso3, | ||
created_at, | ||
created_by, | ||
last_modified_at | ||
FROM admin_regions | ||
WHERE gid_0 IS NOT NULL AND gid_1 IS NULL AND gid_2 IS NULL | ||
); | ||
ALTER TABLE admin_regions DROP COLUMN bbox; | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters