Skip to content

Commit

Permalink
#3043: Configure mssql and psql for geosearch queries
Browse files Browse the repository at this point in the history
 - Enable PostGIS extension on PostgreSQL
 - Add db migration for adding geometry column to locations
 - Add db migration to populate geometry from existing lat,lng columns
 - Update test data with geometry values
 - Update test data conversion script for geometry values
 - Add gradle task for dropping postgis extension and make sure that it
   runs before dbDrop task otherwise drop-all liquibase command fails.
  • Loading branch information
ysf-simsoft committed Jul 6, 2020
1 parent cfde6d6 commit 95ed311
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
13 changes: 12 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,18 @@ task dbRollback(dependsOn: 'compileJava', type: JavaExec) {
}
}

task dbDrop_postgis(dependsOn: "compileJava", type: com.bmuschko.gradle.docker.tasks.container.DockerExecContainer) {
group = "database"
description = "Drops PostGIS extension and related objects on PostgreSQL."
targetContainerId { dbContainerName }
commands = [ ["psql", "-U", run.environment["ANET_DB_USERNAME"], "-d", run.environment["ANET_DB_NAME"], "-c", "DROP EXTENSION IF EXISTS postgis CASCADE"] as String[] ]
}

task dbDrop(dependsOn: 'compileJava', type: JavaExec) {
if(!isMssql) {
dependsOn 'dbDrop_postgis'
tasks.findByName('dbDrop_postgis').mustRunAfter('compileJava')
}
group = "database"
description = "Runs the ANET database drop-all (Liquibase) command."
classpath = sourceSets.main.runtimeClasspath
Expand Down Expand Up @@ -330,7 +341,7 @@ task dockerPushImage(type: com.bmuschko.gradle.docker.tasks.image.DockerPushImag

task dockerPullDB(type: com.bmuschko.gradle.docker.tasks.image.DockerPullImage) {
group = "database container"
image = isMssql ? "ncia/anet-mssql-linux:latest" : "postgres:latest"
image = isMssql ? "ncia/anet-mssql-linux:latest" : "postgis/postgis:latest"
description = "Pulls a docker image for the ANET DB from ${image}."
}

Expand Down
36 changes: 18 additions & 18 deletions insertBaseData-mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -422,24 +422,24 @@ INSERT INTO approvers (approvalStepUuid, positionUuid)
AND approvalSteps.type = 1;

-- Create locations
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'cc49bb27-4d8f-47a8-a9ee-af2b68b992ac', 'St Johns Airport', 47.613442, -52.740936, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'8c138750-91ce-41bf-9b4c-9f0ddc73608b', 'Murray''s Hotel', 47.561517, -52.708760, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'9c982685-5946-4dad-a7ee-0f5a12f5e170', 'Wishingwells Park', 47.560040, -52.736962, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'0855fb0a-995e-4a79-a132-4024ee2983ff', 'General Hospital', 47.571772, -52.741935, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'95446f93-249b-4aa9-b98a-7bd2c4680718', 'Portugal Cove Ferry Terminal', 47.626718, -52.857241, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'c8fdb53f-6f93-46fc-b0fa-f005c7b49667', 'Cabot Tower', 47.570010, -52.681770, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'c7a9f420-457a-490c-a810-b504c022cf1e', 'Fort Amherst', 47.563763, -52.680590, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'7339f9e3-99d1-497a-9e3b-1269c4c287fe', 'Harbour Grace Police Station', 47.705133, -53.214422, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, createdAt, updatedAt)
VALUES (N'f2207d9b-204b-4cb5-874d-3fe6bc6f8acd', 'Conception Bay South Police Station', 47.526784, -52.954739, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'cc49bb27-4d8f-47a8-a9ee-af2b68b992ac', 'St Johns Airport', 47.613442, -52.740936, geometry::Point(-52.740936, 47.613442, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'8c138750-91ce-41bf-9b4c-9f0ddc73608b', 'Murray''s Hotel', 47.561517, -52.708760, geometry::Point(-52.708760, 47.561517, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'9c982685-5946-4dad-a7ee-0f5a12f5e170', 'Wishingwells Park', 47.560040, -52.736962, geometry::Point(-52.736962, 47.560040, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'0855fb0a-995e-4a79-a132-4024ee2983ff', 'General Hospital', 47.571772, -52.741935, geometry::Point(-52.741935, 47.571772, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'95446f93-249b-4aa9-b98a-7bd2c4680718', 'Portugal Cove Ferry Terminal', 47.626718, -52.857241, geometry::Point(-52.857241, 47.626718, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'c8fdb53f-6f93-46fc-b0fa-f005c7b49667', 'Cabot Tower', 47.570010, -52.681770, geometry::Point(-52.681770, 47.570010, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'c7a9f420-457a-490c-a810-b504c022cf1e', 'Fort Amherst', 47.563763, -52.680590, geometry::Point(-52.680590, 47.563763, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'7339f9e3-99d1-497a-9e3b-1269c4c287fe', 'Harbour Grace Police Station', 47.705133, -53.214422, geometry::Point(-53.214422, 47.705133, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, lat, lng, geometry, createdAt, updatedAt)
VALUES (N'f2207d9b-204b-4cb5-874d-3fe6bc6f8acd', 'Conception Bay South Police Station', 47.526784, -52.954739, geometry::Point(-52.954739, 47.526784, 3857), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, createdAt, updatedAt)
VALUES (N'e0ff0d6c-e663-4639-a44d-b075bf1e690d', 'MoD Headquarters Kabul', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO locations (uuid, name, createdAt, updatedAt)
Expand Down
2 changes: 2 additions & 0 deletions mssql2pg.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
s/cast\((\S+) as datetime2\((\d+)\)\)/"date_trunc(" . ($2 eq '3' ? "'milliseconds'" : "'second'") . ", $1)"/ie;
# Function to generate uuid's
s/lower\(newid\(\)\)/uuid_generate_v4()/g;
# convert point geometry functions
s/geometry::Point\((-\d{2}\.\d{6}, \d{2}\.\d{6})/ST_SetSRID(ST_MakePoint($1)/g;
2 changes: 2 additions & 0 deletions prepare-psql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ CREATE OR REPLACE AGGREGATE tsvector_agg (tsvector) (

-- make sure the preconditions for UUID prefix search are met
CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE EXTENSION IF NOT EXISTS postgis;
12 changes: 12 additions & 0 deletions src/main/resources/migrations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3148,4 +3148,16 @@
</addColumn>
</changeSet>

<changeSet id="add-geometry-column-to-locations" author="ysf">
<addColumn tableName="locations">
<column name="geometry" type="geometry" />
</addColumn>
<sql dbms="postgresql">
UPDATE locations SET "geometry" = ST_SetSRID(ST_MakePoint(lng, lat), 3857);
</sql>
<sql dbms="mssql">
UPDATE locations SET "geometry" = geometry::Point(lng, lat, 3857) WHERE lat IS NOT NULL AND lng IS NOT NULL;
</sql>
</changeSet>

</databaseChangeLog>

0 comments on commit 95ed311

Please sign in to comment.