Skip to content

Commit fe048fa

Browse files
committed
Extract database schema
1 parent ab3fbe6 commit fe048fa

File tree

4 files changed

+56
-93
lines changed

4 files changed

+56
-93
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ CREATE DATABASE geofeatures;
2121
\c geofeatures;
2222
```
2323

24-
There enter the SQL statements at the top of these two files (in that order):
24+
Next, load the database schema:
2525

2626
```
27-
services/features.js
28-
services/visits.js
27+
psql -d geofeatures < schema.sql
2928
```
3029

3130
Make sure the frontend user has access to the geofeatures database:

schema.sql

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
CREATE USER frontend PASSWORD '[euro4sure]';
2+
3+
CREATE EXTENSION postgis;
4+
5+
------------------------------------------------------------------------------
6+
-- for services/features.js
7+
------------------------------------------------------------------------------
8+
9+
CREATE TABLE features
10+
(
11+
id character varying(64) NOT NULL,
12+
13+
name character varying(128) NOT NULL,
14+
layer character varying(32) NOT NULL,
15+
16+
properties jsonb NOT NULL,
17+
18+
hull geometry NOT NULL,
19+
20+
created_at timestamp NOT NULL,
21+
updated_at timestamp NOT NULL,
22+
23+
CONSTRAINT nodes_pkey PRIMARY KEY (id),
24+
25+
CONSTRAINT enforce_srid_hull CHECK (st_srid(hull) = 4326)
26+
);
27+
28+
CREATE INDEX features_hull_index ON features USING gist (hull);
29+
30+
GRANT SELECT, UPDATE, INSERT, DELETE ON features TO frontend;
31+
32+
------------------------------------------------------------------------------
33+
-- for services/visits.js
34+
------------------------------------------------------------------------------
35+
36+
CREATE TABLE visits
37+
(
38+
id uuid NOT NULL,
39+
user_id character varying(128) NOT NULL,
40+
feature_id character varying(64) NOT NULL,
41+
42+
start bigint NOT NULL,
43+
finish bigint NOT NULL,
44+
45+
created_at timestamp NOT NULL,
46+
updated_at timestamp NOT NULL,
47+
48+
CONSTRAINT visits_pkey PRIMARY KEY (id)
49+
);
50+
51+
GRANT SELECT, UPDATE, INSERT, DELETE ON visits TO frontend;
52+
53+
CREATE INDEX visits_start_index ON visits (start);
54+
CREATE INDEX visits_userid_index ON visits (user_id);

services/features.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,6 @@ const async = require('async'),
1010
ServiceError = common.utils.ServiceError,
1111
turf = require('turf'),
1212
url = require('url');
13-
/*
14-
15-
kubectl run postgis-shell -i --tty --image=timfpark/postgis:9.6 -- bash
16-
17-
CREATE USER frontend PASSWORD '[euro4sure]';
18-
19-
CREATE EXTENSION postgis;
20-
21-
CREATE TABLE features
22-
(
23-
id character varying(64) NOT NULL,
24-
25-
name character varying(128) NOT NULL,
26-
layer character varying(32) NOT NULL,
27-
28-
properties jsonb NOT NULL,
29-
30-
hull geometry NOT NULL,
31-
32-
created_at timestamp NOT NULL,
33-
updated_at timestamp NOT NULL,
34-
35-
CONSTRAINT nodes_pkey PRIMARY KEY (id),
36-
37-
CONSTRAINT enforce_srid_hull CHECK (st_srid(hull) = 4326)
38-
);
39-
40-
CREATE INDEX features_hull_index
41-
ON features
42-
USING gist
43-
(hull);
44-
45-
46-
GRANT SELECT, UPDATE, INSERT, DELETE ON features TO frontend;
47-
48-
*/
4913

5014
let featureDatabasePool;
5115

services/visits.js

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,6 @@ const async = require('async'),
1414
let featureTablePool;
1515
let redlock;
1616

17-
/*
18-
19-
CREATE EXTENSION postgis;
20-
21-
CREATE TABLE visits
22-
(
23-
id uuid NOT NULL,
24-
user_id character varying(128) NOT NULL,
25-
feature_id character varying(64) NOT NULL,
26-
27-
start bigint NOT NULL,
28-
finish bigint NOT NULL,
29-
30-
created_at timestamp NOT NULL,
31-
updated_at timestamp NOT NULL,
32-
33-
CONSTRAINT visits_pkey PRIMARY KEY (id)
34-
);
35-
36-
GRANT SELECT, UPDATE, INSERT, DELETE ON visits TO frontend;
37-
38-
CREATE INDEX visits_start_index
39-
ON visits
40-
(start);
41-
42-
CREATE INDEX visits_userid_index
43-
ON visits
44-
(user_id);
45-
46-
SELECT v.feature_id, count(v.feature_id), sum(v.finish-v.start) AS duration, f.name
47-
FROM visits as v
48-
JOIN features as f on f.id=v.feature_id
49-
WHERE v.user_id='10152875766888406'
50-
GROUP BY v.feature_id, f.name
51-
ORDER BY duration DESC;
52-
53-
SELECT v.feature_id, f.name, v.start, v.finish, (v.finish-v.start)/1000 as duration
54-
FROM visits as v
55-
JOIN features as f on f.id=v.feature_id
56-
WHERE v.user_id='10152875766888406'
57-
ORDER BY v.start;
58-
59-
SELECT v.feature_id,
60-
(v.finish-v.start)/(1000*3600.0) AS duration,
61-
to_timestamp(v.start/1000.0) AT TIME ZONE 'PST' AS start,
62-
to_timestamp(v.finish/1000.0) AT TIME ZONE 'PST' AS finish,
63-
f.name
64-
FROM visits as v
65-
JOIN features as vi serf on f.id=v.feature_id
66-
WHERE v.user_id='10152875766888406' and v.feature_id='wof-102191581'
67-
ORDER BY duration DESC;
68-
69-
*/
70-
7117
function rowToVisit(row) {
7218
if (!row) return;
7319

0 commit comments

Comments
 (0)