Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions cli/macrostrat/cli/database/migrations/people/main.sql
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
-- SCHEMA

-- SCHEMA
CREATE SCHEMA IF NOT EXISTS ecosystem;

-- PEOPLE
CREATE TABLE IF NOT EXISTS ecosystem.people (
id serial PRIMARY KEY,
person_id serial PRIMARY KEY,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use GENERATED ALWAYS AS IDENTITY instead of serial throughout

name text NOT NULL,
email text NOT NULL UNIQUE,
title text NOT NULL,
website text,
img_id text,
active_start timestamp with time zone NOT NULL DEFAULT now(),
active_start timestamp with time zone DEFAULT now(),
active_end timestamp with time zone
);

-- ROLES
CREATE TABLE IF NOT EXISTS ecosystem.roles (
id serial PRIMARY KEY,
role_id serial PRIMARY KEY,
name text NOT NULL UNIQUE,
description text NOT NULL
);

-- PEOPLE-ROLES MAPPING
CREATE TABLE IF NOT EXISTS ecosystem.people_roles (
person_id integer NOT NULL REFERENCES ecosystem.people(id) ON DELETE CASCADE,
role_id integer NOT NULL REFERENCES ecosystem.roles(id) ON DELETE CASCADE,
person_id integer NOT NULL REFERENCES ecosystem.people(person_id) ON DELETE CASCADE,
role_id integer NOT NULL REFERENCES ecosystem.roles(role_id) ON DELETE CASCADE,
PRIMARY KEY (person_id, role_id)
);

-- CONTRIBUTIONS
CREATE TABLE IF NOT EXISTS ecosystem.contributions (
id serial PRIMARY KEY,
person_id integer NOT NULL REFERENCES ecosystem.people(id) ON DELETE CASCADE,
contribution_id serial PRIMARY KEY,
person_id integer NOT NULL REFERENCES ecosystem.people(person_id) ON DELETE CASCADE,
contribution text NOT NULL,
description text,
date timestamp with time zone NOT NULL DEFAULT now(),
Expand All @@ -39,8 +40,8 @@ CREATE TABLE IF NOT EXISTS ecosystem.contributions (

-- PEOPLE-CONTRIBUTIONS MAPPING
CREATE TABLE IF NOT EXISTS ecosystem.people_contributions (
person_id integer NOT NULL REFERENCES ecosystem.people(id) ON DELETE CASCADE,
contribution_id integer NOT NULL REFERENCES ecosystem.contributions(id) ON DELETE CASCADE,
person_id integer NOT NULL REFERENCES ecosystem.people(person_id) ON DELETE CASCADE,
contribution_id integer NOT NULL REFERENCES ecosystem.contributions(contribution_id) ON DELETE CASCADE,
PRIMARY KEY (person_id, contribution_id)
);

Expand All @@ -50,5 +51,42 @@ INSERT INTO ecosystem.roles (name, description) VALUES
('Researcher', 'Conducts academic or applied research'),
('Developer', 'Writes and maintains software code'),
('Leader', 'Leads research or development projects and mentors others'),
('Collaborator', 'Contributes to joint projects'),
('Collaborator', 'Contributes to joint projects')
ON CONFLICT (name) DO NOTHING;

-- CREATE POSTGREST VIEWS
CREATE OR REPLACE VIEW macrostrat_api.people AS
SELECT * FROM ecosystem.people;

CREATE OR REPLACE VIEW macrostrat_api.people_roles AS
SELECT * FROM ecosystem.people_roles;

CREATE OR REPLACE VIEW macrostrat_api.roles AS
SELECT * FROM ecosystem.roles;

CREATE OR REPLACE VIEW macrostrat_api.people_with_roles AS
SELECT
p.person_id,
p.name,
p.email,
p.title,
p.website,
p.img_id,
p.active_start,
p.active_end,
COALESCE(json_agg(json_build_object('name', r.name, 'description', r.description)) FILTER (WHERE r.role_id IS NOT NULL)) AS roles
FROM ecosystem.people p
LEFT JOIN ecosystem.people_roles pr ON p.person_id = pr.person_id
LEFT JOIN ecosystem.roles r ON pr.role_id = r.role_id
GROUP BY p.person_id;


-- DEFAULT PRIVILEGES
GRANT SELECT, INSERT, UPDATE, DELETE ON
ecosystem.people,
ecosystem.people_roles,
macrostrat_api.people,
macrostrat_api.people_roles
TO web_admin;

GRANT USAGE, SELECT ON SEQUENCE ecosystem.people_person_id_seq TO web_admin;