Skip to content

Commit 6735c54

Browse files
authored
Merge pull request #3 from CatalystCode/extract-schema
Extract database schema
2 parents ab3fbe6 + db6d5d3 commit 6735c54

File tree

4 files changed

+57
-94
lines changed

4 files changed

+57
-94
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ You will be welcomed with the postgres prompt. Now, create the geofeatures datab
1919
```
2020
CREATE DATABASE geofeatures;
2121
\c geofeatures;
22+
23+
CREATE USER frontend PASSWORD your_password_here;
2224
```
2325

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

2628
```
27-
services/features.js
28-
services/visits.js
29+
psql -d geofeatures < schema.sql
2930
```
3031

3132
Make sure the frontend user has access to the geofeatures database:
@@ -37,7 +38,7 @@ GRANT ALL PRIVILEGES ON DATABASE geofeatures TO frontend;
3738
Then, make sure the environment variable for the newly-created user is set:
3839

3940
```
40-
export FEATURES_CONNECTION_STRING='postgres://frontend:[euro4sure]@127.0.0.1/geofeatures'
41+
export FEATURES_CONNECTION_STRING='postgres://frontend:your_password_here@127.0.0.1/geofeatures'
4142
export PORT=3035
4243
```
4344

schema.sql

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