Skip to content

Commit

Permalink
Merge pull request #79 from DPIclimate/postgis_and_db_v2
Browse files Browse the repository at this point in the history
Convert device table location columns to PostGIS geometry datatype
  • Loading branch information
dajtxx authored Jun 7, 2024
2 parents 534bc61 + 0f7e9e1 commit ab2b821
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 215 deletions.
6 changes: 3 additions & 3 deletions compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: '3.1'

services:
db:
image: postgres:14.2
restart: "no"
image: postgis/postgis:14-3.4
restart: always
env_file:
- .env
volumes:
Expand All @@ -18,7 +18,7 @@ services:
mq:
hostname: "mq"
image: rabbitmq:3.9-management
restart: "no"
restart: always
env_file:
- .env
volumes:
Expand Down
9 changes: 6 additions & 3 deletions db/init.d/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CREATE EXTENSION postgis;
CREATE EXTENSION pgcrypto;

create table if not exists sources (
source_name text primary key not null
);
Expand All @@ -6,7 +9,7 @@ create table if not exists physical_devices (
uid integer generated always as identity primary key,
source_name text not null references sources,
name text not null,
location point,
location geometry('POINT', 4283),
last_seen timestamptz,
-- Store only top level key value pairs here; it is used
-- for quickly finding a device using information carried
Expand Down Expand Up @@ -72,7 +75,7 @@ create table if not exists device_blobs (
create table if not exists logical_devices (
uid integer generated always as identity primary key,
name text not null,
location point,
location geometry('POINT', 4283),
last_seen timestamptz,
properties jsonb not null default '{}'
);
Expand Down Expand Up @@ -107,4 +110,4 @@ create table if not exists version (
create index if not exists pd_src_id_idx on physical_devices using GIN (source_ids);

insert into sources values ('ttn'), ('greenbrain'), ('wombat'), ('ydoc'), ('ict_eagleio');
insert into version values (1);
insert into version values (2);
17 changes: 17 additions & 0 deletions db/upgrade/002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE EXTENSION postgis;
CREATE EXTENSION pgcrypto;

SELECT AddGeometryColumn('logical_devices','geom',4283,'POINT',2);
SELECT AddGeometryColumn('physical_devices','geom',4283,'POINT',2);

UPDATE logical_devices SET geom = ST_MakePoint(location[1], location[0]) WHERE location IS NOT NULL;
UPDATE physical_devices SET geom = ST_MakePoint(location[1], location[0]) WHERE location IS NOT NULL;

ALTER TABLE logical_devices DROP COLUMN location;
ALTER TABLE physical_devices DROP COLUMN location;

ALTER TABLE logical_devices RENAME COLUMN geom TO location;
ALTER TABLE physical_devices RENAME COLUMN geom TO location;

TRUNCATE version;
insert into version values (2);
Loading

0 comments on commit ab2b821

Please sign in to comment.