2018-07-17 04:03:38 -04:00
|
|
|
--
|
|
|
|
-- Geometries table
|
|
|
|
--
|
|
|
|
-- To store building outlines.
|
|
|
|
-- Geometries form the backbone, expected to be provided from other sources.
|
|
|
|
CREATE TABLE geometries (
|
|
|
|
-- integer for internal unique id
|
|
|
|
-- may consider guid for global uniqueness
|
|
|
|
geometry_id serial PRIMARY KEY,
|
|
|
|
-- jsonb document for all data, source ID, attributes
|
|
|
|
geometry_doc jsonb,
|
|
|
|
-- geometry as EPSG:3857 avoiding reprojection for tiles
|
|
|
|
geometry_geom geometry(POLYGON, 3857)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Spatial index over building outlines
|
|
|
|
CREATE INDEX geometries_idx ON geometries USING GIST ( geometry_geom );
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Buildings table
|
|
|
|
--
|
|
|
|
-- To store collected data.
|
|
|
|
-- This is the core dataset to be collected by the application.
|
|
|
|
CREATE TABLE buildings (
|
|
|
|
-- integer for internal unique id
|
|
|
|
-- may consider guid for global uniqueness
|
|
|
|
building_id serial PRIMARY KEY,
|
|
|
|
-- jsonb document for all data, attributes to be specified in application
|
|
|
|
building_doc jsonb,
|
|
|
|
-- reference to geometry, aiming to decouple from geometry provider
|
|
|
|
geometry_id integer REFERENCES geometries
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Index over building geometry (expect to look up building by geometry_id)
|
|
|
|
CREATE INDEX building_geometry_idx ON buildings ( geometry_id );
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Users table
|
|
|
|
--
|
|
|
|
-- Minimal user data to support login and edit history
|
|
|
|
CREATE TABLE users (
|
|
|
|
-- internal unique id
|
2018-08-08 04:05:24 -04:00
|
|
|
user_id uuid PRIMARY KEY,
|
2018-07-17 04:03:38 -04:00
|
|
|
-- username for login (required)
|
2018-08-08 04:05:24 -04:00
|
|
|
username varchar(30) UNIQUE NOT NULL,
|
2018-07-17 04:03:38 -04:00
|
|
|
-- email address for password reset (optional)
|
2018-09-09 06:32:02 -04:00
|
|
|
email varchar(50) UNIQUE,
|
2018-08-08 04:05:24 -04:00
|
|
|
-- password - as generated from hash(salt+password) via pgcrypto
|
2018-09-09 06:32:02 -04:00
|
|
|
pass varchar(60),
|
|
|
|
-- date registered
|
|
|
|
registered timestamp default NOW()
|
2018-07-17 04:03:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX user_username_idx ON users ( username );
|
|
|
|
CREATE INDEX user_email_idx ON users ( email );
|
|
|
|
|
2018-09-09 06:32:02 -04:00
|
|
|
--
|
|
|
|
-- User session table
|
|
|
|
--
|
|
|
|
--
|
|
|
|
CREATE TABLE user_sessions (
|
|
|
|
sid varchar PRIMARY KEY,
|
|
|
|
sess json NOT NULL,
|
|
|
|
expire timestamp(6) NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX user_sessions_expire_idx on user_sessions ( expire );
|
|
|
|
|
2018-07-17 04:03:38 -04:00
|
|
|
--
|
|
|
|
-- Log table
|
|
|
|
--
|
|
|
|
-- To store all changes to building data, recording users and change in data
|
|
|
|
-- over time.
|
|
|
|
CREATE TABLE log (
|
|
|
|
-- integer for internal unique id
|
|
|
|
log_id bigserial PRIMARY KEY,
|
|
|
|
-- default timestamp to time now
|
|
|
|
log_timestamp TIMESTAMP default NOW(),
|
|
|
|
-- log document to be extended in application
|
2018-08-08 04:05:24 -04:00
|
|
|
-- log change diff (CREATE/UPDATE/DELETE)
|
2018-07-17 04:03:38 -04:00
|
|
|
log_doc jsonb,
|
|
|
|
-- log user id
|
2018-08-08 04:05:24 -04:00
|
|
|
user_id uuid REFERENCES users,
|
|
|
|
-- log building id
|
|
|
|
building_id integer REFERENCES buildings
|
2018-07-17 04:03:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX log_timestamp_idx ON log ( log_timestamp );
|
|
|
|
CREATE INDEX log_user_idx ON log ( user_id );
|
2018-08-08 04:05:24 -04:00
|
|
|
CREATE INDEX log_building_idx ON log ( building_id );
|